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

Function TestMultiPoolWithFuncGenericReleaseContext

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

Source from the content-addressed store, hash-verified

1697}
1698
1699func TestMultiPoolWithFuncGenericReleaseContext(t *testing.T) {
1700 ch := make(chan struct{})
1701 mp, err := ants.NewMultiPoolWithFuncGeneric(10, 5, longRunningPoolFuncCh, ants.RoundRobin)
1702 require.NoError(t, err)
1703
1704 for i := 0; i < 50; i++ {
1705 err = mp.Invoke(ch)
1706 require.NoError(t, err)
1707 }
1708 require.EqualValues(t, 50, mp.Running())
1709
1710 close(ch)
1711 err = mp.ReleaseContext(context.Background())
1712 require.NoError(t, err)
1713 require.Zero(t, mp.Running())
1714 require.True(t, mp.IsClosed())
1715
1716 // Calling ReleaseContext on a closed pool should return ErrPoolClosed.
1717 require.ErrorIs(t, mp.ReleaseContext(context.Background()), ants.ErrPoolClosed)
1718
1719 // Test with LeastTasks strategy.
1720 ch = make(chan struct{})
1721 mp, err = ants.NewMultiPoolWithFuncGeneric(10, 5, longRunningPoolFuncCh, ants.LeastTasks)
1722 require.NoError(t, err)
1723 for i := 0; i < 50; i++ {
1724 err = mp.Invoke(ch)
1725 require.NoError(t, err)
1726 }
1727 close(ch)
1728 err = mp.ReleaseContext(context.Background())
1729 require.NoError(t, err)
1730 require.Zero(t, mp.Running())
1731 require.True(t, mp.IsClosed())
1732
1733 // Test that a cancelled context returns an error.
1734 ch = make(chan struct{})
1735 mp, err = ants.NewMultiPoolWithFuncGeneric(10, 5, longRunningPoolFuncCh, ants.RoundRobin)
1736 require.NoError(t, err)
1737 for i := 0; i < 50; i++ {
1738 err = mp.Invoke(ch)
1739 require.NoError(t, err)
1740 }
1741 ctx, cancel := context.WithCancel(context.Background())
1742 cancel()
1743 err = mp.ReleaseContext(ctx)
1744 require.Error(t, err)
1745 close(ch)
1746 require.Eventually(t, func() bool {
1747 return mp.Running() == 0
1748 }, 3*time.Second, 100*time.Millisecond)
1749}
1750
1751func TestRebootNewPoolCalc(t *testing.T) {
1752 atomic.StoreInt32(&sum, 0)

Callers

nothing calls this directly

Calls 4

ReleaseContextMethod · 0.65
InvokeMethod · 0.45
RunningMethod · 0.45
IsClosedMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…