Skip to content

Commit de2736a

Browse files
committed
Remove newline characters from worker lifecycle log messages and add test for log format
1 parent 13ade30 commit de2736a

2 files changed

Lines changed: 32 additions & 2 deletions

File tree

worker.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ func (w *Worker) Start(ctx context.Context) {
5656
}
5757

5858
go func() {
59-
w.logger.Printf(w.name + " started\n")
60-
defer w.logger.Printf(w.name + " stopped\n")
59+
w.logger.Printf(w.name + " started")
60+
defer w.logger.Printf(w.name + " stopped")
6161
defer wg.Done()
6262
defer ticker.Stop()
6363

worker_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package phudong
33
import (
44
"context"
55
"errors"
6+
"reflect"
67
"sync"
78
"testing"
89
"time"
@@ -15,6 +16,35 @@ func TestNewWorker(t *testing.T) {
1516
}
1617
}
1718

19+
type lifecycleLogger struct {
20+
formats []string
21+
}
22+
23+
func (l *lifecycleLogger) Printf(format string, args ...any) {
24+
l.formats = append(l.formats, format)
25+
}
26+
27+
func (l *lifecycleLogger) Errorf(format string, args ...any) {}
28+
29+
func TestWorkerLifecycleLogFormats(t *testing.T) {
30+
logger := &lifecycleLogger{}
31+
worker := NewWorker(
32+
WithLogger(logger),
33+
WithDuration(time.Hour),
34+
)
35+
36+
ctx, cancel := context.WithCancel(context.Background())
37+
cancel()
38+
39+
worker.Start(ctx)
40+
worker.Wait()
41+
42+
expected := []string{"noName worker started", "noName worker stopped"}
43+
if !reflect.DeepEqual(logger.formats, expected) {
44+
t.Errorf("Expected lifecycle log formats %q, got %q", expected, logger.formats)
45+
}
46+
}
47+
1848
func TestWorkerWithOptions(t *testing.T) {
1949
var executed bool
2050
var mu sync.Mutex

0 commit comments

Comments
 (0)