Add barrier

This commit is contained in:
Alexander Neumann 2016-05-09 21:29:13 +02:00
parent c6d934a685
commit fb45ea139d
1 changed files with 17 additions and 11 deletions

View File

@ -96,8 +96,14 @@ type Job struct {
suc chan struct{} suc chan struct{}
} }
func wait(job worker.Job, done <-chan struct{}) (interface{}, error) { func TestPoolCancel(t *testing.T) {
barrier := make(chan struct{})
wait := func(job worker.Job, done <-chan struct{}) (interface{}, error) {
j := job.Data.(Job) j := job.Data.(Job)
<-barrier
select { select {
case j.suc <- struct{}{}: case j.suc <- struct{}{}:
return time.Now(), nil return time.Now(), nil
@ -106,14 +112,14 @@ func wait(job worker.Job, done <-chan struct{}) (interface{}, error) {
} }
} }
func TestPoolCancel(t *testing.T) {
jobCh, resCh, p := newBufferedPool(20, concurrency, wait) jobCh, resCh, p := newBufferedPool(20, concurrency, wait)
suc := make(chan struct{}, 1) suc := make(chan struct{})
for i := 0; i < 20; i++ { for i := 0; i < 20; i++ {
jobCh <- worker.Job{Data: Job{suc: suc}} jobCh <- worker.Job{Data: Job{suc: suc}}
} }
close(barrier)
<-suc <-suc
p.Cancel() p.Cancel()
p.Wait() p.Wait()