forked from digitalocean/go-workers2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fetcher_test.go
164 lines (112 loc) · 3.65 KB
/
fetcher_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package workers
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
)
func buildFetch(queue string, opts Options) Fetcher {
fetch := newSimpleFetcher(queue, opts)
go fetch.Fetch()
return fetch
}
func TestFetchConfig(t *testing.T) {
opts, err := setupTestOptions()
assert.NoError(t, err)
fetch := buildFetch("fetchQueue1", opts)
assert.Equal(t, "fetchQueue1", fetch.Queue())
fetch.Close()
}
func TestGetMessagesToChannel(t *testing.T) {
ctx := context.Background()
opts, err := setupTestOptions()
assert.NoError(t, err)
message, _ := NewMsg("{\"foo\":\"bar\"}")
fetch := buildFetch("fetchQueue2", opts)
rc := opts.client
rc.LPush(ctx, "queue:fetchQueue2", message.ToJson()).Result()
fetch.Ready() <- true
fetchedMessage := <-fetch.Messages()
assert.Equal(t, message, fetchedMessage)
len, err := rc.LLen(ctx, "queue:fetchQueue2").Result()
assert.NoError(t, err)
assert.Equal(t, int64(0), len)
fetch.Close()
}
func TestMoveProgressMessageToPrivateQueue(t *testing.T) {
ctx := context.Background()
opts, err := setupTestOptions()
assert.NoError(t, err)
message, _ := NewMsg("{\"foo\":\"bar\"}")
fetch := buildFetch("fetchQueue3", opts)
rc := opts.client
rc.LPush(ctx, "queue:fetchQueue3", message.ToJson())
fetch.Ready() <- true
<-fetch.Messages()
len, err := rc.LLen(ctx, "queue:fetchQueue3:1:inprogress").Result()
assert.NoError(t, err)
assert.Equal(t, int64(1), len)
messages, err := rc.LRange(ctx, "queue:fetchQueue3:1:inprogress", 0, -1).Result()
assert.NoError(t, err)
assert.Equal(t, message.ToJson(), messages[0])
fetch.Close()
}
func TestRemoveProgressMessageWhenAcked(t *testing.T) {
ctx := context.Background()
opts, err := setupTestOptions()
assert.NoError(t, err)
message, _ := NewMsg("{\"foo\":\"bar\"}")
fetch := buildFetch("fetchQueue4", opts)
rc := opts.client
rc.LPush(ctx, "queue:fetchQueue4", message.ToJson()).Result()
fetch.Ready() <- true
<-fetch.Messages()
fetch.Acknowledge(message)
len, err := rc.LLen(ctx, "queue:fetchQueue4:1:inprogress").Result()
assert.NoError(t, err)
assert.Equal(t, int64(0), len)
fetch.Close()
}
func TestRemoveProgressMessageDifferentSerialization(t *testing.T) {
ctx := context.Background()
opts, err := setupTestOptions()
assert.NoError(t, err)
json := "{\"foo\":\"bar\",\"args\":[]}"
message, _ := NewMsg(json)
assert.NotEqual(t, message.ToJson(), json)
fetch := buildFetch("fetchQueue5", opts)
rc := opts.client
rc.LPush(ctx, "queue:fetchQueue5", json).Result()
fetch.Ready() <- true
<-fetch.Messages()
fetch.Acknowledge(message)
len, err := rc.LLen(ctx, "queue:fetchQueue5:1:inprogress").Result()
assert.NoError(t, err)
assert.Equal(t, int64(0), len)
fetch.Close()
}
func TestRetryInprogressMessages(t *testing.T) {
ctx := context.Background()
opts, err := setupTestOptions()
assert.NoError(t, err)
message, _ := NewMsg("{\"foo\":\"bar\"}")
message2, _ := NewMsg("{\"foo\":\"bar2\"}")
message3, _ := NewMsg("{\"foo\":\"bar3\"}")
rc := opts.client
rc.LPush(ctx, "queue:fetchQueue6:1:inprogress", message.ToJson()).Result()
rc.LPush(ctx, "queue:fetchQueue6:1:inprogress", message2.ToJson()).Result()
rc.LPush(ctx, "queue:fetchQueue6", message3.ToJson()).Result()
fetch := buildFetch("fetchQueue6", opts)
fetch.Ready() <- true
assert.Equal(t, message2, <-fetch.Messages())
fetch.Ready() <- true
assert.Equal(t, message, <-fetch.Messages())
fetch.Ready() <- true
assert.Equal(t, message3, <-fetch.Messages())
fetch.Acknowledge(message)
fetch.Acknowledge(message2)
fetch.Acknowledge(message3)
len, err := rc.LLen(ctx, "queue:fetchQueue6:1:inprogress").Result()
assert.NoError(t, err)
assert.Equal(t, int64(0), len)
fetch.Close()
}