From fb45ea139d26468ba1f2bc7dbee81f429ea1a88c Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Mon, 9 May 2016 21:29:13 +0200 Subject: [PATCH] Add barrier --- src/restic/worker/pool_test.go | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/restic/worker/pool_test.go b/src/restic/worker/pool_test.go index 5c7ca91e4..78cda9cd7 100644 --- a/src/restic/worker/pool_test.go +++ b/src/restic/worker/pool_test.go @@ -96,24 +96,30 @@ type Job struct { suc chan struct{} } -func wait(job worker.Job, done <-chan struct{}) (interface{}, error) { - j := job.Data.(Job) - select { - case j.suc <- struct{}{}: - return time.Now(), nil - case <-done: - return nil, errCancelled - } -} - func TestPoolCancel(t *testing.T) { + barrier := make(chan struct{}) + + wait := func(job worker.Job, done <-chan struct{}) (interface{}, error) { + j := job.Data.(Job) + + <-barrier + + select { + case j.suc <- struct{}{}: + return time.Now(), nil + case <-done: + return nil, errCancelled + } + } + jobCh, resCh, p := newBufferedPool(20, concurrency, wait) - suc := make(chan struct{}, 1) + suc := make(chan struct{}) for i := 0; i < 20; i++ { jobCh <- worker.Job{Data: Job{suc: suc}} } + close(barrier) <-suc p.Cancel() p.Wait()