Skip to content

Commit

Permalink
chain/SkipIfStillRunning: fix bug handling different jobs
Browse files Browse the repository at this point in the history
It was an error in channel scoping that was identified in pull #263.

This adds a unit test to identify that issue and verify the fix.
  • Loading branch information
Rob Figueiredo committed Jan 4, 2020
1 parent 3d516cc commit ccba498
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
4 changes: 2 additions & 2 deletions chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ func DelayIfStillRunning(logger Logger) JobWrapper {
// SkipIfStillRunning skips an invocation of the Job if a previous invocation is
// still running. It logs skips to the given logger at Info level.
func SkipIfStillRunning(logger Logger) JobWrapper {
var ch = make(chan struct{}, 1)
ch <- struct{}{}
return func(j Job) Job {
var ch = make(chan struct{}, 1)
ch <- struct{}{}
return FuncJob(func() {
select {
case v := <-ch:
Expand Down
21 changes: 21 additions & 0 deletions chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,4 +218,25 @@ func TestChainSkipIfStillRunning(t *testing.T) {
}
})

t.Run("different jobs independent", func(t *testing.T) {
var j1, j2 countJob
j1.delay = 10 * time.Millisecond
j2.delay = 10 * time.Millisecond
chain := NewChain(SkipIfStillRunning(DiscardLogger))
wrappedJob1 := chain.Then(&j1)
wrappedJob2 := chain.Then(&j2)
for i := 0; i < 11; i++ {
go wrappedJob1.Run()
go wrappedJob2.Run()
}
time.Sleep(100 * time.Millisecond)
var (
done1 = j1.Done()
done2 = j2.Done()
)
if done1 != 1 || done2 != 1 {
t.Error("expected both jobs executed once, got", done1, "and", done2)
}
})

}

0 comments on commit ccba498

Please sign in to comment.