This repository has been archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
span_test.go
executable file
·159 lines (129 loc) · 3.24 KB
/
span_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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package money
import (
"encoding/json"
"fmt"
"log"
"reflect"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
type stubClock struct{}
func (stubClock) Start() time.Time { return time.Now() }
func (stubClock) End(t time.Time) time.Duration { return time.Since(t) }
func createMockTC() *TraceContext {
return &TraceContext{
TID: "test-trace",
SID: 1,
PID: 1,
}
}
func createMockSpan() *Span {
var tc = createMockTC()
var s time.Time
var d time.Duration
var err = fmt.Errorf("err")
return &Span{
Name: "test-span",
AppName: "test-app",
TC: tc,
Success: true,
Code: 1,
StartTime: s,
Duration: d,
Host: "localhost",
Err: err,
}
}
func TestNewSpan(t *testing.T) {
var tc = createMockTC()
var expected = Span{
Name: "test-span",
TC: tc,
}
assert.Equal(t, expected, NewSpan("test-span", tc))
}
func TestMapFieldToString(t *testing.T) {
var s = createMockSpan()
var m map[string]interface{}
var ourClock stubClock
var startTime = ourClock.Start()
time.Sleep(200000000)
var duration = ourClock.End(startTime)
s.StartTime, s.Duration = startTime, duration
r, err := json.Marshal(s)
if err != nil {
log.Fatal(err)
}
json.Unmarshal(r, &m)
var actual = mapFieldToString(m)
var expected = SpanMap{
"Name": "test-span",
"AppName": "test-app",
"TC": "parent-id=1;span-id=1;trace-id=test-trace",
"Success": "true",
"Code": "1",
"Duration": fmt.Sprintf("%v"+"ns", duration.Nanoseconds()),
"StartTime": startTime.Format("2006-01-02T15:04:05.999999999Z07:00"),
"Err": "Error",
"Host": "localhost",
}
// assert.Equal(t, expected, n)
ok := reflect.DeepEqual(expected, actual)
if !ok {
t.Errorf("Expected:\n%v\n\nActual:\n%v", expected, actual)
}
}
func TestMap(t *testing.T) {
s := createMockSpan()
var ourClock stubClock
var startTime = ourClock.Start()
time.Sleep(200000000)
var duration = ourClock.End(startTime)
s.StartTime, s.Duration = startTime, duration
sm, err := s.Map()
if err != nil {
log.Fatal(err)
}
var expected = SpanMap{
"Name": "test-span",
"AppName": "test-app",
"TC": "parent-id=1;span-id=1;trace-id=test-trace",
"Success": "true",
"Code": "1",
"Duration": fmt.Sprintf("%v"+"ns", duration.Nanoseconds()),
"StartTime": startTime.Format("2006-01-02T15:04:05.999999999Z07:00"),
"Err": "Error",
"Host": "localhost",
}
assert.Equal(t, expected, sm)
}
func TestString(t *testing.T) {
var ourClock stubClock
var startTime = ourClock.Start()
time.Sleep(200000000)
var duration = ourClock.End(startTime)
var startTimeString = startTime.Format("2006-01-02T15:04:05.999999999Z07:00")
var durationString = fmt.Sprintf("%v"+"ns", duration.Nanoseconds())
var s = &Span{
Name: "test-span",
AppName: "test-app",
TC: createMockTC(),
Success: true,
Code: 1,
StartTime: startTime,
Duration: duration,
Host: "localhost",
}
var expected = "span-name=test-span" +
";app-name=test-app" +
";span-duration=" + durationString +
";span-success=true" +
";parent-id=1" +
";span-id=1" +
";trace-id=test-trace" +
";start-time=" + startTimeString +
";host=localhost" +
";response-code=1"
assert.Equal(t, s.String(), expected)
}