File tree 2 files changed +25
-7
lines changed
2 files changed +25
-7
lines changed Original file line number Diff line number Diff line change @@ -244,3 +244,18 @@ func ExportSetH2GoawayTimeout(d time.Duration) (restore func()) {
244
244
}
245
245
246
246
func (r * Request ) ExportIsReplayable () bool { return r .isReplayable () }
247
+
248
+ // ExportCloseTransportConnsAbruptly closes all idle connections from
249
+ // tr in an abrupt way, just reaching into the underlying Conns and
250
+ // closing them, without telling the Transport or its persistConns
251
+ // that it's doing so. This is to simulate the server closing connections
252
+ // on the Transport.
253
+ func ExportCloseTransportConnsAbruptly (tr * Transport ) {
254
+ tr .idleMu .Lock ()
255
+ for _ , pcs := range tr .idleConn {
256
+ for _ , pc := range pcs {
257
+ pc .conn .Close ()
258
+ }
259
+ }
260
+ tr .idleMu .Unlock ()
261
+ }
Original file line number Diff line number Diff line change @@ -737,6 +737,8 @@ func TestTransportRemovesDeadIdleConnections(t *testing.T) {
737
737
}
738
738
}
739
739
740
+ // Test that the Transport notices when a server hangs up on its
741
+ // unexpectedly (a keep-alive connection is closed).
740
742
func TestTransportServerClosingUnexpectedly (t * testing.T ) {
741
743
setParallel (t )
742
744
defer afterTest (t )
@@ -773,13 +775,14 @@ func TestTransportServerClosingUnexpectedly(t *testing.T) {
773
775
body1 := fetch (1 , 0 )
774
776
body2 := fetch (2 , 0 )
775
777
776
- ts .CloseClientConnections () // surprise!
777
-
778
- // This test has an expected race. Sleeping for 25 ms prevents
779
- // it on most fast machines, causing the next fetch() call to
780
- // succeed quickly. But if we do get errors, fetch() will retry 5
781
- // times with some delays between.
782
- time .Sleep (25 * time .Millisecond )
778
+ // Close all the idle connections in a way that's similar to
779
+ // the server hanging up on us. We don't use
780
+ // httptest.Server.CloseClientConnections because it's
781
+ // best-effort and stops blocking after 5 seconds. On a loaded
782
+ // machine running many tests concurrently it's possible for
783
+ // that method to be async and cause the body3 fetch below to
784
+ // run on an old connection. This function is synchronous.
785
+ ExportCloseTransportConnsAbruptly (c .Transport .(* Transport ))
783
786
784
787
body3 := fetch (3 , 5 )
785
788
You can’t perform that action at this time.
0 commit comments