sseclient_test.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package sseclient
  2. import (
  3. "testing"
  4. "time"
  5. )
  6. // #1563: una conexión que llegó a establecerse reinicia el backoff, sin
  7. // importar cuántos cortes limpios hubiera antes.
  8. func TestRetryDelayResetsAfterCleanDisconnect(t *testing.T) {
  9. c := &Client{minRetry: 5 * time.Second, maxRetry: 5 * time.Minute}
  10. for _, current := range []time.Duration{5 * time.Second, 40 * time.Second, 5 * time.Minute} {
  11. delay, next := c.retryDelay(current, true)
  12. if delay != c.minRetry || next != c.minRetry {
  13. t.Fatalf("corte limpio con current=%s: delay=%s next=%s, quería %s/%s",
  14. current, delay, next, c.minRetry, c.minRetry)
  15. }
  16. }
  17. }
  18. // Solo los fallos consecutivos hacen crecer la espera, y nunca por encima de
  19. // maxRetry.
  20. func TestRetryDelayGrowsOnFailures(t *testing.T) {
  21. c := &Client{minRetry: 5 * time.Second, maxRetry: 5 * time.Minute}
  22. cases := []struct {
  23. current time.Duration
  24. delay time.Duration
  25. next time.Duration
  26. }{
  27. {5 * time.Second, 5 * time.Second, 10 * time.Second},
  28. {10 * time.Second, 10 * time.Second, 20 * time.Second},
  29. {4 * time.Minute, 4 * time.Minute, 5 * time.Minute},
  30. {5 * time.Minute, 5 * time.Minute, 5 * time.Minute},
  31. }
  32. for _, tc := range cases {
  33. delay, next := c.retryDelay(tc.current, false)
  34. if delay != tc.delay || next != tc.next {
  35. t.Fatalf("fallo con current=%s: delay=%s next=%s, quería %s/%s",
  36. tc.current, delay, next, tc.delay, tc.next)
  37. }
  38. }
  39. }