diff options
| author | Paul Buetow <paul@buetow.org> | 2026-04-14 10:33:13 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-04-14 10:33:13 +0300 |
| commit | 79384e6439c2faf26d68d286a3e814da39e127eb (patch) | |
| tree | 45620c3c125052f2e9790e3abbeac1c3614de779 /internal | |
| parent | a13819bd1e02fddc502d59fc4c8a70157ea23880 (diff) | |
test(daemon): race-safe log buffers in Run tests (j3)
Use a mutex-wrapped buffer for slog and io.Copy so polling String()
does not race concurrent writes from Run and background goroutines.
Made-with: Cursor
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/daemon/daemon_test.go | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/internal/daemon/daemon_test.go b/internal/daemon/daemon_test.go index 143ec3a..60c6afa 100644 --- a/internal/daemon/daemon_test.go +++ b/internal/daemon/daemon_test.go @@ -10,10 +10,28 @@ import ( "os" "path/filepath" "strings" + "sync" "testing" "time" ) +type syncBuffer struct { + mu sync.Mutex + b bytes.Buffer +} + +func (s *syncBuffer) Write(p []byte) (int, error) { + s.mu.Lock() + defer s.mu.Unlock() + return s.b.Write(p) +} + +func (s *syncBuffer) String() string { + s.mu.Lock() + defer s.mu.Unlock() + return s.b.String() +} + func TestHealth(t *testing.T) { srv := httptest.NewServer(Handler(t.TempDir())) defer srv.Close() @@ -299,7 +317,7 @@ func TestRunEmptyAddr(t *testing.T) { } func TestRunWritesDaemonListenToLogOutput(t *testing.T) { - var buf bytes.Buffer + var buf syncBuffer ctx, cancel := context.WithCancel(context.Background()) cfg := Config{StatsDir: t.TempDir(), Addr: "127.0.0.1:0", LogOutput: &buf} done := make(chan struct{}) @@ -326,7 +344,7 @@ func TestRunUsesStdoutWhenLogOutputNil(t *testing.T) { t.Fatal(err) } os.Stdout = pw - var logBuf bytes.Buffer + var logBuf syncBuffer copyDone := make(chan struct{}) go func() { _, _ = io.Copy(&logBuf, pr) |
