Skip to content

Commit

Permalink
#102 fix for stream with Async and AsyncVaried, should be working now
Browse files Browse the repository at this point in the history
though behavior has changed a bit
  • Loading branch information
sckott committed Feb 9, 2019
1 parent 235ae59 commit f4d745a
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 21 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Roxygen: list(markdown = TRUE)
Encoding: UTF-8
Language: en-US
Imports:
curl (<= 3.2),
curl (>= 3.3),
R6 (>= 2.2.0),
urltools (>= 1.6.0),
httpcode (>= 0.2.0),
Expand Down
6 changes: 4 additions & 2 deletions R/asyncvaried.R
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,13 @@ AsyncVaried <- R6::R6Class(
)
} else if (is.null(w$disk) && !is.null(w$stream)) {
stopifnot(is.function(w$stream))
# assign empty response since stream is a user supplied function to write
# somewhere of their choosing
multi_res[[i]] <<- make_async_error("", w)
curl::multi_add(
handle = h,
done = function(res) multi_res[[i]] <<- res,
done = w$stream,
fail = function(res) multi_res[[i]] <<- make_async_error(res, w),
data = w$stream,
pool = crulpool
)
}
Expand Down
8 changes: 4 additions & 4 deletions R/writing-options.R
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
#' close(file(g))
#'
#' ### stream - to console
#' fun <- function(x) cat(rawToChar(x))
#' fun <- function(x) print(x)
#' req1 <- HttpRequest$new(url = "https://httpbin.org/get"
#' )$get(query = list(foo = "bar"), stream = fun)
#' req2 <- HttpRequest$new(url = "https://httpbin.org/get"
Expand All @@ -56,15 +56,15 @@
#' out$content()
#'
#' ### stream - to an R object
#' lst <- c()
#' fun <- function(x) lst <<- c(lst, x)
#' lst <- list()
#' fun <- function(x) lst <<- append(lst, list(x))
#' req1 <- HttpRequest$new(url = "https://httpbin.org/get"
#' )$get(query = list(foo = "bar"), stream = fun)
#' req2 <- HttpRequest$new(url = "https://httpbin.org/get"
#' )$get(query = list(hello = "world"), stream = fun)
#' (out <- AsyncVaried$new(req1, req2))
#' out$request()
#' lst
#' cat(rawToChar(lst))
#' cat(vapply(lst, function(z) rawToChar(z$content), ""), sep = "\n")
#' }
NULL
8 changes: 4 additions & 4 deletions man/writing-options.Rd

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

20 changes: 13 additions & 7 deletions tests/testthat/test-async.R
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,8 @@ test_that("Async - streaming to disk works", {
bb <- Async$new(urls = c(hb('/get?a=5'),
hb('/get?b=6'),
hb('/get?c=7')))
mylist <- c()
fun <- function(x) mylist <<- c(mylist, x)
lst <- c()
fun <- function(x) lst <<- append(lst, list(x))
out <- bb$get(stream = fun)

expect_is(bb, "Async")
Expand All @@ -337,8 +337,10 @@ test_that("Async - streaming to disk works", {
expect_identical(out[[2]]$content, raw(0))
expect_identical(out[[3]]$content, raw(0))

expect_is(mylist, "raw")
expect_is(rawToChar(mylist), "character")
expect_is(lst, "list")
expect_is(rawToChar(lst[[1]]$content), "character")
expect_is(rawToChar(lst[[2]]$content), "character")
expect_is(rawToChar(lst[[3]]$content), "character")
})


Expand Down Expand Up @@ -432,7 +434,7 @@ test_that("Async - failure behavior", {
skip_on_cran()

mylist <- c()
fun <- function(x) mylist <<- c(mylist, x)
fun <- function(x) mylist <<- append(mylist, list(x))

urls <- c("http://stuffthings.gvb", "https://foo.com", "https://scottchamberlain.info")
conn <- Async$new(urls = urls)
Expand All @@ -444,13 +446,17 @@ test_that("Async - failure behavior", {
expect_is(res[[2]], "HttpResponse")
expect_is(res[[3]], "HttpResponse")

# this doesn't mean anything really since we give a templated repsonse with
# status_code of 0
expect_equal(res[[1]]$status_code, 0)
expect_equal(res[[2]]$status_code, 0)
expect_equal(res[[3]]$status_code, 200)
expect_equal(res[[3]]$status_code, 0)

# this doesn't mean anything really since we give a templated repsonse with
# status_code of 0
expect_false(res[[1]]$success())
expect_false(res[[2]]$success())
expect_true(res[[3]]$success())
expect_false(res[[3]]$success())

# when fails on async, has the error message
expect_match(res[[1]]$parse("UTF-8"), "resolve host")
Expand Down
9 changes: 6 additions & 3 deletions tests/testthat/test-asyncvaried.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ test_that("AsyncVaried - streaming to disk works", {
skip_on_cran()

lst <- c()
fun <- function(x) lst <<- c(lst, x)
fun <- function(x) lst <<- append(lst, list(x))
req1 <- HttpRequest$new(url = hb("/get")
)$get(query = list(foo = "bar"), stream = fun)
req2 <- HttpRequest$new(url = hb("/get")
Expand All @@ -119,8 +119,11 @@ test_that("AsyncVaried - streaming to disk works", {
expect_identical(out$responses()[[1]]$content, raw(0))
expect_identical(out$responses()[[2]]$content, raw(0))

expect_is(lst, "raw")
expect_is(rawToChar(lst), "character")
expect_is(lst, "list")
expect_is(lst[[1]], "list")
expect_is(lst[[2]], "list")
expect_is(rawToChar(lst[[1]]$content), "character")
expect_is(rawToChar(lst[[2]]$content), "character")
})


Expand Down

0 comments on commit f4d745a

Please sign in to comment.