Merge pull request #516 from restic/fix-flaky-test

Fix flaky worker cancel test
This commit is contained in:
Alexander Neumann 2016-05-09 22:12:39 +02:00
commit 6bc7a71e55

View file

@ -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) {
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 <-time.After(j.d):
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)
for i := 0; i < 20; i++ {
jobCh <- worker.Job{Data: Job{suc: suc, d: time.Second}}
}
jobCh, resCh, p := newBufferedPool(20, concurrency, wait)
suc := make(chan struct{})
for i := 0; i < 20; i++ {
jobCh <- worker.Job{Data: Job{suc: suc}}
}
close(barrier)
<-suc
p.Cancel()
p.Wait()