Skip to content

Commit

Permalink
fix logic to wait for executor stop (#392)
Browse files Browse the repository at this point in the history
* fix logic to wait for executor stop

* fix test

Co-authored-by: hiroebe <hiroebe41@gmail.com>
  • Loading branch information
hiroebe and hiroebe authored Oct 27, 2022
1 parent e315112 commit 1db6f41
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
10 changes: 6 additions & 4 deletions executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ const (
type executor struct {
jobFunctions chan jobFunction
stopCh chan struct{}
stoppedCh chan struct{}
limitMode limitMode
maxRunningJobs *semaphore.Weighted
}

func newExecutor() executor {
return executor{
jobFunctions: make(chan jobFunction, 1),
stopCh: make(chan struct{}, 1),
stopCh: make(chan struct{}),
stoppedCh: make(chan struct{}),
}
}

Expand Down Expand Up @@ -113,13 +115,13 @@ func (e *executor) start() {
case <-e.stopCh:
cancel()
runningJobsWg.Wait()
e.stopCh <- struct{}{}
close(e.stoppedCh)
return
}
}
}

func (e *executor) stop() {
e.stopCh <- struct{}{}
<-e.stopCh
close(e.stopCh)
<-e.stoppedCh
}
6 changes: 3 additions & 3 deletions executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ func Test_ExecutorExecute(t *testing.T) {
runState: &runState,
}

e.stop()
wg.Wait()
e.stop()
}

func Test_ExecutorPanicHandling(t *testing.T) {
panicHandled := make(chan bool)
panicHandled := make(chan bool, 1)

handler := func(jobName string, recoverData interface{}) {
fmt.Println("PanicHandler called:")
Expand Down Expand Up @@ -60,8 +60,8 @@ func Test_ExecutorPanicHandling(t *testing.T) {
runState: &runState,
}

e.stop()
wg.Wait()
e.stop()

state := <-panicHandled
assert.Equal(t, state, true)
Expand Down

0 comments on commit 1db6f41

Please sign in to comment.