(t *testing.T)
| 173 | } |
| 174 | |
| 175 | func TestBatchHandlerHandle(t *testing.T) { |
| 176 | ctx := context.Background() |
| 177 | |
| 178 | beforeLogs := []*Log{} |
| 179 | writeLogs := []*Log{} |
| 180 | |
| 181 | h := NewBatchHandler(BatchOptions{ |
| 182 | BatchSize: 3, |
| 183 | BeforeAddFunc: func(_ context.Context, log *Log) bool { |
| 184 | beforeLogs = append(beforeLogs, log) |
| 185 | |
| 186 | // skip test2 log |
| 187 | return log.Message != "test2" |
| 188 | }, |
| 189 | WriteFunc: func(_ context.Context, logs []*Log) error { |
| 190 | writeLogs = logs |
| 191 | return nil |
| 192 | }, |
| 193 | }) |
| 194 | |
| 195 | h.Handle(ctx, slog.NewRecord(time.Now(), slog.LevelInfo, "test1", 0)) |
| 196 | h.Handle(ctx, slog.NewRecord(time.Now(), slog.LevelInfo, "test2", 0)) |
| 197 | h.Handle(ctx, slog.NewRecord(time.Now(), slog.LevelInfo, "test3", 0)) |
| 198 | |
| 199 | // no batch write |
| 200 | { |
| 201 | checkLogMessages([]string{"test1", "test2", "test3"}, beforeLogs, t) |
| 202 | |
| 203 | checkLogMessages([]string{"test1", "test3"}, h.logs, t) |
| 204 | |
| 205 | // should be empty because no batch write has happened yet |
| 206 | if totalWriteLogs := len(writeLogs); totalWriteLogs != 0 { |
| 207 | t.Fatalf("Expected %d writeLogs, got %d", 0, totalWriteLogs) |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | // add one more log to trigger the batch write |
| 212 | { |
| 213 | h.Handle(ctx, slog.NewRecord(time.Now(), slog.LevelInfo, "test4", 0)) |
| 214 | |
| 215 | // should be empty after the batch write |
| 216 | checkLogMessages([]string{}, h.logs, t) |
| 217 | |
| 218 | checkLogMessages([]string{"test1", "test3", "test4"}, writeLogs, t) |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | func TestBatchHandlerWriteAll(t *testing.T) { |
| 223 | ctx := context.Background() |
nothing calls this directly
no test coverage detected
searching dependent graphs…