mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-24 09:40:24 +08:00
38 lines
600 B
Go
38 lines
600 B
Go
|
package threading
|
||
|
|
||
|
import (
|
||
|
"runtime"
|
||
|
"sync"
|
||
|
"sync/atomic"
|
||
|
"testing"
|
||
|
|
||
|
"github.com/stretchr/testify/assert"
|
||
|
)
|
||
|
|
||
|
func TestRoutinePool(t *testing.T) {
|
||
|
times := 100
|
||
|
pool := NewTaskRunner(runtime.NumCPU())
|
||
|
|
||
|
var counter int32
|
||
|
var waitGroup sync.WaitGroup
|
||
|
for i := 0; i < times; i++ {
|
||
|
waitGroup.Add(1)
|
||
|
pool.Schedule(func() {
|
||
|
atomic.AddInt32(&counter, 1)
|
||
|
waitGroup.Done()
|
||
|
})
|
||
|
}
|
||
|
|
||
|
waitGroup.Wait()
|
||
|
|
||
|
assert.Equal(t, times, int(counter))
|
||
|
}
|
||
|
|
||
|
func BenchmarkRoutinePool(b *testing.B) {
|
||
|
queue := NewTaskRunner(runtime.NumCPU())
|
||
|
for i := 0; i < b.N; i++ {
|
||
|
queue.Schedule(func() {
|
||
|
})
|
||
|
}
|
||
|
}
|