Debian Codegraph @

tests/testthat/test-run_now.R

raw
1 jitter <- 0.017 * 2 # Compensate for imprecision in system timer
2
3 test_that("run_now waits and returns FALSE if no tasks", {
4 x <- system.time({
5 result <- run_now(0.5)
6 })
7 expect_gte(as.numeric(x[["elapsed"]]), 0.5 - jitter)
8 expect_identical(result, FALSE)
9
10 x <- system.time({
11 result <- run_now(3)
12 })
13 expect_gte(as.numeric(x[["elapsed"]]), 3 - jitter)
14 expect_identical(result, FALSE)
15 })
16
17 test_that("run_now returns immediately after executing a task", {
18 x <- system.time({
19 later(~ {}, 0)
20 result <- run_now(2)
21 })
22 expect_lt(as.numeric(x[["elapsed"]]), 0.25)
23 expect_identical(result, TRUE)
24 })
25
26 test_that("run_now executes all scheduled tasks, not just one", {
27 later(~ {}, 0)
28 later(~ {}, 0)
29 result1 <- run_now()
30 result2 <- run_now()
31 expect_identical(result1, TRUE)
32 expect_identical(result2, FALSE)
33 })
34
35 test_that("run_now executes just one scheduled task, if requested", {
36 result1 <- run_now()
37 expect_identical(result1, FALSE)
38
39 later(~ {}, 0)
40 later(~ {}, 0)
41
42 result2 <- run_now(all = FALSE)
43 expect_identical(result2, TRUE)
44
45 result3 <- run_now(all = FALSE)
46 expect_identical(result3, TRUE)
47
48 result4 <- run_now()
49 expect_identical(result4, FALSE)
50 })
51
52 test_that("run_now doesn't go past a failed task", {
53 later(~ stop("boom"), 0)
54 later(~ {}, 0)
55 expect_snapshot(error = TRUE, run_now())
56 expect_true(run_now())
57 })
58
59 test_that("run_now wakes up when a background thread calls later()", {
60 # Skip due to false positives on UBSAN
61 skip_if(using_ubsan())
62
63 env <- new.env()
64 Rcpp::sourceCpp(system.file("bgtest.cpp", package = "later"), env = env)
65 # The background task sleeps
66 env$launchBgTask(1)
67
68 x <- system.time({
69 result <- run_now(3)
70 })
71 # Wait for up to 1.5 seconds (for slow systems)
72 expect_lt(as.numeric(x[["elapsed"]]), 1.5)
73 expect_true(result)
74 })
75
76 test_that("When callbacks have tied timestamps, they respect order of creation", {
77 # Skip due to false positives on UBSAN
78 skip_if(using_ubsan())
79
80 expect_snapshot(testCallbackOrdering())
81
82 Rcpp::sourceCpp(
83 code = '
84 #include <Rcpp.h>
85 #include <later_api.h>
86
87 void* max_seen = 0;
88
89 void callback(void* data) {
90 if (data < max_seen) {
91 Rcpp::stop("Bad ordering detected");
92 }
93 max_seen = data;
94 }
95
96 // [[Rcpp::depends(later)]]
97 // [[Rcpp::export]]
98 void checkLaterOrdering() {
99 max_seen = 0;
100 for (size_t i = 0; i < 10000; i++) {
101 later::later(callback, (void*)i, 0);
102 }
103 }
104 '
105 )
106 checkLaterOrdering()
107 while (!loop_empty()) {
108 run_now(0.1)
109 }
110 })
111
112
113 test_that("Callbacks cannot affect the caller", {
114 # This is based on a pattern used in the callCC function. Normally, simply
115 # touching `throw` will cause the expression to be evaluated and f() to return
116 # early. (This test does not involve later.)
117 f <- function() {
118 delayedAssign("throw", return(100))
119 g <- function() {
120 throw
121 }
122 g()
123 return(200)
124 }
125 expect_equal(f(), 100)
126
127 # When later runs callbacks, it wraps the call in R_ToplevelExec(), which
128 # creates a boundary on the call stack that the early return can't cross.
129 f <- function() {
130 delayedAssign("throw", return(100))
131 later(function() {
132 throw
133 })
134
135 run_now(1)
136 return(200)
137 }
138 # jcheng 2024-10-24: Apparently this works now, maybe because having
139 # RCPP_USING_UNWIND_PROTECT means we don't need to use R_ToplevelExec to call
140 # callbacks?
141 # expect_error(f())
142 expect_identical(f(), 100)
143
144 # In this case, f() should return normally, and then when g() causes later to
145 # run the callback with `throw`, it should be an error -- there's no function
146 # to return from because it (f()) already returned.
147 f <- function() {
148 delayedAssign("throw", return(100))
149 later(function() {
150 throw
151 })
152 return(200)
153 }
154 g <- function() {
155 run_now(2)
156 }
157 expect_equal(f(), 200)
158 expect_snapshot(error = TRUE, g())
159 })
160
161
162 test_that("interrupt and exception handling, R", {
163 # =======================================================
164 # Errors and interrupts in R callbacks
165 # =======================================================
166
167 # R error
168 error_obj <- FALSE
169 tryCatch(
170 {
171 later(function() {
172 stop("oopsie")
173 })
174 run_now()
175 },
176 error = function(e) {
177 error_obj <<- e
178 }
179 )
180 expect_true(grepl("oopsie", error_obj$message))
181
182 # interrupt
183 interrupted <- FALSE
184 tryCatch(
185 {
186 later(function() {
187 rlang::interrupt()
188 Sys.sleep(100)
189 })
190 run_now()
191 },
192 interrupt = function(e) {
193 interrupted <<- TRUE
194 }
195 )
196 expect_true(interrupted)
197 })
198
199 test_that("interrupt and exception handling, C++", {
200 # Skip as cpp_error(4) test seen producing error on some platforms on rhub
201 skip_on_cran()
202 # Skip due to false positives on UBSAN
203 skip_if(using_ubsan())
204 # Skip on Windows i386 because of known bad behavior
205 if (R.version$os == "mingw32" && R.version$arch == "i386") {
206 skip("C++ exceptions in later callbacks are known bad on Windows i386")
207 }
208
209 # =======================================================
210 # Exceptions in C++ callbacks
211 # =======================================================
212
213 # In these tests, in C++, later schedules a C++ callback in which an
214 # exception is thrown or interrupt occurs.
215 #
216 # Some of these callbacks in turn call R functions.
217
218 Rcpp::cppFunction(
219 depends = "later",
220 includes = '
221 #include <later_api.h>
222 #include <stdio.h>
223 #include <sys/types.h>
224 #include <unistd.h>
225 #include <signal.h>
226
227 void oof(void* data) {
228 int* v = (int*)data;
229 int value = *v;
230 delete v;
231
232 if (value == 1) {
233 throw std::runtime_error("This is a C++ exception.");
234
235 } else if (value == 2) {
236 // Throw an arbitrary object
237 throw std::string();
238
239 } else if (value == 3) {
240 // Interrupt the interpreter
241 Rf_onintr();
242 } else if (value == 4) {
243 // Calls R function via Rcpp, which interrupts.
244 // sleeps.
245 Function("r_interrupt")();
246
247 } else if (value == 5) {
248 // Calls R function via Rcpp which calls stop().
249 Function("r_error")();
250
251 } else if (value == 6) {
252 // Calls the `r_error` function via R\'s C API instead of Rcpp.
253 SEXP e;
254 PROTECT(e = Rf_lang1(Rf_install("r_error")));
255
256 Rf_eval(e, R_GlobalEnv);
257
258 UNPROTECT(1);
259 }
260 }
261 ',
262 code = '
263 void cpp_error(int value) {
264 int* v = new int(value);
265 later::later(oof, v, 0);
266 }
267 '
268 )
269
270 # cpp_error() searches in the global environment for these R functions, so we
271 # need to define them there.
272 .GlobalEnv$r_interrupt <- function() {
273 rlang::interrupt()
274 }
275 .GlobalEnv$r_error <- function() {
276 stop("oopsie")
277 }
278 on.exit(rm(r_interrupt, r_error, envir = .GlobalEnv), add = TRUE)
279
280 errored <- FALSE
281 tryCatch(
282 {
283 cpp_error(1)
284 run_now(Inf)
285 },
286 error = function(e) errored <<- TRUE
287 )
288 expect_true(errored)
289
290 errored <- FALSE
291 tryCatch(
292 {
293 cpp_error(2)
294 run_now(-1)
295 },
296 error = function(e) errored <<- TRUE
297 )
298 expect_true(errored)
299
300 errored <- FALSE
301 tryCatch(
302 {
303 cpp_error(5)
304 run_now()
305 },
306 error = function(e) errored <<- TRUE
307 )
308 expect_true(errored)
309
310 errored <- FALSE
311 tryCatch(
312 {
313 cpp_error(6)
314 run_now()
315 },
316 error = function(e) errored <<- TRUE
317 )
318 expect_true(errored)
319
320 interrupted <- FALSE
321 tryCatch(
322 {
323 cpp_error(3)
324 run_now()
325 },
326 interrupt = function(e) interrupted <<- TRUE
327 )
328 expect_true(interrupted)
329
330 interrupted <- FALSE
331 tryCatch(
332 {
333 cpp_error(4)
334 run_now()
335 },
336 interrupt = function(e) interrupted <<- TRUE
337 )
338 expect_true(interrupted)
339 })