Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: fix TestPoolPanic in gopool #155

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion util/gopool/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package gopool

import (
"context"
"runtime"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -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) {
Expand Down