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

Function TestPurgePool

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

Source from the content-addressed store, hash-verified

438}
439
440func TestPurgePool(t *testing.T) {
441 size := 500
442 ch := make(chan struct{})
443
444 p, err := ants.NewPool(size)
445 require.NoErrorf(t, err, "create TimingPool failed: %v", err)
446 defer p.Release()
447
448 for i := 0; i < size; i++ {
449 j := i + 1
450 _ = p.Submit(func() {
451 <-ch
452 d := j % 100
453 time.Sleep(time.Duration(d) * time.Millisecond)
454 })
455 }
456 require.EqualValuesf(t, size, p.Running(), "pool should be full, expected: %d, but got: %d", size, p.Running())
457
458 close(ch)
459 time.Sleep(5 * ants.DefaultCleanIntervalTime)
460 require.EqualValuesf(t, 0, p.Running(), "pool should be empty after purge, but got %d", p.Running())
461
462 ch = make(chan struct{})
463 f := func(i any) {
464 <-ch
465 d := i.(int) % 100
466 time.Sleep(time.Duration(d) * time.Millisecond)
467 }
468
469 p1, err := ants.NewPoolWithFunc(size, f)
470 require.NoErrorf(t, err, "create TimingPoolWithFunc failed: %v", err)
471 defer p1.Release()
472
473 for i := 0; i < size; i++ {
474 _ = p1.Invoke(i)
475 }
476 require.EqualValuesf(t, size, p1.Running(), "pool should be full, expected: %d, but got: %d", size, p1.Running())
477
478 close(ch)
479 time.Sleep(5 * ants.DefaultCleanIntervalTime)
480 require.EqualValuesf(t, 0, p1.Running(), "pool should be empty after purge, but got %d", p1.Running())
481
482 ch = make(chan struct{})
483 f1 := func(i int) {
484 <-ch
485 d := i % 100
486 time.Sleep(time.Duration(d) * time.Millisecond)
487 }
488
489 p2, err := ants.NewPoolWithFuncGeneric(size, f1)
490 require.NoErrorf(t, err, "create TimingPoolWithFunc failed: %v", err)
491 defer p2.Release()
492
493 for i := 0; i < size; i++ {
494 _ = p2.Invoke(i)
495 }
496 require.EqualValuesf(t, size, p2.Running(), "pool should be full, expected: %d, but got: %d", size, p2.Running())
497

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…