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
/
Copy pathtrace_test.go
132 lines (112 loc) · 2.91 KB
/
trace_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
package money
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestDecodeTraceContext(t *testing.T) {
tests := []struct {
name string
i string
o *TraceContext
e error
}{
{
i: "",
o: nil,
e: errPairsCount,
},
{
name: "ideal",
i: "trace-id=de305d54-75b4-431b-adb2-eb6b9e546013;parent-id=3285573610483682037;span-id=3285573610483682037",
o: &TraceContext{
PID: 3285573610483682037,
SID: 3285573610483682037,
TID: "de305d54-75b4-431b-adb2-eb6b9e546013",
},
e: nil,
},
{
name: "duplicateEntries",
i: "parent-id=1;parent-id=3285573610483682037;span-id=3285573610483682037",
o: nil,
e: errBadTrace,
},
{
name: "badPair",
i: "parent-id=de305d54-75b=4-431b-adb2-eb6b9e546013;parent-id=3285573610483682037;span-id=3285573610483682037",
o: nil,
e: errBadPair,
},
{
name: "NoRealPairs",
i: "one=1;two=2;three=3",
o: nil,
e: errBadTrace,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert := assert.New(t)
actualO, actualE := decodeTraceContext(test.i)
assert.Equal(test.e, actualE)
assert.Equal(test.o, actualO)
})
}
}
func TestDecodeTraceContextOtherCases(t *testing.T) {
t.Run("NonIntSID", func(t *testing.T) {
i := "trace-id=de305d54-75b4-431b-adb2-eb6b9e546013;parent-id=3285573610483682037;span-id=NaN"
tc, e := decodeTraceContext(i)
if tc != nil || e == nil {
t.Errorf("expected tc to be nil and error to be non-nil but got '%v' and '%v'", tc, e)
}
})
t.Run("NonIntPID", func(t *testing.T) {
i := "trace-id=de305d54-75b4-431b-adb2-eb6b9e546013;parent-id=NaN;span-id=123"
tc, e := decodeTraceContext(i)
if tc != nil || e == nil {
t.Errorf("expected tc to be nil and error to be non-nil but got '%v' and '%v'", tc, e)
}
})
}
//TODO: need to if a string is in the right order accordance.
// Tests if typeInferenceTC outputs a the fields of a TID in the correct order.
func TestTypeInferenceTC(t *testing.T) {
in := map[string]interface{}{
"PID": 1,
"SID": 1,
"TID": "one",
}
var expected = "parent-id=1;span-id=1;trace-id=one"
var actual = typeInferenceTC(in)
if actual != expected {
t.Errorf("Wrong Format for Trace Context string, need '%v' but got '%v'", expected, actual)
}
}
func TestEncodeTraceContext(t *testing.T) {
in := &TraceContext{
PID: 1,
TID: "one",
SID: 1,
}
actual, expected := EncodeTraceContext(in), "parent-id=1;span-id=1;trace-id=one"
if actual != expected {
t.Errorf("Expected %v but got %v", expected, actual)
}
}
func TestSubtrace(t *testing.T) {
current := &TraceContext{
TID: "123",
SID: 1,
}
st := SubTrace(current)
if st.PID != current.SID {
t.Errorf("Expected pid to be %v but got %v", current.PID, st.PID)
}
if st.SID == 0 {
t.Error("Expected sid to be defined")
}
if st.TID != current.TID {
t.Errorf("Expected tid to be %v but got %v", current.TID, st.TID)
}
}