diff --git a/testutil/fake_sink.go b/testutil/fake_sink.go index 243292e361..a2e19b4faa 100644 --- a/testutil/fake_sink.go +++ b/testutil/fake_sink.go @@ -12,21 +12,33 @@ import ( // tests can assert on what was logged. It requires a testing.TB // as it is only meant for use in tests. type FakeSink struct { + t testing.TB mu sync.RWMutex entries []slog.SinkEntry + tDone bool } // NewFakeSink returns a FakeSink ready for use. -func NewFakeSink(_ testing.TB) *FakeSink { - return &FakeSink{} +func NewFakeSink(t testing.TB) *FakeSink { + fs := &FakeSink{t: t} + t.Cleanup(func() { + fs.mu.Lock() + fs.tDone = true + fs.mu.Unlock() + }) + return fs } // LogEntry implements slog.Sink. It appends the entry to the // internal slice. func (s *FakeSink) LogEntry(_ context.Context, e slog.SinkEntry) { s.mu.Lock() - defer s.mu.Unlock() s.entries = append(s.entries, e) + shouldLog := !s.tDone + s.mu.Unlock() + if shouldLog { + s.t.Log(e.Message, e.Fields) + } } // Sync implements slog.Sink.