mirror of https://github.com/panjf2000/ants.git
🎭 Fix race error
This commit is contained in:
parent
aa7dd6f8cc
commit
05fbdb8d5b
62
ants_test.go
62
ants_test.go
|
@ -95,40 +95,40 @@ func TestAntsPoolWithFuncWaitToGetWorker(t *testing.T) {
|
||||||
t.Logf("memory usage:%d MB", curMem)
|
t.Logf("memory usage:%d MB", curMem)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestAntsPoolGetWorkerFromCache is used to test getting worker from sync.Pool.
|
TestAntsPoolGetWorkerFromCache is used to test getting worker from sync.Pool.
|
||||||
// func TestAntsPoolGetWorkerFromCache(t *testing.T) {
|
func TestAntsPoolGetWorkerFromCache(t *testing.T) {
|
||||||
// p, _ := ants.NewPool(TestSize)
|
p, _ := ants.NewPool(TestSize)
|
||||||
// defer p.Release()
|
defer p.Release()
|
||||||
|
|
||||||
// for i := 0; i < AntsSize; i++ {
|
for i := 0; i < AntsSize; i++ {
|
||||||
// p.Submit(demoFunc)
|
p.Submit(demoFunc)
|
||||||
// }
|
}
|
||||||
// time.Sleep(2 * ants.DefaultCleanIntervalTime * time.Second)
|
time.Sleep(2 * ants.DefaultCleanIntervalTime * time.Second)
|
||||||
// p.Submit(demoFunc)
|
p.Submit(demoFunc)
|
||||||
// t.Logf("pool, running workers number:%d", p.Running())
|
t.Logf("pool, running workers number:%d", p.Running())
|
||||||
// mem := runtime.MemStats{}
|
mem := runtime.MemStats{}
|
||||||
// runtime.ReadMemStats(&mem)
|
runtime.ReadMemStats(&mem)
|
||||||
// curMem = mem.TotalAlloc/MiB - curMem
|
curMem = mem.TotalAlloc/MiB - curMem
|
||||||
// t.Logf("memory usage:%d MB", curMem)
|
t.Logf("memory usage:%d MB", curMem)
|
||||||
// }
|
}
|
||||||
|
|
||||||
// TestAntsPoolWithFuncGetWorkerFromCache is used to test getting worker from sync.Pool.
|
TestAntsPoolWithFuncGetWorkerFromCache is used to test getting worker from sync.Pool.
|
||||||
// func TestAntsPoolWithFuncGetWorkerFromCache(t *testing.T) {
|
func TestAntsPoolWithFuncGetWorkerFromCache(t *testing.T) {
|
||||||
// dur := 10
|
dur := 10
|
||||||
// p, _ := ants.NewPoolWithFunc(TestSize, demoPoolFunc)
|
p, _ := ants.NewPoolWithFunc(TestSize, demoPoolFunc)
|
||||||
// defer p.Release()
|
defer p.Release()
|
||||||
|
|
||||||
// for i := 0; i < AntsSize; i++ {
|
for i := 0; i < AntsSize; i++ {
|
||||||
// p.Invoke(dur)
|
p.Invoke(dur)
|
||||||
// }
|
}
|
||||||
// time.Sleep(2 * ants.DefaultCleanIntervalTime * time.Second)
|
time.Sleep(2 * ants.DefaultCleanIntervalTime * time.Second)
|
||||||
// p.Invoke(dur)
|
p.Invoke(dur)
|
||||||
// t.Logf("pool with func, running workers number:%d", p.Running())
|
t.Logf("pool with func, running workers number:%d", p.Running())
|
||||||
// mem := runtime.MemStats{}
|
mem := runtime.MemStats{}
|
||||||
// runtime.ReadMemStats(&mem)
|
runtime.ReadMemStats(&mem)
|
||||||
// curMem = mem.TotalAlloc/MiB - curMem
|
curMem = mem.TotalAlloc/MiB - curMem
|
||||||
// t.Logf("memory usage:%d MB", curMem)
|
t.Logf("memory usage:%d MB", curMem)
|
||||||
// }
|
}
|
||||||
|
|
||||||
//-------------------------------------------------------------------------------------------
|
//-------------------------------------------------------------------------------------------
|
||||||
// Contrast between goroutines without a pool and goroutines with ants pool.
|
// Contrast between goroutines without a pool and goroutines with ants pool.
|
||||||
|
|
1
pool.go
1
pool.go
|
@ -235,6 +235,5 @@ func (p *Pool) revertWorker(worker *Worker) bool {
|
||||||
// Notify the invoker stuck in 'retrieveWorker()' of there is an available worker in the worker queue.
|
// Notify the invoker stuck in 'retrieveWorker()' of there is an available worker in the worker queue.
|
||||||
p.cond.Signal()
|
p.cond.Signal()
|
||||||
p.lock.Unlock()
|
p.lock.Unlock()
|
||||||
p.workerCache.Put(worker)
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
|
@ -239,6 +239,5 @@ func (p *PoolWithFunc) revertWorker(worker *WorkerWithFunc) bool {
|
||||||
// Notify the invoker stuck in 'retrieveWorker()' of there is an available worker in the worker queue.
|
// Notify the invoker stuck in 'retrieveWorker()' of there is an available worker in the worker queue.
|
||||||
p.cond.Signal()
|
p.cond.Signal()
|
||||||
p.lock.Unlock()
|
p.lock.Unlock()
|
||||||
p.workerCache.Put(worker)
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue