routinegroup_test.go 671 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. package threading
  2. import (
  3. "io/ioutil"
  4. "log"
  5. "sync"
  6. "sync/atomic"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestRoutineGroupRun(t *testing.T) {
  11. var count int32
  12. group := NewRoutineGroup()
  13. for i := 0; i < 3; i++ {
  14. group.Run(func() {
  15. atomic.AddInt32(&count, 1)
  16. })
  17. }
  18. group.Wait()
  19. assert.Equal(t, int32(3), count)
  20. }
  21. func TestRoutingGroupRunSafe(t *testing.T) {
  22. log.SetOutput(ioutil.Discard)
  23. var count int32
  24. group := NewRoutineGroup()
  25. var once sync.Once
  26. for i := 0; i < 3; i++ {
  27. group.RunSafe(func() {
  28. once.Do(func() {
  29. panic("")
  30. })
  31. atomic.AddInt32(&count, 1)
  32. })
  33. }
  34. group.Wait()
  35. assert.Equal(t, int32(2), count)
  36. }