Browse Source

chain/SkipIfStillRunning: fix bug handling different jobs

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.
Rob Figueiredo 6 years ago
parent
commit
ccba498c39
2 changed files with 23 additions and 2 deletions
  1. 2 2
      chain.go
  2. 21 0
      chain_test.go

+ 2 - 2
chain.go

@@ -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:

+ 21 - 0
chain_test.go

@@ -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)
+		}
+	})
+
 }