forked from adjust/rmq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleaner_test.go
140 lines (114 loc) · 4.02 KB
/
cleaner_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
package rmq
import (
"testing"
"time"
. "github.com/adjust/gocheck"
)
func TestCleanerSuite(t *testing.T) {
TestingSuiteT(&CleanerSuite{}, t)
}
type CleanerSuite struct{}
func (suite *CleanerSuite) TestCleaner(c *C) {
flushConn := OpenConnection("cleaner-flush", "tcp", "localhost:6379", 1)
flushConn.flushDb()
flushConn.StopHeartbeat()
conn := OpenConnection("cleaner-conn1", "tcp", "localhost:6379", 1)
c.Check(conn.GetOpenQueues(), HasLen, 0)
queue := conn.OpenQueue("q1").(*redisQueue)
c.Check(conn.GetOpenQueues(), HasLen, 1)
conn.OpenQueue("q2")
c.Check(conn.GetOpenQueues(), HasLen, 2)
c.Check(queue.ReadyCount(), Equals, 0)
queue.Publish("del1")
c.Check(queue.ReadyCount(), Equals, 1)
queue.Publish("del2")
c.Check(queue.ReadyCount(), Equals, 2)
queue.Publish("del3")
c.Check(queue.ReadyCount(), Equals, 3)
queue.Publish("del4")
c.Check(queue.ReadyCount(), Equals, 4)
queue.Publish("del5")
c.Check(queue.ReadyCount(), Equals, 5)
queue.Publish("del6")
c.Check(queue.ReadyCount(), Equals, 6)
c.Check(queue.UnackedCount(), Equals, 0)
queue.StartConsuming(2, time.Millisecond)
time.Sleep(time.Millisecond)
c.Check(queue.UnackedCount(), Equals, 2)
c.Check(queue.ReadyCount(), Equals, 4)
consumer := NewTestConsumer("c-A")
consumer.AutoFinish = false
consumer.AutoAck = false
queue.AddConsumer("consumer1", consumer)
time.Sleep(2 * time.Millisecond)
c.Check(queue.UnackedCount(), Equals, 3)
c.Check(queue.ReadyCount(), Equals, 3)
c.Assert(consumer.LastDelivery, NotNil)
c.Check(consumer.LastDelivery.Payload(), Equals, "del1")
c.Check(consumer.LastDelivery.Ack(), Equals, true)
time.Sleep(2 * time.Millisecond)
c.Check(queue.UnackedCount(), Equals, 2)
c.Check(queue.ReadyCount(), Equals, 3)
consumer.Finish()
time.Sleep(2 * time.Millisecond)
c.Check(queue.UnackedCount(), Equals, 3)
c.Check(queue.ReadyCount(), Equals, 2)
c.Check(consumer.LastDelivery.Payload(), Equals, "del2")
queue.StopConsuming()
conn.StopHeartbeat()
time.Sleep(time.Millisecond)
conn = OpenConnection("cleaner-conn1", "tcp", "localhost:6379", 1)
queue = conn.OpenQueue("q1").(*redisQueue)
queue.Publish("del7")
c.Check(queue.ReadyCount(), Equals, 3)
queue.Publish("del7")
c.Check(queue.ReadyCount(), Equals, 4)
queue.Publish("del8")
c.Check(queue.ReadyCount(), Equals, 5)
queue.Publish("del9")
c.Check(queue.ReadyCount(), Equals, 6)
queue.Publish("del10")
c.Check(queue.ReadyCount(), Equals, 7)
c.Check(queue.UnackedCount(), Equals, 0)
queue.StartConsuming(2, time.Millisecond)
time.Sleep(time.Millisecond)
c.Check(queue.UnackedCount(), Equals, 2)
c.Check(queue.ReadyCount(), Equals, 5)
consumer = NewTestConsumer("c-B")
consumer.AutoFinish = false
consumer.AutoAck = false
queue.AddConsumer("consumer2", consumer)
time.Sleep(2 * time.Millisecond)
c.Check(queue.UnackedCount(), Equals, 3)
c.Check(queue.ReadyCount(), Equals, 4)
c.Check(consumer.LastDelivery.Payload(), Equals, "del5")
consumer.Finish() // unacked
time.Sleep(2 * time.Millisecond)
c.Check(queue.UnackedCount(), Equals, 4)
c.Check(queue.ReadyCount(), Equals, 3)
c.Check(consumer.LastDelivery.Payload(), Equals, "del6")
c.Check(consumer.LastDelivery.Ack(), Equals, true)
time.Sleep(2 * time.Millisecond)
c.Check(queue.UnackedCount(), Equals, 3)
c.Check(queue.ReadyCount(), Equals, 3)
queue.StopConsuming()
conn.StopHeartbeat()
time.Sleep(time.Millisecond)
cleanerConn := OpenConnection("cleaner-conn", "tcp", "localhost:6379", 1)
cleaner := NewCleaner(cleanerConn)
c.Check(cleaner.Clean(), IsNil)
c.Check(queue.ReadyCount(), Equals, 9) // 2 of 11 were acked above
c.Check(conn.GetOpenQueues(), HasLen, 2)
conn = OpenConnection("cleaner-conn1", "tcp", "localhost:6379", 1)
queue = conn.OpenQueue("q1").(*redisQueue)
queue.StartConsuming(10, time.Millisecond)
consumer = NewTestConsumer("c-C")
queue.AddConsumer("consumer3", consumer)
time.Sleep(10 * time.Millisecond)
c.Check(consumer.LastDeliveries, HasLen, 9)
queue.StopConsuming()
conn.StopHeartbeat()
time.Sleep(time.Millisecond)
c.Check(cleaner.Clean(), IsNil)
cleanerConn.StopHeartbeat()
}