@@ -381,21 +381,23 @@ func (s *server) start(t *testing.T, port int, serverConfig *ServerConfig, ht hT
381381 h := & testStreamHandler {t : transport .(* http2Server )}
382382 s .h = h
383383 s .mu .Unlock ()
384+ ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
385+ defer cancel ()
384386 switch ht {
385387 case notifyCall :
386- go transport .HandleStreams (context . Background () , h .handleStreamAndNotify )
388+ go transport .HandleStreams (ctx , h .handleStreamAndNotify )
387389 case suspended :
388- go transport .HandleStreams (context . Background () , func (* ServerStream ) {})
390+ go transport .HandleStreams (ctx , func (* ServerStream ) {})
389391 case misbehaved :
390- go transport .HandleStreams (context . Background () , func (s * ServerStream ) {
392+ go transport .HandleStreams (ctx , func (s * ServerStream ) {
391393 go h .handleStreamMisbehave (t , s )
392394 })
393395 case encodingRequiredStatus :
394- go transport .HandleStreams (context . Background () , func (s * ServerStream ) {
396+ go transport .HandleStreams (ctx , func (s * ServerStream ) {
395397 go h .handleStreamEncodingRequiredStatus (s )
396398 })
397399 case invalidHeaderField :
398- go transport .HandleStreams (context . Background () , func (s * ServerStream ) {
400+ go transport .HandleStreams (ctx , func (s * ServerStream ) {
399401 go h .handleStreamInvalidHeaderField (s )
400402 })
401403 case delayRead :
@@ -404,15 +406,15 @@ func (s *server) start(t *testing.T, port int, serverConfig *ServerConfig, ht hT
404406 s .mu .Lock ()
405407 close (s .ready )
406408 s .mu .Unlock ()
407- go transport .HandleStreams (context . Background () , func (s * ServerStream ) {
409+ go transport .HandleStreams (ctx , func (s * ServerStream ) {
408410 go h .handleStreamDelayRead (t , s )
409411 })
410412 case pingpong :
411- go transport .HandleStreams (context . Background () , func (s * ServerStream ) {
413+ go transport .HandleStreams (ctx , func (s * ServerStream ) {
412414 go h .handleStreamPingPong (t , s )
413415 })
414416 default :
415- go transport .HandleStreams (context . Background () , func (s * ServerStream ) {
417+ go transport .HandleStreams (ctx , func (s * ServerStream ) {
416418 go h .handleStream (t , s )
417419 })
418420 }
@@ -464,13 +466,15 @@ func setUpWithOptions(t *testing.T, port int, sc *ServerConfig, ht hType, copts
464466 addr := resolver.Address {Addr : "localhost:" + server .port }
465467 copts .ChannelzParent = channelzSubChannel (t )
466468
467- connectCtx , cancel := context .WithDeadline (context .Background (), time .Now ().Add (2 * time .Second ))
468- ct , connErr := NewHTTP2Client (connectCtx , context .Background (), addr , copts , func (GoAwayReason ) {})
469+ ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
470+ t .Cleanup (cancel )
471+ connectCtx , cCancel := context .WithTimeout (context .Background (), 2 * time .Second )
472+ ct , connErr := NewHTTP2Client (connectCtx , ctx , addr , copts , func (GoAwayReason ) {})
469473 if connErr != nil {
470- cancel () // Do not cancel in success path.
474+ cCancel () // Do not cancel in success path.
471475 t .Fatalf ("failed to create transport: %v" , connErr )
472476 }
473- return server , ct .(* http2Client ), cancel
477+ return server , ct .(* http2Client ), cCancel
474478}
475479
476480func setUpWithNoPingServer (t * testing.T , copts ConnectOptions , connCh chan net.Conn ) (* http2Client , func ()) {
@@ -495,18 +499,20 @@ func setUpWithNoPingServer(t *testing.T, copts ConnectOptions, connCh chan net.C
495499 }
496500 connCh <- conn
497501 }()
498- connectCtx , cancel := context .WithDeadline (context .Background (), time .Now ().Add (2 * time .Second ))
499- tr , err := NewHTTP2Client (connectCtx , context .Background (), resolver.Address {Addr : lis .Addr ().String ()}, copts , func (GoAwayReason ) {})
502+ ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
503+ t .Cleanup (cancel )
504+ connectCtx , cCancel := context .WithTimeout (context .Background (), 2 * time .Second )
505+ tr , err := NewHTTP2Client (connectCtx , ctx , resolver.Address {Addr : lis .Addr ().String ()}, copts , func (GoAwayReason ) {})
500506 if err != nil {
501- cancel () // Do not cancel in success path.
507+ cCancel () // Do not cancel in success path.
502508 // Server clean-up.
503509 lis .Close ()
504510 if conn , ok := <- connCh ; ok {
505511 conn .Close ()
506512 }
507513 t .Fatalf ("Failed to dial: %v" , err )
508514 }
509- return tr .(* http2Client ), cancel
515+ return tr .(* http2Client ), cCancel
510516}
511517
512518// TestInflightStreamClosing ensures that closing in-flight stream
@@ -739,7 +745,7 @@ func (s) TestLargeMessageWithDelayRead(t *testing.T) {
739745 Host : "localhost" ,
740746 Method : "foo.Large" ,
741747 }
742- ctx , cancel := context .WithDeadline (context .Background (), time .Now (). Add ( time . Second * 10 ) )
748+ ctx , cancel := context .WithTimeout (context .Background (), time .Second * 10 )
743749 defer cancel ()
744750 s , err := ct .NewStream (ctx , callHdr )
745751 if err != nil {
@@ -833,7 +839,7 @@ func (s) TestGracefulClose(t *testing.T) {
833839 // Correctly clean up the server
834840 server .stop ()
835841 }()
836- ctx , cancel := context .WithDeadline (context .Background (), time .Now (). Add ( time . Second * 10 ) )
842+ ctx , cancel := context .WithTimeout (context .Background (), time .Second * 10 )
837843 defer cancel ()
838844
839845 // Create a stream that will exist for this whole test and confirm basic
@@ -969,7 +975,7 @@ func (s) TestMaxStreams(t *testing.T) {
969975 // Try and create a new stream.
970976 go func () {
971977 defer close (done )
972- ctx , cancel := context .WithDeadline (context .Background (), time .Now (). Add ( time . Second * 10 ) )
978+ ctx , cancel := context .WithTimeout (context .Background (), time .Second * 10 )
973979 defer cancel ()
974980 if _ , err := ct .NewStream (ctx , callHdr ); err != nil {
975981 t .Errorf ("Failed to open stream: %v" , err )
@@ -1353,7 +1359,9 @@ func (s) TestClientHonorsConnectContext(t *testing.T) {
13531359
13541360 parent := channelzSubChannel (t )
13551361 copts := ConnectOptions {ChannelzParent : parent }
1356- _ , err = NewHTTP2Client (connectCtx , context .Background (), resolver.Address {Addr : lis .Addr ().String ()}, copts , func (GoAwayReason ) {})
1362+ ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
1363+ defer cancel ()
1364+ _ , err = NewHTTP2Client (connectCtx , ctx , resolver.Address {Addr : lis .Addr ().String ()}, copts , func (GoAwayReason ) {})
13571365 if err == nil {
13581366 t .Fatalf ("NewHTTP2Client() returned successfully; wanted error" )
13591367 }
@@ -1365,7 +1373,7 @@ func (s) TestClientHonorsConnectContext(t *testing.T) {
13651373 // Test context deadline.
13661374 connectCtx , cancel = context .WithTimeout (context .Background (), 100 * time .Millisecond )
13671375 defer cancel ()
1368- _ , err = NewHTTP2Client (connectCtx , context . Background () , resolver.Address {Addr : lis .Addr ().String ()}, copts , func (GoAwayReason ) {})
1376+ _ , err = NewHTTP2Client (connectCtx , ctx , resolver.Address {Addr : lis .Addr ().String ()}, copts , func (GoAwayReason ) {})
13691377 if err == nil {
13701378 t .Fatalf ("NewHTTP2Client() returned successfully; wanted error" )
13711379 }
@@ -1440,12 +1448,14 @@ func (s) TestClientWithMisbehavedServer(t *testing.T) {
14401448 }
14411449 }
14421450 }()
1443- connectCtx , cancel := context .WithDeadline (context .Background (), time . Now (). Add ( 2 * time .Second ) )
1451+ connectCtx , cancel := context .WithTimeout (context .Background (), 2 * time .Second )
14441452 defer cancel ()
14451453
14461454 parent := channelzSubChannel (t )
14471455 copts := ConnectOptions {ChannelzParent : parent }
1448- ct , err := NewHTTP2Client (connectCtx , context .Background (), resolver.Address {Addr : lis .Addr ().String ()}, copts , func (GoAwayReason ) {})
1456+ ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
1457+ defer cancel ()
1458+ ct , err := NewHTTP2Client (connectCtx , ctx , resolver.Address {Addr : lis .Addr ().String ()}, copts , func (GoAwayReason ) {})
14491459 if err != nil {
14501460 t .Fatalf ("Error while creating client transport: %v" , err )
14511461 }
@@ -1779,9 +1789,11 @@ func waitWhileTrue(t *testing.T, condition func() (bool, error)) {
17791789// If any error occurs on a call to Stream.Read, future calls
17801790// should continue to return that same error.
17811791func (s ) TestReadGivesSameErrorAfterAnyErrorOccurs (t * testing.T ) {
1792+ ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
1793+ defer cancel ()
17821794 testRecvBuffer := newRecvBuffer ()
17831795 s := & Stream {
1784- ctx : context . Background () ,
1796+ ctx : ctx ,
17851797 buf : testRecvBuffer ,
17861798 requestRead : func (int ) {},
17871799 }
@@ -2450,7 +2462,7 @@ func (s) TestClientHandshakeInfo(t *testing.T) {
24502462 Addr : "localhost:" + server .port ,
24512463 Attributes : attributes .New (testAttrKey , testAttrVal ),
24522464 }
2453- ctx , cancel := context .WithDeadline (context .Background (), time . Now (). Add ( 2 * time .Second ) )
2465+ ctx , cancel := context .WithTimeout (context .Background (), 2 * time .Second )
24542466 defer cancel ()
24552467 creds := & attrTransportCreds {}
24562468
@@ -2485,7 +2497,7 @@ func (s) TestClientHandshakeInfoDialer(t *testing.T) {
24852497 Addr : "localhost:" + server .port ,
24862498 Attributes : attributes .New (testAttrKey , testAttrVal ),
24872499 }
2488- ctx , cancel := context .WithDeadline (context .Background (), time . Now (). Add ( 2 * time .Second ) )
2500+ ctx , cancel := context .WithTimeout (context .Background (), 2 * time .Second )
24892501 defer cancel ()
24902502
24912503 var attr * attributes.Attributes
@@ -2829,7 +2841,7 @@ func (s) TestClientCloseReturnsAfterReaderCompletes(t *testing.T) {
28292841
28302842 // Create a client transport with a custom dialer that hangs the Read()
28312843 // after Close().
2832- ct , err := NewHTTP2Client (ctx , context . Background () , addr , copts , func (GoAwayReason ) {})
2844+ ct , err := NewHTTP2Client (ctx , ctx , addr , copts , func (GoAwayReason ) {})
28332845 if err != nil {
28342846 t .Fatalf ("Failed to create transport: %v" , err )
28352847 }
@@ -2915,14 +2927,14 @@ func (s) TestClientCloseReturnsEarlyWhenGoAwayWriteHangs(t *testing.T) {
29152927 }
29162928 copts := ConnectOptions {Dialer : dialer }
29172929 copts .ChannelzParent = channelzSubChannel (t )
2930+ ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
2931+ defer cancel ()
29182932 // Create client transport with custom dialer
2919- ct , connErr := NewHTTP2Client (connectCtx , context . Background () , addr , copts , func (GoAwayReason ) {})
2933+ ct , connErr := NewHTTP2Client (connectCtx , ctx , addr , copts , func (GoAwayReason ) {})
29202934 if connErr != nil {
29212935 t .Fatalf ("failed to create transport: %v" , connErr )
29222936 }
29232937
2924- ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
2925- defer cancel ()
29262938 if _ , err := ct .NewStream (ctx , & CallHdr {}); err != nil {
29272939 t .Fatalf ("Failed to open stream: %v" , err )
29282940 }
0 commit comments