waiterset_test.go 821 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. // Copyright (c) Tailscale Inc & AUTHORS
  2. // SPDX-License-Identifier: BSD-3-Clause
  3. package ipnserver
  4. import (
  5. "context"
  6. "sync"
  7. "testing"
  8. )
  9. func TestWaiterSet(t *testing.T) {
  10. var s waiterSet
  11. wantLen := func(want int, when string) {
  12. t.Helper()
  13. if got := len(s); got != want {
  14. t.Errorf("%s: len = %v; want %v", when, got, want)
  15. }
  16. }
  17. wantLen(0, "initial")
  18. var mu sync.Mutex
  19. ctx, cancel := context.WithCancel(context.Background())
  20. ready, cleanup := s.add(&mu, ctx)
  21. wantLen(1, "after add")
  22. select {
  23. case <-ready:
  24. t.Fatal("should not be ready")
  25. default:
  26. }
  27. s.wakeAll()
  28. <-ready
  29. wantLen(1, "after fire")
  30. cleanup()
  31. wantLen(0, "after cleanup")
  32. // And again but on an already-expired ctx.
  33. cancel()
  34. ready, cleanup = s.add(&mu, ctx)
  35. <-ready // shouldn't block
  36. cleanup()
  37. wantLen(0, "at end")
  38. }