forked from TrueCloudLab/restic
Merge pull request #516 from restic/fix-flaky-test
Fix flaky worker cancel test
This commit is contained in:
commit
6bc7a71e55
1 changed files with 20 additions and 17 deletions
|
@ -94,29 +94,32 @@ var errCancelled = errors.New("cancelled")
|
||||||
|
|
||||||
type Job struct {
|
type Job struct {
|
||||||
suc chan 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) {
|
func TestPoolCancel(t *testing.T) {
|
||||||
jobCh, resCh, p := newBufferedPool(20, concurrency, wait)
|
barrier := make(chan struct{})
|
||||||
|
|
||||||
suc := make(chan struct{}, 1)
|
wait := func(job worker.Job, done <-chan struct{}) (interface{}, error) {
|
||||||
for i := 0; i < 20; i++ {
|
j := job.Data.(Job)
|
||||||
jobCh <- worker.Job{Data: Job{suc: suc, d: time.Second}}
|
|
||||||
|
<-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{})
|
||||||
|
for i := 0; i < 20; i++ {
|
||||||
|
jobCh <- worker.Job{Data: Job{suc: suc}}
|
||||||
|
}
|
||||||
|
|
||||||
|
close(barrier)
|
||||||
<-suc
|
<-suc
|
||||||
p.Cancel()
|
p.Cancel()
|
||||||
p.Wait()
|
p.Wait()
|
||||||
|
|
Loading…
Add table
Reference in a new issue