(t *testing.T)
| 93 | } |
| 94 | |
| 95 | func 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 |
nothing calls this directly
no test coverage detected