Skip to content

Commit

Permalink
Allow resetting the finish notification of a runner
Browse files Browse the repository at this point in the history
  • Loading branch information
yahavi committed Dec 22, 2023
1 parent 6d742be commit 7f5373a
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
11 changes: 11 additions & 0 deletions parallel/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Runner interface {
SetMaxParallel(int)
GetFinishedNotification() chan bool
SetFinishedNotification(bool)
ResetFinishNotification()
}

type TaskFunc func(int) error
Expand Down Expand Up @@ -209,6 +210,16 @@ func (r *runner) SetFinishedNotification(toEnable bool) {
r.finishedNotificationEnabled = toEnable
}

// Recreates the finish notification channel.
// This method helps manage a scenario involving two runners: "1" assigns tasks to "2".
// Runner "2" might occasionally encounter periods without assigned tasks.
// As a result, the finish notification for "2" might be triggered.
// To tackle this issue, use the ResetFinishNotification after all tasks assigned to "1" have been completed.
func (r *runner) ResetFinishNotification() {
r.finishedNotifier = make(chan bool, 1)
r.finishedNotifierChannelClosed = false
}

func (r *runner) SetMaxParallel(newVal int) {
if newVal < 1 {
newVal = 1
Expand Down
46 changes: 46 additions & 0 deletions parallel/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,49 @@ func TestMaxParallel(t *testing.T) {
runner.Run()
assert.Equal(t, uint32(capacity), runner.started)
}

func TestResetFinishNotification(t *testing.T) {
// Create 2 runners
const capacity = 10
const parallelism = 3
runnerOne := NewRunner(parallelism, capacity, false)
runnerOne.SetFinishedNotification(true)
runnerTwo := NewRunner(parallelism, capacity, false)
runnerTwo.SetFinishedNotification(true)

// Add 10 tasks to runner one. Each task provides tasks to runner two.
for i := 0; i < capacity; i++ {
_, err := runnerOne.AddTask(func(int) error {
time.Sleep(time.Millisecond * 10)
_, err := runnerTwo.AddTask(func(int) error {
time.Sleep(time.Millisecond)
return nil
})
assert.NoError(t, err)
return nil
})
assert.NoError(t, err)
}

// Create a goroutine waiting for the finish notification of the first runner before running "Done".
go func() {
<-runnerOne.GetFinishedNotification()
runnerOne.Done()
}()

// Start running the second runner in a different goroutine to make it non-blocking.
go func() {
runnerTwo.Run()
}()

// Run the first runner. This is a blocking method.
runnerOne.Run()

// Reset runner two's finish notification to ensure we receive it only after all tasks assigned to runner two are completed.
runnerTwo.ResetFinishNotification()

// Receive the finish notification and ensure that we have truly completed the task.
<-runnerTwo.GetFinishedNotification()
assert.Zero(t, runnerTwo.ActiveThreads())
runnerTwo.Done()
}

0 comments on commit 7f5373a

Please sign in to comment.