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

Function TestOnFlushCallback

log/buffered_test.go:95–130  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

93}
94
95func TestOnFlushCallback(t *testing.T) {
96 var (
97 flushCount uint32
98 flushedEntries int
99 buf bytes.Buffer
100 )
101
102 callback := func(entries uint32) {
103 flushCount++
104 flushedEntries += int(entries)
105 }
106
107 // Do NOT specify a flush period because we want to be in control of the flushes done by this test.
108 bufLog := NewBufferedLogger(&buf, 2,
109 WithPrellocatedBuffer(bufferSize),
110 WithFlushCallback(callback),
111 )
112
113 l := log.NewLogfmtLogger(bufLog)
114 //nolint: loggercheck
115 require.NoError(t, l.Log("line"))
116 //nolint: loggercheck
117 require.NoError(t, l.Log("line"))
118 // first flush
119 //nolint: loggercheck
120 require.NoError(t, l.Log("line"))
121
122 // pre-condition check: the last Log() call should have flushed previous entries.
123 require.Equal(t, uint32(1), bufLog.Size())
124
125 // force a second
126 require.NoError(t, bufLog.Flush())
127
128 require.Equal(t, uint32(2), flushCount)
129 require.Len(t, strings.Split(buf.String(), "\n"), flushedEntries+1)
130}
131
132// outFile creates a real OS file for testing.
133// We cannot use stdout/stderr since we need to read the contents afterwards to validate, and we have to write to a file

Callers

nothing calls this directly

Calls 9

SizeMethod · 0.95
FlushMethod · 0.95
NewBufferedLoggerFunction · 0.85
WithPrellocatedBufferFunction · 0.85
WithFlushCallbackFunction · 0.85
StringMethod · 0.65
LogMethod · 0.45
EqualMethod · 0.45
LenMethod · 0.45

Tested by

no test coverage detected