1
0
mirror of https://github.com/nxshock/gwp.git synced 2024-11-27 03:31:02 +05:00
gwp/gwp_test.go

52 lines
764 B
Go
Raw Normal View History

2021-02-03 23:28:12 +05:00
package gwp
import (
2021-02-04 21:23:43 +05:00
"errors"
2021-02-03 23:28:12 +05:00
"sync/atomic"
"testing"
"github.com/stretchr/testify/assert"
)
func TestBasic(t *testing.T) {
for i := 0; i < 10; i++ {
wp := New(i)
count := new(int64)
for j := 0; j < 100; j++ {
wp.Add(func() error {
atomic.AddInt64(count, 1)
return nil
})
}
wp.CloseAndWait()
assert.EqualValues(t, 100, *count)
2021-02-04 21:23:43 +05:00
assert.EqualValues(t, 100, wp.processedCount)
}
}
func TestErrorCounter(t *testing.T) {
for i := 0; i < 10; i++ {
wp := New(i)
for j := 0; j < 100; j++ {
n := j
wp.Add(func() error {
if n%2 == 0 {
return errors.New("error")
}
return nil
})
}
wp.CloseAndWait()
assert.EqualValues(t, 100, wp.processedCount)
assert.EqualValues(t, 50, wp.errorCount)
2021-02-03 23:28:12 +05:00
}
}