File: qlog_test.go

package info (click to toggle)
golang-golang-x-net 1%3A0.24.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 8,460 kB
  • sloc: asm: 18; makefile: 12; sh: 7
file content (151 lines) | stat: -rw-r--r-- 3,124 bytes parent folder | download | duplicates (4)
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright 2023 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build go1.21

package qlog

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"log/slog"
	"reflect"
	"testing"
	"time"
)

// QLog tests are mostly in the quic package, where we can test event generation
// and serialization together.

func TestQLogHandlerEvents(t *testing.T) {
	for _, test := range []struct {
		name string
		f    func(*slog.Logger)
		want []map[string]any // events, not counting the trace header
	}{{
		name: "various types",
		f: func(log *slog.Logger) {
			log.Info("message",
				"bool", true,
				"duration", time.Duration(1*time.Second),
				"float", 0.0,
				"int", 0,
				"string", "value",
				"uint", uint64(0),
				slog.Group("group",
					"a", 0,
				),
			)
		},
		want: []map[string]any{{
			"name": "message",
			"data": map[string]any{
				"bool":     true,
				"duration": float64(1000),
				"float":    float64(0.0),
				"int":      float64(0),
				"string":   "value",
				"uint":     float64(0),
				"group": map[string]any{
					"a": float64(0),
				},
			},
		}},
	}, {
		name: "WithAttrs",
		f: func(log *slog.Logger) {
			log = log.With(
				"with_a", "a",
				"with_b", "b",
			)
			log.Info("m1", "field", "1")
			log.Info("m2", "field", "2")
		},
		want: []map[string]any{{
			"name": "m1",
			"data": map[string]any{
				"with_a": "a",
				"with_b": "b",
				"field":  "1",
			},
		}, {
			"name": "m2",
			"data": map[string]any{
				"with_a": "a",
				"with_b": "b",
				"field":  "2",
			},
		}},
	}, {
		name: "WithGroup",
		f: func(log *slog.Logger) {
			log = log.With(
				"with_a", "a",
				"with_b", "b",
			)
			log.Info("m1", "field", "1")
			log.Info("m2", "field", "2")
		},
		want: []map[string]any{{
			"name": "m1",
			"data": map[string]any{
				"with_a": "a",
				"with_b": "b",
				"field":  "1",
			},
		}, {
			"name": "m2",
			"data": map[string]any{
				"with_a": "a",
				"with_b": "b",
				"field":  "2",
			},
		}},
	}} {
		var out bytes.Buffer
		opts := HandlerOptions{
			Level: slog.LevelDebug,
			NewTrace: func(TraceInfo) (io.WriteCloser, error) {
				return nopCloseWriter{&out}, nil
			},
		}
		h, err := newJSONTraceHandler(opts, []slog.Attr{
			slog.String("group_id", "group"),
			slog.Group("vantage_point",
				slog.String("type", "client"),
			),
		})
		if err != nil {
			t.Fatal(err)
		}
		log := slog.New(h)
		test.f(log)
		got := []map[string]any{}
		for i, e := range bytes.Split(out.Bytes(), []byte{0x1e}) {
			// i==0: empty string before the initial record separator
			// i==1: trace header; not part of this test
			if i < 2 {
				continue
			}
			var val map[string]any
			if err := json.Unmarshal(e, &val); err != nil {
				panic(fmt.Errorf("log unmarshal failure: %v\n%q", err, string(e)))
			}
			delete(val, "time")
			got = append(got, val)
		}
		if !reflect.DeepEqual(got, test.want) {
			t.Errorf("event mismatch\ngot:  %v\nwant: %v", got, test.want)
		}
	}

}

type nopCloseWriter struct {
	io.Writer
}

func (nopCloseWriter) Close() error { return nil }