syncs_test.go 758 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Copyright (c) 2020 Tailscale Inc & AUTHORS All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package syncs
  5. import "testing"
  6. func TestWaitGroupChan(t *testing.T) {
  7. wg := NewWaitGroupChan()
  8. wantNotDone := func() {
  9. t.Helper()
  10. select {
  11. case <-wg.DoneChan():
  12. t.Fatal("done too early")
  13. default:
  14. }
  15. }
  16. wantDone := func() {
  17. t.Helper()
  18. select {
  19. case <-wg.DoneChan():
  20. default:
  21. t.Fatal("expected to be done")
  22. }
  23. }
  24. wg.Add(2)
  25. wantNotDone()
  26. wg.Decr()
  27. wantNotDone()
  28. wg.Decr()
  29. wantDone()
  30. wantDone()
  31. }
  32. func TestClosedChan(t *testing.T) {
  33. ch := ClosedChan()
  34. for i := 0; i < 2; i++ {
  35. select {
  36. case <-ch:
  37. default:
  38. t.Fatal("not closed")
  39. }
  40. }
  41. }