1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
package logic
import (
"io"
"os"
"path/filepath"
"rss_parrot/shared"
"testing"
"time"
)
func TestProfilerPurgesOnlyItsOwnOldDumps(t *testing.T) {
dir := t.TempDir()
now := time.Date(2026, 10, 28, 12, 0, 0, 0, time.UTC)
old := now.AddDate(0, 0, -10)
write := func(name string, mtime time.Time) string {
t.Helper()
p := filepath.Join(dir, name)
if err := os.MkdirAll(filepath.Dir(p), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(p, []byte("x"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.Chtimes(p, mtime, mtime); err != nil {
t.Fatal(err)
}
return p
}
oldDump := write(old.Format(dumpTimeFormat)+".txt", old)
newDump := write(now.Add(-time.Hour).Format(dumpTimeFormat)+".txt", now.Add(-time.Hour))
// Anything else in the directory is left alone, however old.
others := []string{
write("kakapo.db", old),
write("notes.txt", old),
write("sub/"+old.Format(dumpTimeFormat)+".txt", old),
}
if err := purgeOld(dir, 7, now); err != nil {
t.Fatal(err)
}
if _, err := os.Stat(oldDump); !os.IsNotExist(err) {
t.Errorf("old dump %s was kept", oldDump)
}
for _, p := range append(others, newDump) {
if _, err := os.Stat(p); err != nil {
t.Errorf("%s was deleted", p)
}
}
}
func TestProfilerDumpNameMatchesWhatItWrites(t *testing.T) {
dir := t.TempDir()
now := time.Date(2026, 10, 28, 12, 0, 0, 0, time.UTC)
if err := saveProfile(dir, now); err != nil {
t.Fatal(err)
}
entries, err := os.ReadDir(dir)
if err != nil || len(entries) != 1 {
t.Fatalf("dir holds %v, %v; want one dump", entries, err)
}
if name := entries[0].Name(); !dumpName.MatchString(name) {
t.Errorf("dump %q does not match dumpName, so it would never be purged", name)
}
}
func TestProfilerOffWithoutProfileDir(t *testing.T) {
// A profiler that runs would start a goroutine writing a dump every
// minute for the rest of the test binary; only the unset case is run.
prof := NewProfiler(&shared.Config{}, shared.NewLogger(io.Discard, "Error")).(*profiler)
if prof.profileDir != "" {
t.Fatalf("profileDir = %q", prof.profileDir)
}
if prof.profileKeepDays != defaultProfileKeepDays {
t.Errorf("profileKeepDays = %d, want the default %d", prof.profileKeepDays, defaultProfileKeepDays)
}
if prof.running {
t.Error("the dump loop runs without profile_dir; it used to write into the working directory")
}
}
|