maint: clean up testutils/TestLogger

This commit is contained in:
Adriano Caloiaro 2023-09-05 10:41:16 +02:00 committed by Adriano Caloiaro
parent bc8df98894
commit 2ce00761c7
5 changed files with 18 additions and 11 deletions

View file

@ -35,6 +35,9 @@ changelog:
- title: Features - title: Features
regexp: '^.*?feat(\([[:word:]]+\))??!?:.+$' regexp: '^.*?feat(\([[:word:]]+\))??!?:.+$'
order: 0 order: 0
- title: Maintenance
regexp: '^.*?maint(\([[:word:]]+\))??!?:.+$'
order: 1
- title: "Bug fixes" - title: "Bug fixes"
regexp: '^.*?fix(\([[:word:]]+\))??!?:.+$' regexp: '^.*?fix(\([[:word:]]+\))??!?:.+$'
- title: Others - title: Others

View file

@ -4,7 +4,6 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"log"
"os" "os"
"strings" "strings"
"testing" "testing"
@ -324,7 +323,7 @@ func TestBasicJobProcessingWithErrors(t *testing.T) {
return return
}) })
nq.SetLogger(testutils.TestLogger{L: log.New(testutils.ChanWriter{Ch: logsChan}, "", 0)}) nq.SetLogger(testutils.NewTestLogger(logsChan))
err = nq.Start(ctx, queue, h) err = nq.Start(ctx, queue, h)
if err != nil { if err != nil {

View file

@ -266,8 +266,7 @@ func TestJobProcessingWithOptions(t *testing.T) {
} }
defer nq.Shutdown(ctx) defer nq.Shutdown(ctx)
logger := testutils.TestLogger{L: log.New(&testutils.ChanWriter{Ch: logsChan}, "", 0)} nq.SetLogger(testutils.NewTestLogger(logsChan))
nq.SetLogger(logger)
h := handler.New(func(_ context.Context) (err error) { h := handler.New(func(_ context.Context) (err error) {
time.Sleep(50 * time.Millisecond) time.Sleep(50 * time.Millisecond)

View file

@ -4,7 +4,6 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"log"
"os" "os"
"strings" "strings"
"testing" "testing"
@ -206,7 +205,7 @@ func TestSetLogger(t *testing.T) {
} }
defer nq.Shutdown(ctx) 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) { h := handler.New(func(ctx context.Context) (err error) {
err = errTrigger err = errTrigger

View file

@ -7,31 +7,38 @@ import (
"strings" "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 // TestLogger is a utility for logging in tests
type TestLogger struct { type TestLogger struct {
L *log.Logger 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) { func (h TestLogger) Info(m string, args ...any) {
h.L.Println(m, args) 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) { func (h TestLogger) Debug(m string, args ...any) {
h.L.Println(m, args) 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) { func (h TestLogger) Error(m string, args ...any) {
h.L.Println(m, args) h.L.Println(m, args)
} }
type ChanWriter struct { type ChanWriter struct {
Ch chan string ch chan string
} }
func (c ChanWriter) Write(p []byte) (n int, err error) { 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 return len(p), nil
} }