Debian Codegraph @

tests/testthat/test-later-fd.R

raw
1 test_that("later_fd", {
2 skip_if_not_installed("nanonext")
3
4 result <- NULL
5 callback <- function(x) result <<- x
6 s1 <- nanonext::socket(listen = "inproc://nanonext")
7 on.exit(close(s1))
8 s2 <- nanonext::socket(dial = "inproc://nanonext")
9 on.exit(close(s2), add = TRUE)
10 fd1 <- nanonext::opt(s1, "recv-fd")
11 fd2 <- nanonext::opt(s2, "recv-fd")
12
13 # timeout
14 later_fd(callback, c(fd1, fd2), timeout = 0)
15 run_now(1)
16 expect_equal(result, c(FALSE, FALSE))
17 later_fd(callback, c(fd1, fd2), exceptfds = c(fd1, fd2), timeout = 0)
18 run_now(1)
19 expect_equal(result, c(FALSE, FALSE, FALSE, FALSE))
20
21 # cancellation
22 result <- NULL
23 cancel <- later_fd(callback, c(fd1, fd2), timeout = 0.2)
24 expect_type(cancel, "closure")
25 expect_true(cancel())
26 Sys.sleep(0.25)
27 expect_false(cancel())
28 expect_invisible(cancel())
29 run_now()
30 expect_null(result)
31
32 # timeout (> 1 loop)
33 later_fd(callback, c(fd1, fd2), timeout = 1.1)
34 run_now(1.3)
35 expect_equal(result, c(FALSE, FALSE))
36
37 # fd1 ready
38 later_fd(callback, c(fd1, fd2), timeout = 0.9)
39 res <- nanonext::send(s2, "msg")
40 run_now(1)
41 expect_equal(result, c(TRUE, FALSE))
42
43 # both fd1, fd2 ready
44 res <- nanonext::send(s1, "msg")
45 Sys.sleep(0.1)
46 later_fd(callback, c(fd1, fd2), timeout = 1)
47 run_now(1)
48 expect_equal(result, c(TRUE, TRUE))
49
50 # no exceptions
51 later_fd(callback, c(fd1, fd2), exceptfds = c(fd1, fd2), timeout = -0.1)
52 run_now(1)
53 expect_equal(result, c(TRUE, TRUE, FALSE, FALSE))
54
55 # fd2 ready
56 res <- nanonext::recv(s1)
57 later_fd(callback, c(fd1, fd2), timeout = 1L)
58 run_now(1)
59 expect_equal(result, c(FALSE, TRUE))
60
61 # fd2 invalid
62 res <- nanonext::recv(s2)
63 later_fd(callback, c(fd1, fd2), exceptfds = c(fd1, fd2), timeout = 0.1)
64 close(s2)
65 run_now(1)
66 expect_length(result, 4L)
67
68 # both fd1, fd2 invalid
69 close(s1)
70 later_fd(callback, c(fd1, fd2), c(fd1, fd2), timeout = 0)
71 run_now(1)
72 expect_equal(result, c(NA, NA, NA, NA))
73
74 # no fds supplied
75 later_fd(callback, timeout = -1)
76 run_now(1)
77 expect_equal(result, logical())
78
79 on.exit()
80 })
81
82 test_that("loop_empty() reflects later_fd callbacks", {
83 skip_if_not_installed("nanonext")
84
85 s1 <- nanonext::socket(listen = "inproc://nanotest2")
86 on.exit(close(s1))
87 s2 <- nanonext::socket(dial = "inproc://nanotest2")
88 on.exit(close(s2), add = TRUE)
89
90 fd1 <- nanonext::opt(s1, "recv-fd")
91
92 expect_true(loop_empty())
93
94 cancel <- later_fd(~ {}, fd1)
95 expect_false(loop_empty())
96 cancel()
97 Sys.sleep(1.25) # check for cancellation happens every ~1 sec
98 expect_true(loop_empty())
99
100 later_fd(~ {}, fd1, timeout = 0)
101 expect_false(loop_empty())
102 run_now(1)
103 expect_true(loop_empty())
104 })
105
106 test_that("later_fd() errors when passed destroyed loops", {
107 loop <- create_loop()
108 destroy_loop(loop)
109 expect_snapshot(error = TRUE, later_fd(identity, loop = loop))
110 })
111
112 test_that("later_fd C API works", {
113 skip_if(using_ubsan())
114 env <- new.env()
115 Rcpp::cppFunction(
116 depends = 'later',
117 includes = '
118 #include <later_api.h>
119 void func(int *value, void *data) {}
120 ',
121 code = '
122 int testfd() {
123 later::later_fd(func, nullptr, 0, nullptr, 0.0, 0);
124 return 0;
125 }
126 ',
127 env = env
128 )
129 expect_equal(env$testfd(), 0L)
130 run_now()
131 })