-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueuectx_test.go
More file actions
91 lines (71 loc) · 1.71 KB
/
queuectx_test.go
File metadata and controls
91 lines (71 loc) · 1.71 KB
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
package nbjobqueue
import (
"cmp"
"context"
"sync"
"testing"
"time"
"github.com/google/go-cmp/cmp/cmpopts"
"gotest.tools/v3/assert"
)
func TestQueueCtx(t *testing.T) {
jq := NewWithContext(context.Background(), 3)
var items []int
var lock sync.Mutex
for i := 0; i < 10; i++ {
jq.AddJob(func(jobCtx context.Context) {
lock.Lock()
defer lock.Unlock()
items = append(items, i)
})
}
jq.Shutdown()
assert.Assert(t, jq.Closed())
assert.DeepEqual(t, []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, items, cmpopts.SortSlices(cmp.Less[int]))
}
func TestQueueCtxCancel(t *testing.T) {
jq := NewWithContext(context.Background(), 3)
var items []int
var lock sync.Mutex
for i := 0; i < 10; i++ {
_ = jq.AddJobCheck(func(jobCtx context.Context) {
if i%2 == 0 {
time.Sleep(100 * time.Millisecond)
select {
case <-jobCtx.Done():
return
default:
}
}
lock.Lock()
defer lock.Unlock()
items = append(items, i)
})
}
jq.Shutdown(WithShutdownDrain(false), WithShutdownCancel(true))
assert.DeepEqual(t, []int{1, 3, 5, 7, 9}, items, cmpopts.SortSlices(cmp.Less[int]))
}
func TestQueueCtxClose(t *testing.T) {
jq := NewWithContext(context.Background(), 3)
var items []int
var lock sync.Mutex
for i := 0; i < 10; i++ {
jq.AddJob(func(ctx context.Context) {
lock.Lock()
defer lock.Unlock()
items = append(items, i)
})
}
jq.Close()
for i := 10; i < 20; i++ {
err := jq.AddJobCheck(func(ctx context.Context) {
lock.Lock()
defer lock.Unlock()
items = append(items, i)
})
assert.ErrorIs(t, err, ErrClosed)
}
jq.Shutdown()
assert.Assert(t, jq.Closed())
assert.DeepEqual(t, []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, items, cmpopts.SortSlices(cmp.Less[int]))
}