Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions internal/jobcompleter/job_completer.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ func (c *InlineCompleter) JobSetStateIfRunning(ctx context.Context, stats *jobst
return err
}

// The driver intentionally returns 0 rows when a job is deleted while the
// completer is finalizing it (see UnknownJobIgnored shared driver test).
// Guard against an index-out-of-range panic in that case.
if len(jobs) == 0 {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just as a minor stylistic nit, can you change these to if len(jobs) < 1?

return nil
}

stats.CompleteDuration = c.Time.Now().Sub(start)
c.subscribeCh <- []CompleterJobUpdated{{
Job: jobs[0],
Expand Down Expand Up @@ -200,6 +207,13 @@ func (c *AsyncCompleter) JobSetStateIfRunning(ctx context.Context, stats *jobsta
return err
}

// The driver intentionally returns 0 rows when a job is deleted while the
// completer is finalizing it (see UnknownJobIgnored shared driver test).
// Guard against an index-out-of-range panic in that case.
if len(jobs) == 0 {
return nil
}

stats.CompleteDuration = c.Time.Now().Sub(start)
c.subscribeCh <- []CompleterJobUpdated{{
Job: jobs[0],
Expand Down
45 changes: 45 additions & 0 deletions internal/jobcompleter/job_completer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,28 @@ func TestInlineJobCompleter_Complete(t *testing.T) {
require.Equal(t, numRetries, attempt)
}

func TestInlineJobCompleter_CompleteDeletedJob(t *testing.T) {
t.Parallel()

ctx := context.Background()

// The driver intentionally returns 0 rows when a job is deleted while the
// completer is finalizing it (UnknownJobIgnored contract). Verify that the
// InlineCompleter does not panic in that case.
execMock := &partialExecutorMock{}
execMock.JobSetStateIfRunningManyFunc = func(ctx context.Context, params *riverdriver.JobSetStateIfRunningManyParams) ([]*rivertype.JobRow, error) {
return []*rivertype.JobRow{}, nil
}

subscribeCh := make(chan []CompleterJobUpdated, 10)
t.Cleanup(riverinternaltest.DiscardContinuously(subscribeCh))

completer := NewInlineCompleter(riversharedtest.BaseServiceArchetype(t), "", execMock, &riverpilot.StandardPilot{}, subscribeCh)
t.Cleanup(completer.Stop)

require.NoError(t, completer.JobSetStateIfRunning(ctx, &jobstats.JobStatistics{}, riverdriver.JobSetStateCompleted(1, time.Now(), nil)))
}

func TestInlineJobCompleter_Subscribe(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -228,6 +250,29 @@ func TestAsyncJobCompleter_Complete(t *testing.T) {
resultCh <- nil
}

func TestAsyncJobCompleter_CompleteDeletedJob(t *testing.T) {
t.Parallel()

ctx := context.Background()

// The driver intentionally returns 0 rows when a job is deleted while the
// completer is finalizing it (UnknownJobIgnored contract). Verify that the
// AsyncCompleter does not panic in that case.
execMock := &partialExecutorMock{}
execMock.JobSetStateIfRunningManyFunc = func(ctx context.Context, params *riverdriver.JobSetStateIfRunningManyParams) ([]*rivertype.JobRow, error) {
return []*rivertype.JobRow{}, nil
}

subscribeCh := make(chan []CompleterJobUpdated, 10)
t.Cleanup(riverinternaltest.DiscardContinuously(subscribeCh))

completer := newAsyncCompleterWithConcurrency(riversharedtest.BaseServiceArchetype(t), "", execMock, &riverpilot.StandardPilot{}, 2, subscribeCh)
require.NoError(t, completer.Start(ctx))
t.Cleanup(completer.Stop)

require.NoError(t, completer.JobSetStateIfRunning(ctx, &jobstats.JobStatistics{}, riverdriver.JobSetStateCompleted(1, time.Now(), nil)))
}

func TestAsyncJobCompleter_Subscribe(t *testing.T) {
t.Parallel()

Expand Down
Loading