/
server_test.go
1370 lines (1257 loc) · 38.9 KB
/
server_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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2016-2019 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package server
import (
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"reflect"
"runtime"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
natsd "github.com/nats-io/nats-server/v2/server"
"github.com/nats-io/nats-streaming-server/logger"
"github.com/nats-io/nats-streaming-server/stores"
"github.com/nats-io/nats-streaming-server/test"
"github.com/nats-io/nats.go"
"github.com/nats-io/nuid"
"github.com/nats-io/stan.go"
"github.com/nats-io/stan.go/pb"
_ "github.com/go-sql-driver/mysql" // mysql driver
_ "github.com/lib/pq" // postgres driver
_ "github.com/nats-io/nats-streaming-server/stores/pqdeadlines" // wrapper for postgres that gives read/write deadlines
)
const (
clusterName = DefaultClusterID
clientName = "me"
)
// So that we can pass tests and benchmarks...
type tLogger interface {
Fatalf(format string, args ...interface{})
Errorf(format string, args ...interface{})
}
var (
defaultDataStore string
testLogger logger.Logger
errOnPurpose = errors.New("on purpose")
benchStoreType = stores.TypeMemory
persistentStoreType = stores.TypeFile
testUseEncryption bool
testEncryptionKey = "testkey"
)
// The SourceAdmin is used by the test setup to have access
// to the database server and create the test streaming database.
// The Source contains the URL that the Store needs to actually
// connect to the server and use the database.
const (
testDefaultDatabaseName = "test_server_nats_streaming"
testDefaultMySQLSource = "nss:password@/" + testDefaultDatabaseName
testDefaultMySQLSourceAdmin = "nss:password@/"
testDefaultPostgresSource = "sslmode=disable dbname=" + testDefaultDatabaseName
testDefaultPostgresSourceAdmin = "sslmode=disable"
)
var (
testSQLDriver = test.DriverMySQL
testSQLSource = testDefaultMySQLSource
testSQLSourceAdmin = testDefaultMySQLSourceAdmin
testSQLDatabaseName = testDefaultDatabaseName
testDBSuffixes = []string{"", "_a", "_b", "_c"}
doSQL = false
)
func TestMain(m *testing.M) {
var (
bst string
pst string
sqlCreateDb bool
sqlDeleteDb bool
)
flag.StringVar(&bst, "bench_store", "", "store type for bench tests (mem, file)")
flag.StringVar(&pst, "persistent_store", "", "store type for server recovery related tests (file)")
// This one is added here so that if we want to disable sql for stores tests
// we can use the same param for all packages as in "go test -v ./... -sql=false"
flag.Bool("sql", false, "Not used for server tests")
// Those 2 sql related flags are handled here, not in AddSQLFlags
flag.BoolVar(&sqlCreateDb, "sql_create_db", true, "create sql database on startup")
flag.BoolVar(&sqlDeleteDb, "sql_delete_db", true, "delete sql database on exit")
flag.BoolVar(&testUseEncryption, "encrypt", false, "use encryption")
flag.StringVar(&testEncryptionKey, "encryption_key", string(testEncryptionKey), "encryption key")
test.AddSQLFlags(flag.CommandLine, &testSQLDriver, &testSQLSource, &testSQLSourceAdmin, &testSQLDatabaseName)
flag.Parse()
bst = strings.ToUpper(bst)
pst = strings.ToUpper(pst)
switch bst {
case "":
// use default
case stores.TypeMemory, stores.TypeFile, stores.TypeSQL:
benchStoreType = bst
default:
fmt.Printf("Unknown store %q for bench tests\n", bst)
os.Exit(2)
}
// Will add DB store and others when avail
switch pst {
case "":
// use default
case stores.TypeFile, stores.TypeSQL:
persistentStoreType = pst
default:
fmt.Printf("Unknown or unsupported store %q for persistent store server tests\n", pst)
os.Exit(2)
}
// If either (or both) bench or tests select an SQL store, we need to do
// so initializing and cleaning at the end of the test.
doSQL = benchStoreType == stores.TypeSQL || persistentStoreType == stores.TypeSQL
if doSQL {
defaultSources := make(map[string][]string)
defaultSources[test.DriverMySQL] = []string{testDefaultMySQLSource, testDefaultMySQLSourceAdmin}
defaultSources[test.DriverPostgres] = []string{testDefaultPostgresSource, testDefaultPostgresSourceAdmin}
if err := test.ProcessSQLFlags(flag.CommandLine, defaultSources); err != nil {
fmt.Println(err.Error())
os.Exit(2)
}
if sqlCreateDb {
for _, n := range testDBSuffixes {
// Create the SQL Database once, the cleanup is simply deleting
// content from tables (so we don't have to recreate them).
if err := test.CreateSQLDatabase(testSQLDriver, testSQLSourceAdmin,
testSQLSource+n, testSQLDatabaseName+n); err != nil {
fmt.Printf("Error initializing SQL Datastore: %v", err)
os.Exit(2)
}
}
}
}
ret := m.Run()
if doSQL && sqlDeleteDb {
// Now that the tests/benchs are done, delete the database
for _, n := range testDBSuffixes {
test.DeleteSQLDatabase(testSQLDriver, testSQLSourceAdmin, testSQLDatabaseName+n)
}
}
os.Exit(ret)
}
func init() {
tmpDir, err := ioutil.TempDir(".", "data_server_")
if err != nil {
panic("Could not create tmp dir")
}
if err := os.Remove(tmpDir); err != nil {
panic(fmt.Errorf("Error removing temp directory: %v", err))
}
defaultDataStore = tmpDir
// Set debug and trace for this file.
defaultOptions.Trace = true
defaultOptions.Debug = true
// For FT tests, reduce the HB/Timeout intervals
setFTTestsHBInterval()
// Dummy/no-op Logger
testLogger = logger.NewStanLogger()
// Make the server interpret all our sub's AckWait() as milliseconds instead
// of seconds.
testAckWaitIsInMillisecond = true
// For tests that use FileStore and do message expiration, etc..
stores.FileStoreTestSetBackgroundTaskInterval(15 * time.Millisecond)
}
func stackFatalf(t tLogger, f string, args ...interface{}) {
msg := fmt.Sprintf(f, args...) + "\n" + stack()
t.Fatalf(msg)
// For staticcheck SA0511...
panic("unreachable code")
}
func stack() string {
lines := make([]string, 0, 32)
// Generate the Stack of callers:
for i := 1; true; i++ {
_, file, line, ok := runtime.Caller(i)
if !ok {
break
}
msg := fmt.Sprintf("%d - %s:%d", i, file, line)
lines = append(lines, msg)
}
return strings.Join(lines, "\n")
}
func printAllStacks() {
size := 1024 * 1024
buf := make([]byte, size)
n := 0
for {
n = runtime.Stack(buf, true)
if n < size {
break
}
size *= 2
buf = make([]byte, size)
}
fmt.Printf("Go-routines:\n%s\n", string(buf[:n]))
}
func msgStoreFirstAndLastSequence(t tLogger, ms stores.MsgStore) (uint64, uint64) {
f, l, err := ms.FirstAndLastSequence()
if err != nil {
stackFatalf(t, "Error getting first and last sequence: %v", err)
}
return f, l
}
func msgStoreFirstMsg(t tLogger, ms stores.MsgStore) *pb.MsgProto {
m, err := ms.FirstMsg()
if err != nil {
stackFatalf(t, "Error getting sequence first message: %v", err)
}
return m
}
func msgStoreState(t tLogger, ms stores.MsgStore) (int, uint64) {
n, b, err := ms.State()
if err != nil {
stackFatalf(t, "Error getting message state: %v", err)
}
return n, b
}
func channelsGet(t tLogger, cs *channelStore, name string) *channel {
c := cs.get(name)
if c == nil {
stackFatalf(t, "Channel %q should exist", name)
}
return c
}
func channelsLookupOrCreate(t tLogger, s *StanServer, name string) *channel {
c, err := s.lookupOrCreateChannel(name)
if err != nil {
stackFatalf(t, "Error creating/looking up channel %q: %v", name, err)
}
return c
}
// Helper function to shutdown last, a server that is being restarted in a test.
func shutdownRestartedServerOnTestExit(s **StanServer) {
srv := *s
srv.Shutdown()
srv = nil
}
// Helper function to shutdown last, a NATS server that is being restarted in a test.
func shutdownRestartedNATSServerOnTestExit(s **natsd.Server) {
srv := *s
srv.Shutdown()
srv = nil
}
// Helper function that checks that the number returned by function `f`
// is equal to `expected`, otherwise fails.
func checkCount(t tLogger, expected int, f func() (string, int)) {
if label, count := f(); count != expected {
stackFatalf(t, "Incorrect number of %s, expected %v got %v", label, expected, count)
}
}
// Helper function that waits up to totalWait for the function `f` to return
// without error. When f returns error, this function sleeps for waitInBetween.
// At the end of the totalWait, the last reported error from `f` causes the
// test to fail.
func waitFor(t tLogger, totalWait, waitInBetween time.Duration, f func() error) {
timeout := time.Now().Add(totalWait)
var err error
for time.Now().Before(timeout) {
err = f()
if err == nil {
return
}
time.Sleep(waitInBetween)
}
if err != nil {
stackFatalf(t, err.Error())
}
}
// Helper function that waits that the number returned by function `f`
// is equal to `expected` for a certain period of time, otherwise fails.
func waitForCount(t tLogger, expected int, f func() (string, int)) {
waitFor(t, 5*time.Second, 10*time.Millisecond, func() error {
label, count := f()
if count != expected {
return fmt.Errorf("Timeout waiting to get %v %s, got %v", expected, label, count)
}
return nil
})
}
// Helper function that fails if number of clients is not as expected
func checkClients(t tLogger, s *StanServer, expected int) {
checkCount(t, expected, func() (string, int) { return getClientsCountFunc(s) })
}
// Helper function that waits for a while to get the expected number of clients,
// otherwise fails.
func waitForNumClients(t tLogger, s *StanServer, expected int) {
waitForCount(t, expected, func() (string, int) { return getClientsCountFunc(s) })
}
// Helper function that returns the number of clients
func getClientsCountFunc(s *StanServer) (string, int) {
return "clients", s.clients.count()
}
// Helper function that fails if number of subscriptions is not as expected
func checkSubs(t tLogger, s *StanServer, ID string, expected int) []*subState {
// Since we need to return the array and we want the array to match
// the expected value, use the "public" API here.
subs := s.clients.getSubs(ID)
checkCount(t, expected, func() (string, int) { return "subscriptions", len(subs) })
return subs
}
// Helper function that waits for a while to get the expected number of subscriptions,
// otherwise fails.
func waitForNumSubs(t tLogger, s *StanServer, ID string, expected int) {
waitForCount(t, expected, func() (string, int) {
// We avoid getting a copy of the subscriptions array here
// by directly returning the length of the array.
c := s.clients.lookup(ID)
if c == nil {
// Could happen in clustering mode when creation
// of channel did not happen yet in a node and test
// if checking that node. Just return something different
// from expected to cause waitForCount to try again.
return "subscriptions", -1
}
c.RLock()
defer c.RUnlock()
return "subscriptions", len(c.subs)
})
}
func waitForAcks(t tLogger, s *StanServer, ID string, subID uint64, expected int) {
var sub *subState
waitFor(t, 5*time.Second, 15*time.Millisecond, func() error {
subs := s.clients.getSubs(ID)
for _, s := range subs {
s.RLock()
sID := s.ID
s.RUnlock()
if sID == subID {
sub = s
break
}
}
if sub == nil {
return fmt.Errorf("Subscription %v not found", subID)
}
return nil
})
waitForCount(t, expected, func() (string, int) {
sub.RLock()
count := len(sub.acksPending)
sub.RUnlock()
return "ack pending", count
})
}
func createConnectionWithNatsOpts(t tLogger, clientName string,
natsOpts ...nats.Option) (stan.Conn, *nats.Conn) {
opts := nats.DefaultOptions
opts.Servers = []string{nats.DefaultURL}
for _, opt := range natsOpts {
if err := opt(&opts); err != nil {
stackFatalf(t, "Unexpected error on setting options: %v", err)
}
}
nc, err := opts.Connect()
if err != nil {
stackFatalf(t, "Unexpected error on connect: %v", err)
}
sc, err := stan.Connect(clusterName, clientName, stan.NatsConn(nc))
if err != nil {
nc.Close()
stackFatalf(t, "Unexpected error on connect: %v", err)
}
return sc, nc
}
func NewDefaultConnection(t tLogger) stan.Conn {
sc, err := stan.Connect(clusterName, clientName)
if err != nil {
stackFatalf(t, "Expected to connect correctly, got err %v", err)
}
return sc
}
func cleanupDatastore(t tLogger) {
switch persistentStoreType {
case stores.TypeFile:
if err := os.RemoveAll(defaultDataStore); err != nil {
stackFatalf(t, "Error cleaning up datastore: %v", err)
}
case stores.TypeSQL:
for _, n := range testDBSuffixes {
test.CleanupSQLDatastore(t, testSQLDriver, testSQLSource+n)
}
}
}
func getTestDefaultOptsForPersistentStore() *Options {
opts := GetDefaultOptions()
opts.StoreType = persistentStoreType
switch persistentStoreType {
case stores.TypeFile:
opts.FilestoreDir = defaultDataStore
opts.FileStoreOpts.BufferSize = 1024
// Go 1.12 on macOS is very slow at doing sync writes...
if runtime.GOOS == "darwin" && strings.HasPrefix(runtime.Version(), "go1.12") {
opts.FileStoreOpts.DoSync = false
}
case stores.TypeSQL:
opts.SQLStoreOpts.Driver = testSQLDriver
opts.SQLStoreOpts.Source = testSQLSource
default:
panic(fmt.Sprintf("Need to specify configuration for store: %q", persistentStoreType))
}
return opts
}
func ackWaitInMs(val int) time.Duration {
// When creating a subscription without AckWait(), the library
// sends AckWaitInSecs==30 for 30 seconds. If we want to
// use milliseconds we are going to send a negative value
// corresponding to the number of milliseconds we want.
// The server variable testAckWaitIsInMillisecond is set
// to true at the beginning of the test suite. With that,
// the server will use the absoulute value and interpret
// is as milliseconds.
return time.Duration(val*-1) * time.Second
}
// 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")
}
func runServerWithOpts(t *testing.T, sOpts *Options, nOpts *natsd.Options) *StanServer {
if testUseEncryption {
if sOpts == nil {
sOpts = GetDefaultOptions()
}
sOpts.Encrypt = true
sOpts.EncryptionKey = []byte(testEncryptionKey)
}
s, err := RunServerWithOpts(sOpts, nOpts)
if err != nil {
stackFatalf(t, err.Error())
}
if testUseEncryption {
// Since the key was cleared when creating the crypto store,
// restore now for the rest of tests to work properly.
sOpts.EncryptionKey = []byte(testEncryptionKey)
}
return s
}
func runServer(t *testing.T, clusterName string) *StanServer {
sOpts := GetDefaultOptions()
sOpts.ID = clusterName
nOpts := DefaultNatsServerOptions
return runServerWithOpts(t, sOpts, &nOpts)
}
type testChannelStoreFailStore struct{ stores.Store }
func (s *testChannelStoreFailStore) CreateChannel(name string) (*stores.Channel, error) {
return nil, errOnPurpose
}
type dummyLogger struct {
sync.Mutex
msg string
}
func (d *dummyLogger) log(format string, args ...interface{}) {
d.Lock()
d.msg = fmt.Sprintf(format, args...)
d.Unlock()
}
func (d *dummyLogger) Noticef(format string, args ...interface{}) { d.log(format, args...) }
func (d *dummyLogger) Debugf(format string, args ...interface{}) { d.log(format, args...) }
func (d *dummyLogger) Tracef(format string, args ...interface{}) { d.log(format, args...) }
func (d *dummyLogger) Errorf(format string, args ...interface{}) { d.log(format, args...) }
func (d *dummyLogger) Fatalf(format string, args ...interface{}) { d.log(format, args...) }
func (d *dummyLogger) Warnf(format string, args ...interface{}) { d.log(format, args...) }
func TestVersionMatchesTag(t *testing.T) {
tag := os.Getenv("TRAVIS_TAG")
if tag == "" {
t.SkipNow()
}
// We expect a tag of the form vX.Y.Z. If that's not the case,
// we need someone to have a look. So fail if first letter is not
// a `v`
if tag[0] != 'v' {
t.Fatalf("Expect tag to start with `v`, tag is: %s", tag)
}
// Strip the `v` from the tag for the version comparison.
if VERSION != tag[1:] {
t.Fatalf("Version (%s) does not match tag (%s)", VERSION, tag[1:])
}
}
func TestChannelStore(t *testing.T) {
s := runServer(t, clusterName)
defer s.Shutdown()
cs := newChannelStore(s, s.store)
if cs.get("foo") != nil {
t.Fatal("Nothing should be returned")
}
c, err := cs.createChannel(s, "foo")
if err != nil {
t.Fatalf("Error creating channel: %v", err)
}
c2 := cs.get("foo")
if c2 != c {
t.Fatalf("Channels should be same, got %v vs %v", c2, c)
}
c3, err := cs.createChannel(s, "foo")
if err != nil {
t.Fatalf("Error creating channel: %v", err)
}
if c3 != c {
t.Fatalf("Channels should be same, got %v vs %v", c3, c)
}
c4, err := cs.createChannel(s, "bar")
if err != nil {
t.Fatalf("Error creating channel: %v", err)
}
if cs.count() != 2 {
t.Fatalf("Expected 2 channels, got %v", err)
}
channels := cs.getAll()
for k, v := range channels {
if k != "foo" && k != "bar" {
t.Fatalf("Unexpected channel name: %v", k)
}
if k == "foo" && v != c {
t.Fatalf("Unexpected channel for foo, expected %v, got %v", c, v)
} else if k == "bar" && v != c4 {
t.Fatalf("Unexpected channel for bar, expected %v, got %v", c4, v)
}
}
if _, _, err := cs.msgsState("baz"); err == nil || !strings.Contains(err.Error(), "not found") {
t.Fatalf("Channel baz does not exist, call should have failed, got %v", err)
}
c.store.Msgs.Store(&pb.MsgProto{Sequence: 1, Data: []byte("foo")})
c4.store.Msgs.Store(&pb.MsgProto{Sequence: 1, Data: []byte("bar")})
if n, _, err := cs.msgsState(""); n != 2 || err != nil {
t.Fatalf("Expected 2 messages, got %v err=%v", n, err)
}
// Produce store failure
cs.Lock()
cs.store = &testChannelStoreFailStore{Store: cs.store}
cs.Unlock()
if c, err := cs.createChannel(s, "error"); c != nil || err == nil {
t.Fatalf("Should have failed, got %v err=%v", c, err)
}
}
func TestDefaultOptions(t *testing.T) {
opts := GetDefaultOptions()
opts.Debug = !defaultOptions.Debug
opts2 := GetDefaultOptions()
if opts2.Debug == opts.Debug {
t.Fatal("Modified original default options")
}
}
func TestOptionsClone(t *testing.T) {
opts := GetDefaultOptions()
opts.Trace = true
opts.PerChannel = make(map[string]*stores.ChannelLimits)
cl := &stores.ChannelLimits{}
cl.MaxMsgs = 100
opts.PerChannel["foo"] = cl
clone := opts.Clone()
if !reflect.DeepEqual(opts, clone) {
t.Fatalf("Expected %#v, got %#v", opts, clone)
}
// Change a field
opts.Trace = false
// Expecting the clone to now be different
if reflect.DeepEqual(opts, clone) {
t.Fatal("Expected clone to be different after original changed, was not")
}
// Revert the field change
opts.Trace = true
// Should be same again
if !reflect.DeepEqual(opts, clone) {
t.Fatalf("Expected %#v, got %#v", opts, clone)
}
// Change a per channel's element
cl.MaxMsgs = 50
// Expecting the clone to now be different
if reflect.DeepEqual(opts, clone) {
t.Fatal("Expected clone to be different after original changed, was not")
}
// Add one channel to original
cl2 := *cl
opts.PerChannel["bar"] = &cl2
// Verify it is not added to the cloned.
if _, exist := clone.PerChannel["bar"]; exist {
t.Fatal("The channel bar should not be in the cloned options")
}
opts = GetDefaultOptions()
opts.Clustering.Peers = []string{"a", "b", "c"}
clone = opts.Clone()
if !reflect.DeepEqual(opts.Clustering.Peers, clone.Clustering.Peers) {
t.Fatalf("Expected %#v, got %#v", opts.Clustering.Peers, clone.Clustering.Peers)
}
opts.Clustering.Peers[0] = "z"
if reflect.DeepEqual(opts.Clustering.Peers, clone.Clustering.Peers) {
t.Fatal("Expected clone to be different after original changed, was not")
}
}
func TestConcurrentLookupOfChannels(t *testing.T) {
// The original test (TestGetSubStoreRace) was a test to detect a race
// that previously existed when creating a channel's subStore. However,
// the internal code around that has drastically changed and does not
// make that test really relevant. Will keep this to perform concurrent
// lookup or create or channels, but lower the number of channels to
// 100 (down from 8000) because otherwise with -race, this takes a lot
// of memory, slowing down some other tests below...
numChans := 100
opts := GetDefaultOptions()
opts.MaxChannels = numChans + 1
s := runServerWithOpts(t, opts, nil)
defer s.Shutdown()
errs := make(chan error, 2)
var wg sync.WaitGroup
wg.Add(2)
chanNames := make([]string, numChans)
// Create the channel names in advance to increase concurrency
// of critical code in the function below.
for i := 0; i < numChans; i++ {
chanNames[i] = fmt.Sprintf("channel_%v", i)
}
// Perform lookup of channel and access subStore
f := func() {
defer wg.Done()
for i := 0; i < numChans; i++ {
cs, err := s.lookupOrCreateChannel(chanNames[i])
if err != nil {
errs <- err
return
}
if cs.ss == nil {
errs <- fmt.Errorf("subStore is nil")
return
}
}
}
// Run two go routines to cause parallel execution
go f()
go f()
// Wait for them to return
wg.Wait()
// Report possible errors
if len(errs) > 0 {
t.Fatalf("%v", <-errs)
}
}
func TestIOChannel(t *testing.T) {
// TODO: When running tests on my Windows VM, looks like we are getting
// a slow consumer scenario (the NATS Streaming server being the slow
// consumer). So skip for now.
if runtime.GOOS == "windows" {
t.SkipNow()
}
run := func(opts *Options) {
s := runServerWithOpts(t, opts, nil)
defer s.Shutdown()
sc := NewDefaultConnection(t)
defer sc.Close()
ackCb := func(guid string, ackErr error) {
if ackErr != nil {
panic(fmt.Errorf("%v - Ack for %v: %v", time.Now(), guid, ackErr))
}
}
total := s.opts.IOBatchSize + 100
msg := []byte("Hello")
var err error
for i := 0; i < total; i++ {
if i < total-1 {
_, err = sc.PublishAsync("foo", msg, ackCb)
} else {
err = sc.Publish("foo", msg)
}
if err != nil {
stackFatalf(t, "Unexpected error on publish: %v", err)
}
}
// Make sure we have all our messages stored in the server
checkCount(t, total, func() (string, int) {
n, _, _ := s.channels.msgsState("foo")
return "Messages", n
})
// For IOBatchSize > 0, check that the actual limit was never crossed.
if opts.IOBatchSize > 0 {
// Check that the server's ioChannel did not grow bigger than expected
ioChannelSize := int(atomic.LoadInt64(&(s.ioChannelStatsMaxBatchSize)))
if ioChannelSize > opts.IOBatchSize {
stackFatalf(t, "Expected max channel size to be smaller than %v, got %v", opts.IOBatchSize, ioChannelSize)
}
}
}
sOpts := GetDefaultOptions()
run(sOpts)
sOpts = GetDefaultOptions()
sOpts.IOBatchSize = 50
run(sOpts)
sOpts = GetDefaultOptions()
sOpts.IOBatchSize = 0
run(sOpts)
sOpts = GetDefaultOptions()
sOpts.IOSleepTime = 500
run(sOpts)
}
func TestProtocolOrder(t *testing.T) {
for i := 0; i < 2; i++ {
func() {
cleanupDatastore(t)
defer cleanupDatastore(t)
opts := getTestDefaultOptsForPersistentStore()
if i == 1 {
opts.Partitioning = true
opts.AddPerChannel("foo", &stores.ChannelLimits{})
opts.AddPerChannel("bar", &stores.ChannelLimits{})
opts.AddPerChannel("baz", &stores.ChannelLimits{})
}
s := runServerWithOpts(t, opts, nil)
defer s.Shutdown()
pubSc := NewDefaultConnection(t)
defer pubSc.Close()
total := 1000
for i := 0; i < total; i++ {
if _, err := pubSc.PublishAsync("baz", []byte("hello"), nil); err != nil {
t.Fatalf("Unexpected error on publish: %v", err)
}
}
pubSc.Close()
// The barrier for close just guarantees that all the clientPublish
// callbacks have been invoked (where we check that the pub message
// comes from a valid connection), not that messages have been stored.
// We will check the channel's store msgs count but expect that we
// may not get the correct count right away.
timeout := time.Now().Add(5 * time.Second)
count := 0
for time.Now().Before(timeout) {
c := s.channels.get("baz")
if c != nil {
count, _ = msgStoreState(t, c.store.Msgs)
if count == total {
break
}
}
time.Sleep(50 * time.Millisecond)
}
if count != total {
t.Fatalf("There should have been %d messages, got %v", total, count)
}
sc := NewDefaultConnection(t)
defer sc.Close()
for i := 0; i < 10; i++ {
if err := sc.Publish("bar", []byte("hello")); err != nil {
t.Fatalf("Unexpected error on publish: %v", err)
}
}
ch := make(chan bool)
errCh := make(chan error)
recv := int32(0)
mode := 0
var sc2 stan.Conn
cb := func(m *stan.Msg) {
count := atomic.AddInt32(&recv, 1)
if err := m.Ack(); err != nil {
errCh <- err
return
}
if count == 10 {
var err error
switch mode {
case 1:
err = m.Sub.Unsubscribe()
case 2:
err = m.Sub.Close()
case 3:
err = sc2.Close()
case 4:
err = m.Sub.Unsubscribe()
if err == nil {
err = sc2.Close()
}
case 5:
err = m.Sub.Close()
if err == nil {
err = sc2.Close()
}
}
if err != nil {
errCh <- err
} else {
ch <- true
}
}
}
total = 50
// Unsubscribe should not be processed before last processed Ack
mode = 1
for i := 0; i < total; i++ {
atomic.StoreInt32(&recv, 0)
if _, err := sc.Subscribe("bar", cb,
stan.SetManualAckMode(), stan.DeliverAllAvailable()); err != nil {
t.Fatalf("Unexpected error on subscribe: %v", err)
}
// Wait confirmation of received message
select {
case <-ch:
case e := <-errCh:
t.Fatal(e)
case <-time.After(5 * time.Second):
t.Fatal("Timed-out waiting for messages")
}
}
// Subscription close should not be processed before last processed Ack
mode = 2
for i := 0; i < total; i++ {
atomic.StoreInt32(&recv, 0)
if _, err := sc.Subscribe("bar", cb,
stan.SetManualAckMode(), stan.DeliverAllAvailable()); err != nil {
t.Fatalf("Unexpected error on subscribe: %v", err)
}
// Wait confirmation of received message
select {
case <-ch:
case e := <-errCh:
t.Fatal(e)
case <-time.After(5 * time.Second):
t.Fatal("Timed-out waiting for messages")
}
}
// Connection close should not be processed before last processed Ack
mode = 3
for i := 0; i < total; i++ {
atomic.StoreInt32(&recv, 0)
conn, err := stan.Connect(clusterName, "otherclient")
if err != nil {
t.Fatalf("Expected to connect correctly, got err %v", err)
}
sc2 = conn
if _, err := sc2.Subscribe("bar", cb,
stan.SetManualAckMode(), stan.DeliverAllAvailable()); err != nil {
t.Fatalf("Unexpected error on subscribe: %v", err)
}
// Wait confirmation of received message
select {
case <-ch:
case e := <-errCh:
t.Fatal(e)
case <-time.After(5 * time.Second):
t.Fatal("Timed-out waiting for messages")
}
}
// Connection close should not be processed before unsubscribe and last processed Ack
mode = 4
for i := 0; i < total; i++ {
atomic.StoreInt32(&recv, 0)
conn, err := stan.Connect(clusterName, "otherclient")
if err != nil {
t.Fatalf("Expected to connect correctly, got err %v", err)
}
sc2 = conn
if _, err := sc2.Subscribe("bar", cb,
stan.SetManualAckMode(), stan.DeliverAllAvailable()); err != nil {
t.Fatalf("Unexpected error on subscribe: %v", err)
}
// Wait confirmation of received message
select {
case <-ch:
case e := <-errCh:
t.Fatal(e)
case <-time.After(5 * time.Second):
t.Fatal("Timed-out waiting for messages")
}
}
// Connection close should not be processed before sub close and last processed Ack
mode = 5
for i := 0; i < total; i++ {
atomic.StoreInt32(&recv, 0)
conn, err := stan.Connect(clusterName, "otherclient")
if err != nil {
t.Fatalf("Expected to connect correctly, got err %v", err)
}
sc2 = conn
if _, err := sc2.Subscribe("bar", cb,
stan.SetManualAckMode(), stan.DeliverAllAvailable()); err != nil {
t.Fatalf("Unexpected error on subscribe: %v", err)
}
// Wait confirmation of received message
select {
case <-ch:
case e := <-errCh:
t.Fatal(e)
case <-time.After(5 * time.Second):
t.Fatal("Timed-out waiting for messages")
}
}
// Mix pub and subscribe calls
ch = make(chan bool)
errCh = make(chan error)
startSubAt := 50
var sub stan.Subscription
var err error
for i := 1; i <= 100; i++ {
if err := sc.Publish("foo", []byte("hello")); err != nil {
t.Fatalf("Unexpected error on publish: %v", err)
}
if i == startSubAt {
sub, err = sc.Subscribe("foo", func(m *stan.Msg) {
if m.Sequence == uint64(startSubAt)+1 {
ch <- true
} else if len(errCh) == 0 {
errCh <- fmt.Errorf("Received message %v instead of %v", m.Sequence, startSubAt+1)
}
})
if err != nil {
t.Fatalf("Unexpected error on subscribe: %v", err)