-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnsqd_test.go
218 lines (174 loc) · 5.04 KB
/
nsqd_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
package nsqd
import (
"fmt"
"io/ioutil"
"log"
"os"
"path"
"strconv"
"testing"
"time"
"github.com/bitly/go-simplejson"
"github.com/bmizerany/assert"
)
func getMetadata(n *NSQD) (*simplejson.Json, error) {
fn := fmt.Sprintf(path.Join(n.options.DataPath, "nsqd.%d.dat"), n.options.ID)
data, err := ioutil.ReadFile(fn)
if err != nil {
if !os.IsNotExist(err) {
log.Printf("ERROR: failed to read channel metadata from %s - %s", fn, err.Error())
}
return nil, err
}
js, err := simplejson.NewJson(data)
if err != nil {
log.Printf("ERROR: failed to parse metadata - %s", err.Error())
return nil, err
}
return js, nil
}
func TestStartup(t *testing.T) {
log.SetOutput(ioutil.Discard)
defer log.SetOutput(os.Stdout)
iterations := 300
doneExitChan := make(chan int)
options := NewNSQDOptions()
options.MemQueueSize = 100
options.MaxBytesPerFile = 10240
_, _, nsqd := mustStartNSQD(options)
topicName := "nsqd_test" + strconv.Itoa(int(time.Now().Unix()))
exitChan := make(chan int)
go func() {
<-exitChan
nsqd.Exit()
doneExitChan <- 1
}()
// verify nsqd metadata shows no topics
err := nsqd.PersistMetadata()
assert.Equal(t, err, nil)
metaData, err := getMetadata(nsqd)
assert.Equal(t, err, nil)
topics, err := metaData.Get("topics").Array()
assert.Equal(t, err, nil)
assert.Equal(t, len(topics), 0)
body := make([]byte, 256)
topic := nsqd.GetTopic(topicName)
for i := 0; i < iterations; i++ {
msg := NewMessage(<-nsqd.idChan, body)
topic.PutMessage(msg)
}
log.Printf("pulling from channel")
channel1 := topic.GetChannel("ch1")
log.Printf("read %d msgs", iterations/2)
for i := 0; i < iterations/2; i++ {
msg := <-channel1.clientMsgChan
log.Printf("read message %d", i+1)
assert.Equal(t, msg.Body, body)
}
for {
if channel1.Depth() == int64(iterations/2) {
break
}
time.Sleep(50 * time.Millisecond)
}
// make sure metadata shows the topic
metaData, err = getMetadata(nsqd)
assert.Equal(t, err, nil)
topics, err = metaData.Get("topics").Array()
assert.Equal(t, err, nil)
assert.Equal(t, len(topics), 1)
observedTopicName, err := metaData.Get("topics").GetIndex(0).Get("name").String()
assert.Equal(t, observedTopicName, topicName)
assert.Equal(t, err, nil)
exitChan <- 1
<-doneExitChan
// start up a new nsqd w/ the same folder
options = NewNSQDOptions()
options.MemQueueSize = 100
options.MaxBytesPerFile = 10240
_, _, nsqd = mustStartNSQD(options)
go func() {
<-exitChan
nsqd.Exit()
doneExitChan <- 1
}()
topic = nsqd.GetTopic(topicName)
// should be empty; channel should have drained everything
count := topic.Depth()
assert.Equal(t, count, int64(0))
channel1 = topic.GetChannel("ch1")
for {
if channel1.Depth() == int64(iterations/2) {
break
}
time.Sleep(50 * time.Millisecond)
}
// read the other half of the messages
for i := 0; i < iterations/2; i++ {
msg := <-channel1.clientMsgChan
log.Printf("read message %d", i+1)
assert.Equal(t, msg.Body, body)
}
// verify we drained things
assert.Equal(t, len(topic.memoryMsgChan), 0)
assert.Equal(t, topic.backend.Depth(), int64(0))
exitChan <- 1
<-doneExitChan
}
func TestEphemeralChannel(t *testing.T) {
// a normal channel sticks around after clients disconnect; an ephemeral channel is
// lazily removed after the last client disconnects
log.SetOutput(ioutil.Discard)
defer log.SetOutput(os.Stdout)
options := NewNSQDOptions()
options.MemQueueSize = 100
_, _, nsqd := mustStartNSQD(options)
topicName := "ephemeral_test" + strconv.Itoa(int(time.Now().Unix()))
doneExitChan := make(chan int)
exitChan := make(chan int)
go func() {
<-exitChan
nsqd.Exit()
doneExitChan <- 1
}()
body := []byte("an_ephemeral_message")
topic := nsqd.GetTopic(topicName)
ephemeralChannel := topic.GetChannel("ch1#ephemeral")
client := newClientV2(0, nil, &context{nsqd})
ephemeralChannel.AddClient(client.ID, client)
msg := NewMessage(<-nsqd.idChan, body)
topic.PutMessage(msg)
msg = <-ephemeralChannel.clientMsgChan
assert.Equal(t, msg.Body, body)
log.Printf("pulling from channel")
ephemeralChannel.RemoveClient(client.ID)
time.Sleep(50 * time.Millisecond)
topic.Lock()
numChannels := len(topic.channelMap)
topic.Unlock()
assert.Equal(t, numChannels, 0)
exitChan <- 1
<-doneExitChan
}
func metadataForChannel(n *NSQD, topicIndex int, channelIndex int) *simplejson.Json {
metadata, _ := getMetadata(n)
mChannels := metadata.Get("topics").GetIndex(topicIndex).Get("channels")
return mChannels.GetIndex(channelIndex)
}
func TestPauseMetadata(t *testing.T) {
log.SetOutput(ioutil.Discard)
defer log.SetOutput(os.Stdout)
options := NewNSQDOptions()
_, _, nsqd := mustStartNSQD(options)
topicName := "pause_metadata" + strconv.Itoa(int(time.Now().Unix()))
topic := nsqd.GetTopic(topicName)
channel := topic.GetChannel("ch")
b, _ := metadataForChannel(nsqd, 0, 0).Get("paused").Bool()
assert.Equal(t, b, false)
channel.Pause()
b, _ = metadataForChannel(nsqd, 0, 0).Get("paused").Bool()
assert.Equal(t, b, true)
channel.UnPause()
b, _ = metadataForChannel(nsqd, 0, 0).Get("paused").Bool()
assert.Equal(t, b, false)
}