Debian Codegraph @

tests/testthat/test-private-loops.R

raw
1 test_that("Private event loop changes current_loop()", {
2 expect_identical(current_loop(), global_loop())
3
4 with_temp_loop({
5 expect_false(identical(current_loop(), global_loop()))
6 })
7 })
8
9 test_that("Private event loop runs only its own tasks", {
10 x <- 0
11 later(
12 ~ {
13 x <<- 1
14 },
15 0
16 )
17 with_temp_loop({
18 expect_true(loop_empty())
19
20 later(
21 ~ {
22 x <<- 2
23 }
24 )
25 run_now()
26
27 expect_identical(x, 2)
28
29 run_now(loop = global_loop())
30 expect_identical(x, 1)
31 })
32 })
33
34 test_that("Private event loops", {
35 l <- create_loop(parent = NULL)
36 x <- 0
37
38 expect_true(exists_loop(l))
39
40 with_loop(l, {
41 later(function() x <<- x + 1)
42 run_now()
43 })
44 expect_equal(x, 1)
45
46 with_loop(l, {
47 later(function() x <<- x + 1)
48 run_now()
49
50 later(function() x <<- x + 1)
51 later(function() x <<- x + 1)
52 })
53 expect_equal(x, 2)
54
55 with_loop(l, run_now())
56 expect_equal(x, 4)
57
58 destroy_loop(l)
59 expect_false(exists_loop(l))
60
61 # Can't run later-y things with destroyed loop
62 expect_snapshot(error = TRUE, with_loop(l, later(function() message("foo"))))
63 expect_snapshot(error = TRUE, run_now(loop = l))
64
65 # GC with functions in destroyed loops, even if callback isn't executed.
66 l <- create_loop(parent = NULL)
67 x <- 0
68 gc()
69 with_loop(l, {
70 later(
71 local({
72 reg.finalizer(environment(), function(e) x <<- x + 1)
73 function() message("foo")
74 })
75 )
76 })
77 gc()
78 expect_identical(x, 0)
79
80 destroy_loop(l)
81 gc()
82 expect_identical(x, 1)
83
84 # A GC'd loop object will cause its queue to be deleted, which will allow GC
85 # of any resources
86 l <- create_loop(parent = NULL)
87 x <- 0
88 gc()
89 with_loop(l, {
90 later(
91 local({
92 reg.finalizer(environment(), function(e) x <<- x + 1)
93 function() message("foo")
94 })
95 )
96 })
97 gc()
98 expect_identical(x, 0)
99
100 # Delete the reference to the loop, and GC. This causes the queue to be
101 # deleted, which removes references to items in the queue. However, the items
102 # in the queue won't be GC'd yet. (At least not as of R 3.5.2.)
103 rm(l)
104 gc()
105 expect_identical(x, 0)
106
107 # A second GC triggers the finalizer for an item that was in the queue.
108 gc()
109 expect_identical(x, 1)
110
111 # Can't destroy global loop
112 expect_snapshot(error = TRUE, destroy_loop(global_loop()))
113 })
114
115
116 test_that("Temporary event loops", {
117 l <- NULL
118 x <- 0
119 with_temp_loop({
120 l <- current_loop()
121 later(function() x <<- x + 1)
122 run_now()
123 })
124
125 expect_false(exists_loop(l))
126 expect_snapshot(
127 error = TRUE,
128 with_loop(l, {
129 later(function() x <<- x + 1)
130 run_now()
131 })
132 )
133
134 # Test GC
135 # Make sure that items captured in later callbacks are GC'd after the callback
136 # is executed.
137 x <- 0
138 with_temp_loop({
139 later(
140 local({
141 reg.finalizer(environment(), function(e) x <<- x + 1)
142 function() 1
143 })
144 )
145 gc()
146 run_now()
147 })
148 expect_identical(x, 0)
149 gc()
150 expect_identical(x, 1)
151
152 # Test that objects are GC'd after loop is destroyed, even if callback hasn't
153 # been executed.
154 x <- 0
155 with_temp_loop({
156 later(
157 local({
158 reg.finalizer(environment(), function(e) x <<- x + 1)
159 function() 1
160 })
161 )
162 run_now()
163
164 later(
165 local({
166 e <- environment()
167 reg.finalizer(environment(), function(e) x <<- x + 1)
168 function() 1
169 })
170 )
171 gc()
172 })
173 expect_identical(x, 1)
174 gc()
175 expect_identical(x, 2)
176 })
177
178 test_that("Destroying loop and loop ID", {
179 l <- create_loop()
180 expect_true(is.integer(l$id))
181 expect_true(destroy_loop(l))
182 expect_false(exists_loop(l))
183
184 # Should return false on subsequent calls to destroy_loop()
185 expect_false(destroy_loop(l))
186 # Destroying a second time shouldn't cause warnings.
187 expect_silent(destroy_loop(l))
188 })
189
190 test_that("Can't destroy current loop", {
191 errored <- FALSE
192 with_temp_loop({
193 later(function() {
194 # We can't do expect_error in a later() callback, so use a tryCatch
195 # instead to check that an error occurs.
196 tryCatch(
197 destroy_loop(current_loop()),
198 error = function(e) {
199 errored <<- TRUE
200 }
201 )
202 })
203 run_now()
204 })
205
206 expect_true(errored)
207 })
208
209 test_that("Can't GC current loop", {
210 collected <- FALSE
211 l <- create_loop()
212 reg.finalizer(l, function(x) {
213 collected <<- TRUE
214 })
215 with_loop(l, {
216 rm(l, inherits = TRUE)
217 gc()
218 gc()
219 })
220 expect_false(collected)
221 gc()
222 expect_true(collected)
223 })
224
225
226 test_that("When auto-running a child loop, it will be reported as current_loop()", {
227 l <- create_loop(parent = global_loop())
228 x <- NULL
229 later(
230 function() {
231 x <<- current_loop()
232 },
233 loop = l
234 )
235 run_now(loop = global_loop())
236 expect_identical(x, l)
237 })
238
239
240 test_that("CallbackRegistry exists until its callbacks are run", {
241 # If the R loop handle object is GC'd, it doesn't necessarily destroy the
242 # underlying CallbackRegistry (in C++). The CallbackRegistry is only destroyed
243 # when the R loop handle is GC'd AND the CallbackRegistry contains no more
244 # callbacks.
245 x <- 0
246 callback <- function() {
247 x <<- x + 1
248 }
249 local({
250 l <- create_loop()
251 later(callback, loop = l)
252 })
253 gc()
254 run_now()
255 expect_identical(x, 1)
256 })
257
258 test_that("Auto-running grandchildren loops", {
259 l1_ran <- FALSE
260 l11_ran <- FALSE
261 l12_ran <- FALSE
262 l13_ran <- FALSE
263 l2_ran <- FALSE
264 l21_ran <- FALSE
265 l22_ran <- FALSE
266 l23_ran <- FALSE
267
268 l1 <- create_loop()
269 l2 <- create_loop(parent = NULL)
270
271 # l1 should auto-run, along with l11 and l12. l13 should not, because it has
272 # no parent.
273 with_loop(l1, {
274 later(function() l1_ran <<- TRUE)
275 l11 <- create_loop()
276 l12 <- create_loop()
277 l13 <- create_loop(parent = NULL)
278 later(function() l11_ran <<- TRUE, loop = l11)
279 later(function() l12_ran <<- TRUE, loop = l12)
280 later(function() l13_ran <<- TRUE, loop = l13)
281 })
282
283 # None of these should auto-run, because l2 has no parent.
284 with_loop(l2, {
285 later(function() l2_ran <<- TRUE)
286 l21 <- create_loop()
287 l22 <- create_loop()
288 l23 <- create_loop(parent = NULL)
289 later(function() l21_ran <<- TRUE, loop = l21)
290 later(function() l22_ran <<- TRUE, loop = l22)
291 later(function() l23_ran <<- TRUE, loop = l23)
292 })
293
294 run_now()
295 expect_true(l1_ran)
296 expect_true(l11_ran)
297 expect_true(l12_ran)
298 expect_false(l13_ran)
299 expect_false(l2_ran)
300 expect_false(l21_ran)
301 expect_false(l22_ran)
302 expect_false(l23_ran)
303 })
304
305 test_that("Grandchildren loops whose parent is destroyed should not autorun", {
306 l_ran <- 0
307 l1_ran <- 0
308 l <- create_loop()
309
310 with_loop(l, {
311 later(function() l_ran <<- l_ran + 1)
312 l1 <- create_loop()
313 later(function() l1_ran <<- l1_ran + 1, loop = l1)
314 })
315
316 notify_r_ref_deleted(l)
317 run_now()
318 # l will run, because the underlying registry exists until empty. It also
319 # causes l1 to run.
320 expect_identical(l_ran, 1)
321 expect_identical(l1_ran, 1)
322 expect_false(exists_loop(l))
323 # l1 should still exist because we still have a reference to it.
324 expect_true(exists_loop(l1))
325
326 # Schedule another function that we don't expect to actually run.
327 # Use finalizer to keep
328 l1_finalized <- FALSE
329 later(
330 local({
331 reg.finalizer(environment(), function(e) l1_finalized <<- TRUE)
332 function() l1_ran <<- l1_ran + 1
333 }),
334 loop = l1
335 )
336 run_now()
337 # l1 won't run again
338 expect_identical(l1_ran, 1)
339 expect_true(exists_loop(l1))
340 expect_false(l1_finalized)
341 # If the reference is lost (like when the loop handle is GC'd) l1 will take
342 # effect immediately.
343 expect_true(notify_r_ref_deleted(l1))
344 expect_false(exists_loop(l1))
345 gc() # Make the finalizer run
346 expect_true(l1_finalized)
347 })
348
349
350 test_that("Removing parent loop allows loop to be deleted", {
351 # Create parent loop, then create a child loop, then add a finalizer to a
352 # callback (actually, the env for the callback) in the child loop.
353 l <- create_loop()
354 l1 <- create_loop(parent = l)
355
356 x <- 0
357 with_loop(l1, {
358 later(
359 local({
360 reg.finalizer(environment(), function(e) x <<- x + 1)
361 function() NULL
362 })
363 )
364 })
365
366 # Removing the ref to the child should NOT cause the finalizer to run -- the
367 # loop won't actually be destroyed because it (A) has a parent AND (B) has a
368 # callback. notify_r_ref_deleted(l1)
369 rm(l1)
370 gc()
371 gc()
372 expect_identical(x, 0)
373
374 # If we destroy the parent loop, then the finalizer will be called, because
375 # even though the child loop has a callback, it no longer has a parent.
376 # Because both the handle and its parent have been GC'd, there's no way to run
377 # callbacks in the child, so the internal representation of the child loop can
378 # be deleted, along with all the callbacks it contains.
379 rm(l)
380 # Use 2 GC's because the first causes the loops to be GC'd; the second causes
381 # the function that was queued in a loop to be GC'd.
382 gc()
383 gc()
384 expect_identical(x, 1)
385 })
386
387 test_that("Interrupt while running in private loop won't result in stuck loop", {
388 l <- create_loop()
389 later(
390 function() {
391 rlang::interrupt()
392 },
393 loop = l
394 )
395 tryCatch(
396 {
397 run_now(loop = l)
398 },
399 interrupt = function(e) NULL
400 )
401 expect_identical(current_loop(), global_loop())
402
403 tryCatch(
404 {
405 with_loop(l, {
406 rlang::interrupt()
407 })
408 },
409 interrupt = function(e) NULL
410 )
411 expect_identical(current_loop(), global_loop())
412 })
413
414 test_that("list_queue", {
415 l <- create_loop(parent = NULL)
416 q <- NULL
417 f <- function() 1 # A dummy function
418
419 with_loop(l, {
420 later(f)
421 q <- list_queue()
422 })
423 expect_equal(length(q), 1)
424 expect_identical(q[[1]]$callback, f)
425
426 with_loop(l, {
427 run_now()
428 q <- list_queue()
429 })
430 expect_equal(length(q), 0)
431
432 with_loop(l, {
433 later(f)
434 later(f)
435 later(sum)
436 q <- list_queue()
437 })
438 expect_equal(length(q), 3)
439 expect_identical(q[[1]]$callback, f)
440 expect_identical(q[[2]]$callback, f)
441 expect_identical(q[[3]]$callback, sum)
442
443 # Empty the queue by calling run now. Also test calling list_queue by passing
444 # in a loop handle.
445 with_loop(l, run_now())
446 q <- list_queue(l)
447 expect_equal(length(q), 0)
448
449 destroy_loop(l)
450 expect_snapshot(error = TRUE, list_queue(l))
451 })
452
453 test_that("next_op_secs works", {
454 loop <- with_temp_loop({
455 expect_identical(next_op_secs(), Inf)
456
457 later(function() {}, 0)
458 expect_true(next_op_secs() <= 0)
459 run_now()
460
461 later(function() {}, 0.1)
462 expect_true(next_op_secs() <= 0.1)
463 run_now(2)
464
465 current_loop()
466 })
467 expect_snapshot(error = TRUE, next_op_secs(loop))
468 })
469
470 test_that("parameter validation works", {
471 loop <- create_loop(parent = NULL)
472 expect_snapshot(error = TRUE, with_loop(loop, destroy_loop(loop)))
473 expect_true(destroy_loop(loop))
474 expect_false(destroy_loop(loop))
475 expect_snapshot(error = TRUE, with_loop(loop, {}))
476 expect_snapshot(error = TRUE, loop_empty(loop))
477 expect_snapshot(error = TRUE, create_loop(parent = "invalid"))
478 })
479
480 test_that("print.event_loop works correctly", {
481 loop <- create_loop(parent = NULL)
482
483 output <- capture.output(print(loop))
484 expect_match(output, "<event loop> ID: [0-9]+")
485
486 destroy_loop(loop)
487 output_destroyed <- capture.output(print(loop))
488 expect_match(output_destroyed, "\\(destroyed\\)")
489 })
490
491 test_that("esoteric error handlers", {
492 loop <- create_loop(parent = NULL)
493 expect_snapshot(error = TRUE, notify_r_ref_deleted(global_loop()))
494 expect_snapshot(error = TRUE, with_loop(loop, notify_r_ref_deleted(loop)))
495 expect_snapshot(error = TRUE, {
496 with_loop(loop, {
497 .loops[[as.character(loop$id)]] <- NULL
498 current_loop()
499 })
500 })
501 })