Skip to content

Commit

Permalink
Add test coverage for the generated logs.
Browse files Browse the repository at this point in the history
  • Loading branch information
bojanz committed Jun 4, 2024
1 parent 628c035 commit d267e91
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions nanoq_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package nanoq_test

import (
"bytes"
"context"
"errors"
"fmt"
Expand Down Expand Up @@ -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++
Expand Down Expand Up @@ -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")
})
Expand Down Expand Up @@ -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)
})
Expand Down Expand Up @@ -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"))
})
Expand Down Expand Up @@ -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") {
Expand Down Expand Up @@ -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"))
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
},
}))
}

0 comments on commit d267e91

Please sign in to comment.