logger_test.go 746 B

123456789101112131415161718192021222324252627282930
  1. package logger
  2. import (
  3. "fmt"
  4. "testing"
  5. )
  6. // TestGetLogs_ReturnsAtMostC guards the documented "up to c entries" contract.
  7. // The loop condition must cap output at c (ERROR entries are queried at "debug"
  8. // level so the level filter passes all of them, isolating the count).
  9. func TestGetLogs_ReturnsAtMostC(t *testing.T) {
  10. logBufferMu.Lock()
  11. logBuffer = nil
  12. logBufferMu.Unlock()
  13. for i := 0; i < 5; i++ {
  14. addToBuffer("ERROR", fmt.Sprintf("m%d", i))
  15. }
  16. cases := []struct{ c, want int }{
  17. {0, 0},
  18. {2, 2},
  19. {5, 5},
  20. {10, 5}, // capped at what's available
  21. }
  22. for _, tc := range cases {
  23. if got := GetLogs(tc.c, "debug"); len(got) != tc.want {
  24. t.Errorf("GetLogs(%d) returned %d entries, want %d", tc.c, len(got), tc.want)
  25. }
  26. }
  27. }