-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathworker_internal_test.go
More file actions
104 lines (92 loc) · 2.44 KB
/
worker_internal_test.go
File metadata and controls
104 lines (92 loc) · 2.44 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
92
93
94
95
96
97
98
99
100
101
102
103
104
package queue
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestWorkerConfig_Validate(t *testing.T) {
tests := []struct {
name string
config WorkerConfig
wantErr error
}{
{
name: "valid config",
config: WorkerConfig{Concurrency: 4, Queues: map[string]int{"q": 1}},
wantErr: nil,
},
{
name: "zero concurrency",
config: WorkerConfig{Concurrency: 0, Queues: map[string]int{"q": 1}},
wantErr: ErrInvalidWorkerConcurrency,
},
{
name: "negative concurrency",
config: WorkerConfig{Concurrency: -1, Queues: map[string]int{"q": 1}},
wantErr: ErrInvalidWorkerConcurrency,
},
{
name: "empty queues",
config: WorkerConfig{Concurrency: 1, Queues: map[string]int{}},
wantErr: ErrInvalidWorkerQueues,
},
{
name: "nil queues",
config: WorkerConfig{Concurrency: 1},
wantErr: ErrInvalidWorkerQueues,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := tt.config.Validate()
if tt.wantErr != nil {
assert.ErrorIs(t, err, tt.wantErr)
} else {
assert.NoError(t, err)
}
})
}
}
func TestIsFailure(t *testing.T) {
w := &Worker{}
assert.True(t, w.isFailure(assert.AnError))
assert.False(t, w.isFailure(&ErrRateLimit{RetryAfter: time.Second}))
assert.False(t, w.isFailure(ErrTransientIssue))
}
func TestWorkerGroup_ReusesGroupByName(t *testing.T) {
w := &Worker{groups: make(map[string]*Group)}
email := w.Group("email")
sameEmail := w.Group("email")
other := w.Group("other")
assert.Same(t, email, sameEmail)
assert.NotSame(t, email, other)
assert.Same(t, w, email.worker)
}
func TestGroupRegister_ComposesGroupMiddlewareBeforeOptions(t *testing.T) {
w := &Worker{
groups: make(map[string]*Group),
handlers: make(map[string]*Handler),
}
calls := make([]string, 0, 2)
middleware := func(name string) MiddlewareFunc {
return func(next HandlerFunc) HandlerFunc {
return func(ctx context.Context, job *Job) error {
calls = append(calls, name)
return next(ctx, job)
}
}
}
group := w.Group("email")
group.Use(middleware("group"))
err := group.Register(
"email:send",
func(context.Context, *Job) error { return nil },
WithMiddleware(middleware("handler")),
)
assert.NoError(t, err)
handler := w.handlers["email:send"]
assert.NotNil(t, handler)
assert.NoError(t, handler.Process(context.Background(), &Job{Type: "email:send"}))
assert.Equal(t, []string{"group", "handler"}, calls)
}