@@ -817,15 +817,16 @@ func TestWebSocketClient_BadHandshake(t *testing.T) {
817
817
// TestWebSocketClient_HeartbeatTimeout tests the heartbeat by forcing a
818
818
// timeout by setting the ping period greater than the deadline.
819
819
func TestWebSocketClient_HeartbeatTimeout (t * testing.T ) {
820
+ blockRequestCtx , unblockRequest := context .WithCancel (context .Background ())
821
+ defer unblockRequest ()
820
822
// Create fake WebSocket server which blocks.
821
823
websocketServer := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , req * http.Request ) {
822
824
conns , err := webSocketServerStreams (req , w , streamOptionsFromRequest (req ))
823
825
if err != nil {
824
826
t .Fatalf ("error on webSocketServerStreams: %v" , err )
825
827
}
826
828
defer conns .conn .Close ()
827
- // Block server; heartbeat timeout (or test timeout) will fire before this returns.
828
- time .Sleep (1 * time .Second )
829
+ <- blockRequestCtx .Done ()
829
830
}))
830
831
defer websocketServer .Close ()
831
832
// Create websocket client connecting to fake server.
@@ -840,8 +841,8 @@ func TestWebSocketClient_HeartbeatTimeout(t *testing.T) {
840
841
}
841
842
streamExec := exec .(* wsStreamExecutor )
842
843
// Ping period is greater than the ping deadline, forcing the timeout to fire.
843
- pingPeriod := 20 * time . Millisecond
844
- pingDeadline := 5 * time .Millisecond
844
+ pingPeriod := wait . ForeverTestTimeout // this lets the heartbeat deadline expire without renewing it
845
+ pingDeadline := time .Second // this gives setup 1 second to establish streams
845
846
streamExec .heartbeatPeriod = pingPeriod
846
847
streamExec .heartbeatDeadline = pingDeadline
847
848
// Send some random data to the websocket server through STDIN.
@@ -859,8 +860,7 @@ func TestWebSocketClient_HeartbeatTimeout(t *testing.T) {
859
860
}()
860
861
861
862
select {
862
- case <- time .After (pingPeriod * 5 ):
863
- // Give up after about five ping attempts
863
+ case <- time .After (wait .ForeverTestTimeout ):
864
864
t .Fatalf ("expected heartbeat timeout, got none." )
865
865
case err := <- errorChan :
866
866
// Expecting heartbeat timeout error.
@@ -1116,6 +1116,14 @@ func TestWebSocketClient_HeartbeatSucceeds(t *testing.T) {
1116
1116
wg .Wait ()
1117
1117
}
1118
1118
1119
+ func TestLateStreamCreation (t * testing.T ) {
1120
+ c := newWSStreamCreator (nil )
1121
+ c .closeAllStreamReaders (nil )
1122
+ if err := c .setStream (0 , nil ); err == nil {
1123
+ t .Fatal ("expected error adding stream after closeAllStreamReaders" )
1124
+ }
1125
+ }
1126
+
1119
1127
func TestWebSocketClient_StreamsAndExpectedErrors (t * testing.T ) {
1120
1128
// Validate Stream functions.
1121
1129
c := newWSStreamCreator (nil )
0 commit comments