🐾 Refactor unit tests to support sync.Pool testing

This commit is contained in:
Andy Pan 2019-01-27 03:14:47 +08:00
parent 1ce7524890
commit 049de4139f
1 changed files with 10 additions and 7 deletions

View File

@ -53,8 +53,8 @@ const (
var curMem uint64 var curMem uint64
// TestAntsPoolWithFunc makes sure that code coverage of PoolWithFunc is fully or nearly complete. // TestAntsPoolWithFuncWaitToGetWorker is used to test waiting to get worker.
func TestAntsPoolWithFuncCoverage(t *testing.T) { func TestAntsPoolWithFuncWaitToGetWorker(t *testing.T) {
var wg sync.WaitGroup var wg sync.WaitGroup
p, _ := ants.NewPoolWithFunc(AntsSize, func(i interface{}) { p, _ := ants.NewPoolWithFunc(AntsSize, func(i interface{}) {
demoPoolFunc(i) demoPoolFunc(i)
@ -74,16 +74,19 @@ func TestAntsPoolWithFuncCoverage(t *testing.T) {
t.Logf("memory usage:%d MB", curMem) t.Logf("memory usage:%d MB", curMem)
} }
// TestAntsPool makes sure that code coverage of PoolWithFunc is fully or nearly complete. // TestAntsPoolGetWorkerFromCache is used to test getting worker from sync.Pool.
func TestAntsPoolCoverage(t *testing.T) { func TestAntsPoolGetWorkerFromCache(t *testing.T) {
var wg sync.WaitGroup var wg sync.WaitGroup
p, _ := ants.NewPool(AntsSize) p, _ := ants.NewPool(AntsSize)
defer p.Release() defer p.Release()
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
if i == n/2 {
time.Sleep(ants.DefaultCleanIntervalTime * time.Second)
}
wg.Add(1) wg.Add(1)
p.Submit(func() { p.Submit(func() {
demoPoolFunc(Param) demoFunc()
wg.Done() wg.Done()
}) })
} }
@ -226,7 +229,7 @@ func TestPurge(t *testing.T) {
t.Fatalf("create TimingPoolWithFunc failed: %s", err.Error()) t.Fatalf("create TimingPoolWithFunc failed: %s", err.Error())
} }
p1.Serve(1) p1.Serve(1)
time.Sleep(5 * time.Second) time.Sleep(ants.DefaultCleanIntervalTime * time.Second)
if p.Running() != 0 { if p.Running() != 0 {
t.Error("all p should be purged") 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, running workers number:%d", p.Running())
t.Logf("pool with func, free workers number:%d", p.Free()) t.Logf("pool with func, free workers number:%d", p.Free())
p.Tune(TestSize) 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()) t.Logf("pool with func, after tuning capacity, capacity:%d, running:%d", p.Cap(), p.Running())
} }