Skip to content

Commit f90b3c1

Browse files
joeybloggsjoeybloggs
authored andcommitted
Fixed race condition in test not library and fix batch missed unlocking mutex.
1 parent 29877a8 commit f90b3c1

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

batch.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func (b *Batch) Queue(fn WorkFunc) {
3838
b.m.Lock()
3939

4040
if b.closed {
41+
b.m.Unlock()
4142
return
4243
}
4344

pool_test.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package pool
22

33
import (
44
"os"
5+
"sync"
56
"testing"
67
"time"
78

@@ -68,7 +69,9 @@ func TestPool(t *testing.T) {
6869

6970
func TestCancel(t *testing.T) {
7071

71-
var res []*WorkUnit
72+
m := new(sync.RWMutex)
73+
var closed bool
74+
c := make(chan *WorkUnit, 100)
7275

7376
pool := gpool
7477
defer pool.Close()
@@ -80,19 +83,32 @@ func TestCancel(t *testing.T) {
8083
}
8184
}
8285

83-
go func() {
86+
go func(ch chan *WorkUnit) {
8487
for i := 0; i < 40; i++ {
85-
wu := pool.Queue(newFunc(time.Second * 1))
86-
res = append(res, wu)
88+
89+
go func(ch chan *WorkUnit) {
90+
m.RLock()
91+
if closed {
92+
m.RUnlock()
93+
return
94+
}
95+
96+
ch <- pool.Queue(newFunc(time.Second * 1))
97+
m.RUnlock()
98+
}(ch)
8799
}
88-
}()
100+
}(c)
89101

90102
time.Sleep(time.Second * 1)
91103
pool.Cancel()
104+
m.Lock()
105+
closed = true
106+
close(c)
107+
m.Unlock()
92108

93109
var count int
94110

95-
for _, wu := range res {
111+
for wu := range c {
96112
<-wu.Done
97113

98114
if wu.Error != nil {

0 commit comments

Comments
 (0)