forked from mirror/ants
Fix the bug that blocks forever when call Release() before all tasks are done
Fixes #202
This commit is contained in:
parent
1bd4304727
commit
8d03fcf77f
75
ants_test.go
75
ants_test.go
|
@ -590,6 +590,81 @@ func TestInfinitePoolWithFunc(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestReleaseWhenRunningPool(t *testing.T) {
|
||||
var wg sync.WaitGroup
|
||||
p, _ := NewPool(1)
|
||||
wg.Add(2)
|
||||
go func() {
|
||||
t.Log("start aaa")
|
||||
defer func() {
|
||||
wg.Done()
|
||||
t.Log("stop aaa")
|
||||
}()
|
||||
for i := 0; i < 30; i++ {
|
||||
j := i
|
||||
p.Submit(func() {
|
||||
t.Log("do task", j)
|
||||
time.Sleep(1 * time.Second)
|
||||
})
|
||||
}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
t.Log("start bbb")
|
||||
defer func() {
|
||||
wg.Done()
|
||||
t.Log("stop bbb")
|
||||
}()
|
||||
for i := 100; i < 130; i++ {
|
||||
j := i
|
||||
p.Submit(func() {
|
||||
t.Log("do task", j)
|
||||
time.Sleep(1 * time.Second)
|
||||
})
|
||||
}
|
||||
}()
|
||||
|
||||
time.Sleep(3 * time.Second)
|
||||
p.Release()
|
||||
t.Log("wait for all goroutines to exit...")
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func TestReleaseWhenRunningPoolWithFunc(t *testing.T) {
|
||||
var wg sync.WaitGroup
|
||||
p, _ := NewPoolWithFunc(1, func(i interface{}) {
|
||||
t.Log("do task", i)
|
||||
time.Sleep(1 * time.Second)
|
||||
})
|
||||
wg.Add(2)
|
||||
go func() {
|
||||
t.Log("start aaa")
|
||||
defer func() {
|
||||
wg.Done()
|
||||
t.Log("stop aaa")
|
||||
}()
|
||||
for i := 0; i < 30; i++ {
|
||||
p.Invoke(i)
|
||||
}
|
||||
}()
|
||||
|
||||
go func() {
|
||||
t.Log("start bbb")
|
||||
defer func() {
|
||||
wg.Done()
|
||||
t.Log("stop bbb")
|
||||
}()
|
||||
for i := 100; i < 130; i++ {
|
||||
p.Invoke(i)
|
||||
}
|
||||
}()
|
||||
|
||||
time.Sleep(3 * time.Second)
|
||||
p.Release()
|
||||
t.Log("wait for all goroutines to exit...")
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func TestRestCodeCoverage(t *testing.T) {
|
||||
_, err := NewPool(-1, WithExpiryDuration(-1))
|
||||
t.Log(err)
|
||||
|
|
1
pool.go
1
pool.go
|
@ -276,6 +276,7 @@ func (p *Pool) retrieveWorker() (w *goWorker) {
|
|||
// revertWorker puts a worker back into free pool, recycling the goroutines.
|
||||
func (p *Pool) revertWorker(worker *goWorker) bool {
|
||||
if capacity := p.Cap(); (capacity > 0 && p.Running() > capacity) || p.IsClosed() {
|
||||
p.cond.Broadcast()
|
||||
return false
|
||||
}
|
||||
worker.recycleTime = time.Now()
|
||||
|
|
|
@ -303,6 +303,7 @@ func (p *PoolWithFunc) retrieveWorker() (w *goWorkerWithFunc) {
|
|||
// revertWorker puts a worker back into free pool, recycling the goroutines.
|
||||
func (p *PoolWithFunc) revertWorker(worker *goWorkerWithFunc) bool {
|
||||
if capacity := p.Cap(); (capacity > 0 && p.Running() > capacity) || p.IsClosed() {
|
||||
p.cond.Broadcast()
|
||||
return false
|
||||
}
|
||||
worker.recycleTime = time.Now()
|
||||
|
|
Loading…
Reference in New Issue