-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcompat_optskeys_test.go
More file actions
204 lines (183 loc) · 5.77 KB
/
Copy pathcompat_optskeys_test.go
File metadata and controls
204 lines (183 loc) · 5.77 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
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
package gobullmq
import (
"context"
"encoding/json"
"fmt"
"strings"
"testing"
)
func errFake(i int) error { return fmt.Errorf("fail %d", i) }
// Upstream stores shortened option keys (optsAsJSON: failParentOnFailure->fpof,
// keepLogs->kl, removeDependencyOnFailure->rdof) in the job opts JSON and the
// msgpack opts. Go must write and read the same short keys.
func TestOptsUseUpstreamShortKeys(t *testing.T) {
ctx := context.Background()
c := testClient(t)
q, prefix := testQueue(t, c)
job, err := q.Add(ctx, "test", map[string]any{},
AddWithFailParentOnFailure(true),
AddWithRemoveDependencyOnFailure(true),
AddWithKeepLogs(3),
)
if err != nil {
t.Fatal(err)
}
optsJSON, err := c.HGet(ctx, prefix+job.ID(), "opts").Result()
if err != nil {
t.Fatal(err)
}
var opts map[string]any
if err := json.Unmarshal([]byte(optsJSON), &opts); err != nil {
t.Fatal(err)
}
if opts["fpof"] != true || opts["rdof"] != true || opts["kl"] != float64(3) {
t.Fatalf("stored opts lack short keys: %s", optsJSON)
}
for _, long := range []string{"failParentOnFailure", "removeDependencyOnFailure", "keepLogs"} {
if _, has := opts[long]; has {
t.Fatalf("stored opts contain long key %q: %s", long, optsJSON)
}
}
// A JS-written opts blob must parse into the Go fields.
parsed, err := jobOptsFromJson(`{"attempts":0,"delay":0,"fpof":true,"kl":5,"rdof":true}`)
if err != nil {
t.Fatal(err)
}
if !parsed.FailParentOnFailure || !parsed.RemoveDependencyOnFailure || parsed.KeepLogs != 5 {
t.Fatalf("JS opts blob not parsed: %+v", parsed)
}
}
// keepLogs (kl) caps the job's log list on every Log call (job.ts:393-411).
func TestKeepLogsTrimsJobLogs(t *testing.T) {
ctx := context.Background()
c := testClient(t)
q, _ := testQueue(t, c)
job, err := q.Add(ctx, "test", map[string]any{}, AddWithKeepLogs(2))
if err != nil {
t.Fatal(err)
}
for _, m := range []string{"one", "two", "three"} {
n, err := job.Log(ctx, m)
if err != nil {
t.Fatal(err)
}
if n > 2 {
t.Fatalf("Log returned count %d beyond keepLogs", n)
}
}
logs, total, err := job.Logs(ctx, 0, -1)
if err != nil || total != 2 {
t.Fatalf("logs = %v total %d (%v), want the last 2", logs, total, err)
}
if logs[0] != "two" || logs[1] != "three" {
t.Fatalf("kept wrong entries: %v", logs)
}
}
// stackTraceLimit caps the stacktrace array kept on failures (job.ts:1153-1154,
// upstream keeps the first N entries).
func TestStackTraceLimit(t *testing.T) {
ctx := context.Background()
c := testClient(t)
name := testQueueName(t, c)
prefix := "bull:" + name + ":"
q, err := NewQueue[map[string]any](name, c, nil)
if err != nil {
t.Fatal(err)
}
job, err := q.Add(ctx, "test", map[string]any{}, AddWithAttempts(5), AddWithStackTraceLimit(1))
if err != nil {
t.Fatal(err)
}
w, err := NewWorker[map[string]any, string](name, c, nil, nil)
if err != nil {
t.Fatal(err)
}
w.runCtx = ctx
// Two failing attempts accumulate two entries; the limit keeps one.
for i := 0; i < 2; i++ {
token := w.token.String() + ":" + strings.Repeat("y", i+1)
fetched, err := w.moveToActive(ctx, token, "")
if err != nil || fetched == nil {
t.Fatalf("moveToActive attempt %d: %v %v", i, fetched, err)
}
w.handleJobError(*fetched, token, errFake(i))
}
raw, err := c.HGet(ctx, prefix+job.ID(), "stacktrace").Result()
if err != nil {
t.Fatal(err)
}
var st []string
if err := json.Unmarshal([]byte(raw), &st); err != nil {
t.Fatal(err)
}
if len(st) != 1 {
t.Fatalf("stacktrace has %d entries, want 1 (stackTraceLimit): %v", len(st), st)
}
}
// sizeLimit rejects oversized job data at add time (job.ts:1111-1116).
func TestSizeLimitRejectsOversizedData(t *testing.T) {
ctx := context.Background()
c := testClient(t)
q, _ := testQueue(t, c)
_, err := q.Add(ctx, "test", map[string]any{"blob": strings.Repeat("x", 100)}, AddWithSizeLimit(10))
if err == nil || !strings.Contains(err.Error(), "exceeds the limit") {
t.Fatalf("oversized data accepted (err=%v); upstream throws", err)
}
if _, err := q.Add(ctx, "test", map[string]any{"k": 1}, AddWithSizeLimit(1000)); err != nil {
t.Fatalf("small data rejected: %v", err)
}
}
// DefaultJobOptions merge under per-call options, like upstream jobsOpts.
func TestQueueDefaultJobOptions(t *testing.T) {
ctx := context.Background()
c := testClient(t)
name := testQueueName(t, c)
prefix := "bull:" + name + ":"
q, err := NewQueue[map[string]any](name, c, &QueueOptions{
DefaultJobOptions: &JobOptions{Attempts: 5, Backoff: &BackoffOptions{Type: "fixed", Delay: 100}},
})
if err != nil {
t.Fatal(err)
}
job, err := q.Add(ctx, "defaults", map[string]any{})
if err != nil {
t.Fatal(err)
}
optsJSON, _ := c.HGet(ctx, prefix+job.ID(), "opts").Result()
if !strings.Contains(optsJSON, `"attempts":5`) || !strings.Contains(optsJSON, `"type":"fixed"`) {
t.Fatalf("defaults not applied: %s", optsJSON)
}
// Per-call options override defaults.
job2, err := q.Add(ctx, "override", map[string]any{}, AddWithAttempts(2))
if err != nil {
t.Fatal(err)
}
optsJSON, _ = c.HGet(ctx, prefix+job2.ID(), "opts").Result()
if !strings.Contains(optsJSON, `"attempts":2`) {
t.Fatalf("per-call option did not override default: %s", optsJSON)
}
}
// Worker emits 'closed' when close completes (worker.ts:784).
func TestWorkerClosedEvent(t *testing.T) {
ctx := context.Background()
c := testClient(t)
name := testQueueName(t, c)
w, err := NewWorker[map[string]any, string](name, c,
func(ctx context.Context, job *Job[map[string]any]) (string, error) { return "", nil }, nil)
if err != nil {
t.Fatal(err)
}
closed := make(chan struct{}, 1)
w.On("closed", func(...any) { closed <- struct{}{} })
if err := w.Run(ctx); err != nil {
t.Fatal(err)
}
if err := w.Close(); err != nil {
t.Fatal(err)
}
select {
case <-closed:
default:
t.Fatal("no 'closed' event emitted by Close")
}
}