github.com/lingyao2333/mo-zero@v1.4.1/core/threading/taskrunner_test.go (about)

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