forked from nats-io/nats.go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
natspkg_test.go
245 lines (209 loc) · 6.57 KB
/
natspkg_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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package nats
////////////////////////////////////////////////////////////////////////////////
// Package scoped specific tests here..
////////////////////////////////////////////////////////////////////////////////
import (
"errors"
"reflect"
"testing"
"time"
"github.com/nats-io/gnatsd/server"
gnatsd "github.com/nats-io/gnatsd/test"
)
// Dumb wait program to sync on callbacks, etc... Will timeout
func Wait(ch chan bool) error {
return WaitTime(ch, 5*time.Second)
}
func WaitTime(ch chan bool, timeout time.Duration) error {
select {
case <-ch:
return nil
case <-time.After(timeout):
}
return errors.New("timeout")
}
////////////////////////////////////////////////////////////////////////////////
// Reconnect tests
////////////////////////////////////////////////////////////////////////////////
var reconnectOpts = Options{
Url: "nats://localhost:22222",
AllowReconnect: true,
MaxReconnect: 10,
ReconnectWait: 100 * time.Millisecond,
Timeout: DefaultTimeout,
}
func RunServerOnPort(port int) *server.Server {
opts := gnatsd.DefaultTestOptions
opts.Port = port
return RunServerWithOptions(opts)
}
func RunServerWithOptions(opts server.Options) *server.Server {
return gnatsd.RunServer(&opts)
}
func TestReconnectServerStats(t *testing.T) {
ts := RunServerOnPort(22222)
opts := reconnectOpts
nc, _ := opts.Connect()
defer nc.Close()
nc.Flush()
ts.Shutdown()
// server is stopped here...
ts = RunServerOnPort(22222)
defer ts.Shutdown()
if err := nc.FlushTimeout(5 * time.Second); err != nil {
t.Fatalf("Error on Flush: %v", err)
}
// Make sure the server who is reconnected has the reconnects stats reset.
_, cur := nc.currentServer()
if cur.reconnects != 0 {
t.Fatalf("Current Server's reconnects should be 0 vs %d\n", cur.reconnects)
}
}
func TestParseStateReconnectFunctionality(t *testing.T) {
ts := RunServerOnPort(22222)
ch := make(chan bool)
opts := reconnectOpts
dch := make(chan bool)
opts.DisconnectedCB = func(_ *Conn) {
dch <- true
}
nc, errc := opts.Connect()
if errc != nil {
t.Fatalf("Failed to create a connection: %v\n", errc)
}
ec, errec := NewEncodedConn(nc, DEFAULT_ENCODER)
if errec != nil {
nc.Close()
t.Fatalf("Failed to create an encoded connection: %v\n", errec)
}
defer ec.Close()
testString := "bar"
ec.Subscribe("foo", func(s string) {
if s != testString {
t.Fatal("String doesn't match")
}
ch <- true
})
ec.Flush()
// Got a RACE condition with Travis build. The locking below does not
// really help because the parser running in the readLoop accesses
// nc.ps without the connection lock. Sleeping may help better since
// it would make the memory write in parse.go (when processing the
// pong) further away from the modification below.
time.Sleep(1 * time.Second)
// Simulate partialState, this needs to be cleared
nc.mu.Lock()
nc.ps.state = OP_PON
nc.mu.Unlock()
ts.Shutdown()
// server is stopped here...
if err := Wait(dch); err != nil {
t.Fatal("Did not get the DisconnectedCB")
}
if err := ec.Publish("foo", testString); err != nil {
t.Fatalf("Failed to publish message: %v\n", err)
}
ts = RunServerOnPort(22222)
defer ts.Shutdown()
if err := ec.FlushTimeout(5 * time.Second); err != nil {
t.Fatalf("Error on Flush: %v", err)
}
if err := Wait(ch); err != nil {
t.Fatal("Did not receive our message")
}
expectedReconnectCount := uint64(1)
reconnectedCount := ec.Conn.Stats().Reconnects
if reconnectedCount != expectedReconnectCount {
t.Fatalf("Reconnect count incorrect: %d vs %d\n",
reconnectedCount, expectedReconnectCount)
}
}
////////////////////////////////////////////////////////////////////////////////
// ServerPool tests
////////////////////////////////////////////////////////////////////////////////
var testServers = []string{
"nats://localhost:1222",
"nats://localhost:1223",
"nats://localhost:1224",
"nats://localhost:1225",
"nats://localhost:1226",
"nats://localhost:1227",
"nats://localhost:1228",
}
func TestServersRandomize(t *testing.T) {
opts := DefaultOptions
opts.Servers = testServers
nc := &Conn{Opts: opts}
if err := nc.setupServerPool(); err != nil {
t.Fatalf("Problem setting up Server Pool: %v\n", err)
}
// Build []string from srvPool
clientServers := []string{}
for _, s := range nc.srvPool {
clientServers = append(clientServers, s.url.String())
}
// In theory this could happen..
if reflect.DeepEqual(testServers, clientServers) {
t.Fatalf("ServerPool list not randomized\n")
}
// Now test that we do not randomize if proper flag is set.
opts = DefaultOptions
opts.Servers = testServers
opts.NoRandomize = true
nc = &Conn{Opts: opts}
if err := nc.setupServerPool(); err != nil {
t.Fatalf("Problem setting up Server Pool: %v\n", err)
}
// Build []string from srvPool
clientServers = []string{}
for _, s := range nc.srvPool {
clientServers = append(clientServers, s.url.String())
}
if !reflect.DeepEqual(testServers, clientServers) {
t.Fatalf("ServerPool list should not be randomized\n")
}
}
func TestSelectNextServer(t *testing.T) {
opts := DefaultOptions
opts.Servers = testServers
opts.NoRandomize = true
nc := &Conn{Opts: opts}
if err := nc.setupServerPool(); err != nil {
t.Fatalf("Problem setting up Server Pool: %v\n", err)
}
if nc.url != nc.srvPool[0].url {
t.Fatalf("Wrong default selection: %v\n", nc.url)
}
sel, err := nc.selectNextServer()
if err != nil {
t.Fatalf("Got an err: %v\n", err)
}
// Check that we are now looking at #2, and current is now last.
if len(nc.srvPool) != len(testServers) {
t.Fatalf("List is incorrect size: %d vs %d\n", len(nc.srvPool), len(testServers))
}
if nc.url.String() != testServers[1] {
t.Fatalf("Selection incorrect: %v vs %v\n", nc.url, testServers[1])
}
if nc.srvPool[len(nc.srvPool)-1].url.String() != testServers[0] {
t.Fatalf("Did not push old to last position\n")
}
if sel != nc.srvPool[0] {
t.Fatalf("Did not return correct server: %v vs %v\n", sel.url, nc.srvPool[0].url)
}
// Test that we do not keep servers where we have tried to reconnect past our limit.
nc.srvPool[0].reconnects = int(opts.MaxReconnect)
if _, err := nc.selectNextServer(); err != nil {
t.Fatalf("Got an err: %v\n", err)
}
// Check that we are now looking at #3, and current is not in the list.
if len(nc.srvPool) != len(testServers)-1 {
t.Fatalf("List is incorrect size: %d vs %d\n", len(nc.srvPool), len(testServers)-1)
}
if nc.url.String() != testServers[2] {
t.Fatalf("Selection incorrect: %v vs %v\n", nc.url, testServers[2])
}
if nc.srvPool[len(nc.srvPool)-1].url.String() == testServers[1] {
t.Fatalf("Did not throw away the last server correctly\n")
}
}