MCPcopy
hub / github.com/pocketbase/pocketbase / TestBaseAppLoggerWrites

Function TestBaseAppLoggerWrites

core/base_test.go:268–331  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

266}
267
268func TestBaseAppLoggerWrites(t *testing.T) {
269 t.Parallel()
270
271 app, _ := tests.NewTestApp()
272 defer app.Cleanup()
273
274 // reset
275 if err := app.DeleteOldLogs(time.Now()); err != nil {
276 t.Fatal(err)
277 }
278
279 const logsThreshold = 200
280
281 totalLogs := func(app core.App, t *testing.T) int {
282 var total int
283
284 err := app.LogQuery().Select("count(*)").Row(&total)
285 if err != nil {
286 t.Fatalf("Failed to fetch total logs: %v", err)
287 }
288
289 return total
290 }
291
292 t.Run("disabled logs retention", func(t *testing.T) {
293 app.Settings().Logs.MaxDays = 0
294
295 for i := 0; i < logsThreshold+1; i++ {
296 app.Logger().Error("test")
297 }
298
299 if total := totalLogs(app, t); total != 0 {
300 t.Fatalf("Expected no logs, got %d", total)
301 }
302 })
303
304 t.Run("test batch logs writes", func(t *testing.T) {
305 app.Settings().Logs.MaxDays = 1
306
307 for i := 0; i < logsThreshold-1; i++ {
308 app.Logger().Error("test")
309 }
310
311 if total := totalLogs(app, t); total != 0 {
312 t.Fatalf("Expected no logs, got %d", total)
313 }
314
315 // should trigger batch write
316 app.Logger().Error("test")
317
318 // should be added for the next batch write
319 app.Logger().Error("test")
320
321 if total := totalLogs(app, t); total != logsThreshold {
322 t.Fatalf("Expected %d logs, got %d", logsThreshold, total)
323 }
324
325 // wait for ~3 secs to check the timer trigger

Callers

nothing calls this directly

Calls 9

CleanupMethod · 0.95
NewTestAppFunction · 0.92
SelectMethod · 0.80
DeleteOldLogsMethod · 0.65
LogQueryMethod · 0.65
SettingsMethod · 0.65
ErrorMethod · 0.65
LoggerMethod · 0.65
RunMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…