-
-
Notifications
You must be signed in to change notification settings - Fork 671
/
Copy pathtracing_test.go
52 lines (40 loc) · 1.37 KB
/
tracing_test.go
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
package tracing
import (
"os"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestTracer_Start(t *testing.T) {
t.Parallel()
tracer := NewTracer(t.TempDir() + "/tracing.txt")
currentTime, err := time.Parse(time.DateTime, "2025-01-02 15:42:23")
require.NoError(t, err)
tracer.timeFn = func() time.Time {
return currentTime
}
task1 := tracer.Start("task one")
currentTime = currentTime.Add(time.Second)
// special chars handling: will be replaced with "namespace|task two" in the output
task2 := tracer.Start("namespace:task two")
tracer.Start("task three - did not finish, should not show up in end result")
currentTime = currentTime.Add(time.Second * 2)
task1.Stop()
currentTime = currentTime.Add(time.Second * 3)
task2.Stop()
// very short tasks should still show up as a point in timeline
tracer.Start("very short task").Stop()
r := require.New(t)
r.NoError(tracer.WriteOutput())
contents, err := os.ReadFile(tracer.outFile)
r.NoError(err)
expectedContents := `gantt
title Task Execution Timeline
dateFormat YYYY-MM-DD HH:mm:ss.SSS
axisFormat %X
task one [3s] :done, 2025-01-02 15:42:23.000, 2025-01-02 15:42:26.000
namespace|task two [5s] :done, 2025-01-02 15:42:24.000, 2025-01-02 15:42:29.000
very short task [0s] :done, 2025-01-02 15:42:29.000, 2025-01-02 15:42:29.000
`
r.Equal(expectedContents, string(contents))
}