-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathattr_test.go
126 lines (103 loc) · 3.01 KB
/
attr_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
package ctxlog_test
import (
"context"
"log/slog"
"sync"
"testing"
"github.com/cyrusaf/ctxlog"
)
func TestTags(t *testing.T) {
ctx := context.Background()
attr1 := slog.String("hello", "world")
attr2 := slog.Int("foo", 5)
ctx = ctxlog.WithAttrs(ctx, attr1)
ctx = ctxlog.WithAttrs(ctx, attr2)
// Test global attrs. Use _ = to drop returned context so the global flow
// is properly tested.
ctx = ctxlog.AnchorGlobalAttrs(ctx)
attr3 := slog.Int("bar", 1)
attr4 := slog.Int("baz", 2)
_ = ctxlog.WithGlobalAttrs(ctx, attr3)
_ = ctxlog.WithGlobalAttrs(ctx, attr4)
expectedAttrs := []slog.Attr{attr1, attr2, attr3, attr4}
attrs := ctxlog.GetAttrs(ctx)
assertAttrs(t, expectedAttrs, attrs)
}
func TestWithGlobalAttrsWithoutAnchor(t *testing.T) {
ctx := context.Background()
attr1 := slog.String("foo", "bar")
ctx2 := ctxlog.WithGlobalAttrs(ctx, attr1)
attrs := ctxlog.GetAttrs(ctx2)
assertAttrs(t, []slog.Attr{attr1}, attrs)
attrs = ctxlog.GetAttrs(ctx)
if len(attrs) != 0 {
t.Fatalf("expected 0 attrs, but got %d instead", len(attrs))
}
}
func TestOverwriteAttr(t *testing.T) {
ctx := context.Background()
attr1 := slog.String("foo", "bar")
ctx = ctxlog.WithAttrs(ctx, attr1)
attr2 := slog.String("foo", "baz")
ctx = ctxlog.WithAttrs(ctx, attr2)
attrs := ctxlog.GetAttrs(ctx)
assertAttrs(t, []slog.Attr{attr2}, attrs)
}
func TestWithAttrsRace(t *testing.T) {
ctx := context.Background()
ctx = ctxlog.WithAttrs(ctx, slog.String("foo", "bar"))
ctx = ctxlog.AnchorGlobalAttrs(ctx)
wg := sync.WaitGroup{}
for i := 0; i < 10; i++ {
wg.Add(1)
i := 1
go func() {
defer wg.Done()
ctxlog.WithAttrs(ctx, slog.Int("local", i))
ctxlog.WithGlobalAttrs(ctx, slog.Int("global", i))
}()
}
wg.Wait()
}
func TestSecondAnchor(t *testing.T) {
ctx := context.Background()
// Test global attrs. Use _ = to drop returned context so the global flow
// is properly tested.
ctx = ctxlog.AnchorGlobalAttrs(ctx)
attr1 := slog.String("hello", "world")
attr2 := slog.Int("foo", 5)
_ = ctxlog.WithGlobalAttrs(ctx, attr1)
_ = ctxlog.WithGlobalAttrs(ctx, attr2)
ctx = ctxlog.AnchorGlobalAttrs(ctx)
attr3 := slog.Int("bar", 1)
attr4 := slog.Int("baz", 2)
_ = ctxlog.WithGlobalAttrs(ctx, attr3)
_ = ctxlog.WithGlobalAttrs(ctx, attr4)
expectedAttrs := []slog.Attr{attr1, attr2, attr3, attr4}
attrs := ctxlog.GetAttrs(ctx)
assertAttrs(t, expectedAttrs, attrs)
}
func assertAttrs(t *testing.T, expected, actual []slog.Attr) {
t.Helper()
if len(expected) != len(actual) {
t.Fatalf("expected %d attrs, but got %d instead", len(expected), len(actual))
}
expectedMap := toMap(expected)
actualMap := toMap(actual)
for key, expected := range expectedMap {
actual, ok := actualMap[key]
if !ok {
t.Fatalf("missing attr %+v", expected)
}
if !expected.Equal(actual) {
t.Fatalf("attrs not equal - expected: %+v, actual: %+v", expected, actual)
}
}
}
func toMap(attrs []slog.Attr) map[string]slog.Attr {
m := make(map[string]slog.Attr, len(attrs))
for _, attr := range attrs {
m[attr.Key] = attr
}
return m
}