workergroup_test.go 519 B

123456789101112131415161718192021222324252627
  1. package threading
  2. import (
  3. "fmt"
  4. "runtime"
  5. "sync"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/tal-tech/go-zero/core/lang"
  9. )
  10. func TestWorkerGroup(t *testing.T) {
  11. m := make(map[string]lang.PlaceholderType)
  12. var lock sync.Mutex
  13. var wg sync.WaitGroup
  14. wg.Add(runtime.NumCPU())
  15. group := NewWorkerGroup(func() {
  16. lock.Lock()
  17. m[fmt.Sprint(RoutineId())] = lang.Placeholder
  18. lock.Unlock()
  19. wg.Done()
  20. }, runtime.NumCPU())
  21. go group.Start()
  22. wg.Wait()
  23. assert.Equal(t, runtime.NumCPU(), len(m))
  24. }