Skip to content

Commit

Permalink
testfactory: automatically set FinalizedAt if finalized state (#585)
Browse files Browse the repository at this point in the history
If the job being created is in a finalized state, automatically set the
`FinalizedAt` timestamp so that callers don't always have to do so.
  • Loading branch information
bgentry committed Sep 9, 2024
1 parent 8669cbc commit f147584
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
6 changes: 4 additions & 2 deletions internal/riverinternaltest/riverdrivertest/riverdrivertest.go
Original file line number Diff line number Diff line change
Expand Up @@ -1091,9 +1091,11 @@ func Exercise[TTx any](ctx context.Context, t *testing.T,
exec, _ := setup(ctx, t)
// Create a job with the target state but without a finalized_at,
// expect an error:
_, err := exec.JobInsertFull(ctx, testfactory.Job_Build(t, &testfactory.JobOpts{
params := testfactory.Job_Build(t, &testfactory.JobOpts{
State: &state,
}))
})
params.FinalizedAt = nil
_, err := exec.JobInsertFull(ctx, params)
require.ErrorContains(t, err, "violates check constraint \"finalized_or_finalized_at_null\"")
})

Expand Down
9 changes: 8 additions & 1 deletion internal/riverinternaltest/testfactory/test_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ func Job_Build(tb testing.TB, opts *JobOpts) *riverdriver.JobInsertFullParams {
encodedArgs = []byte("{}")
}

finalizedAt := opts.FinalizedAt
if finalizedAt == nil && (opts.State != nil && (*opts.State == rivertype.JobStateCompleted ||
*opts.State == rivertype.JobStateCancelled ||
*opts.State == rivertype.JobStateDiscarded)) {
finalizedAt = ptrutil.Ptr(time.Now())
}

metadata := opts.Metadata
if opts.Metadata == nil {
metadata = []byte("{}")
Expand All @@ -68,7 +75,7 @@ func Job_Build(tb testing.TB, opts *JobOpts) *riverdriver.JobInsertFullParams {
CreatedAt: opts.CreatedAt,
EncodedArgs: encodedArgs,
Errors: opts.Errors,
FinalizedAt: opts.FinalizedAt,
FinalizedAt: finalizedAt,
Kind: ptrutil.ValOrDefault(opts.Kind, "fake_job"),
MaxAttempts: ptrutil.ValOrDefault(opts.MaxAttempts, rivercommon.MaxAttemptsDefault),
Metadata: metadata,
Expand Down
2 changes: 1 addition & 1 deletion riverdriver/river_driver_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,9 @@ type Notification struct {
}

type JobCancelParams struct {
ID int64
CancelAttemptedAt time.Time
ControlTopic string
ID int64
}

type JobDeleteBeforeParams struct {
Expand Down

0 comments on commit f147584

Please sign in to comment.