-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcontext_test.go
74 lines (52 loc) · 1.48 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
package tlog
import (
"bytes"
"context"
"testing"
"github.com/nikandfor/assert"
"nikand.dev/go/hacked/low"
)
func TestContextWithSpan(t *testing.T) {
var buf, bufl low.Buf
DefaultLogger = New(NewConsoleWriter(&buf, 0))
DefaultLogger.NewID = testRandID(2)
l := New(NewConsoleWriter(&bufl, 0))
l.NewID = DefaultLogger.NewID
id := ID{10, 20}
tr := Span{Logger: DefaultLogger, ID: id}
ctx := ContextWithSpan(context.Background(), tr)
res := SpanFromContext(ctx)
assert.Equal(t, tr, res)
tr = SpawnFromContext(ctx, "spawn_1")
if assert.NotZero(t, tr) {
assert.Equal(t, "spawn_1 _s=2f8282cb _k=s _p=0a140000\n", string(buf))
}
//
ctx = ContextWithSpan(context.Background(), Span{})
res = SpanFromContext(ctx)
assert.Zero(t, res)
//
DefaultLogger = nil
tr = Span{Logger: l, ID: id}
ctx = ContextWithSpan(context.Background(), tr)
res = SpanFromContext(ctx)
assert.Equal(t, tr, res)
tr = SpawnFromContext(ctx, "spawn_2")
if assert.NotZero(t, tr) {
assert.Equal(t, "spawn_2 _s=d967dc28 _k=s _p=0a140000\n", string(bufl))
}
}
func TestContextResetSpan(t *testing.T) {
defer func(old *Logger) {
DefaultLogger = old
}(DefaultLogger)
var buf bytes.Buffer
DefaultLogger = New(NewConsoleWriter(&buf, 0))
DefaultLogger.NewID = testRandID(3)
tr := Start("root")
ctx := ContextWithSpan(context.Background(), tr)
//
ctx2 := ContextWithSpan(ctx, tr.V("nope"))
tr2 := SpawnFromContext(ctx2, "spawn")
assert.Zero(t, tr2)
}