-
Notifications
You must be signed in to change notification settings - Fork 912
/
glog_context_test.go
62 lines (53 loc) · 1.86 KB
/
glog_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
package glog
import (
"context"
"flag"
"testing"
"github.com/golang/glog/internal/logsink"
)
type contextKey string
type fakeLogSink struct {
context context.Context
}
var ctxKey = contextKey("key")
var ctxValue = "some-value"
var originalSinks = logsink.StructuredSinks
func (s *fakeLogSink) Printf(meta *logsink.Meta, format string, args ...any) (int, error) {
s.context = meta.Context
return 0, nil
}
// Test that log.(Info|Error|Warning)Context functions behave the same as non context variants
// and pass right context.
func TestLogContext(t *testing.T) {
fakeLogSink := &fakeLogSink{}
logsink.StructuredSinks = append([]logsink.Structured{fakeLogSink}, originalSinks...)
funcs := map[string]func(ctx context.Context, args ...any){
"InfoContext": InfoContext,
"InfoContextDepth": func(ctx context.Context, args ...any) { InfoContextDepth(ctx, 2, args) },
"ErrorContext": ErrorContext,
"WarningContext": WarningContext,
}
ctx := context.WithValue(context.Background(), ctxKey, ctxValue)
for name, f := range funcs {
f(ctx, "test")
want := ctxValue
if got := fakeLogSink.context.Value(ctxKey); got != want {
t.Errorf("%s: context value unexpectedly missing: got %q, want %q", name, got, want)
}
}
}
// Test that V.InfoContext behaves the same as V.Info and passes right context.
func TestVInfoContext(t *testing.T) {
fakeLogSink := &fakeLogSink{}
logsink.StructuredSinks = append([]logsink.Structured{fakeLogSink}, originalSinks...)
if err := flag.Lookup("v").Value.Set("2"); err != nil {
t.Fatalf("Failed to set -v=2: %v", err)
}
defer flag.Lookup("v").Value.Set("0")
ctx := context.WithValue(context.Background(), ctxKey, ctxValue)
V(2).InfoContext(ctx, "test")
want := ctxValue
if got := fakeLogSink.context.Value(ctxKey); got != want {
t.Errorf("V.InfoContext: context value unexpectedly missing: got %q, want %q", got, want)
}
}