| 1 |
test_that("Timeout works", { |
| 2 |
h1 <- new_handle(url = httpbin("delay/10")) |
| 3 |
h2 <- new_handle(url = httpbin("post"), postfields = "bla bla") |
| 4 |
h3 <- new_handle(url = "https://urldoesnotexist.xyz", connecttimeout = 1) |
| 5 |
h4 <- new_handle(url = "http://localhost:14", connecttimeout = 1) |
| 6 |
m <- new_pool() |
| 7 |
multi_add(h1, pool = m) |
| 8 |
multi_add(h2, pool = m) |
| 9 |
multi_add(h3, pool = m) |
| 10 |
multi_add(h4, pool = m) |
| 11 |
rm(h1, h2, h3, h4) |
| 12 |
gc() |
| 13 |
spent <- system.time(out <- multi_run(timeout = 2, pool = m)) |
| 14 |
expect_lte(out$pending, 2) |
| 15 |
expect_lte(spent[['elapsed']], 4) |
| 16 |
}) |
| 17 |
|
| 18 |
test_that("Callbacks work", { |
| 19 |
total = 0; |
| 20 |
h1 <- new_handle(url = httpbin("get")) |
| 21 |
multi_add(h1, done = function(...){ |
| 22 |
total <<- total + 1 |
| 23 |
multi_add(h1, done = function(...){ |
| 24 |
total <<- total + 1 |
| 25 |
}) |
| 26 |
}) |
| 27 |
gc() # test that callback functions are protected |
| 28 |
out <- multi_run() |
| 29 |
expect_equal(out$pending, 0) |
| 30 |
expect_equal(out$success + out$error, 2) |
| 31 |
expect_equal(total, 2) |
| 32 |
}) |
| 33 |
|
| 34 |
test_that("Multi cancel works", { |
| 35 |
expect_length(multi_list(), 0) |
| 36 |
h1 <- new_handle(url = httpbin("get")) |
| 37 |
multi_add(h1) |
| 38 |
expect_length(multi_list(), 1) |
| 39 |
expect_error(multi_add(h1), "locked") |
| 40 |
expect_equal(multi_run(timeout = 0), list(success = 0, error = 0, pending = 1)) |
| 41 |
expect_length(multi_list(), 1) |
| 42 |
expect_is(multi_cancel(h1), "curl_handle") |
| 43 |
expect_length(multi_list(), 0) |
| 44 |
expect_is(multi_add(h1), "curl_handle") |
| 45 |
expect_length(multi_list(), 1) |
| 46 |
expect_equal(multi_run(), list(success = 1, error = 0, pending = 0)) |
| 47 |
expect_length(multi_list(), 0) |
| 48 |
}) |
| 49 |
|
| 50 |
test_that("Errors in Callbacks", { |
| 51 |
pool <- new_pool() |
| 52 |
cb <- function(req){ |
| 53 |
stop("testerror in callback!") |
| 54 |
} |
| 55 |
curl_fetch_multi(httpbin("get"), pool = pool, done = cb) |
| 56 |
curl_fetch_multi(httpbin("status/404"), pool = pool, done = cb) |
| 57 |
curl_fetch_multi("https://urldoesnotexist.xyz", pool = pool, fail = cb) |
| 58 |
gc() |
| 59 |
expect_equal(total_handles(), 3) |
| 60 |
expect_error(multi_run(pool = pool), "testerror") |
| 61 |
gc() |
| 62 |
expect_equal(total_handles(), 2) |
| 63 |
expect_error(multi_run(pool = pool), "testerror") |
| 64 |
gc() |
| 65 |
expect_equal(total_handles(), 1) |
| 66 |
expect_error(multi_run(pool = pool), "testerror") |
| 67 |
gc() |
| 68 |
expect_equal(total_handles(), 0) |
| 69 |
expect_equal(multi_run(pool = pool), list(success = 0, error = 0, pending = 0)) |
| 70 |
}) |
| 71 |
|
| 72 |
test_that("Data callback", { |
| 73 |
con <- rawConnection(raw(0), "r+") |
| 74 |
on.exit(close(con)) |
| 75 |
hx <- new_handle() |
| 76 |
handle_setopt(hx, COPYPOSTFIELDS = jsonlite::toJSON(mtcars)); |
| 77 |
handle_setheaders(hx, "Content-Type" = "application/json") |
| 78 |
status <- NULL |
| 79 |
curl_fetch_multi(httpbin("post"), done = function(res){ |
| 80 |
status <<- res$status_code |
| 81 |
}, fail = stop, data = con, handle = hx) |
| 82 |
|
| 83 |
rawheaders <- NULL |
| 84 |
buffer <- raw() |
| 85 |
hx <- new_handle(accept_encoding = 'identity') |
| 86 |
curl_fetch_multi(httpbin("get"), handle = hx, done = function(res){ |
| 87 |
rawheaders <<- res$headers |
| 88 |
#this somehow breaks the gc |
| 89 |
#expect_equal(res$status_code, 200) |
| 90 |
}, fail = stop, data = function(x, finalize = FALSE){ |
| 91 |
buffer <<- c(buffer, x) |
| 92 |
# also breaks gc. Looks like circular protect caused by testthat? |
| 93 |
# expect_is(x, "raw") |
| 94 |
}) |
| 95 |
|
| 96 |
# test that callback functions are protected |
| 97 |
gc() |
| 98 |
|
| 99 |
# perform requests |
| 100 |
out <- multi_run() |
| 101 |
expect_equal(out$success, 2) |
| 102 |
expect_equal(status, 200) |
| 103 |
|
| 104 |
# output from callback functions |
| 105 |
content_len <- curl::parse_headers_list(rawheaders)[['content-length']] |
| 106 |
expect_length(buffer, as.numeric(content_len)) |
| 107 |
|
| 108 |
# get data from buffer |
| 109 |
content <- rawConnectionValue(con) |
| 110 |
output <- jsonlite::fromJSON(rawToChar(content)) |
| 111 |
expect_is(output$json, "data.frame") |
| 112 |
expect_equal(sort(names(output$json)), sort(names(mtcars))) |
| 113 |
}) |
| 114 |
|
| 115 |
test_that("callback protection", { |
| 116 |
done <- function(res){ |
| 117 |
expect_is(res$status_code, "integer") |
| 118 |
} |
| 119 |
fail <- function(...){ |
| 120 |
print("error") |
| 121 |
} |
| 122 |
data <- function(x, final){ |
| 123 |
expect_is(x, "raw") |
| 124 |
} |
| 125 |
pool <- new_pool() |
| 126 |
handle <- new_handle(url = httpbin("get")) |
| 127 |
multi_add(handle, done = done, fail = fail, data = data, pool = pool) |
| 128 |
rm(handle, done, fail, data) |
| 129 |
gc(); gc(); |
| 130 |
out <- multi_run(pool = pool) |
| 131 |
expect_equal(out$success, 1) |
| 132 |
}) |
| 133 |
|
| 134 |
test_that("total_con and multi_fdset", { |
| 135 |
skip_on_os("solaris") |
| 136 |
skip_if_not(strsplit(curl_version()$version, "-")[[1]][1] >= as.numeric_version("7.30"), |
| 137 |
"libcurl does not support host_connections") |
| 138 |
total_con <- 4 |
| 139 |
pool <- new_pool(total_con = total_con, multiplex = FALSE) |
| 140 |
for (i in c(4, 3, 2, 1, 0, 1, 2, 3, 4)) { |
| 141 |
h1 <- new_handle(url = httpbin(paste0("delay/", i))) |
| 142 |
multi_add(h1, done = force, fail = cat, pool = pool) |
| 143 |
} |
| 144 |
while(length(multi_list(pool = pool))){ |
| 145 |
res <- multi_run(pool = pool, poll = 1) |
| 146 |
fdset <- multi_fdset(pool = pool) |
| 147 |
expect_lte(length(c(fdset$reads, fdset$writes)), min(total_con, res$pending) +1) |
| 148 |
} |
| 149 |
}) |
| 150 |
|
| 151 |
test_that("host_con and multi_fdset", { |
| 152 |
skip_on_os("solaris") |
| 153 |
skip_if_not(strsplit(curl_version()$version, "-")[[1]][1] >= as.numeric_version("7.30"), |
| 154 |
"libcurl does not support host_connections") |
| 155 |
host_con <- 4 |
| 156 |
pool <- new_pool(host_con = host_con, multiplex = FALSE) |
| 157 |
for (i in c(4, 3, 2, 1, 0, 1, 2, 3, 4)) { |
| 158 |
h1 <- new_handle(url = httpbin(paste0("delay/", i))) |
| 159 |
multi_add(h1, done = force, fail = cat, pool = pool) |
| 160 |
} |
| 161 |
while(length(multi_list(pool = pool))){ |
| 162 |
res <- multi_run(pool = pool, poll = 1) |
| 163 |
fdset <- multi_fdset(pool = pool) |
| 164 |
expect_lte(length(c(fdset$reads, fdset$writes)), min(host_con, res$pending) +1) |
| 165 |
} |
| 166 |
}) |
| 167 |
|
| 168 |
test_that("GC works", { |
| 169 |
gc() |
| 170 |
expect_equal(total_handles(), 0L) |
| 171 |
}) |