barrier_test.go 897 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. }
  37. func TestGuard(t *testing.T) {
  38. const total = 10000
  39. var count int
  40. var lock sync.Mutex
  41. wg := new(sync.WaitGroup)
  42. wg.Add(total)
  43. for i := 0; i < total; i++ {
  44. go Guard(&lock, func() {
  45. count++
  46. wg.Done()
  47. })
  48. }
  49. wg.Wait()
  50. assert.Equal(t, total, count)
  51. }