From c3313133b996ed74b98b75a6d66f286a06a4b0f8 Mon Sep 17 00:00:00 2001 From: Anton Tolchanov Date: Tue, 31 Oct 2023 22:12:41 +0000 Subject: [PATCH] derp/derphttp: close DERP client to avoid data race in test Fixes #10041 Signed-off-by: Anton Tolchanov --- derp/derphttp/derphttp_test.go | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/derp/derphttp/derphttp_test.go b/derp/derphttp/derphttp_test.go index 0ec14e0ee..4a232d37e 100644 --- a/derp/derphttp/derphttp_test.go +++ b/derp/derphttp/derphttp_test.go @@ -261,6 +261,13 @@ func (c *Client) breakConnection(brokenClient *derp.Client) { // updates after a different thread breaks and reconnects the connection, while // the watcher is waiting on recv(). func TestBreakWatcherConnRecv(t *testing.T) { + // Set the wait time before a retry after connection failure to be much lower. + // This needs to be early in the test, for defer to run right at the end after + // the DERP client has finished. + origRetryInterval := retryInterval + retryInterval = 50 * time.Millisecond + defer func() { retryInterval = origRetryInterval }() + var wg sync.WaitGroup defer wg.Wait() // Make the watcher server @@ -282,11 +289,6 @@ func TestBreakWatcherConnRecv(t *testing.T) { watcherChan := make(chan int, 1) - // Set the wait time after a connection fails to much lower - origRetryInterval := retryInterval - retryInterval = 50 * time.Millisecond - defer func() { retryInterval = origRetryInterval }() - // Start the watcher thread (which connects to the watched server) wg.Add(1) // To avoid using t.Logf after the test ends. See https://golang.org/issue/40343 go func() { @@ -329,6 +331,13 @@ func TestBreakWatcherConnRecv(t *testing.T) { // updates after a different thread breaks and reconnects the connection, while // the watcher is not waiting on recv(). func TestBreakWatcherConn(t *testing.T) { + // Set the wait time before a retry after connection failure to be much lower. + // This needs to be early in the test, for defer to run right at the end after + // the DERP client has finished. + origRetryInterval := retryInterval + retryInterval = 50 * time.Millisecond + defer func() { retryInterval = origRetryInterval }() + var wg sync.WaitGroup defer wg.Wait() // Make the watcher server @@ -351,11 +360,6 @@ func TestBreakWatcherConn(t *testing.T) { watcherChan := make(chan int, 1) breakerChan := make(chan bool, 1) - // Set the wait time after a connection fails to much lower - origRetryInterval := retryInterval - retryInterval = 50 * time.Millisecond - defer func() { retryInterval = origRetryInterval }() - // Start the watcher thread (which connects to the watched server) wg.Add(1) // To avoid using t.Logf after the test ends. See https://golang.org/issue/40343 go func() {