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
|
package autotrack
import (
"os"
"strings"
"syscall"
"testing"
"time"
"ticktock/internal/config"
)
func TestRunFlushesOpenSegmentOnSignal(t *testing.T) {
dir := t.TempDir()
sigc := make(chan os.Signal, 1)
sample := func() Sample {
return Sample{Idle: 0, Raw: &Ctx{App: "TestApp", Title: "doc"}}
}
done := make(chan struct{})
go func() {
Run(dir, config.Tracking{PollSecs: 1, IdleGraceSecs: 900, MinSecs: 0}, sample, sigc)
close(done)
}()
time.Sleep(50 * time.Millisecond) // let the first poll open a segment
sigc <- syscall.SIGTERM
select {
case <-done:
case <-time.After(2 * time.Second):
t.Fatal("Run did not return after signal")
}
b, err := os.ReadFile(LogPath(dir, time.Now()))
if err != nil {
t.Fatalf("flush should have written today's log: %v", err)
}
line := strings.TrimSpace(string(b))
if !strings.Contains(line, `"app": "TestApp"`) || !strings.Contains(line, `"idle": false`) {
t.Errorf("flushed segment malformed: %s", line)
}
}
|