@@ -147,7 +147,9 @@ func (s) TestReportLoad_ConnectionCreation(t *testing.T) {
147147 if err != nil {
148148 t .Fatalf ("client.ReportLoad() failed: %v" , err )
149149 }
150- defer loadStore1 .Stop (defaultTestShortTimeout )
150+ ssCtx , ssCancel := context .WithTimeout (context .Background (), defaultTestShortTimeout )
151+ defer ssCancel ()
152+ defer loadStore1 .Stop (ssCtx )
151153
152154 // Call the load reporting API to report load to the first management
153155 // server, and ensure that a connection to the server is created.
@@ -232,7 +234,9 @@ func (s) TestReportLoad_ConnectionCreation(t *testing.T) {
232234 }
233235
234236 // Stop this load reporting stream, server should see error canceled.
235- loadStore2 .Stop (defaultTestShortTimeout )
237+ ssCtx , ssCancel = context .WithTimeout (context .Background (), defaultTestShortTimeout )
238+ defer ssCancel ()
239+ loadStore2 .Stop (ssCtx )
236240
237241 // Server should receive a stream canceled error. There may be additional
238242 // load reports from the client in the channel.
@@ -419,15 +423,19 @@ func (s) TestReportLoad_StreamCreation(t *testing.T) {
419423
420424 // Cancel the first load reporting call, and ensure that the stream does not
421425 // close (because we have another call open).
422- loadStore1 .Stop (defaultTestShortTimeout )
426+ ssCtx , ssCancel := context .WithTimeout (context .Background (), defaultTestShortTimeout )
427+ defer ssCancel ()
428+ loadStore1 .Stop (ssCtx )
423429 sCtx , sCancel = context .WithTimeout (context .Background (), defaultTestShortTimeout )
424430 defer sCancel ()
425431 if _ , err := lrsServer .LRSStreamCloseChan .Receive (sCtx ); err != context .DeadlineExceeded {
426432 t .Fatal ("LRS stream closed when expected to stay open" )
427433 }
428434
429435 // Stop the second load reporting call, and ensure the stream is closed.
430- loadStore2 .Stop (defaultTestShortTimeout )
436+ ssCtx , ssCancel = context .WithTimeout (context .Background (), defaultTestShortTimeout )
437+ defer ssCancel ()
438+ loadStore2 .Stop (ssCtx )
431439 if _ , err := lrsServer .LRSStreamCloseChan .Receive (ctx ); err != nil {
432440 t .Fatal ("Timeout waiting for LRS stream to close" )
433441 }
@@ -442,16 +450,18 @@ func (s) TestReportLoad_StreamCreation(t *testing.T) {
442450 if _ , err := lrsServer .LRSStreamOpenChan .Receive (ctx ); err != nil {
443451 t .Fatalf ("Timeout when waiting for LRS stream to be created: %v" , err )
444452 }
445- loadStore3 .Stop (defaultTestShortTimeout )
453+ ssCtx , ssCancel = context .WithTimeout (context .Background (), defaultTestShortTimeout )
454+ defer ssCancel ()
455+ loadStore3 .Stop (ssCtx )
446456}
447457
448- // TestReportLoad_StopWithTimeout tests the behavior of LoadStore.Stop() when
449- // called with a timeout duration . It verifies that:
450- // - Stop() blocks until the timeout expires or final load send attempt is
458+ // TestReportLoad_StopWithContext tests the behavior of LoadStore.Stop() when
459+ // called with a context . It verifies that:
460+ // - Stop() blocks until the context expires or final load send attempt is
451461// made.
452462// - Final load report is seen on the server after stop is called.
453463// - The stream is closed after Stop() returns.
454- func (s ) TestReportLoad_StopWithTimeout (t * testing.T ) {
464+ func (s ) TestReportLoad_StopWithContext (t * testing.T ) {
455465 ctx , cancel := context .WithTimeout (context .Background (), defaultTestTimeout )
456466 defer cancel ()
457467
@@ -536,11 +546,11 @@ func (s) TestReportLoad_StopWithTimeout(t *testing.T) {
536546 t .Fatalf ("Unexpected diff in LRS request (-got, +want):\n %s" , diff )
537547 }
538548
539- // Create a timeout duration for Stop() that remains until the end of test
540- // to ensure that only possibility of Stop() to finish is if final load
541- // send attempt is made. If final load attempt is not made, test itself
542- // will timeout.
543- largeStopTimeout := 10 * defaultTestTimeout
549+ // Create a context for Stop() that remains until the end of test to ensure
550+ // that only possibility of Stop()s to finish is if final load send attempt
551+ // is made. If final load attempt is not made, test will timeout.
552+ stopCtx , stopCancel := context . WithCancel ( ctx )
553+ defer stopCancel ()
544554
545555 // Push more loads.
546556 loadStore .ReporterForCluster ("cluster2" , "eds2" ).CallDropped ("test" )
@@ -549,7 +559,7 @@ func (s) TestReportLoad_StopWithTimeout(t *testing.T) {
549559 // Call Stop in a separate goroutine. It will block until
550560 // final load send attempt is made.
551561 go func () {
552- loadStore .Stop (largeStopTimeout )
562+ loadStore .Stop (stopCtx )
553563 close (stopCalled )
554564 }()
555565 <- stopCalled
0 commit comments