-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchannel_test.go
223 lines (178 loc) · 5.86 KB
/
channel_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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package nsqd
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"strconv"
"testing"
"time"
"github.com/nsqio/nsq/internal/test"
)
// ensure that we can push a message through a topic and get it out of a channel
func TestPutMessage(t *testing.T) {
opts := NewOptions()
opts.Logger = test.NewTestLogger(t)
_, _, nsqd := mustStartNSQD(opts)
defer os.RemoveAll(opts.DataPath)
defer nsqd.Exit()
topicName := "test_put_message" + strconv.Itoa(int(time.Now().Unix()))
topic := nsqd.GetTopic(topicName)
channel1 := topic.GetChannel("ch")
var id MessageID
msg := NewMessage(id, []byte("test"))
topic.PutMessage(msg)
outputMsg := <-channel1.memoryMsgChan
test.Equal(t, msg.ID, outputMsg.ID)
test.Equal(t, msg.Body, outputMsg.Body)
}
// ensure that both channels get the same message
func TestPutMessage2Chan(t *testing.T) {
opts := NewOptions()
opts.Logger = test.NewTestLogger(t)
_, _, nsqd := mustStartNSQD(opts)
defer os.RemoveAll(opts.DataPath)
defer nsqd.Exit()
topicName := "test_put_message_2chan" + strconv.Itoa(int(time.Now().Unix()))
topic := nsqd.GetTopic(topicName)
channel1 := topic.GetChannel("ch1")
channel2 := topic.GetChannel("ch2")
var id MessageID
msg := NewMessage(id, []byte("test"))
topic.PutMessage(msg)
outputMsg1 := <-channel1.memoryMsgChan
test.Equal(t, msg.ID, outputMsg1.ID)
test.Equal(t, msg.Body, outputMsg1.Body)
outputMsg2 := <-channel2.memoryMsgChan
test.Equal(t, msg.ID, outputMsg2.ID)
test.Equal(t, msg.Body, outputMsg2.Body)
}
func TestInFlightWorker(t *testing.T) {
count := 250
opts := NewOptions()
opts.Logger = test.NewTestLogger(t)
opts.MsgTimeout = 100 * time.Millisecond
opts.QueueScanRefreshInterval = 100 * time.Millisecond
_, _, nsqd := mustStartNSQD(opts)
defer os.RemoveAll(opts.DataPath)
defer nsqd.Exit()
topicName := "test_in_flight_worker" + strconv.Itoa(int(time.Now().Unix()))
topic := nsqd.GetTopic(topicName)
channel := topic.GetChannel("channel")
for i := 0; i < count; i++ {
msg := NewMessage(topic.GenerateID(), []byte("test"))
channel.StartInFlightTimeout(msg, 0, opts.MsgTimeout)
}
channel.Lock()
inFlightMsgs := len(channel.inFlightMessages)
channel.Unlock()
test.Equal(t, count, inFlightMsgs)
channel.inFlightMutex.Lock()
inFlightPQMsgs := len(channel.inFlightPQ)
channel.inFlightMutex.Unlock()
test.Equal(t, count, inFlightPQMsgs)
// the in flight worker has a resolution of 100ms so we need to wait
// at least that much longer than our msgTimeout (in worst case)
time.Sleep(4 * opts.MsgTimeout)
channel.Lock()
inFlightMsgs = len(channel.inFlightMessages)
channel.Unlock()
test.Equal(t, 0, inFlightMsgs)
channel.inFlightMutex.Lock()
inFlightPQMsgs = len(channel.inFlightPQ)
channel.inFlightMutex.Unlock()
test.Equal(t, 0, inFlightPQMsgs)
}
func TestChannelEmpty(t *testing.T) {
opts := NewOptions()
opts.Logger = test.NewTestLogger(t)
_, _, nsqd := mustStartNSQD(opts)
defer os.RemoveAll(opts.DataPath)
defer nsqd.Exit()
topicName := "test_channel_empty" + strconv.Itoa(int(time.Now().Unix()))
topic := nsqd.GetTopic(topicName)
channel := topic.GetChannel("channel")
msgs := make([]*Message, 0, 25)
for i := 0; i < 25; i++ {
msg := NewMessage(topic.GenerateID(), []byte("test"))
channel.StartInFlightTimeout(msg, 0, opts.MsgTimeout)
msgs = append(msgs, msg)
}
channel.RequeueMessage(0, msgs[len(msgs)-1].ID, 100*time.Millisecond)
test.Equal(t, 24, len(channel.inFlightMessages))
test.Equal(t, 24, len(channel.inFlightPQ))
test.Equal(t, 1, len(channel.deferredMessages))
test.Equal(t, 1, len(channel.deferredPQ))
channel.Empty()
test.Equal(t, 0, len(channel.inFlightMessages))
test.Equal(t, 0, len(channel.inFlightPQ))
test.Equal(t, 0, len(channel.deferredMessages))
test.Equal(t, 0, len(channel.deferredPQ))
test.Equal(t, int64(0), channel.Depth())
}
func TestChannelEmptyConsumer(t *testing.T) {
opts := NewOptions()
opts.Logger = test.NewTestLogger(t)
tcpAddr, _, nsqd := mustStartNSQD(opts)
defer os.RemoveAll(opts.DataPath)
defer nsqd.Exit()
conn, _ := mustConnectNSQD(tcpAddr)
defer conn.Close()
topicName := "test_channel_empty" + strconv.Itoa(int(time.Now().Unix()))
topic := nsqd.GetTopic(topicName)
channel := topic.GetChannel("channel")
client := newClientV2(0, conn, &context{nsqd})
client.SetReadyCount(25)
channel.AddClient(client.ID, client)
for i := 0; i < 25; i++ {
msg := NewMessage(topic.GenerateID(), []byte("test"))
channel.StartInFlightTimeout(msg, 0, opts.MsgTimeout)
client.SendingMessage()
}
for _, cl := range channel.clients {
stats := cl.Stats()
test.Equal(t, int64(25), stats.InFlightCount)
}
channel.Empty()
for _, cl := range channel.clients {
stats := cl.Stats()
test.Equal(t, int64(0), stats.InFlightCount)
}
}
func TestChannelHealth(t *testing.T) {
opts := NewOptions()
opts.Logger = test.NewTestLogger(t)
opts.MemQueueSize = 2
_, httpAddr, nsqd := mustStartNSQD(opts)
defer os.RemoveAll(opts.DataPath)
defer nsqd.Exit()
topic := nsqd.GetTopic("test")
channel := topic.GetChannel("channel")
channel.backend = &errorBackendQueue{}
msg := NewMessage(topic.GenerateID(), make([]byte, 100))
err := channel.PutMessage(msg)
test.Nil(t, err)
msg = NewMessage(topic.GenerateID(), make([]byte, 100))
err = channel.PutMessage(msg)
test.Nil(t, err)
msg = NewMessage(topic.GenerateID(), make([]byte, 100))
err = channel.PutMessage(msg)
test.NotNil(t, err)
url := fmt.Sprintf("http://%s/ping", httpAddr)
resp, err := http.Get(url)
test.Nil(t, err)
test.Equal(t, 500, resp.StatusCode)
body, _ := ioutil.ReadAll(resp.Body)
resp.Body.Close()
test.Equal(t, "NOK - never gonna happen", string(body))
channel.backend = &errorRecoveredBackendQueue{}
msg = NewMessage(topic.GenerateID(), make([]byte, 100))
err = channel.PutMessage(msg)
test.Nil(t, err)
resp, err = http.Get(url)
test.Nil(t, err)
test.Equal(t, 200, resp.StatusCode)
body, _ = ioutil.ReadAll(resp.Body)
resp.Body.Close()
test.Equal(t, "OK", string(body))
}