package logfile import ( "bytes" "fmt" "os" "path/filepath" "strings" "testing" ) func TestRotatesAtSizeCap(t *testing.T) { dir := t.TempDir() path := filepath.Join(dir, "stream-nre.log") w, err := Open(path, 1024, 3) if err != nil { t.Fatal(err) } defer w.Close() line := strings.Repeat("x", 100) + "\n" for i := 0; i < 50; i++ { if _, err := w.Write([]byte(line)); err != nil { t.Fatal(err) } } // The live file must stay near the cap, not grow to 5 KiB. st, err := os.Stat(path) if err != nil { t.Fatal(err) } if st.Size() > 1024+int64(len(line)) { t.Errorf("live log is %d bytes, cap is 1024", st.Size()) } // Generations exist, and no more than were asked for. if _, err := os.Stat(path + ".1"); err != nil { t.Errorf("expected a rotated generation: %v", err) } if _, err := os.Stat(path + ".3"); err == nil { t.Error("generation 3 should not exist with generations=3 (0,1,2)") } } func TestGenerationCountRespected(t *testing.T) { dir := t.TempDir() path := filepath.Join(dir, "x.log") w, err := Open(path, 200, 2) if err != nil { t.Fatal(err) } defer w.Close() for i := 0; i < 40; i++ { _, _ = w.Write([]byte(strings.Repeat("y", 50) + "\n")) } ents, err := os.ReadDir(dir) if err != nil { t.Fatal(err) } // generations=2 means x.log and x.log.1 only. if len(ents) > 2 { var names []string for _, e := range ents { names = append(names, e.Name()) } t.Errorf("want at most 2 files, got %v", names) } } // Reopening must append, not truncate: a restart has to leave the previous // run's output in place, which is the whole point of this package. func TestReopenAppendsAndBannerMarksRuns(t *testing.T) { dir := t.TempDir() path := filepath.Join(dir, "run.log") w1, err := OpenWithBanner(path, 1<<20, 3, "nre") if err != nil { t.Fatal(err) } _, _ = w1.Write([]byte("first run output\n")) _ = w1.Close() w2, err := OpenWithBanner(path, 1<<20, 3, "nre") if err != nil { t.Fatal(err) } _, _ = w2.Write([]byte("second run output\n")) _ = w2.Close() b, err := os.ReadFile(path) if err != nil { t.Fatal(err) } body := string(b) if !strings.Contains(body, "first run output") { t.Error("the previous run's output was lost — this is the O_TRUNC bug") } if !strings.Contains(body, "second run output") { t.Error("the new run's output is missing") } if n := strings.Count(body, "==== nre start "); n != 2 { t.Errorf("want 2 start banners, got %d", n) } } func TestTail(t *testing.T) { dir := t.TempDir() path := filepath.Join(dir, "t.log") var buf bytes.Buffer for i := 0; i < 500; i++ { fmt.Fprintf(&buf, "line %d\n", i) } if err := os.WriteFile(path, buf.Bytes(), 0o644); err != nil { t.Fatal(err) } out, err := Tail(path, 10) if err != nil { t.Fatal(err) } lines := strings.Split(strings.TrimRight(string(out), "\n"), "\n") if len(lines) != 10 { t.Fatalf("want 10 lines, got %d: %q", len(lines), string(out)) } if lines[9] != "line 499" { t.Errorf("last line = %q, want %q", lines[9], "line 499") } if lines[0] != "line 490" { t.Errorf("first line = %q, want %q", lines[0], "line 490") } // Asking for more lines than exist returns the whole file. out, err = Tail(path, 10000) if err != nil { t.Fatal(err) } if !bytes.Equal(out, buf.Bytes()) { t.Error("tail of a short file should return all of it") } } func TestTailEmptyAndMissing(t *testing.T) { dir := t.TempDir() empty := filepath.Join(dir, "empty.log") if err := os.WriteFile(empty, nil, 0o644); err != nil { t.Fatal(err) } if out, err := Tail(empty, 10); err != nil || len(out) != 0 { t.Errorf("empty file: got %q %v", out, err) } if _, err := Tail(filepath.Join(dir, "nope.log"), 10); err == nil { t.Error("a missing file should error") } } func TestWriteAfterCloseDoesNotPanic(t *testing.T) { path := filepath.Join(t.TempDir(), "c.log") w, err := Open(path, 1<<20, 2) if err != nil { t.Fatal(err) } _ = w.Close() if _, err := w.Write([]byte("x")); err == nil { t.Error("writing to a closed writer should error, not panic") } // Double close is harmless. if err := w.Close(); err != nil { t.Errorf("second Close should be a no-op, got %v", err) } } func TestConcurrentWrites(t *testing.T) { path := filepath.Join(t.TempDir(), "p.log") // An exec.Cmd gets this same writer as both Stdout and Stderr, so // concurrent writes are the normal case, not an edge case. w, err := Open(path, 4096, 3) if err != nil { t.Fatal(err) } defer w.Close() done := make(chan struct{}) for g := 0; g < 4; g++ { go func(g int) { defer func() { done <- struct{}{} }() for i := 0; i < 100; i++ { _, _ = w.Write([]byte(fmt.Sprintf("writer %d line %d\n", g, i))) } }(g) } for i := 0; i < 4; i++ { <-done } }