forked from tlog-dev/tlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context_test.go
117 lines (87 loc) · 2.35 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
package tlog
import (
"bytes"
"context"
"testing"
"github.com/stretchr/testify/assert"
)
func TestContextWithID(t *testing.T) {
defer func(old *Logger) {
DefaultLogger = old
}(DefaultLogger)
var buf bytes.Buffer
DefaultLogger = New(NewConsoleWriter(&buf, Lspans))
DefaultLogger.randID = testRandID()
ctx := ContextWithID(context.Background(), ID{})
tr := SpawnFromContext(ctx)
assert.Zero(t, tr)
tr = SpawnFromContextOrStart(ctx)
assert.NotZero(t, tr.ID)
//
id := ID{10, 20}
ctx = ContextWithID(context.Background(), id)
res := IDFromContext(ctx)
assert.Equal(t, id, res)
tr = SpawnFromContext(ctx)
if assert.NotZero(t, tr) {
assert.Equal(t, `0194fdc2fa2ffcc0 Span started
6e4ff95ff662a5ee Span spawned from 0a14000000000000
`, buf.String())
}
//
DefaultLogger = nil
tr = SpawnFromContext(ctx)
assert.Zero(t, tr)
tr = SpawnFromContextOrStart(ctx)
assert.Zero(t, tr)
}
func TestContextWithSpan(t *testing.T) {
defer func(old *Logger) {
DefaultLogger = old
}(DefaultLogger)
var buf bytes.Buffer
DefaultLogger = New(NewConsoleWriter(&buf, Lspans))
DefaultLogger.randID = testRandID()
id := ID{10, 20}
ctx := ContextWithSpan(context.Background(), Span{ID: id})
tr := SpanFromContext(ctx)
assert.Equal(t, id, tr.ID)
res := IDFromContext(ctx)
assert.Equal(t, id, res)
tr = SpawnFromContext(ctx)
if assert.NotZero(t, tr) {
assert.Equal(t, "0194fdc2fa2ffcc0 Span spawned from 0a14000000000000\n", buf.String())
}
//
ctx = ContextWithID(context.Background(), id)
tr = SpanFromContext(ctx)
assert.Zero(t, tr)
//
ctx = ContextWithSpan(context.Background(), Span{})
res = IDFromContext(ctx)
assert.Zero(t, res)
//
DefaultLogger = nil
ctx = ContextWithSpan(context.Background(), Span{ID: id})
tr = SpanFromContext(ctx)
assert.Zero(t, tr)
}
func TestContextWithRandom(t *testing.T) {
defer func(old *Logger) {
DefaultLogger = old
}(DefaultLogger)
var buf bytes.Buffer
DefaultLogger = New(NewConsoleWriter(&buf, Lspans))
DefaultLogger.randID = testRandID()
id := ID{10, 20}
ctx := ContextWithIDOrRandom(context.Background(), id)
res := IDFromContext(ctx)
assert.Equal(t, id, res)
ctx = ContextWithIDOrRandom(context.Background(), ID{})
res = IDFromContext(ctx)
assert.NotZero(t, res)
assert.NotEqual(t, id, res)
ctx = ContextWithRandomID(context.Background())
res = IDFromContext(ctx)
assert.NotZero(t, res)
}