Skip to content

Commit

Permalink
maint: clean up testutils/TestLogger
Browse files Browse the repository at this point in the history
  • Loading branch information
acaloiaro committed Sep 7, 2023
1 parent bc8df98 commit 2ce0076
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ changelog:
- title: Features
regexp: '^.*?feat(\([[:word:]]+\))??!?:.+$'
order: 0
- title: Maintenance
regexp: '^.*?maint(\([[:word:]]+\))??!?:.+$'
order: 1
- title: "Bug fixes"
regexp: '^.*?fix(\([[:word:]]+\))??!?:.+$'
- title: Others
Expand Down
3 changes: 1 addition & 2 deletions backends/postgres/postgres_backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"log"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -324,7 +323,7 @@ func TestBasicJobProcessingWithErrors(t *testing.T) {
return
})

nq.SetLogger(testutils.TestLogger{L: log.New(testutils.ChanWriter{Ch: logsChan}, "", 0)})
nq.SetLogger(testutils.NewTestLogger(logsChan))

err = nq.Start(ctx, queue, h)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions backends/redis/redis_backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,7 @@ func TestJobProcessingWithOptions(t *testing.T) {
}
defer nq.Shutdown(ctx)

logger := testutils.TestLogger{L: log.New(&testutils.ChanWriter{Ch: logsChan}, "", 0)}
nq.SetLogger(logger)
nq.SetLogger(testutils.NewTestLogger(logsChan))

h := handler.New(func(_ context.Context) (err error) {
time.Sleep(50 * time.Millisecond)
Expand Down
3 changes: 1 addition & 2 deletions neoq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"log"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -206,7 +205,7 @@ func TestSetLogger(t *testing.T) {
}
defer nq.Shutdown(ctx)

nq.SetLogger(testutils.TestLogger{L: log.New(testutils.ChanWriter{Ch: logsChan}, "", 0)})
nq.SetLogger(testutils.NewTestLogger(logsChan))

h := handler.New(func(ctx context.Context) (err error) {
err = errTrigger
Expand Down
17 changes: 12 additions & 5 deletions testutils/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,38 @@ import (
"strings"
)

// NewTestLogger returns a new TestLogger that logs messages to the given channel
func NewTestLogger(ch chan string) *TestLogger {
return &TestLogger{
L: log.New(ChanWriter{ch: ch}, "", 0),
}
}

// TestLogger is a utility for logging in tests
type TestLogger struct {
L *log.Logger
}

// Info prints to stdout and signals its done channel
// Info prints to stdout, with args separated by spaces
func (h TestLogger) Info(m string, args ...any) {
h.L.Println(m, args)
}

// Debug prints to stdout and signals its done channel
// Debug prints to stdout, with args separates by spaces
func (h TestLogger) Debug(m string, args ...any) {
h.L.Println(m, args)
}

// Error prints to stdout and signals its done channel
// Error prints to stdout with args separated by spaces
func (h TestLogger) Error(m string, args ...any) {
h.L.Println(m, args)
}

type ChanWriter struct {
Ch chan string
ch chan string
}

func (c ChanWriter) Write(p []byte) (n int, err error) {
c.Ch <- strings.Trim(string(p), "\n")
c.ch <- strings.Trim(string(p), "\n")
return len(p), nil
}

0 comments on commit 2ce0076

Please sign in to comment.