-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtaskq_shutdown_test.go
111 lines (98 loc) · 2.28 KB
/
taskq_shutdown_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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package taskq_test
import (
"context"
"sync/atomic"
"testing"
"time"
"github.com/antonmashko/taskq"
)
func TestGracefulShutdownWithWait_Ok(t *testing.T) {
res := int32(0)
tq := taskq.New(0)
if err := tq.Start(); err != nil {
panic(err)
}
tf := taskq.TaskFunc(func(ctx context.Context) error {
time.Sleep(35 * time.Millisecond)
atomic.AddInt32(&res, 1)
return nil
})
expected := 100
for i := 0; i < expected; i++ {
if _, err := tq.Enqueue(context.Background(), tf); err != nil {
panic(err)
}
}
if err := tq.Shutdown(taskq.ContextWithWait(context.Background())); err != nil {
panic(err)
}
if atomic.LoadInt32(&res) != int32(expected) {
t.Fail()
}
}
func TestGracefulShutdownWithoutWait_Ok(t *testing.T) {
res := int32(0)
tq := taskq.New(10)
tf := taskq.TaskFunc(func(ctx context.Context) error {
time.Sleep(2 * time.Second)
atomic.AddInt32(&res, 1)
return nil
})
if err := tq.Start(); err != nil {
panic(err)
}
expected := 100
for i := 0; i < expected; i++ {
if _, err := tq.Enqueue(context.Background(), tf); err != nil {
panic(err)
}
}
if err := tq.Close(); err != nil {
panic(err)
}
// taskq should not complete all tasks from
// if result equal to expected than graceful should working incorrect
if atomic.LoadInt32(&res) == int32(expected) {
t.Fail()
}
}
func TestGracefulShutdownWithTimeout_Ok(t *testing.T) {
var result bool
tq := taskq.New(10)
ch := make(chan struct{})
tf := taskq.TaskFunc(func(ctx context.Context) error {
ch <- struct{}{}
time.Sleep(10 * time.Second)
result = true
return nil
})
if err := tq.Start(); err != nil {
panic(err)
}
if _, err := tq.Enqueue(context.Background(), tf); err != nil {
panic(err)
}
<-ch
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
err := tq.Shutdown(ctx)
if err == nil {
t.Fatal("Shutdown return nil err")
}
if err != context.DeadlineExceeded {
t.Fatalf("invalid error. expected: %T actual %T", err, context.DeadlineExceeded)
}
if result {
t.Fail()
}
}
func TestCloseAfterClose_Err(t *testing.T) {
tq := taskq.New(0)
err := tq.Close()
if err != nil {
t.Fatal("error on first close:", err)
}
if err = tq.Close(); err != taskq.ErrClosed {
t.Fatalf("invalid error. expected=%s got=%s", taskq.ErrClosed, err)
}
}