-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelayer_test.go
93 lines (89 loc) · 2.35 KB
/
delayer_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
package delaying
import (
"context"
"testing"
)
func TestNewFunction(t *testing.T) {
testNewDelayer(t, NewFunction)
}
func TestNewDelayer(t *testing.T) {
testNewDelayer(t, NewDelayer)
}
func testNewDelayer(t *testing.T, newDelayer func(
id string,
implementation any,
enqueueWork func(c context.Context, params Params, args ...interface{}) error,
enqueueWorkMulti func(c context.Context, params Params, args ...[]interface{}) error,
) Delayer) {
t.Run("EnqueueWork", func(t *testing.T) {
var singleArgs []any
enqueueWork := func(c context.Context, params Params, args ...any) error {
singleArgs = args
return nil
}
enqueueWorkMulti := func(c context.Context, params Params, args ...[]any) error {
panic("unexpected call")
}
f := NewDelayer("EnqueueWorkTest", func() {}, enqueueWork, enqueueWorkMulti)
if f == nil {
t.Fatal("f is nil")
}
if singleArgs != nil {
t.Fatal("singleArgs is not nil")
}
err := f.EnqueueWork(context.Background(), With("queue1", "path1", 0), 1, 2, 3)
if err != nil {
t.Fatal(err)
}
if singleArgs == nil {
t.Fatal("singleArgs is nil")
}
if len(singleArgs) != 3 {
t.Fatal("singleArgs is not 3")
}
for i, v := range singleArgs {
if v != i+1 {
t.Fatalf("singleArgs[%d] is not %d", i, i+1)
}
}
})
t.Run("EnqueueWorkMulti", func(t *testing.T) {
var multiArgs [][]any
enqueueWork := func(c context.Context, params Params, args ...any) error {
panic("unexpected call")
}
enqueueWorkMulti := func(c context.Context, params Params, args ...[]any) error {
multiArgs = args
return nil
}
f := newDelayer("EnqueueWorkMultiTest", func() {}, enqueueWork, enqueueWorkMulti)
err := f.EnqueueWorkMulti(context.Background(), With("queue1", "path1", 0), []any{1, 2}, []any{3, 4})
if err != nil {
t.Fatal(err)
}
if multiArgs == nil {
t.Fatal("multiArgs is nil")
}
if len(multiArgs) != 2 {
t.Fatal("len(multiArgs) is not 2")
}
if len(multiArgs[0]) != 2 {
t.Fatal("len(multiArgs[0]) is not 2")
}
if len(multiArgs[1]) != 2 {
t.Fatal("len(multiArgs[1]) is not 2")
}
if multiArgs[0][0] != 1 {
t.Errorf("multiArgs[0][0] is not 1")
}
if multiArgs[0][1] != 2 {
t.Errorf("multiArgs[0][1] is not 2")
}
if multiArgs[1][0] != 3 {
t.Errorf("multiArgs[1][0] is not 3")
}
if multiArgs[1][1] != 4 {
t.Errorf("multiArgs[1][1] is not 4")
}
})
}