-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathpublisher_test.go
60 lines (56 loc) · 1.26 KB
/
publisher_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
package delayqueue
import (
"context"
"log"
"os"
"strconv"
"testing"
"time"
"github.com/redis/go-redis/v9"
)
func TestPublisher(t *testing.T) {
redisCli := redis.NewClient(&redis.Options{
Addr: "127.0.0.1:6379",
})
redisCli.FlushDB(context.Background())
size := 1000
retryCount := 3
deliveryCount := make(map[string]int)
cb := func(s string) bool {
deliveryCount[s]++
i, _ := strconv.ParseInt(s, 10, 64)
return i%2 == 0
}
logger := log.New(os.Stderr, "[DelayQueue]", log.LstdFlags)
queue := NewQueue("test", redisCli, cb).WithLogger(logger)
publisher := NewPublisher("test", redisCli).WithLogger(logger)
for i := 0; i < size; i++ {
err := publisher.SendDelayMsg(strconv.Itoa(i), 0, WithRetryCount(retryCount), WithMsgTTL(time.Hour))
if err != nil {
t.Error(err)
}
}
for i := 0; i < 10*size; i++ {
ids, err := queue.beforeConsume()
if err != nil {
t.Errorf("consume error: %v", err)
return
}
for _, id := range ids {
queue.callback(id)
}
queue.afterConsume()
}
for k, v := range deliveryCount {
i, _ := strconv.ParseInt(k, 10, 64)
if i%2 == 0 {
if v != 1 {
t.Errorf("expect 1 delivery, actual %d", v)
}
} else {
if v != retryCount+1 {
t.Errorf("expect %d delivery, actual %d", retryCount+1, v)
}
}
}
}