diff --git a/nanoq_test.go b/nanoq_test.go index 4c34af7..2860b77 100644 --- a/nanoq_test.go +++ b/nanoq_test.go @@ -1,6 +1,7 @@ package nanoq_test import ( + "bytes" "context" "errors" "fmt" @@ -152,7 +153,7 @@ func TestProcessor_Run(t *testing.T) { db, mock, _ := sqlmock.New() defer db.Close() client := nanoq.NewClient(sqlx.NewDb(db, "sqlmock")) - processor := nanoq.NewProcessor(client, slog.New(slog.NewTextHandler(io.Discard, nil))) + processor := nanoq.NewProcessor(client, newLogger(io.Discard)) retryPolicyCalled := 0 processor.RetryPolicy(func(t nanoq.Task) time.Duration { retryPolicyCalled++ @@ -225,7 +226,7 @@ func TestProcessor_Run_RetriesExhausted(t *testing.T) { db, mock, _ := sqlmock.New() defer db.Close() client := nanoq.NewClient(sqlx.NewDb(db, "sqlmock")) - processor := nanoq.NewProcessor(client, slog.New(slog.NewTextHandler(io.Discard, nil))) + processor := nanoq.NewProcessor(client, newLogger(io.Discard)) processor.Handle("my-type", func(ctx context.Context, task nanoq.Task) error { return errors.New("temporary error") }) @@ -285,7 +286,7 @@ func TestProcessor_Run_SkipRetry(t *testing.T) { db, mock, _ := sqlmock.New() defer db.Close() client := nanoq.NewClient(sqlx.NewDb(db, "sqlmock")) - processor := nanoq.NewProcessor(client, slog.New(slog.NewTextHandler(io.Discard, nil))) + processor := nanoq.NewProcessor(client, newLogger(io.Discard)) processor.Handle("my-type", func(ctx context.Context, task nanoq.Task) error { return fmt.Errorf("something terrible happened: %w", nanoq.ErrSkipRetry) }) @@ -333,7 +334,7 @@ func TestProcessor_Run_Panic(t *testing.T) { db, mock, _ := sqlmock.New() defer db.Close() client := nanoq.NewClient(sqlx.NewDb(db, "sqlmock")) - processor := nanoq.NewProcessor(client, slog.New(slog.NewTextHandler(io.Discard, nil))) + processor := nanoq.NewProcessor(client, newLogger(io.Discard)) processor.Handle("my-type", func(ctx context.Context, task nanoq.Task) error { panic(errors.New("oh no")) }) @@ -381,7 +382,7 @@ func TestProcessor_Run_NoHandler(t *testing.T) { db, mock, _ := sqlmock.New() defer db.Close() client := nanoq.NewClient(sqlx.NewDb(db, "sqlmock")) - processor := nanoq.NewProcessor(client, slog.New(slog.NewTextHandler(io.Discard, nil))) + processor := nanoq.NewProcessor(client, newLogger(io.Discard)) errorHandlerCalled := 0 processor.OnError(func(ctx context.Context, task nanoq.Task, err error) { if !errors.Is(err, nanoq.ErrSkipRetry) || !strings.Contains(err.Error(), "no handler found for task type my-type") { @@ -429,7 +430,7 @@ func TestProcessor_Run_Middleware(t *testing.T) { db, mock, _ := sqlmock.New() defer db.Close() client := nanoq.NewClient(sqlx.NewDb(db, "sqlmock")) - processor := nanoq.NewProcessor(client, slog.New(slog.NewTextHandler(io.Discard, nil))) + processor := nanoq.NewProcessor(client, newLogger(io.Discard)) processor.Use(func(next nanoq.Handler) nanoq.Handler { return func(ctx context.Context, t nanoq.Task) error { middlewareValue := ctx.Value(contextKey("middleware")) @@ -523,7 +524,8 @@ func TestProcessor_Run_Cancel(t *testing.T) { db, mock, _ := sqlmock.New() defer db.Close() client := nanoq.NewClient(sqlx.NewDb(db, "sqlmock")) - processor := nanoq.NewProcessor(client, slog.New(slog.NewTextHandler(io.Discard, nil))) + logOutput := &bytes.Buffer{} + processor := nanoq.NewProcessor(client, newLogger(logOutput)) processor.Handle("my-type", func(ctx context.Context, task nanoq.Task) error { for { select { @@ -561,13 +563,23 @@ func TestProcessor_Run_Cancel(t *testing.T) { if err != nil { t.Error(err) } + + wantLog := strings.Join([]string{ + `level=INFO msg="Starting processor" concurrency=1`, + `level=INFO msg="Shutting down processor" timeout=1ms`, + `level=ERROR msg="Could not process task" error="task 01HQJHTZCAT5WDCGVTWJ640VMM canceled: shutdown timeout reached"`, + }, "\n") + gotLog := strings.TrimSpace(logOutput.String()) + if wantLog != gotLog { + t.Errorf("log output does not match. \nExpected:\n%v \nActual:\n%v", wantLog, gotLog) + } } func TestProcessor_Run_Timeout(t *testing.T) { db, mock, _ := sqlmock.New() defer db.Close() client := nanoq.NewClient(sqlx.NewDb(db, "sqlmock")) - processor := nanoq.NewProcessor(client, slog.New(slog.NewTextHandler(io.Discard, nil))) + processor := nanoq.NewProcessor(client, newLogger(io.Discard)) processor.Handle("my-type", func(ctx context.Context, task nanoq.Task) error { for { select { @@ -617,3 +629,14 @@ func TestProcessor_Run_Timeout(t *testing.T) { t.Errorf("erorr handler called %v times instead of %v", errorHandlerCalled, 1) } } + +func newLogger(w io.Writer) *slog.Logger { + return slog.New(slog.NewTextHandler(w, &slog.HandlerOptions{ + ReplaceAttr: func(_ []string, a slog.Attr) slog.Attr { + if a.Key == slog.TimeKey { + return slog.Attr{} + } + return a + }, + })) +}