forked from xmidt-org/golang-money
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrace_test.go
114 lines (99 loc) · 2.51 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
package money
import (
"reflect"
"testing"
)
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) {
actualO, actualE := decodeTraceContext(test.i)
if actualE != test.e || !reflect.DeepEqual(actualO, test.o) {
t.Errorf("I was expecting '%v' '%v'", test.e, test.o)
t.Errorf("but got '%v' '%v'", actualE, 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)
}
})
}
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)
}
}