Fix the timing issue in the TestNonblockingSubmitWithFunc (#172)

On some machines this unit would fail due to a timing issue.  Since the Invoke in the loop was using nil as the param, it would (depending on timing) lead to the workers finishing the  (w *goWorkerWithFunc) run() in the range loop over w.args as args would == nil and return.

If an explicit time.Sleep(1 * time.Second) is added before the "	assert.EqualError(t, p.Invoke(nil), ErrPoolOverload.Error()," it is easily reproducible.

Co-authored-by: Jeffrey Damick <jdamick@amazon.com>
This commit is contained in:
jdamick 2021-07-12 10:52:47 -04:00 committed by GitHub
parent 4b16a81116
commit 63489606ef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 6 deletions

View File

@ -427,24 +427,25 @@ func TestMaxBlockingSubmit(t *testing.T) {
func TestNonblockingSubmitWithFunc(t *testing.T) { func TestNonblockingSubmitWithFunc(t *testing.T) {
poolSize := 10 poolSize := 10
ch1 := make(chan struct{}) var wg sync.WaitGroup
p, err := NewPoolWithFunc(poolSize, func(i interface{}) { p, err := NewPoolWithFunc(poolSize, func(i interface{}) {
longRunningPoolFunc(i) longRunningPoolFunc(i)
close(ch1) wg.Done()
}, WithNonblocking(true)) }, WithNonblocking(true))
assert.NoError(t, err, "create TimingPool failed: %v", err) assert.NoError(t, err, "create TimingPool failed: %v", err)
defer p.Release() defer p.Release()
for i := 0; i < poolSize-1; i++ {
assert.NoError(t, p.Invoke(nil), "nonblocking submit when pool is not full shouldn't return error")
}
ch := make(chan struct{}) ch := make(chan struct{})
wg.Add(poolSize)
for i := 0; i < poolSize-1; i++ {
assert.NoError(t, p.Invoke(ch), "nonblocking submit when pool is not full shouldn't return error")
}
// p is full now. // p is full now.
assert.NoError(t, p.Invoke(ch), "nonblocking submit when pool is not full shouldn't return error") assert.NoError(t, p.Invoke(ch), "nonblocking submit when pool is not full shouldn't return error")
assert.EqualError(t, p.Invoke(nil), ErrPoolOverload.Error(), assert.EqualError(t, p.Invoke(nil), ErrPoolOverload.Error(),
"nonblocking submit when pool is full should get an ErrPoolOverload") "nonblocking submit when pool is full should get an ErrPoolOverload")
// interrupt f to get an available worker // interrupt f to get an available worker
close(ch) close(ch)
<-ch1 wg.Wait()
assert.NoError(t, p.Invoke(nil), "nonblocking submit when pool is not full shouldn't return error") assert.NoError(t, p.Invoke(nil), "nonblocking submit when pool is not full shouldn't return error")
} }