diff options
| author | Murilo Ijanc <[email protected]> | 2026-07-26 12:42:37 -0300 |
|---|---|---|
| committer | Murilo Ijanc <[email protected]> | 2026-07-26 12:42:37 -0300 |
| commit | 11d49bc25eb1643f1b742f9527212d8781f2e0de (patch) | |
| tree | bae1cb090cbf6c9f82ab5abbaf1194206f92f539 /tests/test-retry.R | |
| download | r-11d49bc25eb1643f1b742f9527212d8781f2e0de.tar.gz | |
Initial importv0.1.0
Diffstat (limited to 'tests/test-retry.R')
| -rw-r--r-- | tests/test-retry.R | 121 |
1 files changed, 121 insertions, 0 deletions
diff --git a/tests/test-retry.R b/tests/test-retry.R new file mode 100644 index 0000000..bb93d4e --- /dev/null +++ b/tests/test-retry.R @@ -0,0 +1,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) + } +) |