-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcontext_test.go
122 lines (99 loc) · 3.01 KB
/
context_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
package strc
import (
"context"
"math/rand"
"net/http"
"strings"
"testing"
)
func TestTraceID(t *testing.T) {
src = rand.NewSource(0)
var tid TraceID
var r *http.Request = &http.Request{
Header: http.Header{},
}
if string(EmptyTraceID) != strings.Repeat("0", traceLength) {
t.Error("EmptyTraceID is not correct")
}
if TraceID(tid.String()) != EmptyTraceID {
t.Error("Uninitialized TraceID is not EmptyTraceID")
}
tid = NewTraceID()
if tid.String() != "bqzcRlJahlbbBZH" {
t.Errorf("NewTraceID() = %s want something else", tid)
}
if len(tid) != traceLength {
t.Errorf("NewTraceID() = %s want length %d", tid, traceLength)
}
if len(EmptyTraceID) != traceLength {
t.Errorf("EmptyTraceID = %s want length %d", EmptyTraceID, traceLength)
}
tid = NewTraceID()
if tid.String() != "IvQORsVBkYcTpgn" {
t.Errorf("NewTraceID() = %s want something else", tid)
}
ctx := WithTraceID(context.Background(), NewTraceID())
tid = TraceIDFromContext(ctx)
if tid.String() != "VIPEcESnuyuufaH" {
t.Errorf("NewTraceID() = %s want something else", tid)
}
ctx = WithTraceID(context.Background(), NewTraceID())
AddTraceIDHeader(ctx, r)
tid = TraceIDFromRequest(r)
if tid.String() != "LOlIxiHprrrvHqD" {
t.Errorf("NewTraceID() = %s want something else", tid)
}
if r.Header.Get(TraceHTTPHeaderName) != "LOlIxiHprrrvHqD" {
t.Error("AddTraceIDHeader() did not add the header")
}
}
func TestSpanID(t *testing.T) {
src = rand.NewSource(0)
var sid SpanID
var r *http.Request = &http.Request{
Header: http.Header{},
}
if string(EmptySpanID) != strings.Repeat("0", spanLength)+"."+strings.Repeat("0", spanLength) {
t.Error("EmptySpanID is not correct")
}
if SpanID(sid.String()) != EmptySpanID {
t.Error("Uninitialized SpanID is not EmptySpanID")
}
sid = NewSpanID(context.Background())
if sid.String() != "0000000.bqzcRlJ" {
t.Errorf("NewSpanID() = %s want something else", sid)
}
if len(sid) != spanLength*2+1 {
t.Errorf("NewSpanID() = %s want length %d", sid, spanLength*2+1)
}
if len(EmptySpanID) != spanLength*2+1 {
t.Errorf("EmptySpanID = %s want length %d", EmptySpanID, spanLength*2+1)
}
sid = NewSpanID(WithSpanID(context.Background(), sid))
if sid.String() != "bqzcRlJ.ahlbbBZ" {
t.Errorf("NewSpanID() = %s want something else", sid)
}
ctx := WithSpanID(context.Background(), sid)
sid = NewSpanID(ctx)
if sid.String() != "ahlbbBZ.IvQORsV" {
t.Errorf("NewSpanID() = %s want something else", sid)
}
sid = NewSpanID(ctx)
if sid.String() != "ahlbbBZ.kYcTpgn" {
t.Errorf("NewSpanID() = %s want something else", sid)
}
ctx = WithSpanID(ctx, NewSpanID(ctx))
sid = SpanIDFromContext(ctx)
if sid.String() != "ahlbbBZ.VIPEcES" {
t.Errorf("NewSpanID() = %s want something else", sid)
}
ctx = WithSpanID(ctx, NewSpanID(ctx))
AddSpanIDHeader(ctx, r)
sid = SpanIDFromRequest(r)
if sid.String() != "VIPEcES.yuufaHI" {
t.Errorf("NewSpanID() = %s want something else", sid)
}
if r.Header.Get(SpanHTTPHeaderName) != "VIPEcES.yuufaHI" {
t.Error("AddSpanIDHeader() did not add the header")
}
}