You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
702 B
Go

9 months ago
package routine
import (
9 months ago
"fmt"
9 months ago
"runtime"
9 months ago
"runtime/debug"
9 months ago
"sync/atomic"
"testing"
"github.com/stretchr/testify/assert"
9 months ago
)
func TestPool(t *testing.T) {
num := runtime.NumCPU()
var sum atomic.Int32
9 months ago
task := func(t T) {
num := t.(int32)
9 months ago
if num < 0 {
panic("unable to handle negative numbers")
}
sum.Add(num)
9 months ago
}
handler := func(r any) {
fmt.Printf("Panic: %v\n %s", r, string(debug.Stack()))
}
pool := NewPool(
WithWorkers(num),
WithCapacity(num),
9 months ago
WithTaskFn(task),
WithPanicHandler(handler),
9 months ago
)
9 months ago
pool.Start()
for i := int32(1000); i >= -1; i-- {
pool.Push(i)
}
pool.Wait()
if assert.Equal(t, int32(500500), sum.Load()) {
t.Log("the sum value is right")
}
}