Skip to content

Commit

Permalink
fix potential block in requestJobCtx when scheduler is shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnRoesler committed Oct 30, 2023
1 parent cc2d713 commit 207e863
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 28 deletions.
85 changes: 58 additions & 27 deletions scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,32 +310,63 @@ func TestScheduler_StopTimeout(t *testing.T) {
}
}

func TestScheduler_Start_Stop_Start_Shutdown(t *testing.T) {
func TestScheduler_Shutdown(t *testing.T) {
goleak.VerifyNone(t)
s, err := NewScheduler(
WithStopTimeout(time.Second),
)
require.NoError(t, err)
_, err = s.NewJob(
DurationJob(
5*time.Millisecond,
),
NewTask(
func() {},
),
WithStartAt(
WithStartImmediately(),
),
)
require.NoError(t, err)

s.Start()
time.Sleep(5 * time.Millisecond)
require.NoError(t, s.StopJobs())

time.Sleep(50 * time.Millisecond)
s.Start()

time.Sleep(5 * time.Millisecond)
require.NoError(t, s.Shutdown())

t.Run("start, stop, start, shutdown", func(t *testing.T) {
s, err := NewScheduler(
WithStopTimeout(time.Second),
)
require.NoError(t, err)
_, err = s.NewJob(
DurationJob(
5*time.Millisecond,
),
NewTask(
func() {},
),
WithStartAt(
WithStartImmediately(),
),
)
require.NoError(t, err)

s.Start()
time.Sleep(5 * time.Millisecond)
require.NoError(t, s.StopJobs())

time.Sleep(50 * time.Millisecond)
s.Start()

time.Sleep(5 * time.Millisecond)
require.NoError(t, s.Shutdown())
})

t.Run("calling Job methods after shutdown errors", func(t *testing.T) {
s, err := NewScheduler(
WithStopTimeout(time.Second),
)
require.NoError(t, err)
j, err := s.NewJob(
DurationJob(
5*time.Millisecond,
),
NewTask(
func() {},
),
WithStartAt(
WithStartImmediately(),
),
)
require.NoError(t, err)

s.Start()
require.NoError(t, s.Shutdown())

_, err = j.LastRun()
assert.ErrorIs(t, err, ErrJobNotFound)

_, err = j.NextRun()
assert.ErrorIs(t, err, ErrJobNotFound)
})
}
8 changes: 7 additions & 1 deletion util.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,15 @@ func requestJobCtx(ctx context.Context, id uuid.UUID, ch chan jobOutRequest) *in
return nil
default:
}
ch <- jobOutRequest{

select {

Check failure on line 52 in util.go

View workflow job for this annotation

GitHub Actions / lint and test (1.21)

unnecessary leading newline (whitespace)

Check failure on line 52 in util.go

View workflow job for this annotation

GitHub Actions / lint and test (1.20)

unnecessary leading newline (whitespace)

case ch <- jobOutRequest{
id: id,
outChan: resp,
}:
default:
return nil
}
var j internalJob
select {
Expand Down

0 comments on commit 207e863

Please sign in to comment.