Debian Codegraph @

tests/testthat/test-nonblocking.R

raw
1 test_that("Non blocking connections ", {
2 h <- new_handle()
3 con <- curl(httpbin("drip?duration=3&numbytes=50&code=200"), handle = h)
4 expect_equal(handle_data(h)$status_code, 0L)
5 open(con, "rb", blocking = FALSE)
6 expect_equal(handle_data(h)$status_code, 200L)
7 n <- 0
8 while(isIncomplete(con)){
9 Sys.sleep(0.01)
10 buf <- readBin(con, raw(), 5)
11 n <- n + length(buf)
12 }
13 expect_equal(n, 50L)
14 rm(h)
15 close(con)
16 gc()
17 expect_equal(total_handles(), 0L)
18 })
19
20 test_that("Non blocking readline", {
21 con <- curl(httpbin("stream/71"))
22 open(con, "r", blocking = FALSE)
23 n <- 0
24 while(isIncomplete(con)){
25 buf <- readLines(con, 5)
26 n <- n + length(buf)
27 }
28 expect_equal(n, 71L)
29 close(con)
30 gc()
31 expect_equal(total_handles(), 0L)
32 })
33
34 test_that("isIncomplete for blocking connections", {
35 con <- curl(httpbin("stream/71"))
36 expect_false(isIncomplete(con))
37 expect_equal(length(readLines(con)), 71L)
38 expect_false(isIncomplete(con))
39 open(con)
40 expect_true(isIncomplete(con))
41 n <- 0
42 while(isIncomplete(con)){
43 buf <- readLines(con, 5)
44 n <- n + length(buf)
45 }
46 expect_equal(n, 71L)
47 close(con)
48 gc()
49 expect_equal(total_handles(), 0L)
50 })
51
52 test_that("Small buffers", {
53 con <- curl(httpbin(""))
54 expect_false(isIncomplete(con = con))
55 open(con)
56 on.exit(close(con), add = TRUE)
57 expect_true(isIncomplete(con = con))
58 readLines(con, 1)
59 expect_true(isIncomplete(con = con))
60 readLines(con, warn = FALSE)
61 expect_false(isIncomplete(con = con))
62 })