logonce_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package logonce
  2. import (
  3. "fmt"
  4. "sync"
  5. "testing"
  6. "time"
  7. )
  8. // capture devuelve un Logger con salida y reloj controlados por el test.
  9. func capture() (l *Logger, lines func() []string, clock *time.Time) {
  10. var mu sync.Mutex
  11. var got []string
  12. t0 := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
  13. clock = &t0
  14. l = NewWithLogf("[test] ", func(format string, args ...any) {
  15. mu.Lock()
  16. defer mu.Unlock()
  17. got = append(got, fmt.Sprintf(format, args...))
  18. })
  19. l.now = func() time.Time { mu.Lock(); defer mu.Unlock(); return *clock }
  20. return l, func() []string { mu.Lock(); defer mu.Unlock(); return append([]string(nil), got...) }, clock
  21. }
  22. func TestPrintfOncePerKey(t *testing.T) {
  23. l, lines, _ := capture()
  24. l.Printf("a", "aviso %d", 1)
  25. l.Printf("a", "aviso %d", 2)
  26. l.Printf("b", "otro")
  27. got := lines()
  28. if len(got) != 2 || got[0] != "[test] aviso 1" || got[1] != "[test] otro" {
  29. t.Fatalf("líneas = %q", got)
  30. }
  31. }
  32. func TestCapLogsSuppressionOnce(t *testing.T) {
  33. l, lines, _ := capture()
  34. for i := 0; i < defaultMax+10; i++ {
  35. l.Printf(fmt.Sprint(i), "clave %d", i)
  36. }
  37. got := lines()
  38. if len(got) != defaultMax+1 {
  39. t.Fatalf("líneas = %d, esperaba %d avisos y una de supresión", len(got), defaultMax+1)
  40. }
  41. if last := got[len(got)-1]; last != "[test] further warnings suppressed" {
  42. t.Errorf("última línea = %q", last)
  43. }
  44. }
  45. func TestLoggersAreIndependent(t *testing.T) {
  46. noisy, _, _ := capture()
  47. quiet, lines, _ := capture()
  48. for i := 0; i < defaultMax+10; i++ {
  49. noisy.Printf(fmt.Sprint(i), "x")
  50. }
  51. quiet.Printf("k", "sigo avisando")
  52. if got := lines(); len(got) != 1 {
  53. t.Fatalf("un Logger lleno no debe callar a otro: %q", got)
  54. }
  55. }
  56. func TestResetAfterWindow(t *testing.T) {
  57. l, lines, clock := capture()
  58. for i := 0; i < defaultMax+1; i++ {
  59. l.Printf(fmt.Sprint(i), "x")
  60. }
  61. l.Printf("0", "x")
  62. before := len(lines())
  63. *clock = clock.Add(defaultWindow + time.Minute)
  64. l.Printf("0", "x")
  65. l.Printf("nueva", "x")
  66. if got := len(lines()) - before; got != 2 {
  67. t.Fatalf("tras la ventana: %d líneas nuevas, esperaba 2 (olvida claves y tope)", got)
  68. }
  69. }
  70. func TestPrefixWithPercent(t *testing.T) {
  71. var got []string
  72. l := NewWithLogf("[100%s] ", func(format string, args ...any) {
  73. got = append(got, fmt.Sprintf(format, args...))
  74. })
  75. l.Printf("k", "valor %d", 7)
  76. if len(got) != 1 || got[0] != "[100%s] valor 7" {
  77. t.Fatalf("líneas = %q (want prefijo literal con %%)", got)
  78. }
  79. }
  80. func TestConcurrent(t *testing.T) {
  81. l, lines, _ := capture()
  82. var wg sync.WaitGroup
  83. for g := 0; g < 8; g++ {
  84. wg.Add(1)
  85. go func(g int) {
  86. defer wg.Done()
  87. for i := 0; i < 200; i++ {
  88. key := fmt.Sprint((i + g) % 100)
  89. l.Printf(key, "clave %s", key)
  90. }
  91. }(g)
  92. }
  93. wg.Wait()
  94. got := lines()
  95. if len(got) != defaultMax+1 {
  96. t.Fatalf("líneas = %d, esperaba %d (%d claves y una de supresión)", len(got), defaultMax+1, defaultMax)
  97. }
  98. suppressed := 0
  99. seen := map[string]bool{}
  100. for _, line := range got {
  101. if line == "[test] further warnings suppressed" {
  102. suppressed++
  103. continue
  104. }
  105. if seen[line] {
  106. t.Fatalf("clave repetida en el log concurrente: %q", line)
  107. }
  108. seen[line] = true
  109. }
  110. if suppressed != 1 {
  111. t.Fatalf("líneas de supresión = %d, esperaba 1", suppressed)
  112. }
  113. if len(seen) != defaultMax {
  114. t.Fatalf("claves distintas = %d, esperaba %d", len(seen), defaultMax)
  115. }
  116. }