From 049de4139ffb555691aa488efe17c05e364546b6 Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Sun, 27 Jan 2019 03:14:47 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=BE=20Refactor=20unit=20tests=20to=20s?= =?UTF-8?q?upport=20sync.Pool=20testing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ants_test.go | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/ants_test.go b/ants_test.go index bd8c23b..232be76 100644 --- a/ants_test.go +++ b/ants_test.go @@ -53,8 +53,8 @@ const ( var curMem uint64 -// TestAntsPoolWithFunc makes sure that code coverage of PoolWithFunc is fully or nearly complete. -func TestAntsPoolWithFuncCoverage(t *testing.T) { +// TestAntsPoolWithFuncWaitToGetWorker is used to test waiting to get worker. +func TestAntsPoolWithFuncWaitToGetWorker(t *testing.T) { var wg sync.WaitGroup p, _ := ants.NewPoolWithFunc(AntsSize, func(i interface{}) { demoPoolFunc(i) @@ -74,16 +74,19 @@ func TestAntsPoolWithFuncCoverage(t *testing.T) { t.Logf("memory usage:%d MB", curMem) } -// TestAntsPool makes sure that code coverage of PoolWithFunc is fully or nearly complete. -func TestAntsPoolCoverage(t *testing.T) { +// TestAntsPoolGetWorkerFromCache is used to test getting worker from sync.Pool. +func TestAntsPoolGetWorkerFromCache(t *testing.T) { var wg sync.WaitGroup p, _ := ants.NewPool(AntsSize) defer p.Release() for i := 0; i < n; i++ { + if i == n/2 { + time.Sleep(ants.DefaultCleanIntervalTime * time.Second) + } wg.Add(1) p.Submit(func() { - demoPoolFunc(Param) + demoFunc() wg.Done() }) } @@ -226,7 +229,7 @@ func TestPurge(t *testing.T) { t.Fatalf("create TimingPoolWithFunc failed: %s", err.Error()) } p1.Serve(1) - time.Sleep(5 * time.Second) + time.Sleep(ants.DefaultCleanIntervalTime * time.Second) if p.Running() != 0 { t.Error("all p should be purged") } @@ -266,6 +269,6 @@ func TestRestCodeCoverage(t *testing.T) { t.Logf("pool with func, running workers number:%d", p.Running()) t.Logf("pool with func, free workers number:%d", p.Free()) p.Tune(TestSize) - p.Tune(AntsSize) + p.Tune(TestSize / 2) t.Logf("pool with func, after tuning capacity, capacity:%d, running:%d", p.Cap(), p.Running()) }