taskrunner_test.go 600 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package threading
  2. import (
  3. "runtime"
  4. "sync"
  5. "sync/atomic"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. func TestRoutinePool(t *testing.T) {
  10. times := 100
  11. pool := NewTaskRunner(runtime.NumCPU())
  12. var counter int32
  13. var waitGroup sync.WaitGroup
  14. for i := 0; i < times; i++ {
  15. waitGroup.Add(1)
  16. pool.Schedule(func() {
  17. atomic.AddInt32(&counter, 1)
  18. waitGroup.Done()
  19. })
  20. }
  21. waitGroup.Wait()
  22. assert.Equal(t, times, int(counter))
  23. }
  24. func BenchmarkRoutinePool(b *testing.B) {
  25. queue := NewTaskRunner(runtime.NumCPU())
  26. for i := 0; i < b.N; i++ {
  27. queue.Schedule(func() {
  28. })
  29. }
  30. }