-
Notifications
You must be signed in to change notification settings - Fork 995
/
net_transport_test.go
910 lines (780 loc) · 22.3 KB
/
net_transport_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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package raft
import (
"bytes"
"context"
"fmt"
"net"
"reflect"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
"github.com/hashicorp/go-hclog"
"github.com/stretchr/testify/require"
)
type testAddrProvider struct {
addr string
}
func (t *testAddrProvider) ServerAddr(id ServerID) (ServerAddress, error) {
return ServerAddress(t.addr), nil
}
func TestNetworkTransport_CloseStreams(t *testing.T) {
// Transport 1 is consumer
trans1, err := NewTCPTransportWithLogger("localhost:0", nil, 2, time.Second, newTestLogger(t))
if err != nil {
t.Fatalf("err: %v", err)
}
defer trans1.Close()
rpcCh := trans1.Consumer()
// Make the RPC request
args := AppendEntriesRequest{
Term: 10,
PrevLogEntry: 100,
PrevLogTerm: 4,
Entries: []*Log{
{
Index: 101,
Term: 4,
Type: LogNoop,
},
},
LeaderCommitIndex: 90,
RPCHeader: RPCHeader{Addr: []byte("cartman")},
}
resp := AppendEntriesResponse{
Term: 4,
LastLog: 90,
Success: true,
}
// errCh is used to report errors from any of the goroutines
// created in this test.
// It is buffered as to not block.
errCh := make(chan error, 100)
// Listen for a request
go func() {
for {
select {
case rpc := <-rpcCh:
// Verify the command
req := rpc.Command.(*AppendEntriesRequest)
if !reflect.DeepEqual(req, &args) {
errCh <- fmt.Errorf("command mismatch: %#v %#v", *req, args)
return
}
rpc.Respond(&resp, nil)
case <-time.After(200 * time.Millisecond):
return
}
}
}()
// Transport 2 makes outbound request, 3 conn pool
trans2, err := NewTCPTransportWithLogger("localhost:0", nil, 3, time.Second, newTestLogger(t))
if err != nil {
t.Fatalf("err: %v", err)
}
defer trans2.Close()
for i := 0; i < 2; i++ {
// Create wait group
wg := &sync.WaitGroup{}
// Try to do parallel appends, should stress the conn pool
for i = 0; i < 5; i++ {
wg.Add(1)
go func() {
defer wg.Done()
var out AppendEntriesResponse
if err := trans2.AppendEntries("id1", trans1.LocalAddr(), &args, &out); err != nil {
errCh <- err
return
}
// Verify the response
if !reflect.DeepEqual(resp, out) {
errCh <- fmt.Errorf("command mismatch: %#v %#v", resp, out)
return
}
}()
}
// Wait for the routines to finish
wg.Wait()
// Check if we received any errors from the above goroutines.
if len(errCh) > 0 {
t.Fatal(<-errCh)
}
// Check the conn pool size
addr := trans1.LocalAddr()
if len(trans2.connPool[addr]) != 3 {
t.Fatalf("Expected 3 pooled conns!")
}
if i == 0 {
trans2.CloseStreams()
if len(trans2.connPool[addr]) != 0 {
t.Fatalf("Expected no pooled conns after closing streams!")
}
}
}
}
func TestNetworkTransport_StartStop(t *testing.T) {
trans, err := NewTCPTransportWithLogger("localhost:0", nil, 2, time.Second, newTestLogger(t))
if err != nil {
t.Fatalf("err: %v", err)
}
trans.Close()
}
func TestNetworkTransport_Heartbeat_FastPath(t *testing.T) {
// Transport 1 is consumer
trans1, err := NewTCPTransportWithLogger("localhost:0", nil, 2, time.Second, newTestLogger(t))
if err != nil {
t.Fatalf("err: %v", err)
}
defer trans1.Close()
// Make the RPC request
args := AppendEntriesRequest{
Term: 10,
RPCHeader: RPCHeader{ProtocolVersion: ProtocolVersionMax, Addr: []byte("cartman")},
Leader: []byte("cartman"),
}
resp := AppendEntriesResponse{
Term: 4,
LastLog: 90,
Success: true,
}
invoked := false
fastpath := func(rpc RPC) {
// Verify the command
req := rpc.Command.(*AppendEntriesRequest)
if !reflect.DeepEqual(req, &args) {
t.Fatalf("command mismatch: %#v %#v", *req, args)
}
rpc.Respond(&resp, nil)
invoked = true
}
trans1.SetHeartbeatHandler(fastpath)
// Transport 2 makes outbound request
trans2, err := NewTCPTransportWithLogger("localhost:0", nil, 2, time.Second, newTestLogger(t))
if err != nil {
t.Fatalf("err: %v", err)
}
defer trans2.Close()
var out AppendEntriesResponse
if err := trans2.AppendEntries("id1", trans1.LocalAddr(), &args, &out); err != nil {
t.Fatalf("err: %v", err)
}
// Verify the response
if !reflect.DeepEqual(resp, out) {
t.Fatalf("command mismatch: %#v %#v", resp, out)
}
// Ensure fast-path is used
if !invoked {
t.Fatalf("fast-path not used")
}
}
func makeAppendRPC() AppendEntriesRequest {
return AppendEntriesRequest{
Term: 10,
PrevLogEntry: 100,
PrevLogTerm: 4,
Entries: []*Log{
{
Index: 101,
Term: 4,
Type: LogNoop,
},
},
LeaderCommitIndex: 90,
RPCHeader: RPCHeader{Addr: []byte("cartman")},
}
}
func makeAppendRPCResponse() AppendEntriesResponse {
return AppendEntriesResponse{
Term: 4,
LastLog: 90,
Success: true,
}
}
func TestNetworkTransport_AppendEntries(t *testing.T) {
for _, useAddrProvider := range []bool{true, false} {
// Transport 1 is consumer
trans1, err := makeTransport(t, useAddrProvider, "localhost:0")
if err != nil {
t.Fatalf("err: %v", err)
}
defer trans1.Close()
rpcCh := trans1.Consumer()
// Make the RPC request
args := makeAppendRPC()
resp := makeAppendRPCResponse()
// Listen for a request
go func() {
select {
case rpc := <-rpcCh:
// Verify the command
req := rpc.Command.(*AppendEntriesRequest)
if !reflect.DeepEqual(req, &args) {
t.Errorf("command mismatch: %#v %#v", *req, args)
return
}
rpc.Respond(&resp, nil)
case <-time.After(200 * time.Millisecond):
t.Errorf("timeout")
}
}()
// Transport 2 makes outbound request
trans2, err := makeTransport(t, useAddrProvider, string(trans1.LocalAddr()))
if err != nil {
t.Fatalf("err: %v", err)
}
defer trans2.Close()
var out AppendEntriesResponse
if err := trans2.AppendEntries("id1", trans1.LocalAddr(), &args, &out); err != nil {
t.Fatalf("err: %v", err)
}
// Verify the response
if !reflect.DeepEqual(resp, out) {
t.Fatalf("command mismatch: %#v %#v", resp, out)
}
}
}
func TestNetworkTransport_AppendEntriesPipeline(t *testing.T) {
for _, useAddrProvider := range []bool{true, false} {
// Transport 1 is consumer
trans1, err := makeTransport(t, useAddrProvider, "localhost:0")
if err != nil {
t.Fatalf("err: %v", err)
}
defer trans1.Close()
rpcCh := trans1.Consumer()
// Make the RPC request
args := makeAppendRPC()
resp := makeAppendRPCResponse()
// Listen for a request
go func() {
for i := 0; i < 10; i++ {
select {
case rpc := <-rpcCh:
// Verify the command
req := rpc.Command.(*AppendEntriesRequest)
if !reflect.DeepEqual(req, &args) {
t.Errorf("command mismatch: %#v %#v", *req, args)
return
}
rpc.Respond(&resp, nil)
case <-time.After(200 * time.Millisecond):
t.Errorf("timeout")
return
}
}
}()
// Transport 2 makes outbound request
trans2, err := makeTransport(t, useAddrProvider, string(trans1.LocalAddr()))
if err != nil {
t.Fatalf("err: %v", err)
}
defer trans2.Close()
pipeline, err := trans2.AppendEntriesPipeline("id1", trans1.LocalAddr())
if err != nil {
t.Fatalf("err: %v", err)
}
for i := 0; i < 10; i++ {
out := new(AppendEntriesResponse)
if _, err := pipeline.AppendEntries(&args, out); err != nil {
t.Fatalf("err: %v", err)
}
}
respCh := pipeline.Consumer()
for i := 0; i < 10; i++ {
select {
case ready := <-respCh:
// Verify the response
if !reflect.DeepEqual(&resp, ready.Response()) {
t.Fatalf("command mismatch: %#v %#v", &resp, ready.Response())
}
case <-time.After(200 * time.Millisecond):
t.Fatalf("timeout")
}
}
pipeline.Close()
}
}
func TestNetworkTransport_AppendEntriesPipeline_CloseStreams(t *testing.T) {
// Transport 1 is consumer
trans1, err := makeTransport(t, true, "localhost:0")
if err != nil {
t.Fatalf("err: %v", err)
}
defer trans1.Close()
rpcCh := trans1.Consumer()
// Make the RPC request
args := makeAppendRPC()
resp := makeAppendRPCResponse()
shutdownCh := make(chan struct{})
defer close(shutdownCh)
// Listen for a request
go func() {
for {
select {
case rpc := <-rpcCh:
// Verify the command
req := rpc.Command.(*AppendEntriesRequest)
if !reflect.DeepEqual(req, &args) {
t.Errorf("command mismatch: %#v %#v", *req, args)
return
}
rpc.Respond(&resp, nil)
case <-shutdownCh:
return
}
}
}()
// Transport 2 makes outbound request
trans2, err := makeTransport(t, true, string(trans1.LocalAddr()))
if err != nil {
t.Fatalf("err: %v", err)
}
defer trans2.Close()
for _, cancelStreams := range []bool{true, false} {
pipeline, err := trans2.AppendEntriesPipeline("id1", trans1.LocalAddr())
if err != nil {
t.Fatalf("err: %v", err)
}
for i := 0; i < 100; i++ {
// On the last one, close the streams on the transport one.
if cancelStreams && i == 10 {
trans1.CloseStreams()
time.Sleep(10 * time.Millisecond)
}
out := new(AppendEntriesResponse)
if _, err := pipeline.AppendEntries(&args, out); err != nil {
break
}
}
var futureErr error
respCh := pipeline.Consumer()
OUTER:
for i := 0; i < 100; i++ {
select {
case ready := <-respCh:
if err := ready.Error(); err != nil {
futureErr = err
break OUTER
}
// Verify the response
if !reflect.DeepEqual(&resp, ready.Response()) {
t.Fatalf("command mismatch: %#v %#v %v", &resp, ready.Response(), ready.Error())
}
case <-time.After(200 * time.Millisecond):
t.Fatalf("timeout when cancel streams is %v", cancelStreams)
}
}
if cancelStreams && futureErr == nil {
t.Fatalf("expected an error due to the streams being closed")
} else if !cancelStreams && futureErr != nil {
t.Fatalf("unexpected error: %v", futureErr)
}
pipeline.Close()
}
}
func TestNetworkTransport_AppendEntriesPipeline_MaxRPCsInFlight(t *testing.T) {
// Test the important cases 0 (default to 2), 1 (disabled), 2 and "some"
for _, max := range []int{0, 1, 2, 10} {
t.Run(fmt.Sprintf("max=%d", max), func(t *testing.T) {
config := &NetworkTransportConfig{
MaxPool: 2,
MaxRPCsInFlight: max,
Timeout: time.Second,
// Don't use test logger as the transport has multiple goroutines and
// causes panics.
ServerAddressProvider: &testAddrProvider{"localhost:0"},
}
// Transport 1 is consumer
trans1, err := NewTCPTransportWithConfig("localhost:0", nil, config)
require.NoError(t, err)
defer trans1.Close()
// Make the RPC request
args := makeAppendRPC()
resp := makeAppendRPCResponse()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
// Transport 2 makes outbound request
config.ServerAddressProvider = &testAddrProvider{string(trans1.LocalAddr())}
trans2, err := NewTCPTransportWithConfig("localhost:0", nil, config)
require.NoError(t, err)
defer trans2.Close()
// Kill the transports on the timeout to unblock. That means things that
// shouldn't have blocked did block.
go func() {
<-ctx.Done()
trans2.Close()
trans1.Close()
}()
// Attempt to pipeline
pipeline, err := trans2.AppendEntriesPipeline("id1", trans1.LocalAddr())
if max == 1 {
// Max == 1 implies no pipelining
require.EqualError(t, err, ErrPipelineReplicationNotSupported.Error())
return
}
require.NoError(t, err)
expectedMax := max
if max == 0 {
// Should have defaulted to 2
expectedMax = 2
}
for i := 0; i < expectedMax-1; i++ {
// We should be able to send `max - 1` rpcs before `AppendEntries`
// blocks. It blocks on the `max` one because it it sends before pushing
// to the chan. It will block forever when it does because nothing is
// responding yet.
out := new(AppendEntriesResponse)
_, err := pipeline.AppendEntries(&args, out)
require.NoError(t, err)
}
// Verify the next send blocks without blocking test forever
errCh := make(chan error, 1)
go func() {
out := new(AppendEntriesResponse)
_, err := pipeline.AppendEntries(&args, out)
errCh <- err
}()
select {
case err := <-errCh:
require.NoError(t, err)
t.Fatalf("AppendEntries didn't block with %d in flight", max)
case <-time.After(50 * time.Millisecond):
// OK it's probably blocked or we got _really_ unlucky with scheduling!
}
// Verify that once we receive/respond another one can be sent.
rpc := <-trans1.Consumer()
rpc.Respond(resp, nil)
// We also need to consume the response from the pipeline in case chan is
// unbuffered (inflight is 2 or 1)
<-pipeline.Consumer()
// The last append should unblock once the response is received.
select {
case <-errCh:
// OK
case <-time.After(50 * time.Millisecond):
t.Fatalf("last append didn't unblock")
}
})
}
}
func TestNetworkTransport_RequestVote(t *testing.T) {
for _, useAddrProvider := range []bool{true, false} {
// Transport 1 is consumer
trans1, err := makeTransport(t, useAddrProvider, "localhost:0")
if err != nil {
t.Fatalf("err: %v", err)
}
defer trans1.Close()
rpcCh := trans1.Consumer()
// Make the RPC request
args := RequestVoteRequest{
Term: 20,
LastLogIndex: 100,
LastLogTerm: 19,
RPCHeader: RPCHeader{Addr: []byte("butters")},
}
resp := RequestVoteResponse{
Term: 100,
Granted: false,
}
// Listen for a request
go func() {
select {
case rpc := <-rpcCh:
// Verify the command
req := rpc.Command.(*RequestVoteRequest)
if !reflect.DeepEqual(req, &args) {
t.Errorf("command mismatch: %#v %#v", *req, args)
return
}
rpc.Respond(&resp, nil)
case <-time.After(200 * time.Millisecond):
t.Errorf("timeout")
return
}
}()
// Transport 2 makes outbound request
trans2, err := makeTransport(t, useAddrProvider, string(trans1.LocalAddr()))
if err != nil {
t.Fatalf("err: %v", err)
}
defer trans2.Close()
var out RequestVoteResponse
if err := trans2.RequestVote("id1", trans1.LocalAddr(), &args, &out); err != nil {
t.Fatalf("err: %v", err)
}
// Verify the response
if !reflect.DeepEqual(resp, out) {
t.Fatalf("command mismatch: %#v %#v", resp, out)
}
}
}
func TestNetworkTransport_InstallSnapshot(t *testing.T) {
for _, useAddrProvider := range []bool{true, false} {
// Transport 1 is consumer
trans1, err := makeTransport(t, useAddrProvider, "localhost:0")
if err != nil {
t.Fatalf("err: %v", err)
}
defer trans1.Close()
rpcCh := trans1.Consumer()
// Make the RPC request
args := InstallSnapshotRequest{
Term: 10,
LastLogIndex: 100,
LastLogTerm: 9,
Peers: []byte("blah blah"),
Size: 10,
RPCHeader: RPCHeader{Addr: []byte("kyle")},
}
resp := InstallSnapshotResponse{
Term: 10,
Success: true,
}
// Listen for a request
go func() {
select {
case rpc := <-rpcCh:
// Verify the command
req := rpc.Command.(*InstallSnapshotRequest)
if !reflect.DeepEqual(req, &args) {
t.Errorf("command mismatch: %#v %#v", *req, args)
return
}
// Try to read the bytes
buf := make([]byte, 10)
rpc.Reader.Read(buf)
// Compare
if !bytes.Equal(buf, []byte("0123456789")) {
t.Errorf("bad buf %v", buf)
return
}
rpc.Respond(&resp, nil)
case <-time.After(200 * time.Millisecond):
t.Errorf("timeout")
}
}()
// Transport 2 makes outbound request
trans2, err := makeTransport(t, useAddrProvider, string(trans1.LocalAddr()))
if err != nil {
t.Fatalf("err: %v", err)
}
defer trans2.Close()
// Create a buffer
buf := bytes.NewBuffer([]byte("0123456789"))
var out InstallSnapshotResponse
if err := trans2.InstallSnapshot("id1", trans1.LocalAddr(), &args, &out, buf); err != nil {
t.Fatalf("err: %v", err)
}
// Verify the response
if !reflect.DeepEqual(resp, out) {
t.Fatalf("command mismatch: %#v %#v", resp, out)
}
}
}
func TestNetworkTransport_EncodeDecode(t *testing.T) {
// Transport 1 is consumer
trans1, err := NewTCPTransportWithLogger("localhost:0", nil, 2, time.Second, newTestLogger(t))
if err != nil {
t.Fatalf("err: %v", err)
}
defer trans1.Close()
local := trans1.LocalAddr()
enc := trans1.EncodePeer("id1", local)
dec := trans1.DecodePeer(enc)
if dec != local {
t.Fatalf("enc/dec fail: %v %v", dec, local)
}
}
func TestNetworkTransport_EncodeDecode_AddressProvider(t *testing.T) {
addressOverride := "localhost:11111"
config := &NetworkTransportConfig{MaxPool: 2, Timeout: time.Second, Logger: newTestLogger(t), ServerAddressProvider: &testAddrProvider{addressOverride}}
trans1, err := NewTCPTransportWithConfig("localhost:0", nil, config)
if err != nil {
t.Fatalf("err: %v", err)
}
defer trans1.Close()
local := trans1.LocalAddr()
enc := trans1.EncodePeer("id1", local)
dec := trans1.DecodePeer(enc)
if dec != ServerAddress(addressOverride) {
t.Fatalf("enc/dec fail: %v %v", dec, addressOverride)
}
}
func TestNetworkTransport_PooledConn(t *testing.T) {
// Transport 1 is consumer
trans1, err := NewTCPTransportWithLogger("localhost:0", nil, 2, time.Second, newTestLogger(t))
if err != nil {
t.Fatalf("err: %v", err)
}
defer trans1.Close()
rpcCh := trans1.Consumer()
// Make the RPC request
args := AppendEntriesRequest{
Term: 10,
PrevLogEntry: 100,
PrevLogTerm: 4,
Entries: []*Log{
{
Index: 101,
Term: 4,
Type: LogNoop,
},
},
LeaderCommitIndex: 90,
RPCHeader: RPCHeader{Addr: []byte("cartman")},
}
resp := AppendEntriesResponse{
Term: 4,
LastLog: 90,
Success: true,
}
// errCh is used to report errors from any of the goroutines
// created in this test.
// It is buffered as to not block.
errCh := make(chan error, 100)
// Listen for a request
go func() {
for {
select {
case rpc := <-rpcCh:
// Verify the command
req := rpc.Command.(*AppendEntriesRequest)
if !reflect.DeepEqual(req, &args) {
errCh <- fmt.Errorf("command mismatch: %#v %#v", *req, args)
return
}
rpc.Respond(&resp, nil)
case <-time.After(200 * time.Millisecond):
return
}
}
}()
// Transport 2 makes outbound request, 3 conn pool
trans2, err := NewTCPTransportWithLogger("localhost:0", nil, 3, time.Second, newTestLogger(t))
if err != nil {
t.Fatalf("err: %v", err)
}
defer trans2.Close()
// Create wait group
wg := &sync.WaitGroup{}
// Try to do parallel appends, should stress the conn pool
for i := 0; i < 5; i++ {
wg.Add(1)
go func() {
defer wg.Done()
var out AppendEntriesResponse
if err := trans2.AppendEntries("id1", trans1.LocalAddr(), &args, &out); err != nil {
errCh <- err
return
}
// Verify the response
if !reflect.DeepEqual(resp, out) {
errCh <- fmt.Errorf("command mismatch: %#v %#v", resp, out)
return
}
}()
}
// Wait for the routines to finish
wg.Wait()
// Check if we received any errors from the above goroutines.
if len(errCh) > 0 {
t.Fatal(<-errCh)
}
// Check the conn pool size
addr := trans1.LocalAddr()
if len(trans2.connPool[addr]) != 3 {
t.Fatalf("Expected 3 pooled conns!")
}
}
func makeTransport(t *testing.T, useAddrProvider bool, addressOverride string) (*NetworkTransport, error) {
config := &NetworkTransportConfig{
MaxPool: 2,
// Setting this because older tests for pipelining were written when this
// was a constant and block forever if it's not large enough.
MaxRPCsInFlight: 130,
Timeout: time.Second,
Logger: newTestLogger(t),
}
if useAddrProvider {
config.ServerAddressProvider = &testAddrProvider{addressOverride}
}
return NewTCPTransportWithConfig("localhost:0", nil, config)
}
type testCountingWriter struct {
t *testing.T
numCalls *int32
}
func (tw testCountingWriter) Write(p []byte) (n int, err error) {
atomic.AddInt32(tw.numCalls, 1)
if !strings.Contains(string(p), "failed to accept connection") {
tw.t.Error("did not receive expected log message")
}
tw.t.Log("countingWriter:", string(p))
return len(p), nil
}
type testCountingStreamLayer struct {
numCalls *int32
}
func (sl testCountingStreamLayer) Accept() (net.Conn, error) {
*sl.numCalls++
return nil, fmt.Errorf("intentional error in test")
}
func (sl testCountingStreamLayer) Close() error {
return nil
}
func (sl testCountingStreamLayer) Addr() net.Addr {
panic("not needed")
}
func (sl testCountingStreamLayer) Dial(address ServerAddress, timeout time.Duration) (net.Conn, error) {
return nil, fmt.Errorf("not needed")
}
// TestNetworkTransport_ListenBackoff tests that Accept() errors in NetworkTransport#listen()
// do not result in a tight loop and spam the log. We verify this here by counting the number
// of calls against Accept() and the logger
func TestNetworkTransport_ListenBackoff(t *testing.T) {
// testTime is the amount of time we will allow NetworkTransport#listen() to run
// This needs to be long enough that to verify that maxDelay is in force,
// but not so long as to be obnoxious when running the test suite.
const testTime = 4 * time.Second
var numAccepts int32
var numLogs int32
countingWriter := testCountingWriter{t, &numLogs}
countingLogger := hclog.New(&hclog.LoggerOptions{
Name: "test",
Output: countingWriter,
Level: hclog.DefaultLevel,
})
transport := NetworkTransport{
logger: countingLogger,
stream: testCountingStreamLayer{&numAccepts},
shutdownCh: make(chan struct{}),
}
go transport.listen()
// sleep (+yield) for testTime seconds before asking the accept loop to shut down
time.Sleep(testTime)
transport.Close()
// Verify that the method exited (but without block this test)
// maxDelay == 1s, so we will give the routine 1.25s to loop around and shut down.
select {
case <-transport.shutdownCh:
case <-time.After(1250 * time.Millisecond):
t.Error("timed out waiting for NetworkTransport to shut down")
}
require.True(t, transport.shutdown)
// In testTime==4s, we expect to loop approximately 12 times
// with the following delays (in ms):
// 0+5+10+20+40+80+160+320+640+1000+1000+1000 == 4275 ms
// Too few calls suggests that the minDelay is not in force; too many calls suggests that the
// maxDelay is not in force or that the back-off isn't working at all.
// We'll leave a little flex; the important thing here is the asymptotic behavior.
// If the minDelay or maxDelay in NetworkTransport#listen() are modified, this test may fail
// and need to be adjusted.
require.True(t, numAccepts > 10)
require.True(t, numAccepts < 13)
require.True(t, numLogs > 10)
require.True(t, numLogs < 13)
}