From 63489606efd8498bdd76c375b2913e7decaa1e63 Mon Sep 17 00:00:00 2001 From: jdamick Date: Mon, 12 Jul 2021 10:52:47 -0400 Subject: [PATCH] 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 --- ants_test.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/ants_test.go b/ants_test.go index 1193aff..5dfe95d 100644 --- a/ants_test.go +++ b/ants_test.go @@ -427,24 +427,25 @@ func TestMaxBlockingSubmit(t *testing.T) { func TestNonblockingSubmitWithFunc(t *testing.T) { poolSize := 10 - ch1 := make(chan struct{}) + var wg sync.WaitGroup p, err := NewPoolWithFunc(poolSize, func(i interface{}) { longRunningPoolFunc(i) - close(ch1) + wg.Done() }, WithNonblocking(true)) assert.NoError(t, err, "create TimingPool failed: %v", err) 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{}) + 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. 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(), "nonblocking submit when pool is full should get an ErrPoolOverload") // interrupt f to get an available worker close(ch) - <-ch1 + wg.Wait() assert.NoError(t, p.Invoke(nil), "nonblocking submit when pool is not full shouldn't return error") }