-
Notifications
You must be signed in to change notification settings - Fork 285
/
server_limits_test.go
490 lines (448 loc) · 13 KB
/
server_limits_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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
// Copyright 2016-2017 Apcera Inc. All rights reserved.
package server
import (
"strings"
"sync/atomic"
"testing"
"time"
"github.com/nats-io/go-nats-streaming"
"github.com/nats-io/go-nats-streaming/pb"
"github.com/nats-io/nats-streaming-server/stores"
)
func TestTooManyChannelsOnCreateSub(t *testing.T) {
sOpts := GetDefaultOptions()
sOpts.ID = clusterName
sOpts.MaxChannels = 1
s := runServerWithOpts(t, sOpts, nil)
defer s.Shutdown()
sc := NewDefaultConnection(t)
defer sc.Close()
// That should create channel foo
if _, err := sc.Subscribe("foo", func(_ *stan.Msg) {}); err != nil {
t.Fatalf("Unexpected error on subscribe: %v", err)
}
// This should fail because we reached the limit
if _, err := sc.Subscribe("bar", func(_ *stan.Msg) {}); err == nil {
t.Fatalf("Expected error due to too many channels, got none")
}
}
func TestTooManyChannelsOnPublish(t *testing.T) {
sOpts := GetDefaultOptions()
sOpts.ID = clusterName
sOpts.MaxChannels = 1
s := runServerWithOpts(t, sOpts, nil)
defer s.Shutdown()
sc := NewDefaultConnection(t)
defer sc.Close()
// That should create channel foo
if err := sc.Publish("foo", []byte("hello")); err != nil {
t.Fatalf("Unexpected error on subscribe: %v", err)
}
// This should fail since we reached the max channels limit
if err := sc.Publish("bar", []byte("hello")); err == nil {
t.Fatalf("Expected error due to too many channels, got none")
}
// Check that channel bar was not created
if s.channels.get("bar") != nil {
t.Fatal("Channel bar should not have been created")
}
}
func TestTooManySubs(t *testing.T) {
sOpts := GetDefaultOptions()
sOpts.ID = clusterName
sOpts.MaxSubscriptions = 1
s := runServerWithOpts(t, sOpts, nil)
defer s.Shutdown()
sc := NewDefaultConnection(t)
defer sc.Close()
// This should be ok
if _, err := sc.Subscribe("foo", func(_ *stan.Msg) {}); err != nil {
t.Fatalf("Unexpected error on subscribe: %v", err)
}
check := func() {
cs := channelsGet(t, s.channels, "foo")
ss := cs.ss
ss.RLock()
if ss.psubs == nil || len(ss.psubs) != 1 {
stackFatalf(t, "Expected only one subscription, got %v", len(ss.psubs))
}
ss.RUnlock()
subs := s.clients.getSubs(clientName)
if len(subs) != 1 {
stackFatalf(t, "Expected 1 subscription for client %q, got %+v", clientName, subs)
}
}
// We should get an error here
if _, err := sc.Subscribe("foo", func(_ *stan.Msg) {}); err == nil {
t.Fatal("Expected error on subscribe, go none")
}
check()
// Try with a durable
if _, err := sc.Subscribe("foo", func(_ *stan.Msg) {}, stan.DurableName("dur")); err == nil {
t.Fatal("Expected error on subscribe, go none")
}
check()
// And a queue sub
if _, err := sc.QueueSubscribe("foo", "queue", func(_ *stan.Msg) {}); err == nil {
t.Fatal("Expected error on subscribe, go none")
}
check()
// Finally a durable queue sub
if _, err := sc.QueueSubscribe("foo", "queue", func(_ *stan.Msg) {}, stan.DurableName("dur")); err == nil {
t.Fatal("Expected error on subscribe, go none")
}
check()
}
func TestMaxMsgs(t *testing.T) {
sOpts := GetDefaultOptions()
sOpts.ID = clusterName
sOpts.MaxMsgs = 10
s := runServerWithOpts(t, sOpts, nil)
defer s.Shutdown()
sc := NewDefaultConnection(t)
defer sc.Close()
for i := 0; i < 2*sOpts.MaxMsgs; i++ {
sc.Publish("foo", []byte("msg"))
}
// We should not have more than MaxMsgs
cs := channelsGet(t, s.channels, "foo")
if n, _ := msgStoreState(t, cs.store.Msgs); n != sOpts.MaxMsgs {
t.Fatalf("Expected msgs count to be %v, got %v", sOpts.MaxMsgs, n)
}
}
func TestMaxBytes(t *testing.T) {
payload := []byte("hello")
m := pb.MsgProto{Data: payload, Subject: "foo", Sequence: 1, Timestamp: time.Now().UnixNano()}
msgSize := m.Size()
sOpts := GetDefaultOptions()
sOpts.ID = clusterName
sOpts.MaxBytes = int64(msgSize * 10)
s := runServerWithOpts(t, sOpts, nil)
defer s.Shutdown()
sc := NewDefaultConnection(t)
defer sc.Close()
count := 2 * (int(sOpts.MaxBytes) / len(payload))
for i := 0; i < count; i++ {
sc.Publish("foo", payload)
}
// We should not have more than MaxMsgs
cs := channelsGet(t, s.channels, "foo")
if _, b := msgStoreState(t, cs.store.Msgs); b != uint64(sOpts.MaxBytes) {
t.Fatalf("Expected msgs size to be %v, got %v", sOpts.MaxBytes, b)
}
}
func TestDroppedMessagesOnSendToSub(t *testing.T) {
opts := GetDefaultOptions()
opts.MaxMsgs = 3
s := runServerWithOpts(t, opts, nil)
defer s.Shutdown()
sc := NewDefaultConnection(t)
defer sc.Close()
// Produce 1 message
if err := sc.Publish("foo", []byte("hello")); err != nil {
t.Fatalf("Unexpected error on publish: %v", err)
}
// Start a durable, it should receive the message
ch := make(chan bool)
if _, err := sc.Subscribe("foo", func(_ *stan.Msg) {
ch <- true
}, stan.DurableName("dur"),
stan.DeliverAllAvailable()); err != nil {
t.Fatalf("Unexpected error on subscribe: %v", err)
}
// Wait for message
if err := Wait(ch); err != nil {
t.Fatal("Did not get our message")
}
// Close connection
sc.Close()
// Recreate a connection
sc = NewDefaultConnection(t)
defer sc.Close()
// Send messages 2, 3, 4 and 5. Messages 1 and 2 should be dropped.
for i := 2; i <= 5; i++ {
if err := sc.Publish("foo", []byte("hello")); err != nil {
t.Fatalf("Unexpected error on publish: %v", err)
}
}
// Start the durable, it should receive messages 3, 4 and 5
expectedSeq := uint64(3)
good := 0
cb := func(m *stan.Msg) {
if m.Sequence == expectedSeq {
good++
if good == 3 {
ch <- true
}
}
expectedSeq++
}
if _, err := sc.Subscribe("foo", cb,
stan.DurableName("dur"),
stan.DeliverAllAvailable()); err != nil {
t.Fatalf("Unexpected error on subscribe: %v", err)
}
// Wait for messages:
if err := Wait(ch); err != nil {
t.Fatal("Did not get our messages")
}
}
func TestDroppedMessagesOnSendToQueueSub(t *testing.T) {
opts := GetDefaultOptions()
opts.MaxMsgs = 3
s := runServerWithOpts(t, opts, nil)
defer s.Shutdown()
sc := NewDefaultConnection(t)
defer sc.Close()
// Produce 1 message
if err := sc.Publish("foo", []byte("hello")); err != nil {
t.Fatalf("Unexpected error on publish: %v", err)
}
// Start a queue subscriber, it should receive the message
ch := make(chan bool)
blocked := make(chan bool)
if _, err := sc.QueueSubscribe("foo", "bar", func(m *stan.Msg) {
ch <- true
// Block
<-blocked
m.Ack()
}, stan.MaxInflight(1), stan.DeliverAllAvailable()); err != nil {
t.Fatalf("Unexpected error on subscribe: %v", err)
}
// Wait for message
if err := Wait(ch); err != nil {
t.Fatal("Did not get our message")
}
// Send messages 2, 3, 4 and 5. Messages 1 and 2 should be dropped.
for i := 2; i <= 5; i++ {
if err := sc.Publish("foo", []byte("hello")); err != nil {
t.Fatalf("Unexpected error on publish: %v", err)
}
}
// Start another member, it should receive messages 3, 4 and 5
expectedSeq := uint64(3)
good := 0
cb := func(m *stan.Msg) {
if m.Sequence == expectedSeq {
good++
if good == 3 {
ch <- true
}
}
expectedSeq++
}
if _, err := sc.QueueSubscribe("foo", "bar", cb,
stan.DeliverAllAvailable()); err != nil {
t.Fatalf("Unexpected error on subscribe: %v", err)
}
// Wait for messages:
if err := Wait(ch); err != nil {
t.Fatal("Did not get our messages")
}
// Unlock first member
close(blocked)
}
func TestPerChannelLimits(t *testing.T) {
var opts *Options
f := func(idx int) {
if idx == 0 {
opts = GetDefaultOptions()
} else {
cleanupDatastore(t)
defer cleanupDatastore(t)
opts = getTestDefaultOptsForPersistentStore()
if opts.StoreType == stores.TypeFile {
stores.FileStoreTestSetBackgroundTaskInterval(15 * time.Millisecond)
defer stores.FileStoreTestResetBackgroundTaskInterval()
}
}
opts.MaxMsgs = 10
opts.MaxAge = time.Hour
clfoo := stores.ChannelLimits{}
clfoo.MaxMsgs = 2
clbar := stores.ChannelLimits{}
clbar.MaxBytes = 1000
clbaz := stores.ChannelLimits{}
clbaz.MaxSubscriptions = 1
clbaz.MaxAge = 15 * time.Millisecond
sl := &opts.StoreLimits
sl.AddPerChannel("foo", &clfoo)
sl.AddPerChannel("bar", &clbar)
sl.AddPerChannel("baz", &clbaz)
s := runServerWithOpts(t, opts, nil)
defer s.Shutdown()
sc := NewDefaultConnection(t)
defer sc.Close()
// Sending on foo should be limited to 2 messages
for i := 0; i < 10; i++ {
if err := sc.Publish("foo", []byte("hello")); err != nil {
t.Fatalf("Unexpected error on publish: %v", err)
}
}
// Check messages count
s.mu.RLock()
n, _, err := s.channels.msgsState("foo")
s.mu.RUnlock()
if err != nil {
t.Fatalf("Unexpected error getting state: %v", err)
}
if n != clfoo.MaxMsgs {
t.Fatalf("Expected only %v messages, got %v", clfoo.MaxMsgs, n)
}
// Sending on bar should be limited by the size, or the count of global
// setting
for i := 0; i < 100; i++ {
if err := sc.Publish("bar", []byte("hello")); err != nil {
t.Fatalf("Unexpected error on publish: %v", err)
}
}
// Check messages count
s.mu.RLock()
n, b, err := s.channels.msgsState("bar")
s.mu.RUnlock()
if err != nil {
t.Fatalf("Unexpected error getting state: %v", err)
}
// There should be more than for foo, but no more than opts.MaxMsgs
if n <= clfoo.MaxMsgs {
t.Fatalf("Expected more messages than %v", n)
}
if n > opts.MaxMsgs {
t.Fatalf("Should be limited by parent MaxMsgs of %v, got %v", opts.MaxMsgs, n)
}
// The size should be lower than clbar.MaxBytes
if b > uint64(clbar.MaxBytes) {
t.Fatalf("Expected less than %v bytes, got %v", clbar.MaxBytes, b)
}
// Number of subscriptions on baz should be limited to 1
for i := 0; i < 2; i++ {
if _, err := sc.Subscribe("baz", func(_ *stan.Msg) {}); i == 1 && err == nil {
t.Fatal("Expected max subscriptions to be 1")
}
}
// Max Age for baz should be 1sec.
if err := sc.Publish("baz", []byte("hello")); err != nil {
t.Fatalf("Unexpected error on publish: %v", err)
}
// Wait more than max age
time.Sleep(50 * time.Millisecond)
// Check state
s.mu.RLock()
n, _, err = s.channels.msgsState("baz")
s.mu.RUnlock()
if err != nil {
t.Fatalf("Unexpected error getting state: %v", err)
}
if n != 0 {
t.Fatalf("Message should have expired")
}
}
for i := 0; i < 2; i++ {
f(i)
}
}
func TestUnlimitedPerChannelLimits(t *testing.T) {
opts := GetDefaultOptions()
opts.StoreLimits.MaxChannels = 2
// Set very small global limits
opts.StoreLimits.MaxMsgs = 1
opts.StoreLimits.MaxBytes = 1
opts.StoreLimits.MaxAge = time.Millisecond
opts.StoreLimits.MaxSubscriptions = 1
// Add a channel that has all unlimited values
cl := &stores.ChannelLimits{}
cl.MaxMsgs = -1
cl.MaxBytes = -1
cl.MaxAge = -1
cl.MaxSubscriptions = -1
opts.StoreLimits.AddPerChannel("foo", cl)
s := runServerWithOpts(t, opts, nil)
defer s.Shutdown()
sc := NewDefaultConnection(t)
defer sc.Close()
// Check that we can send more than 1 message of more than 1 byte
total := 10
for i := 0; i < total; i++ {
if err := sc.Publish("foo", []byte("hello")); err != nil {
t.Fatalf("Unexpected error on publish: %v", err)
}
}
count := int32(0)
ch := make(chan bool)
cb := func(_ *stan.Msg) {
if c := atomic.AddInt32(&count, 1); c == int32(2*total) {
ch <- true
}
}
for i := 0; i < 2; i++ {
if _, err := sc.Subscribe("foo", cb, stan.DeliverAllAvailable()); err != nil {
t.Fatalf("Unexpected error on subscribe: %v", err)
}
}
if err := Wait(ch); err != nil {
t.Fatal("Did not get our messages")
}
// Wait for more than the global limit MaxAge and verify messages
// are still there
time.Sleep(15 * time.Millisecond)
s.mu.RLock()
n, _, _ := s.channels.msgsState("foo")
s.mu.RUnlock()
if n != total {
t.Fatalf("Should be %v messages, store reports %v", total, n)
}
// Now use a channel not defined in PerChannel and we should be subject
// to global limits.
for i := 0; i < total; i++ {
if err := sc.Publish("bar", []byte("hello")); err != nil {
t.Fatalf("Unexpected error on publish: %v", err)
}
}
if _, err := sc.Subscribe("bar", func(_ *stan.Msg) {}); err != nil {
t.Fatalf("Unexpected error on subscribe: %v", err)
}
// This one should fail
if _, err := sc.Subscribe("bar", func(_ *stan.Msg) {}); err == nil {
t.Fatal("Expected to fail to subscribe, did not")
}
// Wait more than MaxAge
time.Sleep(15 * time.Millisecond)
// Messages should have all disappear
s.mu.RLock()
n, _, _ = s.channels.msgsState("bar")
s.mu.RUnlock()
if n != 0 {
t.Fatalf("Expected 0 messages, store reports %v", n)
}
}
func TestPerChannelLimitsSetToUnlimitedPrintedCorrectly(t *testing.T) {
opts := GetDefaultOptions()
opts.MaxSubscriptions = 10
opts.MaxMsgs = 10
opts.MaxBytes = 64 * 1024
opts.MaxAge = time.Hour
clfoo := stores.ChannelLimits{}
clfoo.MaxSubscriptions = -1
clfoo.MaxMsgs = -1
clfoo.MaxBytes = -1
clfoo.MaxAge = -1
sl := &opts.StoreLimits
sl.AddPerChannel("foo", &clfoo)
l := &captureNoticesLogger{}
opts.EnableLogging = true
opts.CustomLogger = l
s := runServerWithOpts(t, opts, nil)
defer s.Shutdown()
var notices []string
l.Lock()
for _, line := range l.notices {
if strings.Contains(line, "-1") {
notices = l.notices
break
}
}
l.Unlock()
if notices != nil {
t.Fatalf("There should not be -1 values, got %v", notices)
}
}