-
Notifications
You must be signed in to change notification settings - Fork 0
/
delay_test.go
168 lines (142 loc) · 3.85 KB
/
delay_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
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
package redisson
import (
"context"
"errors"
. "github.com/smartystreets/goconvey/convey"
"testing"
"time"
)
func TestDelay(t *testing.T) {
c := MustNewClient(NewConf(WithDevelopment(false)))
t.Cleanup(func() {
_ = c.Close()
})
if !c.Options().GetDevelopment() {
c.FlushAll(context.Background())
}
var ctx = context.Background()
name := "mock"
prefix := "mock_delay"
task := ([]byte)("task")
Convey("normal delay queue", t, func() {
var notifyChan = make(chan []byte)
var doTime time.Time
q, err := c.NewDelayQueue(name, func(bytes []byte) error {
doTime = nowFunc()
notifyChan <- bytes
return nil
}, WithDelayOptionPrefix(prefix))
So(err, ShouldBeNil)
var l int64
l, err = q.Length(ctx)
So(err, ShouldBeNil)
So(l, ShouldEqual, int64(0))
var addTime = nowFunc()
err = q.Add(ctx, task, 2*time.Second)
So(err, ShouldBeNil)
l, err = q.Length(ctx)
So(err, ShouldBeNil)
So(l, ShouldEqual, int64(1))
select {
case data := <-notifyChan:
So(data, ShouldResemble, task)
So(doTime.Sub(addTime).Seconds(), ShouldBeGreaterThan, 1)
// 保证已经删除掉了
time.Sleep(1 * time.Second)
l, err = q.Length(ctx)
So(err, ShouldBeNil)
So(l, ShouldEqual, int64(0))
So(q.Close(), ShouldBeNil)
}
})
Convey("retry delay queue", t, func() {
var count int
var maxRetryTimes = 4
var afterTimesOK = 2
var notifyChan = make(chan []byte)
q, err := c.NewDelayQueue(name, func(bytes []byte) error {
if count == afterTimesOK {
notifyChan <- bytes
return nil
}
count++
return errors.New("mock error")
}, WithDelayOptionPrefix(prefix), WithDelayOptionRetryTimes(maxRetryTimes))
So(err, ShouldBeNil)
err = q.Add(ctx, task, time.Second)
So(err, ShouldBeNil)
var l int64
l, err = q.Length(ctx)
So(err, ShouldBeNil)
So(l, ShouldEqual, int64(1))
select {
case data := <-notifyChan:
So(data, ShouldResemble, task)
// 保证已经删除掉了
time.Sleep(1 * time.Second)
l, err = q.Length(ctx)
So(err, ShouldBeNil)
So(l, ShouldEqual, int64(0))
So(q.Close(), ShouldBeNil)
}
})
Convey("reclaim delay queue", t, func() {
var notifyChan0, notifyChan1, notifyChan2 = make(chan []byte), make(chan []byte), make(chan struct{})
var q DelayQueue
var err error
var timeout = 3 * time.Second
q, err = c.NewDelayQueue(name, func(bytes []byte) error {
// 当处理的时候,程序崩溃了
_ = q.Close()
notifyChan0 <- bytes
<-notifyChan1
notifyChan2 <- struct{}{}
return nil
}, WithDelayOptionPrefix(prefix), WithDelayOptionTimeout(timeout))
So(err, ShouldBeNil)
err = q.Add(ctx, task, time.Second)
So(err, ShouldBeNil)
var l int64
l, err = q.Length(ctx)
So(err, ShouldBeNil)
So(l, ShouldEqual, int64(1))
select {
case data := <-notifyChan0:
So(data, ShouldResemble, task)
l, err = q.Length(ctx)
So(err, ShouldBeNil)
So(l, ShouldEqual, int64(1))
q, err = c.NewDelayQueue(name, func(bytes []byte) error {
// 重新处理
notifyChan1 <- bytes
return nil
}, WithDelayOptionPrefix(prefix), WithDelayOptionTimeout(timeout))
So(err, ShouldBeNil)
}
<-notifyChan2
So(q.Close(), ShouldBeNil)
})
Convey("delay queue dead letter", t, func() {
var notifyChan = make(chan []byte)
var q DelayQueue
var err error
var timeout = 2 * time.Second
q, err = c.NewDelayQueue(name, func(bytes []byte) error {
return errors.New("mock error")
}, WithDelayOptionPrefix(prefix), WithDelayOptionTimeout(timeout), WithDelayOptionRetryTimes(3), WithDelayOptionHandleDeadLetter(func(bs []byte) {
notifyChan <- bs
}))
So(err, ShouldBeNil)
err = q.Add(ctx, task, time.Second)
So(err, ShouldBeNil)
var l int64
l, err = q.Length(ctx)
So(err, ShouldBeNil)
So(l, ShouldEqual, int64(1))
<-notifyChan
l, err = q.Length(ctx)
So(err, ShouldBeNil)
So(l, ShouldEqual, int64(0))
So(q.Close(), ShouldBeNil)
})
}