From 4025da7285a77829149c5cbff1bd80fda4d607ec Mon Sep 17 00:00:00 2001 From: ag9920 Date: Wed, 28 Sep 2022 11:49:35 +0800 Subject: [PATCH] test: fix TestPoolPanic in gopool --- util/gopool/pool_test.go | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/util/gopool/pool_test.go b/util/gopool/pool_test.go index 6d81bec9..23602d3d 100644 --- a/util/gopool/pool_test.go +++ b/util/gopool/pool_test.go @@ -15,6 +15,7 @@ package gopool import ( + "context" "runtime" "sync" "sync/atomic" @@ -56,8 +57,31 @@ func TestPool(t *testing.T) { } func TestPoolPanic(t *testing.T) { + var ( + calledTimes int + expectTimes int = 8 + ) + // initialize new pool and set panic handler p := NewPool("test", 100, NewConfig()) - p.Go(testPanicFunc) + p.SetPanicHandler(func(ctx context.Context, r interface{}) { + calledTimes++ + }) + + // execute panic function concurrently to see if provided panic handler were called + var wg sync.WaitGroup + for i := 0; i < expectTimes; i++ { + wg.Add(1) + p.Go(func() { + defer wg.Done() + testPanicFunc() + }) + } + wg.Wait() + + // if panic was not recovered, or panic handler was not called accordingly, callTimes would not be equal to expectTimes + if calledTimes != expectTimes { + t.Errorf("panic handler executed wrong times, expectTimes=%d, actualTimes=%d", expectTimes, calledTimes) + } } func BenchmarkPool(b *testing.B) {