]> git.ipfire.org Git - thirdparty/wireguard-go.git/commitdiff
device: fix goroutine leak test
authorJosh Bleecher Snyder <josh@tailscale.com>
Wed, 3 Feb 2021 16:26:27 +0000 (08:26 -0800)
committerJason A. Donenfeld <Jason@zx2c4.com>
Wed, 3 Feb 2021 16:45:22 +0000 (17:45 +0100)
The leak test had rare flakes.
If a system goroutine started at just the wrong moment, you'd get a false positive.
Instead of looping until the goroutines look good and then checking,
exit completely as soon as the number of goroutines looks good.
Also, check more frequently, in an attempt to complete faster.

Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
device/device_test.go

index b7837711fb5a9ab1d401e2d56823a1d9a0f84620..50e3dbc5a0766af7f746c788f888c5024ae62871 100644 (file)
@@ -405,14 +405,15 @@ func goroutineLeakCheck(t *testing.T) {
                        return
                }
                // Give goroutines time to exit, if they need it.
-               for i := 0; i < 1000 && startGoroutines < runtime.NumGoroutine(); i++ {
-                       time.Sleep(10 * time.Millisecond)
-               }
-               if got := runtime.NumGoroutine(); startGoroutines < got {
-                       _, endStacks := goroutines()
-                       t.Logf("starting stacks:\n%s\n", startStacks)
-                       t.Logf("ending stacks:\n%s\n", endStacks)
-                       t.Fatalf("expected %d goroutines, got %d, leak?", startGoroutines, got)
+               for i := 0; i < 10000; i++ {
+                       if runtime.NumGoroutine() <= startGoroutines {
+                               return
+                       }
+                       time.Sleep(1 * time.Millisecond)
                }
+               endGoroutines, endStacks := goroutines()
+               t.Logf("starting stacks:\n%s\n", startStacks)
+               t.Logf("ending stacks:\n%s\n", endStacks)
+               t.Fatalf("expected %d goroutines, got %d, leak?", startGoroutines, endGoroutines)
        })
 }