File tree Expand file tree Collapse file tree 2 files changed +23
-6
lines changed
Expand file tree Collapse file tree 2 files changed +23
-6
lines changed Original file line number Diff line number Diff 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
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ package pool
22
33import (
44 "os"
5+ "sync"
56 "testing"
67 "time"
78
@@ -68,7 +69,9 @@ func TestPool(t *testing.T) {
6869
6970func 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 {
You can’t perform that action at this time.
0 commit comments