MCPcopy
hub / github.com/pocketbase/pocketbase / TestBatchHandlerHandle

Function TestBatchHandlerHandle

tools/logger/batch_handler_test.go:175–220  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

173}
174
175func 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
222func TestBatchHandlerWriteAll(t *testing.T) {
223 ctx := context.Background()

Callers

nothing calls this directly

Calls 3

HandleMethod · 0.95
NewBatchHandlerFunction · 0.85
checkLogMessagesFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…