-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathmonitor_test.go
179 lines (164 loc) · 3.53 KB
/
monitor_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
169
170
171
172
173
174
175
176
177
178
179
package delayqueue
import (
"context"
"log"
"os"
"strconv"
"testing"
"github.com/redis/go-redis/v9"
)
func TestMonitor_get_status(t *testing.T) {
redisCli := redis.NewClient(&redis.Options{
Addr: "127.0.0.1:6379",
})
redisCli.FlushDB(context.Background())
size := 1000
cb := func(s string) bool {
return true
}
logger := log.New(os.Stderr, "[DelayQueue]", log.LstdFlags)
queue := NewQueue("test", redisCli, cb)
monitor := NewMonitor("test", redisCli).WithLogger(logger)
for i := 0; i < size; i++ {
err := queue.SendDelayMsg(strconv.Itoa(i), 0)
if err != nil {
t.Error(err)
}
}
// test pengding count
pending, err := monitor.GetPendingCount()
if err != nil {
t.Error(err)
return
}
if int(pending) != size {
t.Errorf("execting %d, got %d", int(pending), size)
return
}
// test ready count
err = queue.pending2Ready()
if err != nil {
t.Errorf("consume error: %v", err)
return
}
ready, err := monitor.GetReadyCount()
if err != nil {
t.Error(err)
return
}
if int(ready) != size {
t.Errorf("execting %d, got %d", int(pending), size)
return
}
// test processing count
for i := 0; i < size/2; i++ {
_, _ = queue.ready2Unack()
}
processing, err := monitor.GetProcessingCount()
if err != nil {
t.Error(err)
return
}
if int(processing) != size/2 {
t.Errorf("execting %d, got %d", int(pending), size/2)
return
}
}
type MyProfiler struct {
ProduceCount int
DeliverCount int
ConsumeCount int
RetryCount int
FailCount int
}
func (p *MyProfiler) OnEvent(event *Event) {
switch event.Code {
case NewMessageEvent:
p.ProduceCount += event.MsgCount
case DeliveredEvent:
p.DeliverCount += event.MsgCount
case AckEvent:
p.ConsumeCount += event.MsgCount
case RetryEvent:
p.RetryCount += event.MsgCount
case FinalFailedEvent:
p.FailCount += event.MsgCount
}
}
func TestMonitor_listener1(t *testing.T) {
redisCli := redis.NewClient(&redis.Options{
Addr: "127.0.0.1:6379",
})
redisCli.FlushDB(context.Background())
size := 1000
cb := func(s string) bool {
return true
}
queue := NewQueue("test", redisCli, cb)
queue.EnableReport()
monitor := NewMonitor("test", redisCli)
profile := &MyProfiler{}
monitor.ListenEvent(profile)
for i := 0; i < size; i++ {
err := queue.SendDelayMsg(strconv.Itoa(i), 0)
if err != nil {
t.Error(err)
}
}
ids, err := queue.beforeConsume()
if err != nil {
t.Errorf("consume error: %v", err)
return
}
for _, id := range ids {
queue.callback(id)
}
queue.afterConsume()
if profile.ProduceCount != size {
t.Error("wrong produce count")
}
if profile.DeliverCount != size {
t.Error("wrong deliver count")
}
if profile.ConsumeCount != size {
t.Error("wrong consume count")
}
}
func TestMonitor_listener2(t *testing.T) {
redisCli := redis.NewClient(&redis.Options{
Addr: "127.0.0.1:6379",
})
redisCli.FlushDB(context.Background())
size := 1000
cb := func(s string) bool {
return false
}
queue := NewQueue("test", redisCli, cb).WithDefaultRetryCount(1)
queue.EnableReport()
monitor := NewMonitor("test", redisCli)
profile := &MyProfiler{}
monitor.ListenEvent(profile)
for i := 0; i < size; i++ {
err := queue.SendDelayMsg(strconv.Itoa(i), 0)
if err != nil {
t.Error(err)
}
}
for i := 0; i < 3; i++ {
ids, err := queue.beforeConsume()
if err != nil {
t.Errorf("consume error: %v", err)
return
}
for _, id := range ids {
queue.callback(id)
}
queue.afterConsume()
}
if profile.RetryCount != size {
t.Error("wrong deliver count")
}
if profile.FailCount != size {
t.Error("wrong consume count")
}
}