MCPcopy
hub / github.com/grafana/dskit / TestLazySprintf

Function TestLazySprintf

log/gokit_test.go:35–73  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

33}
34
35func TestLazySprintf(t *testing.T) {
36 tests := map[string]struct {
37 id int
38 lvl string
39 format string
40 }{
41 "debug level should log debug messages and call Sprintf": {
42 id: 1,
43 lvl: "debug",
44 format: "debug %d has been logged %v",
45 },
46 "info level should not log debug messages and should not call Sprintf": {
47 id: 2,
48 lvl: "info",
49 format: "info %d has not been logged %v",
50 },
51 }
52
53 buf := bytes.NewBuffer(nil)
54
55 for _, test := range tests {
56 buf.Reset()
57 logger := NewGoKitWithWriter(LogfmtFormat, buf)
58 var lvl Level
59 require.NoError(t, lvl.Set(test.lvl))
60 logger = level.NewFilter(logger, lvl.Option)
61 now := time.Now()
62 expectedMessage := fmt.Sprintf(test.format, test.id, now)
63 lazySprintf := newLazySprintfWithCount("debug %d has been logged %v", test.id, now)
64 level.Debug(logger).Log("msg", lazySprintf)
65 if test.lvl == "debug" {
66 require.True(t, bytes.Contains(buf.Bytes(), []byte(expectedMessage)))
67 require.Equal(t, 1, lazySprintf.count)
68 } else {
69 require.False(t, bytes.Contains(buf.Bytes(), []byte(expectedMessage)))
70 require.Equal(t, 0, lazySprintf.count)
71 }
72 }
73}
74
75func TestNewGoKitWithRateLimitedAndFields(t *testing.T) {
76 buf := bytes.NewBuffer(nil)

Callers

nothing calls this directly

Calls 6

SetMethod · 0.95
NewGoKitWithWriterFunction · 0.85
newLazySprintfWithCountFunction · 0.85
ResetMethod · 0.45
LogMethod · 0.45
EqualMethod · 0.45

Tested by

no test coverage detected