| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- package logonce
- import (
- "fmt"
- "sync"
- "testing"
- "time"
- )
- // capture devuelve un Logger con salida y reloj controlados por el test.
- func capture() (l *Logger, lines func() []string, clock *time.Time) {
- var mu sync.Mutex
- var got []string
- t0 := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
- clock = &t0
- l = NewWithLogf("[test] ", func(format string, args ...any) {
- mu.Lock()
- defer mu.Unlock()
- got = append(got, fmt.Sprintf(format, args...))
- })
- l.now = func() time.Time { mu.Lock(); defer mu.Unlock(); return *clock }
- return l, func() []string { mu.Lock(); defer mu.Unlock(); return append([]string(nil), got...) }, clock
- }
- func TestPrintfOncePerKey(t *testing.T) {
- l, lines, _ := capture()
- l.Printf("a", "aviso %d", 1)
- l.Printf("a", "aviso %d", 2)
- l.Printf("b", "otro")
- got := lines()
- if len(got) != 2 || got[0] != "[test] aviso 1" || got[1] != "[test] otro" {
- t.Fatalf("líneas = %q", got)
- }
- }
- func TestCapLogsSuppressionOnce(t *testing.T) {
- l, lines, _ := capture()
- for i := 0; i < defaultMax+10; i++ {
- l.Printf(fmt.Sprint(i), "clave %d", i)
- }
- got := lines()
- if len(got) != defaultMax+1 {
- t.Fatalf("líneas = %d, esperaba %d avisos y una de supresión", len(got), defaultMax+1)
- }
- if last := got[len(got)-1]; last != "[test] further warnings suppressed" {
- t.Errorf("última línea = %q", last)
- }
- }
- func TestLoggersAreIndependent(t *testing.T) {
- noisy, _, _ := capture()
- quiet, lines, _ := capture()
- for i := 0; i < defaultMax+10; i++ {
- noisy.Printf(fmt.Sprint(i), "x")
- }
- quiet.Printf("k", "sigo avisando")
- if got := lines(); len(got) != 1 {
- t.Fatalf("un Logger lleno no debe callar a otro: %q", got)
- }
- }
- func TestResetAfterWindow(t *testing.T) {
- l, lines, clock := capture()
- for i := 0; i < defaultMax+1; i++ {
- l.Printf(fmt.Sprint(i), "x")
- }
- l.Printf("0", "x")
- before := len(lines())
- *clock = clock.Add(defaultWindow + time.Minute)
- l.Printf("0", "x")
- l.Printf("nueva", "x")
- if got := len(lines()) - before; got != 2 {
- t.Fatalf("tras la ventana: %d líneas nuevas, esperaba 2 (olvida claves y tope)", got)
- }
- }
- func TestPrefixWithPercent(t *testing.T) {
- var got []string
- l := NewWithLogf("[100%s] ", func(format string, args ...any) {
- got = append(got, fmt.Sprintf(format, args...))
- })
- l.Printf("k", "valor %d", 7)
- if len(got) != 1 || got[0] != "[100%s] valor 7" {
- t.Fatalf("líneas = %q (want prefijo literal con %%)", got)
- }
- }
- func TestConcurrent(t *testing.T) {
- l, lines, _ := capture()
- var wg sync.WaitGroup
- for g := 0; g < 8; g++ {
- wg.Add(1)
- go func(g int) {
- defer wg.Done()
- for i := 0; i < 200; i++ {
- key := fmt.Sprint((i + g) % 100)
- l.Printf(key, "clave %s", key)
- }
- }(g)
- }
- wg.Wait()
- got := lines()
- if len(got) != defaultMax+1 {
- t.Fatalf("líneas = %d, esperaba %d (%d claves y una de supresión)", len(got), defaultMax+1, defaultMax)
- }
- suppressed := 0
- seen := map[string]bool{}
- for _, line := range got {
- if line == "[test] further warnings suppressed" {
- suppressed++
- continue
- }
- if seen[line] {
- t.Fatalf("clave repetida en el log concurrente: %q", line)
- }
- seen[line] = true
- }
- if suppressed != 1 {
- t.Fatalf("líneas de supresión = %d, esperaba 1", suppressed)
- }
- if len(seen) != defaultMax {
- t.Fatalf("claves distintas = %d, esperaba %d", len(seen), defaultMax)
- }
- }
|