Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test server improvements #689

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Suggests:
jsonlite,
knitr,
later (>= 1.4.0),
nanonext,
paws.common,
promises,
rmarkdown,
Expand Down
5 changes: 4 additions & 1 deletion R/resp-stream.R
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,10 @@
# again, they'll get the same error rather than reading the stream
# having missed a bunch of bytes.
resp$cache$push_back <- buffer
cli::cli_abort("Streaming read exceeded size limit of {max_size}")
cli::cli_abort(
"Streaming read exceeded size limit of {max_size}",
class = "httr2_streaming_error"
)

Check warning on line 312 in R/resp-stream.R

View check run for this annotation

Codecov / codecov/patch

R/resp-stream.R#L309-L312

Added lines #L309 - L312 were not covered by tests
}

# We didn't have enough data. Attempt to read more
Expand Down
45 changes: 45 additions & 0 deletions tests/testthat/helper-sync.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
sync_req <- function(name = "default", .env = parent.frame()) {
skip_on_cran()
skip_if_not_installed("nanonext")

connected <- FALSE
cv <- nanonext::cv()
sock <- nanonext::socket("req")
withr::defer(nanonext::reap(sock), envir = .env)
nanonext::pipe_notify(sock, cv, add = TRUE)
nanonext::listen(sock, url = sprintf("ipc:///tmp/nanonext%s", name))

function(expr = {}, timeout = 1000L) {
if (!connected) {
nanonext::until_(cv, timeout) || stop("req connect: timed out")
connected <<- TRUE
}
ctx <- nanonext::.context(sock)
saio <- nanonext::send_aio(ctx, 0L, mode = 2L)
expr
r <- nanonext::recv_aio(ctx, mode = 8L, timeout = timeout)[]
nanonext::is_error_value(r) && stop("req sync: ", nanonext::nng_error(r))
}

}

sync_rep <- function(name = "default", .env = parent.frame()) {
connected <- FALSE
cv <- nanonext::cv()
sock <- nanonext::socket("rep")
nanonext::pipe_notify(sock, cv, add = TRUE)
withr::defer(nanonext::reap(sock), envir = .env)
nanonext::dial(sock, url = sprintf("ipc:///tmp/nanonext%s", name))

function(expr = {}, timeout = 1000L) {
if (!connected) {
nanonext::until_(cv, timeout) || stop("resp connect: timed out")
connected <<- TRUE
}
ctx <- nanonext::.context(sock)
r <- nanonext::recv_aio(ctx, mode = 8L, timeout = timeout)[]
nanonext::is_error_value(r) && stop("resp sync: ", nanonext::nng_error(r))
expr
nanonext::send(ctx, 0L, mode = 2L, block = TRUE)
}
}
8 changes: 8 additions & 0 deletions tests/testthat/helper-webfakes.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@ local_app_request <- function(fun, method = "get", frame = parent.frame()) {
}

app <- webfakes::new_app()

app$locals$i <- 0
app$use(function(req, res) {
app$locals$i <- app$locals$i + 1
"next"
})

app[[method]]("/test", fun)
app$locals$sync_rep <- sync_rep
server <- webfakes::local_app_process(app, .local_envir = frame)

req <- request(server$url("/test"))
Expand Down
4 changes: 1 addition & 3 deletions tests/testthat/test-req-perform-connection.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ test_that("reads body on error", {

test_that("can retry a transient error", {
req <- local_app_request(function(req, res) {
i <- res$app$locals$i %||% 1
if (i == 1) {
res$app$locals$i <- 2
if (res$app$locals$i == 1) {
res$
set_status(429)$
set_header("retry-after", 0)$
Expand Down
8 changes: 2 additions & 6 deletions tests/testthat/test-req-perform-parallel.R
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,7 @@ test_that("requests are throttled", {

test_that("can retry an OAuth failure", {
req <- local_app_request(function(req, res) {
i <- res$app$locals$i %||% 1
if (i == 1) {
res$app$locals$i <- 2
if (res$app$locals$i == 1) {
res$
set_status(401)$
set_header("WWW-Authenticate", 'Bearer realm="example", error="invalid_token"')$
Expand Down Expand Up @@ -206,9 +204,7 @@ test_that("but multiple failures causes an error", {

test_that("can retry a transient error", {
req <- local_app_request(function(req, res) {
i <- res$app$locals$i %||% 1
if (i == 1) {
res$app$locals$i <- 2
if (res$app$locals$i == 1) {
res$
set_status(429)$
set_header("retry-after", 2)$
Expand Down
4 changes: 1 addition & 3 deletions tests/testthat/test-req-perform.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ test_that("don't retry curl errors by default", {

test_that("can retry a transient error", {
req <- local_app_request(function(req, res) {
i <- res$app$locals$i %||% 1
if (i == 1) {
res$app$locals$i <- 2
if (res$app$locals$i == 1) {
res$
set_status(429)$
set_header("retry-after", 0)$
Expand Down
Loading
Loading