MCPcopy
hub / github.com/panjf2000/ants / TestPanicHandler

Function TestPanicHandler

ants_test.go:306–350  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

304}
305
306func TestPanicHandler(t *testing.T) {
307 var panicCounter int64
308 var wg sync.WaitGroup
309 p0, err := ants.NewPool(10, ants.WithPanicHandler(func(p any) {
310 defer wg.Done()
311 atomic.AddInt64(&panicCounter, 1)
312 t.Logf("catch panic with PanicHandler: %v", p)
313 }))
314 require.NoErrorf(t, err, "create new pool failed: %v", err)
315 defer p0.Release()
316 wg.Add(1)
317 _ = p0.Submit(func() {
318 panic("Oops!")
319 })
320 wg.Wait()
321 c := atomic.LoadInt64(&panicCounter)
322 require.EqualValuesf(t, 1, c, "panic handler didn't work, panicCounter: %d", c)
323 require.EqualValues(t, 0, p0.Running(), "pool should be empty after panic")
324
325 p1, err := ants.NewPoolWithFunc(10, func(p any) { panic(p) }, ants.WithPanicHandler(func(_ any) {
326 defer wg.Done()
327 atomic.AddInt64(&panicCounter, 1)
328 }))
329 require.NoErrorf(t, err, "create new pool with func failed: %v", err)
330 defer p1.Release()
331 wg.Add(1)
332 _ = p1.Invoke("Oops!")
333 wg.Wait()
334 c = atomic.LoadInt64(&panicCounter)
335 require.EqualValuesf(t, 2, c, "panic handler didn't work, panicCounter: %d", c)
336 require.EqualValues(t, 0, p1.Running(), "pool should be empty after panic")
337
338 p2, err := ants.NewPoolWithFuncGeneric(10, func(s string) { panic(s) }, ants.WithPanicHandler(func(_ any) {
339 defer wg.Done()
340 atomic.AddInt64(&panicCounter, 1)
341 }))
342 require.NoErrorf(t, err, "create new pool with func failed: %v", err)
343 defer p2.Release()
344 wg.Add(1)
345 _ = p2.Invoke("Oops!")
346 wg.Wait()
347 c = atomic.LoadInt64(&panicCounter)
348 require.EqualValuesf(t, 3, c, "panic handler didn't work, panicCounter: %d", c)
349 require.EqualValues(t, 0, p2.Running(), "pool should be empty after panic")
350}
351
352func TestPanicHandlerPreMalloc(t *testing.T) {
353 var panicCounter int64

Callers

nothing calls this directly

Calls 4

SubmitMethod · 0.95
InvokeMethod · 0.95
ReleaseMethod · 0.80
RunningMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…