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.
45 lines
709 B
Go
45 lines
709 B
Go
package routine
|
|
|
|
import (
|
|
"fmt"
|
|
"runtime"
|
|
"runtime/debug"
|
|
"sync/atomic"
|
|
"testing"
|
|
|
|
"github.com/brildum/testify/assert"
|
|
)
|
|
|
|
func TestPool(t *testing.T) {
|
|
num := runtime.NumCPU()
|
|
var sum atomic.Int32
|
|
|
|
task := func(num int32) {
|
|
if num < 0 {
|
|
panic("unable to handle negative numbers")
|
|
}
|
|
|
|
sum.Add(num)
|
|
}
|
|
handler := func(r any) {
|
|
fmt.Printf("Panic: %v\n %s", r, string(debug.Stack()))
|
|
}
|
|
pool := NewPool(
|
|
WithWorkers[int32](num),
|
|
WithCapacity[int32](num),
|
|
WithTaskFn(task),
|
|
WithPanicHandler[int32](handler),
|
|
)
|
|
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")
|
|
}
|
|
}
|