Debian Codegraph @

tests/testthat/test-blockopen.R

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 read_text <- function(x){
2 while (isIncomplete(x)) {
3 Sys.sleep(0.1)
4 txt <- readLines(x)
5 if(length(txt))
6 return(txt)
7 }
8 }
9
10 read_bin <- function(x){
11 while (isIncomplete(x)) {
12 Sys.sleep(0.1)
13 bin <- readBin(x, raw(), 100)
14 if(length(bin))
15 return(bin)
16 }
17 }
18
19 expect_immediate <- function(...){
20 expect_true(system.time(...)['elapsed'] < 0.5)
21 }
22
23 test_that("Non-blocking open does not block", {
24
25 # Get a regular string
26 con <- curl(httpbin("delay/1"))
27 expect_immediate(open(con, "rs", blocking = FALSE))
28 expect_immediate(readLines(con))
29 close(con)
30 })
31
32 test_that("Error handling for non-blocking open", {
33
34 # Get a regular string
35 con <- curl(httpbin("get"))
36 expect_immediate(open(con, "rs", blocking = FALSE))
37 expect_is(read_text(con), "character")
38 close(con)
39
40 # Test error during read text
41 h <- new_handle()
42 con <- curl(httpbin("status/418"), handle = h)
43 expect_immediate(open(con, "rs", blocking = FALSE))
44 expect_is(read_text(con), "character")
45 expect_equal(handle_data(h)$status_code, 418)
46 close(con)
47
48 # Test error during read binary
49 h <- new_handle()
50 con <- curl(httpbin("status/418"), handle = h)
51 expect_immediate(open(con, "rbs", blocking = FALSE))
52 expect_is(read_bin(con), "raw")
53 expect_equal(handle_data(h)$status_code, 418)
54 close(con)
55
56 # DNS error
57 con <- curl("http://this.is.invalid.co.za")
58 expect_immediate(open(con, "rs", blocking = FALSE))
59 expect_error(read_text(con), "resolv", ignore.case = TRUE) # matches "resolve" or "resolving"
60 close(con)
61
62 # Non existing host
63 con <- curl("http://240.0.0.1")
64 expect_immediate(open(con, "rs", blocking = FALSE))
65 expect_error(read_text(con))
66 close(con)
67
68 # Invalid port
69 con <- curl("http://8.8.8.8:666")
70 expect_immediate(open(con, "rs", blocking = FALSE))
71 expect_error(read_text(con))
72 close(con)
73 })