| 350 | } |
| 351 | |
| 352 | func TestPanicHandlerPreMalloc(t *testing.T) { |
| 353 | var panicCounter int64 |
| 354 | var wg sync.WaitGroup |
| 355 | p0, err := ants.NewPool(10, ants.WithPreAlloc(true), ants.WithPanicHandler(func(p any) { |
| 356 | defer wg.Done() |
| 357 | atomic.AddInt64(&panicCounter, 1) |
| 358 | t.Logf("catch panic with PanicHandler: %v", p) |
| 359 | })) |
| 360 | require.NoErrorf(t, err, "create new pool failed: %v", err) |
| 361 | defer p0.Release() |
| 362 | wg.Add(1) |
| 363 | _ = p0.Submit(func() { |
| 364 | panic("Oops!") |
| 365 | }) |
| 366 | wg.Wait() |
| 367 | c := atomic.LoadInt64(&panicCounter) |
| 368 | require.EqualValuesf(t, 1, c, "panic handler didn't work, panicCounter: %d", c) |
| 369 | require.EqualValues(t, 0, p0.Running(), "pool should be empty after panic") |
| 370 | |
| 371 | p1, err := ants.NewPoolWithFunc(10, func(p any) { panic(p) }, ants.WithPreAlloc(true), ants.WithPanicHandler(func(_ any) { |
| 372 | defer wg.Done() |
| 373 | atomic.AddInt64(&panicCounter, 1) |
| 374 | })) |
| 375 | require.NoErrorf(t, err, "create new pool with func failed: %v", err) |
| 376 | defer p1.Release() |
| 377 | wg.Add(1) |
| 378 | _ = p1.Invoke("Oops!") |
| 379 | wg.Wait() |
| 380 | c = atomic.LoadInt64(&panicCounter) |
| 381 | require.EqualValuesf(t, 2, c, "panic handler didn't work, panicCounter: %d", c) |
| 382 | require.EqualValues(t, 0, p1.Running(), "pool should be empty after panic") |
| 383 | |
| 384 | p2, err := ants.NewPoolWithFuncGeneric(10, func(p string) { panic(p) }, ants.WithPreAlloc(true), ants.WithPanicHandler(func(_ any) { |
| 385 | defer wg.Done() |
| 386 | atomic.AddInt64(&panicCounter, 1) |
| 387 | })) |
| 388 | require.NoErrorf(t, err, "create new pool with func failed: %v", err) |
| 389 | defer p2.Release() |
| 390 | wg.Add(1) |
| 391 | _ = p2.Invoke("Oops!") |
| 392 | wg.Wait() |
| 393 | c = atomic.LoadInt64(&panicCounter) |
| 394 | require.EqualValuesf(t, 3, c, "panic handler didn't work, panicCounter: %d", c) |
| 395 | require.EqualValues(t, 0, p1.Running(), "pool should be empty after panic") |
| 396 | } |
| 397 | |
| 398 | func TestPoolPanicWithoutHandler(t *testing.T) { |
| 399 | p0, err := ants.NewPool(10) |