-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic_test.go
309 lines (281 loc) · 7.53 KB
/
basic_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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package taskqueue_test
import (
"context"
"fmt"
"os"
"testing"
"time"
"github.com/alicebob/miniredis/v2"
taskqueue "github.com/stellaraf/go-task-queue"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const zero uint64 = uint64(0)
var Addr string
var Ctx context.Context
var UseMini bool
var RunT func(t miniredis.Tester) *miniredis.Miniredis = func(t miniredis.Tester) *miniredis.Miniredis {
Addr = "localhost:6379"
Ctx = context.Background()
UseMini = false
return nil
}
func init() {
ci := os.Getenv("CI") == "true"
if !ci {
RunT = miniredis.RunT
UseMini = true
}
}
func Test_NewBasicTaskQueue(t *testing.T) {
name := fmt.Sprintf("%s--%s", t.Name(), time.Now().Format(time.RFC3339Nano))
mr := RunT(t)
var ctx, addr taskqueue.Option
if UseMini {
ctx = taskqueue.WithContext(mr.Ctx)
addr = taskqueue.WithHost(mr.Addr())
} else {
ctx = taskqueue.WithContext(Ctx)
addr = taskqueue.WithHost(Addr)
}
t.Run("base", func(t *testing.T) {
t.Parallel()
_, err := taskqueue.NewBasic(name, ctx, addr)
require.NoError(t, err)
})
t.Run("with uri", func(t *testing.T) {
t.Parallel()
_, err := taskqueue.NewBasic(name, ctx, taskqueue.WithURI(fmt.Sprintf("redis://%s", Addr)))
require.NoError(t, err)
})
}
func TestBasicTaskQueue_Clear(t *testing.T) {
mr := RunT(t)
var ctx, addr taskqueue.Option
if UseMini {
ctx = taskqueue.WithContext(mr.Ctx)
addr = taskqueue.WithHost(mr.Addr())
} else {
ctx = taskqueue.WithContext(Ctx)
addr = taskqueue.WithHost(Addr)
}
name := fmt.Sprintf("%s--%s", t.Name(), time.Now().Format(time.RFC3339Nano))
queue, err := taskqueue.NewBasic(name, ctx, addr)
require.NoError(t, err)
t.Run("add", func(t *testing.T) {
err := queue.Add("value")
require.NoError(t, err)
})
t.Run("clear", func(t *testing.T) {
err := queue.Clear()
require.NoError(t, err)
})
t.Run("ensure cleared", func(t *testing.T) {
size := queue.Size()
assert.Equal(t, zero, size)
})
}
func TestBasicTaskQueue_Size(t *testing.T) {
mr := RunT(t)
var ctx, addr taskqueue.Option
if UseMini {
ctx = taskqueue.WithContext(mr.Ctx)
addr = taskqueue.WithHost(mr.Addr())
} else {
ctx = taskqueue.WithContext(Ctx)
addr = taskqueue.WithHost(Addr)
}
name := fmt.Sprintf("%s--%s", t.Name(), time.Now().Format(time.RFC3339Nano))
queue, err := taskqueue.NewBasic(name, ctx, addr)
defer queue.Clear()
require.NoError(t, err)
t.Run("add", func(t *testing.T) {
err := queue.Add("value")
require.NoError(t, err)
})
t.Run("ensure size", func(t *testing.T) {
size := queue.Size()
assert.Equal(t, uint64(1), size)
})
}
func TestBasicTaskQueue_Add(t *testing.T) {
mr := RunT(t)
var ctx, addr taskqueue.Option
if UseMini {
ctx = taskqueue.WithContext(mr.Ctx)
addr = taskqueue.WithHost(mr.Addr())
} else {
ctx = taskqueue.WithContext(Ctx)
addr = taskqueue.WithHost(Addr)
}
name := fmt.Sprintf("%s--%s", t.Name(), time.Now().Format(time.RFC3339Nano))
queue, err := taskqueue.NewBasic(name, ctx, addr)
defer queue.Clear()
require.NoError(t, err)
value := fmt.Sprintf("%s--%s--1", t.Name(), time.Now().Format(time.RFC3339Nano))
t.Run("add", func(t *testing.T) {
err := queue.Add(value)
require.NoError(t, err)
})
t.Run("check size", func(t *testing.T) {
size := queue.Size()
assert.Equal(t, uint64(1), size)
})
t.Run("pop", func(t *testing.T) {
popped := queue.Pop()
assert.Equal(t, value, *popped)
})
}
func TestBasicTaskQueue_Pop(t *testing.T) {
mr := RunT(t)
var ctx, addr taskqueue.Option
if UseMini {
ctx = taskqueue.WithContext(mr.Ctx)
addr = taskqueue.WithHost(mr.Addr())
} else {
ctx = taskqueue.WithContext(Ctx)
addr = taskqueue.WithHost(Addr)
}
name := fmt.Sprintf("%s--%s", t.Name(), time.Now().Format(time.RFC3339Nano))
queue, err := taskqueue.NewBasic(name, ctx, addr)
defer queue.Clear()
require.NoError(t, err)
t.Run("add values", func(t *testing.T) {
err := queue.Add("one")
require.NoError(t, err)
err = queue.Add("two")
require.NoError(t, err)
err = queue.Add("three")
require.NoError(t, err)
})
t.Run("pop values", func(t *testing.T) {
one := queue.Pop()
assert.Equal(t, "one", *one)
two := queue.Pop()
assert.Equal(t, "two", *two)
three := queue.Pop()
assert.Equal(t, "three", *three)
})
t.Run("nil when empty", func(t *testing.T) {
value := queue.Pop()
assert.Nil(t, value)
})
}
func TestBasicTaskQueue_Remove(t *testing.T) {
mr := RunT(t)
var ctx, addr taskqueue.Option
if UseMini {
ctx = taskqueue.WithContext(mr.Ctx)
addr = taskqueue.WithHost(mr.Addr())
} else {
ctx = taskqueue.WithContext(Ctx)
addr = taskqueue.WithHost(Addr)
}
name := fmt.Sprintf("%s--%s", t.Name(), time.Now().Format(time.RFC3339Nano))
queue, err := taskqueue.NewBasic(name, ctx, addr)
defer queue.Clear()
require.NoError(t, err)
value := "value"
t.Run("add value", func(t *testing.T) {
err := queue.Add(value)
require.NoError(t, err)
})
t.Run("remove value", func(t *testing.T) {
err := queue.Remove(value)
require.NoError(t, err)
})
t.Run("ensure removed", func(t *testing.T) {
size := queue.Size()
assert.Equal(t, zero, size)
})
}
func TestBasicTaskQueue(t *testing.T) {
mr := RunT(t)
var ctx, addr taskqueue.Option
if UseMini {
ctx = taskqueue.WithContext(mr.Ctx)
addr = taskqueue.WithHost(mr.Addr())
} else {
ctx = taskqueue.WithContext(Ctx)
addr = taskqueue.WithHost(Addr)
}
name := fmt.Sprintf("%s--%s", t.Name(), time.Now().Format(time.RFC3339Nano))
queue, err := taskqueue.NewBasic(name, ctx, addr)
defer queue.Clear()
require.NoError(t, err)
value := "value"
t.Run("add value", func(t *testing.T) {
err := queue.Add(value)
require.NoError(t, err)
})
t.Run("get value", func(t *testing.T) {
got, err := queue.Get(0)
require.NoError(t, err)
assert.Equal(t, value, got)
})
t.Run("ensure no matching index is empty", func(t *testing.T) {
got, err := queue.Get(5)
require.NoError(t, err)
assert.Equal(t, "", got)
})
}
func TestBasicTaskQueue_RemoveIndex(t *testing.T) {
mr := RunT(t)
var ctx, addr taskqueue.Option
if UseMini {
ctx = taskqueue.WithContext(mr.Ctx)
addr = taskqueue.WithHost(mr.Addr())
} else {
ctx = taskqueue.WithContext(Ctx)
addr = taskqueue.WithHost(Addr)
}
name := fmt.Sprintf("%s--%s", t.Name(), time.Now().Format(time.RFC3339Nano))
queue, err := taskqueue.NewBasic(name, ctx, addr)
defer queue.Clear()
require.NoError(t, err)
value := "value"
t.Run("add value", func(t *testing.T) {
err := queue.Add(value)
require.NoError(t, err)
})
t.Run("remove value by index", func(t *testing.T) {
err := queue.RemoveIndex(0)
require.NoError(t, err)
})
t.Run("ensure value is removed", func(t *testing.T) {
popped := queue.Pop()
assert.Nil(t, popped)
})
t.Run("ensure no removed items errors", func(t *testing.T) {
err := queue.RemoveIndex(5)
require.Error(t, err)
})
}
func TestBasicTaskQueue_Has(t *testing.T) {
mr := RunT(t)
var ctx, addr taskqueue.Option
if UseMini {
ctx = taskqueue.WithContext(mr.Ctx)
addr = taskqueue.WithHost(mr.Addr())
} else {
ctx = taskqueue.WithContext(Ctx)
addr = taskqueue.WithHost(Addr)
}
name := fmt.Sprintf("%s--%s", t.Name(), time.Now().Format(time.RFC3339Nano))
queue, err := taskqueue.NewBasic(name, ctx, addr)
defer queue.Clear()
require.NoError(t, err)
value := "value"
t.Run("add value", func(t *testing.T) {
err := queue.Add(value)
require.NoError(t, err)
})
t.Run("ensure queue has value", func(t *testing.T) {
has := queue.Has(value)
require.True(t, has)
})
t.Run("ensure queue does not have nonexistent value", func(t *testing.T) {
has := queue.Has("does not exist")
require.False(t, has)
})
}