barrier_test.go 633 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package syncx
  2. import (
  3. "sync"
  4. "testing"
  5. "github.com/stretchr/testify/assert"
  6. )
  7. func TestBarrier_Guard(t *testing.T) {
  8. const total = 10000
  9. var barrier Barrier
  10. var count int
  11. var wg sync.WaitGroup
  12. wg.Add(total)
  13. for i := 0; i < total; i++ {
  14. go barrier.Guard(func() {
  15. count++
  16. wg.Done()
  17. })
  18. }
  19. wg.Wait()
  20. assert.Equal(t, total, count)
  21. }
  22. func TestBarrierPtr_Guard(t *testing.T) {
  23. const total = 10000
  24. barrier := new(Barrier)
  25. var count int
  26. wg := new(sync.WaitGroup)
  27. wg.Add(total)
  28. for i := 0; i < total; i++ {
  29. go barrier.Guard(func() {
  30. count++
  31. wg.Done()
  32. })
  33. }
  34. wg.Wait()
  35. assert.Equal(t, total, count)
  36. }