diff --git a/CHANGELOG.md b/CHANGELOG.md index 209612fc..d560548e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - The River CLI now accepts a `--target-version` of 0 with `river migrate-down` to run all down migrations and remove all River tables (previously, -1 was used for this; -1 still works, but now 0 also works). [PR #966](https://github.com/riverqueue/river/pull/966). - **Breaking change:** The `HookWorkEnd` interface's `WorkEnd` function now receives a `JobRow` parameter in addition to the `error` it received before. Having a `JobRow` to work with is fairly crucial to most functionality that a hook would implement, and its previous omission was entirely an error. [PR #970](https://github.com/riverqueue/river/pull/970). - Add maximum bound to each job's `attempted_by` array so that in degenerate cases where a job is run many, many times (say it's snoozed hundreds of times), it doesn't grow to unlimited bounds. [PR #974](https://github.com/riverqueue/river/pull/974). +- A logger passed in via `river.Config` now overrides the default test-based logger when using `rivertest.NewWorker`. [PR #980](https://github.com/riverqueue/river/pull/980). ### Fixed diff --git a/rivertest/worker.go b/rivertest/worker.go index fc377987..c0b9a652 100644 --- a/rivertest/worker.go +++ b/rivertest/worker.go @@ -139,6 +139,9 @@ func (w *Worker[T, TTx]) workJob(ctx context.Context, tb testing.TB, tx TTx, job exec := w.client.Driver().UnwrapExecutor(tx) subscribeCh := make(chan []jobcompleter.CompleterJobUpdated, 1) archetype := riversharedtest.BaseServiceArchetype(tb) + if w.config.Logger != nil { + archetype.Logger = w.config.Logger + } if withStub, ok := timeGen.(baseservice.TimeGeneratorWithStub); ok { archetype.Time = withStub } else { diff --git a/rivertest/worker_test.go b/rivertest/worker_test.go index 61ced076..f29db81f 100644 --- a/rivertest/worker_test.go +++ b/rivertest/worker_test.go @@ -1,8 +1,10 @@ package rivertest import ( + "bytes" "context" "errors" + "log/slog" "testing" "time" @@ -250,6 +252,29 @@ func TestWorker_Work(t *testing.T) { require.Equal(t, river.EventKindJobCancelled, res.EventKind) }) + t.Run("UsesACustomLoggerWhenProvided", func(t *testing.T) { + t.Parallel() + + bundle := setup(t) + + var logBuf bytes.Buffer + bundle.config.Logger = slog.New(slog.NewTextHandler(&logBuf, &slog.HandlerOptions{ + Level: slog.LevelInfo, + })) + + // Return an error here because normal execution doesn't produce any + // logging that we can use to match on. + expectedErr := errors.New("my error that will be logged") + worker := river.WorkFunc(func(ctx context.Context, job *river.Job[testArgs]) error { + return expectedErr + }) + tw := NewWorker(t, bundle.driver, bundle.config, worker) + _, err := tw.Work(ctx, t, bundle.tx, testArgs{Value: "test"}, nil) + require.EqualError(t, err, expectedErr.Error()) + + require.Contains(t, logBuf.String(), expectedErr.Error()) + }) + t.Run("UsesACustomClockWhenProvided", func(t *testing.T) { t.Parallel()