@@ -68,8 +68,8 @@ func TestConnection(t *testing.T) {
68
68
if ! cmp .Equal (got , want , cmp .Comparer (compareErrors )) {
69
69
t .Errorf ("errors do not match. got %v; want %v" , got , want )
70
70
}
71
- connState := atomic .LoadInt64 (& conn .connected )
72
- assert .Equal (t , disconnected , connState , "expected connection state %v, got %v" , disconnected , connState )
71
+ connState := atomic .LoadInt64 (& conn .state )
72
+ assert .Equal (t , connDisconnected , connState , "expected connection state %v, got %v" , connDisconnected , connState )
73
73
})
74
74
t .Run ("handshaker error" , func (t * testing.T ) {
75
75
err := errors .New ("handshaker error" )
@@ -92,8 +92,8 @@ func TestConnection(t *testing.T) {
92
92
if ! cmp .Equal (got , want , cmp .Comparer (compareErrors )) {
93
93
t .Errorf ("errors do not match. got %v; want %v" , got , want )
94
94
}
95
- connState := atomic .LoadInt64 (& conn .connected )
96
- assert .Equal (t , disconnected , connState , "expected connection state %v, got %v" , disconnected , connState )
95
+ connState := atomic .LoadInt64 (& conn .state )
96
+ assert .Equal (t , connDisconnected , connState , "expected connection state %v, got %v" , connDisconnected , connState )
97
97
})
98
98
t .Run ("context is not pinned by connect" , func (t * testing.T ) {
99
99
// connect creates a cancel-able version of the context passed to it and stores the CancelFunc on the
@@ -345,7 +345,7 @@ func TestConnection(t *testing.T) {
345
345
t .Run ("completed context" , func (t * testing.T ) {
346
346
ctx , cancel := context .WithCancel (context .Background ())
347
347
cancel ()
348
- conn := & connection {id : "foobar" , nc : & net.TCPConn {}, connected : connected }
348
+ conn := & connection {id : "foobar" , nc : & net.TCPConn {}, state : connConnected }
349
349
want := ConnectionError {ConnectionID : "foobar" , Wrapped : ctx .Err (), message : "failed to write" }
350
350
got := conn .writeWireMessage (ctx , []byte {})
351
351
if ! cmp .Equal (got , want , cmp .Comparer (compareErrors )) {
@@ -380,7 +380,7 @@ func TestConnection(t *testing.T) {
380
380
message : "failed to set write deadline" ,
381
381
}
382
382
tnc := & testNetConn {deadlineerr : errors .New ("set writeDeadline error" )}
383
- conn := & connection {id : "foobar" , nc : tnc , writeTimeout : tc .timeout , connected : connected }
383
+ conn := & connection {id : "foobar" , nc : tnc , writeTimeout : tc .timeout , state : connConnected }
384
384
got := conn .writeWireMessage (ctx , []byte {})
385
385
if ! cmp .Equal (got , want , cmp .Comparer (compareErrors )) {
386
386
t .Errorf ("errors do not match. got %v; want %v" , got , want )
@@ -397,7 +397,7 @@ func TestConnection(t *testing.T) {
397
397
t .Run ("error" , func (t * testing.T ) {
398
398
err := errors .New ("Write error" )
399
399
tnc := & testNetConn {writeerr : err }
400
- conn := & connection {id : "foobar" , nc : tnc , connected : connected }
400
+ conn := & connection {id : "foobar" , nc : tnc , state : connConnected }
401
401
listener := newTestCancellationListener (false )
402
402
conn .cancellationListener = listener
403
403
@@ -413,7 +413,7 @@ func TestConnection(t *testing.T) {
413
413
})
414
414
t .Run ("success" , func (t * testing.T ) {
415
415
tnc := & testNetConn {}
416
- conn := & connection {id : "foobar" , nc : tnc , connected : connected }
416
+ conn := & connection {id : "foobar" , nc : tnc , state : connConnected }
417
417
listener := newTestCancellationListener (false )
418
418
conn .cancellationListener = listener
419
419
@@ -430,7 +430,7 @@ func TestConnection(t *testing.T) {
430
430
// Simulate context cancellation during a network write.
431
431
432
432
nc := newCancellationWriteConn (& testNetConn {}, 0 )
433
- conn := & connection {id : "foobar" , nc : nc , connected : connected }
433
+ conn := & connection {id : "foobar" , nc : nc , state : connConnected }
434
434
listener := newTestCancellationListener (false )
435
435
conn .cancellationListener = listener
436
436
@@ -451,24 +451,24 @@ func TestConnection(t *testing.T) {
451
451
wg .Wait ()
452
452
want := ConnectionError {ConnectionID : conn .id , Wrapped : context .Canceled , message : writeErrMsg }
453
453
assert .Equal (t , want , err , "expected error %v, got %v" , want , err )
454
- assert .Equal (t , disconnected , conn .connected , "expected connection state %v, got %v" , disconnected ,
455
- conn .connected )
454
+ assert .Equal (t , connDisconnected , conn .state , "expected connection state %v, got %v" , connDisconnected ,
455
+ conn .state )
456
456
})
457
457
t .Run ("connection is closed if context is cancelled even if network write succeeds" , func (t * testing.T ) {
458
458
// Test the race condition between Write and the cancellation listener. The socket write will
459
459
// succeed, but we set the abortedForCancellation flag to true to simulate the context being
460
460
// cancelled immediately after the Write finishes.
461
461
462
462
tnc := & testNetConn {}
463
- conn := & connection {id : "foobar" , nc : tnc , connected : connected }
463
+ conn := & connection {id : "foobar" , nc : tnc , state : connConnected }
464
464
listener := newTestCancellationListener (true )
465
465
conn .cancellationListener = listener
466
466
467
467
want := ConnectionError {ConnectionID : conn .id , Wrapped : context .Canceled , message : writeErrMsg }
468
468
err := conn .writeWireMessage (context .Background (), []byte ("foobar" ))
469
469
assert .Equal (t , want , err , "expected error %v, got %v" , want , err )
470
- assert .Equal (t , conn .connected , disconnected , "expected connection state %v, got %v" , disconnected ,
471
- conn .connected )
470
+ assert .Equal (t , conn .state , connDisconnected , "expected connection state %v, got %v" , connDisconnected ,
471
+ conn .state )
472
472
})
473
473
})
474
474
})
@@ -484,7 +484,7 @@ func TestConnection(t *testing.T) {
484
484
t .Run ("completed context" , func (t * testing.T ) {
485
485
ctx , cancel := context .WithCancel (context .Background ())
486
486
cancel ()
487
- conn := & connection {id : "foobar" , nc : & net.TCPConn {}, connected : connected }
487
+ conn := & connection {id : "foobar" , nc : & net.TCPConn {}, state : connConnected }
488
488
want := ConnectionError {ConnectionID : "foobar" , Wrapped : ctx .Err (), message : "failed to read" }
489
489
_ , got := conn .readWireMessage (ctx , []byte {})
490
490
if ! cmp .Equal (got , want , cmp .Comparer (compareErrors )) {
@@ -519,7 +519,7 @@ func TestConnection(t *testing.T) {
519
519
message : "failed to set read deadline" ,
520
520
}
521
521
tnc := & testNetConn {deadlineerr : errors .New ("set readDeadline error" )}
522
- conn := & connection {id : "foobar" , nc : tnc , readTimeout : tc .timeout , connected : connected }
522
+ conn := & connection {id : "foobar" , nc : tnc , readTimeout : tc .timeout , state : connConnected }
523
523
_ , got := conn .readWireMessage (ctx , []byte {})
524
524
if ! cmp .Equal (got , want , cmp .Comparer (compareErrors )) {
525
525
t .Errorf ("errors do not match. got %v; want %v" , got , want )
@@ -534,7 +534,7 @@ func TestConnection(t *testing.T) {
534
534
t .Run ("size read errors" , func (t * testing.T ) {
535
535
err := errors .New ("Read error" )
536
536
tnc := & testNetConn {readerr : err }
537
- conn := & connection {id : "foobar" , nc : tnc , connected : connected }
537
+ conn := & connection {id : "foobar" , nc : tnc , state : connConnected }
538
538
listener := newTestCancellationListener (false )
539
539
conn .cancellationListener = listener
540
540
@@ -551,7 +551,7 @@ func TestConnection(t *testing.T) {
551
551
t .Run ("full message read errors" , func (t * testing.T ) {
552
552
err := errors .New ("Read error" )
553
553
tnc := & testNetConn {readerr : err , buf : []byte {0x11 , 0x00 , 0x00 , 0x00 }}
554
- conn := & connection {id : "foobar" , nc : tnc , connected : connected }
554
+ conn := & connection {id : "foobar" , nc : tnc , state : connConnected }
555
555
listener := newTestCancellationListener (false )
556
556
conn .cancellationListener = listener
557
557
@@ -587,7 +587,7 @@ func TestConnection(t *testing.T) {
587
587
err := errors .New ("length of read message too large" )
588
588
tnc := & testNetConn {buf : make ([]byte , len (tc .buffer ))}
589
589
copy (tnc .buf , tc .buffer )
590
- conn := & connection {id : "foobar" , nc : tnc , connected : connected , desc : tc .desc }
590
+ conn := & connection {id : "foobar" , nc : tnc , state : connConnected , desc : tc .desc }
591
591
listener := newTestCancellationListener (false )
592
592
conn .cancellationListener = listener
593
593
@@ -604,7 +604,7 @@ func TestConnection(t *testing.T) {
604
604
want := []byte {0x0A , 0x00 , 0x00 , 0x00 , 0x05 , 0x06 , 0x07 , 0x08 , 0x09 , 0x0A }
605
605
tnc := & testNetConn {buf : make ([]byte , len (want ))}
606
606
copy (tnc .buf , want )
607
- conn := & connection {id : "foobar" , nc : tnc , connected : connected }
607
+ conn := & connection {id : "foobar" , nc : tnc , state : connConnected }
608
608
listener := newTestCancellationListener (false )
609
609
conn .cancellationListener = listener
610
610
@@ -634,7 +634,7 @@ func TestConnection(t *testing.T) {
634
634
readBuf := []byte {10 , 0 , 0 , 0 }
635
635
nc := newCancellationReadConn (& testNetConn {}, tc .skip , readBuf )
636
636
637
- conn := & connection {id : "foobar" , nc : nc , connected : connected }
637
+ conn := & connection {id : "foobar" , nc : nc , state : connConnected }
638
638
listener := newTestCancellationListener (false )
639
639
conn .cancellationListener = listener
640
640
@@ -655,22 +655,22 @@ func TestConnection(t *testing.T) {
655
655
wg .Wait ()
656
656
want := ConnectionError {ConnectionID : conn .id , Wrapped : context .Canceled , message : tc .errmsg }
657
657
assert .Equal (t , want , err , "expected error %v, got %v" , want , err )
658
- assert .Equal (t , disconnected , conn .connected , "expected connection state %v, got %v" , disconnected ,
659
- conn .connected )
658
+ assert .Equal (t , connDisconnected , conn .state , "expected connection state %v, got %v" , connDisconnected ,
659
+ conn .state )
660
660
})
661
661
}
662
662
})
663
663
t .Run ("closes connection if context is cancelled even if the socket read succeeds" , func (t * testing.T ) {
664
664
tnc := & testNetConn {buf : []byte {0x0A , 0x00 , 0x00 , 0x00 , 0x05 , 0x06 , 0x07 , 0x08 , 0x09 , 0x0A }}
665
- conn := & connection {id : "foobar" , nc : tnc , connected : connected }
665
+ conn := & connection {id : "foobar" , nc : tnc , state : connConnected }
666
666
listener := newTestCancellationListener (true )
667
667
conn .cancellationListener = listener
668
668
669
669
want := ConnectionError {ConnectionID : conn .id , Wrapped : context .Canceled , message : "unable to read server response" }
670
670
_ , err := conn .readWireMessage (context .Background (), nil )
671
671
assert .Equal (t , want , err , "expected error %v, got %v" , want , err )
672
- assert .Equal (t , disconnected , conn .connected , "expected connection state %v, got %v" , disconnected ,
673
- conn .connected )
672
+ assert .Equal (t , connDisconnected , conn .state , "expected connection state %v, got %v" , connDisconnected ,
673
+ conn .state )
674
674
})
675
675
})
676
676
})
@@ -693,8 +693,8 @@ func TestConnection(t *testing.T) {
693
693
694
694
err := conn .connect (context .Background ())
695
695
assert .NotNil (t , err , "expected handshake error from connect, got nil" )
696
- connState := atomic .LoadInt64 (& conn .connected )
697
- assert .Equal (t , disconnected , connState , "expected connection state %v, got %v" , disconnected , connState )
696
+ connState := atomic .LoadInt64 (& conn .state )
697
+ assert .Equal (t , connDisconnected , connState , "expected connection state %v, got %v" , connDisconnected , connState )
698
698
699
699
err = conn .close ()
700
700
assert .Nil (t , err , "close error: %v" , err )
@@ -703,11 +703,11 @@ func TestConnection(t *testing.T) {
703
703
t .Run ("cancellation listener callback" , func (t * testing.T ) {
704
704
t .Run ("closes connection" , func (t * testing.T ) {
705
705
tnc := & testNetConn {}
706
- conn := & connection {connected : connected , nc : tnc }
706
+ conn := & connection {state : connConnected , nc : tnc }
707
707
708
708
conn .cancellationListenerCallback ()
709
- assert .True (t , conn .connected == disconnected , "expected connection state %v, got %v" , disconnected ,
710
- conn .connected )
709
+ assert .True (t , conn .state == connDisconnected , "expected connection state %v, got %v" , connDisconnected ,
710
+ conn .state )
711
711
assert .True (t , tnc .closed , "expected net.Conn to be closed but was not" )
712
712
})
713
713
})
0 commit comments