-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
tracer_test.go
105 lines (100 loc) · 2.77 KB
/
tracer_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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package tracer_test
import (
"strings"
"testing"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/fujiwara/tracer"
)
var (
testEvents = []tracer.TimelineEvent{
{
Timestamp: time.Date(2021, 1, 2, 3, 4, 5, 123_999_000, time.UTC),
Message: "test message 1",
Source: "test_source 1",
},
{
Timestamp: time.Date(2021, 1, 2, 3, 4, 5, 123_999_999, time.UTC),
Message: "test message 5",
Source: "test_source 5",
},
{
Timestamp: time.Date(2021, 1, 2, 3, 4, 6, 123_999_000, time.UTC),
Message: "test message 2",
Source: "test_source 2",
},
{
// same timestamp to test sort stable
Timestamp: time.Date(2021, 1, 2, 3, 4, 5, 123_999_000, time.UTC),
Message: "test message 3",
Source: "test_source 3",
},
{
// duplicate event
Timestamp: time.Date(2021, 1, 2, 3, 4, 5, 123_999_000, time.UTC),
Message: "test message 3",
Source: "test_source 3",
},
{
Timestamp: aws.ToTime(nil),
Message: "test message ignored",
Source: "test_source ignored",
},
}
expectedOutput = `2021-01-02T03:04:05.123Z test_source 1 test message 1
2021-01-02T03:04:05.123Z test_source 3 test message 3
2021-01-02T03:04:05.123Z test_source 5 test message 5
2021-01-02T03:04:06.123Z test_source 2 test message 2
`
expectedJSONOutput = `{"time":"2021-01-02T03:04:05.123Z","src":"test_source 1","msg":"test message 1"}
{"time":"2021-01-02T03:04:05.123Z","src":"test_source 3","msg":"test message 3"}
{"time":"2021-01-02T03:04:05.123Z","src":"test_source 5","msg":"test message 5"}
{"time":"2021-01-02T03:04:06.123Z","src":"test_source 2","msg":"test message 2"}
`
)
func TestTimelineEvent(t *testing.T) {
t.Setenv("TZ", "UTC")
now := time.Date(2021, 1, 2, 3, 4, 5, 123_999_000, time.UTC)
ev := tracer.TimelineEvent{
Timestamp: now,
Message: "test message",
Source: "test_source",
}
if ev.String() != "2021-01-02T03:04:05.123Z\ttest_source\ttest message\n" {
t.Errorf("unexpected string: %s", ev.String())
}
}
func TestTimeline(t *testing.T) {
t.Setenv("TZ", "UTC")
tl := tracer.NewTimeline()
for _, ev := range testEvents {
ev := ev
tl.Add(&ev)
}
t.Run("Print(plaintext)", func(t *testing.T) {
b := new(strings.Builder)
n, err := tl.Print(b, false)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if n != len(expectedOutput) {
t.Errorf("unexpected length: %d", n)
}
if b.String() != expectedOutput {
t.Errorf("unexpected output: %s", b.String())
}
})
t.Run("Print(json)", func(t *testing.T) {
b := new(strings.Builder)
n, err := tl.Print(b, true)
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if n != len(expectedJSONOutput) {
t.Errorf("unexpected length: %d", n)
}
if b.String() != expectedJSONOutput {
t.Errorf("unexpected output: %s", b.String())
}
})
}