leak_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //go:build e2e_testing
  2. // +build e2e_testing
  3. package e2e
  4. import (
  5. "testing"
  6. "time"
  7. "github.com/slackhq/nebula/cert"
  8. "github.com/slackhq/nebula/cert_test"
  9. "github.com/slackhq/nebula/e2e/router"
  10. "go.uber.org/goleak"
  11. )
  12. // TestNoGoroutineLeaks brings up two nebula instances, completes a tunnel,
  13. // stops both, and asserts no goroutines leak past the shutdown. goleak's
  14. // retry mechanism gives the wg.Wait()-driven goroutines a moment to drain
  15. // before failing the assertion.
  16. //
  17. // IgnoreCurrent is necessary in the parallelized suite: other tests can
  18. // leave goroutines mid-shutdown when this one runs (Stop is async, the
  19. // wg.Wait() drain is not blocking on test return). We're checking that
  20. // *this* test's setup tears down cleanly, not that the whole suite is
  21. // idle at this moment. Intentionally NOT t.Parallel()'d for the same
  22. // reason — concurrent test goroutines would always show up.
  23. func TestNoGoroutineLeaks(t *testing.T) {
  24. defer goleak.VerifyNone(t, goleak.IgnoreCurrent())
  25. ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
  26. myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me", "10.128.0.1/24", nil)
  27. theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.2/24", nil)
  28. myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
  29. theirControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr)
  30. myControl.Start()
  31. theirControl.Start()
  32. r := router.NewR(t, myControl, theirControl)
  33. assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
  34. myControl.Stop()
  35. theirControl.Stop()
  36. r.RenderFlow()
  37. // Settle period: Stop() is non-blocking; the wg-driven goroutines need
  38. // a moment to drain. goleak retries internally too, but a short explicit
  39. // settle reduces flakes when the suite is busy.
  40. time.Sleep(50 * time.Millisecond)
  41. }