Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Feb 24, 2025
1 parent 8a23dd3 commit d9d7478
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 21 deletions.
15 changes: 5 additions & 10 deletions R/req-perform-parallel.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
#'
#' Additionally, it does not respect the `max_tries` argument to `req_retry()`
#' because if you have five requests in flight and the first one gets rate
#' limited, it's likely that all the others do too.
#' limited, it's likely that all the others do too. This also means that
#' the circuit breaker is never triggered.
#'
#' @inherit req_perform_sequential params return
#' @param pool `r lifecycle::badge("deprecated")`. No longer supported;
Expand Down Expand Up @@ -253,20 +254,14 @@ RequestQueue <- R6::R6Class(
},

submit_next = function(deadline) {
next_i <- which(self$status == "pending")[[1]]
i <- which(self$status == "pending")[[1]]

self$token_deadline <- throttle_deadline(self$reqs[[next_i]])
self$token_deadline <- throttle_deadline(self$reqs[[i]])
if (self$token_deadline > unix_time()) {
throttle_return_token(self$reqs[[next_i]])
throttle_return_token(self$reqs[[i]])

Check warning on line 261 in R/req-perform-parallel.R

View check run for this annotation

Codecov / codecov/patch

R/req-perform-parallel.R#L261

Added line #L261 was not covered by tests
return(FALSE)
}

self$submit(next_i)
},

submit = function(i) {
retry_check_breaker(self$reqs[[i]], self$tries[[i]], error_call = error_call)

self$set_status(i, "active")
self$resps[i] <- list(NULL)
self$tries[[i]] <- self$tries[[i]] + 1
Expand Down
3 changes: 2 additions & 1 deletion man/req_perform_parallel.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 23 additions & 10 deletions tests/testthat/test-req-perform-parallel.R
Original file line number Diff line number Diff line change
Expand Up @@ -227,37 +227,50 @@ test_that("can retry a transient error", {

queue <- RequestQueue$new(list(req), progress = FALSE)

# Start processing
# submit the request
expect_null(queue$process1())
expect_equal(queue$queue_status, "working")
expect_equal(queue$n_active, 1)
expect_equal(queue$n_pending, 0)
expect_equal(queue$status[[1]], "active")

expect_null(queue$process1())

# Now we process the request and capture the retry
# process the response and capture the retry
expect_null(queue$process1())
expect_equal(queue$queue_status, "waiting")
expect_equal(queue$rate_limit_deadline, mock_time + 2)
expect_equal(queue$n_pending, 1)
expect_s3_class(queue$resps[[1]], "httr2_http_429")
expect_equal(resp_body_json(queue$resps[[1]]$resp), list(status = "waiting"))

# Now we "wait" 2 seconds
# Starting waiting
expect_null(queue$process1())
expect_equal(queue$queue_status, "working")
expect_equal(queue$queue_status, "waiting")
expect_equal(mock_time, 3)

# Now we go back to working
# Finishing waiting
expect_null(queue$process1())
expect_equal(queue$queue_status, "working")
expect_equal(queue$n_active, 0)
expect_equal(queue$n_pending, 1)

# And we're finally done
# Resubmit
expect_null(queue$process1())
expect_equal(queue$queue_status, "done")
expect_false(queue$process1())
expect_equal(queue$queue_status, "working")
expect_equal(queue$n_active, 1)
expect_equal(queue$n_pending, 0)

# Process the response
expect_null(queue$process1())
expect_equal(queue$queue_status, "working")
expect_equal(queue$n_active, 0)
expect_equal(queue$n_pending, 0)
expect_s3_class(queue$resps[[1]], "httr2_response")
expect_equal(resp_body_json(queue$resps[[1]]), list(status = "done"))

# So we're finally done
expect_null(queue$process1())
expect_equal(queue$queue_status, "done")
expect_false(queue$process1())
})

test_that("throttling is limited by deadline", {
Expand Down

0 comments on commit d9d7478

Please sign in to comment.