From 840634ae51ccf4fb4ffe877897c9d5078f8498fd Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Sun, 27 Jan 2019 05:03:26 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=B3=20Add=20test=20cases=20for=20sync.?= =?UTF-8?q?Pool?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ants_test.go | 56 +++++++++++++++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/ants_test.go b/ants_test.go index 5c8956e..4b1b143 100644 --- a/ants_test.go +++ b/ants_test.go @@ -75,25 +75,39 @@ func TestAntsPoolWithFuncWaitToGetWorker(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++ { -// wg.Add(1) -// p.Submit(func() { -// demoPoolFunc(Param) -// wg.Done() -// }) -// } -// wg.Wait() -// t.Logf("pool, running workers number:%d", p.Running()) -// mem := runtime.MemStats{} -// runtime.ReadMemStats(&mem) -// curMem = mem.TotalAlloc/MiB - curMem -// t.Logf("memory usage:%d MB", curMem) -//} +func TestAntsPoolGetWorkerFromCache(t *testing.T) { + p, _ := ants.NewPool(TestSize) + defer p.Release() + + for i := 0; i < AntsSize; i++ { + p.Submit(demoFunc) + } + time.Sleep(ants.DefaultCleanIntervalTime * time.Second) + p.Submit(demoFunc) + t.Logf("pool, running workers number:%d", p.Running()) + mem := runtime.MemStats{} + runtime.ReadMemStats(&mem) + curMem = mem.TotalAlloc/MiB - curMem + t.Logf("memory usage:%d MB", curMem) +} + +// TestAntsPoolWithFuncGetWorkerFromCache is used to test getting worker from sync.Pool. +func TestAntsPoolWithFuncGetWorkerFromCache(t *testing.T) { + dur := 10 + p, _ := ants.NewPoolWithFunc(TestSize, demoPoolFunc) + defer p.Release() + + for i := 0; i < AntsSize; i++ { + p.Serve(dur) + } + time.Sleep(ants.DefaultCleanIntervalTime * time.Second) + p.Serve(dur) + t.Logf("pool, running workers number:%d", p.Running()) + mem := runtime.MemStats{} + runtime.ReadMemStats(&mem) + curMem = mem.TotalAlloc/MiB - curMem + t.Logf("memory usage:%d MB", curMem) +} //------------------------------------------------------------------------------------------- // Contrast between goroutines without a pool and goroutines with ants pool. @@ -246,9 +260,7 @@ func TestRestCodeCoverage(t *testing.T) { defer p0.Submit(demoFunc) defer p0.Release() for i := 0; i < n; i++ { - p0.Submit(func() { - demoPoolFunc(Param) - }) + p0.Submit(demoFunc) } t.Logf("pool, capacity:%d", p0.Cap()) t.Logf("pool, running workers number:%d", p0.Running())