longwrite_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // longwrite_test.go — issue #2112: los handlers que esperan a una unidad
  2. // (reinstall, uninstall, reemit, instaladores de integración, copia de
  3. // configuración, acciones UCI) superan el WriteTimeout global de 15 s. Sin
  4. // extender el deadline, la respuesta se pierde y el navegador la pinta como
  5. // "no se alcanza el servidor".
  6. package httpapi
  7. import (
  8. "bytes"
  9. "io"
  10. "net/http"
  11. "net/http/httptest"
  12. "testing"
  13. "time"
  14. )
  15. // globalWriteTimeout replica el WriteTimeout de cmd/netpulse/main.go. Si allí
  16. // cambia, este número tiene que seguirlo: es la referencia contra la que se
  17. // comprueba que ningún techo se queda corto.
  18. const globalWriteTimeout = 15 * time.Second
  19. func TestLongWriteDeadlineOutlivesTheWriteTimeout(t *testing.T) {
  20. srv := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
  21. setLongWriteDeadline(w, 2*time.Second)
  22. time.Sleep(300 * time.Millisecond)
  23. writeJSON(w, http.StatusOK, map[string]any{"ok": true})
  24. }))
  25. srv.Config.WriteTimeout = 100 * time.Millisecond
  26. srv.Start()
  27. defer srv.Close()
  28. res, err := http.Get(srv.URL)
  29. if err != nil {
  30. t.Fatalf("el cliente no recibió respuesta: %v", err)
  31. }
  32. defer res.Body.Close()
  33. body, _ := io.ReadAll(res.Body)
  34. if res.StatusCode != http.StatusOK || !bytes.Contains(body, []byte(`"ok":true`)) {
  35. t.Fatalf("respuesta = %d %q, quiero 200 con ok:true", res.StatusCode, body)
  36. }
  37. }
  38. // Gate negativo: el mismo handler sin extender el deadline pierde la respuesta.
  39. // Si este test deja de fallar, el positivo no demuestra nada.
  40. func TestWithoutLongWriteDeadlineTheResponseIsLost(t *testing.T) {
  41. srv := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
  42. time.Sleep(300 * time.Millisecond)
  43. writeJSON(w, http.StatusOK, map[string]any{"ok": true})
  44. }))
  45. srv.Config.WriteTimeout = 100 * time.Millisecond
  46. srv.Start()
  47. defer srv.Close()
  48. res, err := http.Get(srv.URL)
  49. if err == nil {
  50. defer res.Body.Close()
  51. body, _ := io.ReadAll(res.Body)
  52. if bytes.Contains(body, []byte(`"ok":true`)) {
  53. t.Fatalf("llegó la respuesta sin extender el deadline: el gate no prueba nada")
  54. }
  55. }
  56. }
  57. func TestWriteCeilingsExceedTheGlobalTimeout(t *testing.T) {
  58. ceilings := map[string]time.Duration{
  59. "reinstall": reinstallWriteCeiling,
  60. "uninstall": uninstallWriteCeiling,
  61. "rearm": rearmWriteCeiling,
  62. "integration install": integrationWriteCeiling,
  63. "config backup": configBackupWriteCeiling,
  64. "device action": deviceActionWriteCeiling,
  65. "reemit (1 agente)": reemitWriteCeiling(1),
  66. }
  67. for name, d := range ceilings {
  68. if d <= globalWriteTimeout {
  69. t.Errorf("%s: techo %s, el WriteTimeout global es %s", name, d, globalWriteTimeout)
  70. }
  71. }
  72. }
  73. func TestReemitWriteCeilingScalesWithTheFleetAndCaps(t *testing.T) {
  74. // Sin agentes sigue habiendo un margen mínimo.
  75. if got := reemitWriteCeiling(0); got <= globalWriteTimeout {
  76. t.Errorf("0 agentes: %s", got)
  77. }
  78. one, many := reemitWriteCeiling(1), reemitWriteCeiling(6)
  79. if many <= one {
  80. t.Fatalf("6 agentes (%s) debe dar más margen que 1 (%s)", many, one)
  81. }
  82. if got := reemitWriteCeiling(1000); got != reemitMaxCeiling {
  83. t.Errorf("1000 agentes: %s, quiero el tope %s", got, reemitMaxCeiling)
  84. }
  85. }