File: test-blockopen.R

package info (click to toggle)
r-cran-curl 4.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 740 kB
  • sloc: ansic: 2,674; sh: 11; makefile: 5
file content (75 lines) | stat: -rw-r--r-- 1,912 bytes parent folder | download | duplicates (3)
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
context("Non-blocking opening connection")

read_text <- function(x){
  while (isIncomplete(x)) {
    Sys.sleep(0.1)
    txt <- readLines(x)
    if(length(txt))
      return(txt)
  }
}

read_bin <- function(x){
  while (isIncomplete(x)) {
    Sys.sleep(0.1)
    bin <- readBin(x, raw(), 100)
    if(length(bin))
      return(bin)
  }
}

expect_immediate <- function(...){
  expect_true(system.time(...)['elapsed'] < 0.5)
}

test_that("Non-blocking open does not block", {

  # Get a regular string
  con <- curl(httpbin("delay/1"))
  expect_immediate(open(con, "rs", blocking = FALSE))
  expect_immediate(readLines(con))
  close(con)
})

test_that("Error handling for non-blocking open", {

  # Get a regular string
  con <- curl(httpbin("get"))
  expect_immediate(open(con, "rs", blocking = FALSE))
  expect_is(read_text(con), "character")
  close(con)

  # Test error during read text
  h <- new_handle()
  con <- curl(httpbin("status/418"), handle = h)
  expect_immediate(open(con, "rs", blocking = FALSE))
  expect_is(read_text(con), "character")
  expect_equal(handle_data(h)$status_code, 418)
  close(con)

  # Test error during read binary
  h <- new_handle()
  con <- curl(httpbin("status/418"), handle = h)
  expect_immediate(open(con, "rbs", blocking = FALSE))
  expect_is(read_bin(con), "raw")
  expect_equal(handle_data(h)$status_code, 418)
  close(con)

  # DNS error
  #con <- curl("http://this.is.invalid.co.za")
  #expect_immediate(open(con, "rs", blocking = FALSE))
  #expect_error(read_text(con), "resolv", ignore.case = TRUE) # matches "resolve" or "resolving"
  #close(con)

  # Non existing host
  #con <- curl("http://240.0.0.1")
  #expect_immediate(open(con, "rs", blocking = FALSE))
  #expect_error(read_text(con))
  #close(con)

  # Invalid port
  #con <- curl("http://8.8.8.8:666")
  #expect_immediate(open(con, "rs", blocking = FALSE))
  #expect_error(read_text(con))
  #close(con)
})