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
|
test_that("curl fds", {
skip_on_cran()
resp <- list()
errm <- character()
done <- function(x) resp <<- c(resp, list(x))
fail <- function(x) errm <<- c(errm, x)
pool <- curl::new_pool()
url1 <- httpbin$url("/status/200")
url2 <- httpbin$url("/delay/1")
curl::multi_add(pool = pool, curl::new_handle(url = url1, http_version = 2),
done = done, fail = fail)
curl::multi_add(pool = pool, curl::new_handle(url = url1, http_version = 2),
done = done, fail = fail)
curl::multi_add(pool = pool, curl::new_handle(url = url2, http_version = 2),
done = done, fail = fail)
curl::multi_add(pool = pool, curl::new_handle(url = url1, http_version = 2),
done = done, fail = fail)
curl::multi_add(pool = pool, curl::new_handle(url = url1, http_version = 2),
done = done, fail = fail)
# This does not do much, but at least it tests that we can poll()
# libcurl's file descriptors
timeout <- Sys.time() + 5
repeat {
fds <- curl::multi_fdset(pool = pool)
if (length(fds$reads) > 0) {
pr <- poll(list(curl_fds(fds)), 1000)
}
state <- curl::multi_run(timeout = 0.1, pool = pool, poll = TRUE)
if (state$pending == 0 || Sys.time() >= timeout) break;
}
expect_true(Sys.time() < timeout)
expect_equal(vapply(resp, "[[", "", "url"), c(rep(url1, 4), url2))
})
test_that("curl fds before others", {
skip_on_cran()
pool <- curl::new_pool()
url <- httpbin$url("/delay/1")
curl::multi_add(pool = pool, curl::new_handle(url = url, http_version = 2))
timeout <- Sys.time() + 5
repeat {
state <- curl::multi_run(timeout = 1/10000, pool = pool, poll = TRUE)
fds <- curl::multi_fdset(pool = pool)
if (length(fds$reads) > 0) break;
if (Sys.time() >= timeout) break;
}
expect_true(Sys.time() < timeout)
px <- get_tool("px")
pp <- process$new(get_tool("px"), c("sleep", "10"))
on.exit(pp$kill(), add = TRUE)
pr <- poll(list(pp, curl_fds(fds)), 10000)
expect_equal(
pr,
list(c(output = "nopipe", error = "nopipe", process = "silent"),
"event")
)
pp$kill()
})
test_that("process fd before curl fd", {
skip_on_cran()
pool <- curl::new_pool()
url <- httpbin$url("/delay/1")
curl::multi_add(pool = pool, curl::new_handle(url = url, http_version = 2))
timeout <- Sys.time() + 5
repeat {
state <- curl::multi_run(timeout = 1/10000, pool = pool, poll = TRUE)
fds <- curl::multi_fdset(pool = pool)
if (length(fds$reads) > 0) break;
if (Sys.time() >= timeout) break;
}
expect_true(Sys.time() < timeout)
px <- get_tool("px")
pp <- process$new(get_tool("px"), c("outln", "done"))
on.exit(pp$kill(), add = TRUE)
pr <- poll(list(pp, curl_fds(fds)), 10000)
expect_equal(
pr,
list(c(output = "nopipe", error = "nopipe", process = "ready"),
"silent")
)
pp$kill()
})
|