Skip to content

Commit

Permalink
Merge pull request #516 from restic/fix-flaky-test
Browse files Browse the repository at this point in the history
Fix flaky worker cancel test
  • Loading branch information
fd0 committed May 9, 2016
2 parents 2c1e590 + fb45ea1 commit 6bc7a71
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/restic/worker/pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,29 +94,32 @@ var errCancelled = errors.New("cancelled")

type Job struct {
suc chan struct{}
d time.Duration
}

func wait(job worker.Job, done <-chan struct{}) (interface{}, error) {
j := job.Data.(Job)
select {
case j.suc <- struct{}{}:
return time.Now(), nil
case <-time.After(j.d):
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
}
}
}

func TestPoolCancel(t *testing.T) {
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, d: time.Second}}
jobCh <- worker.Job{Data: Job{suc: suc}}
}

close(barrier)
<-suc
p.Cancel()
p.Wait()
Expand Down

0 comments on commit 6bc7a71

Please sign in to comment.