From 00df54e9a93f33cd7e14a6e772887a31fde2f435 Mon Sep 17 00:00:00 2001 From: Blake Gentry Date: Sun, 8 Sep 2024 20:53:34 -0500 Subject: [PATCH] testfactory: automatically set FinalizedAt if finalized state 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. --- .../riverinternaltest/riverdrivertest/riverdrivertest.go | 6 ++++-- internal/riverinternaltest/testfactory/test_factory.go | 9 ++++++++- riverdriver/river_driver_interface.go | 2 +- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/internal/riverinternaltest/riverdrivertest/riverdrivertest.go b/internal/riverinternaltest/riverdrivertest/riverdrivertest.go index 4e73fc9e..752d2e46 100644 --- a/internal/riverinternaltest/riverdrivertest/riverdrivertest.go +++ b/internal/riverinternaltest/riverdrivertest/riverdrivertest.go @@ -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\"") }) diff --git a/internal/riverinternaltest/testfactory/test_factory.go b/internal/riverinternaltest/testfactory/test_factory.go index be73ece1..2ab5fb78 100644 --- a/internal/riverinternaltest/testfactory/test_factory.go +++ b/internal/riverinternaltest/testfactory/test_factory.go @@ -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("{}") @@ -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, diff --git a/riverdriver/river_driver_interface.go b/riverdriver/river_driver_interface.go index 4c079e7b..d47b3eee 100644 --- a/riverdriver/river_driver_interface.go +++ b/riverdriver/river_driver_interface.go @@ -211,9 +211,9 @@ type Notification struct { } type JobCancelParams struct { - ID int64 CancelAttemptedAt time.Time ControlTopic string + ID int64 } type JobDeleteBeforeParams struct {