-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue_test.go
More file actions
102 lines (87 loc) · 1.93 KB
/
queue_test.go
File metadata and controls
102 lines (87 loc) · 1.93 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
package goqueue
import (
"context"
"errors"
"sync/atomic"
"testing"
"time"
)
func silenceLogs() { Log.Info = func(string, ...any) {}; Log.Error = func(string, ...any) {} }
func TestQueueRunsAllJobs(t *testing.T) {
silenceLogs()
q := New("t", 4)
q.Start(2)
var count int32
for range 10 {
if err := q.Add(Job{Name: "j", Action: func(context.Context) error {
atomic.AddInt32(&count, 1)
return nil
}}); err != nil {
t.Fatalf("Add: %v", err)
}
}
q.Close()
q.Wait()
if got := atomic.LoadInt32(&count); got != 10 {
t.Fatalf("ran %d jobs, want 10", got)
}
}
func TestAddAfterStopReturnsError(t *testing.T) {
silenceLogs()
q := New("t", 1)
q.Start(1)
q.Stop()
q.Wait()
err := q.Add(Job{Name: "j", Action: func(context.Context) error { return nil }})
if !errors.Is(err, ErrQueueStopped) {
t.Fatalf("got %v, want ErrQueueStopped", err)
}
}
func TestStopCancelsJobContext(t *testing.T) {
silenceLogs()
q := New("t", 1)
q.Start(1)
started := make(chan struct{})
done := make(chan error, 1)
_ = q.Add(Job{Name: "slow", Action: func(ctx context.Context) error {
close(started)
<-ctx.Done()
done <- ctx.Err()
return ctx.Err()
}})
<-started
q.Stop()
select {
case err := <-done:
if !errors.Is(err, context.Canceled) {
t.Fatalf("job ctx err %v, want Canceled", err)
}
case <-time.After(time.Second):
t.Fatal("job did not observe cancellation")
}
q.Wait()
}
func TestDoubleCloseSafe(t *testing.T) {
silenceLogs()
q := New("t", 1)
q.Start(1)
q.Close()
q.Close()
q.Wait()
}
func TestJobErrorDoesNotStopWorker(t *testing.T) {
silenceLogs()
q := New("t", 2)
q.Start(1)
var ok int32
_ = q.Add(Job{Name: "bad", Action: func(context.Context) error { return errors.New("boom") }})
_ = q.Add(Job{Name: "good", Action: func(context.Context) error {
atomic.StoreInt32(&ok, 1)
return nil
}})
q.Close()
q.Wait()
if atomic.LoadInt32(&ok) != 1 {
t.Fatal("second job did not run after first failed")
}
}