mirror of
https://github.com/octoleo/restic.git
synced 2024-11-10 15:21:03 +00:00
Add barrier
This commit is contained in:
parent
c6d934a685
commit
fb45ea139d
@ -96,24 +96,30 @@ type Job struct {
|
|||||||
suc chan 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) {
|
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)
|
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()
|
||||||
|
Loading…
Reference in New Issue
Block a user