summaryrefslogtreecommitdiffstats
path: root/tests/test-retry.R
blob: bb93d4e3395a5008fac448931867cc4372d627f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
library(cepis)
source(file.path("helper", "utils.R"))

with_server(
  list(route(
    "/01001000",
    response(500L, json(list(erro = "interno"))),
    response(200L, json(SE_ADDRESS))
  )),
  function(client, server) {
    address <- cep_lookup("01001000", client)
    check("a 500 is retried and the second attempt wins", identical(address$uf, "SP"))
    check("it took exactly two attempts", calls(server, "/01001000") == 2L)
  }
)

with_server(
  list(route("/01001000", response(503L, json(list(erro = "interno"))))),
  function(client, server) {
    condition <- check_condition(
      "a 5xx on every attempt signals cepis_server_error",
      cep_lookup("01001000", client),
      "cepis_server_error"
    )
    check("it exposes the status", identical(condition$status, 503L))
    check("three attempts is the documented default", calls(server, "/01001000") == 3L)
  }
)

with_server(
  list(route("/01001000", response(500L, "{}"))),
  function(client, server) {
    single <- cepis_client(base_url = server$url, timeout = 5, max_attempts = 1L)
    check_condition(
      "max_attempts = 1 disables retrying",
      cep_lookup("01001000", single),
      "cepis_server_error"
    )
    check("only one request was made", calls(server, "/01001000") == 1L)
  }
)

with_server(
  list(route("/01001000", response(429L, "{}", list("Retry-After" = "7")))),
  function(client, server) {
    condition <- check_condition(
      "a 429 signals cepis_rate_limited",
      cep_lookup("01001000", client),
      "cepis_rate_limited"
    )
    check("it exposes retry_after in seconds", isTRUE(all.equal(condition$retry_after, 7)))
  }
)

with_server(
  list(route("/01001000", response(429L, "{}", list("Retry-After" = "3600")))),
  function(client, server) {
    started <- proc.time()[["elapsed"]]
    check_condition(
      "an absurd Retry-After still signals cepis_rate_limited",
      cep_lookup("01001000", client),
      "cepis_rate_limited"
    )
    check(
      "backoff_max caps it, so a bad proxy cannot park the session",
      proc.time()[["elapsed"]] - started < 5
    )
  }
)

with_server(
  list(route(
    "/01001000",
    response(429L, "{}", list("Retry-After" = "Wed, 21 Oct 2026 07:28:00 GMT"))
  )),
  function(client, server) {
    condition <- check_condition(
      "Retry-After as an HTTP date is understood",
      cep_lookup("01001000", client),
      "cepis_rate_limited"
    )
    check(
      "the date became a positive number of seconds",
      is.numeric(condition$retry_after) && condition$retry_after > 0
    )
  }
)

refused <- cepis_client(
  base_url = "http://127.0.0.1:1",
  timeout = 1,
  max_attempts = 2L,
  backoff_base = 0.001
)
check_condition(
  "a refused connection signals cepis_network_error",
  cep_lookup("01001000", refused),
  "cepis_network_error"
)

with_server(
  list(route("/01001000", response(500L, "{}"))),
  function(client, server) {
    bounded <- cepis_client(
      base_url = server$url,
      timeout = 5,
      max_attempts = 50L,
      backoff_base = 0.05,
      backoff_max = 0.05,
      total_timeout = 0.3
    )
    started <- proc.time()[["elapsed"]]
    check_condition(
      "total_timeout still ends in cepis_server_error",
      cep_lookup("01001000", bounded),
      "cepis_server_error"
    )
    check("it bounded the whole call", proc.time()[["elapsed"]] - started < 5)
    check("it cut the run short of the 50 attempts", calls(server, "/01001000") < 50L)
  }
)