Skip to content

Commit 0fb877a

Browse files
committed
Add cancellation test
1 parent 077245d commit 0fb877a

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

tests/testthat/test-cancel.R

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,26 @@ test_that("Cancelling callbacks on persistent private loops", {
148148
expect_false(cancel())
149149
expect_identical(x, 0)
150150
})
151+
152+
test_that("Canceling a callback from another a callback", {
153+
# Canceling a callback from another callback should work. Additionally, the
154+
# altered ordering of callbacks shouldn't prevent other callbacks from
155+
# running. In this test, #1 cancels 2 and 3, but we still expect 4 to run. If
156+
# we used the wrong algorithm for traversing the queue and canceling
157+
# callbacks, it would be possible for the cancellation of 2 and 3 to cause 4
158+
# to not run. This test ensures that we do it the right way.
159+
ran_2 <- FALSE
160+
ran_3 <- FALSE
161+
ran_4 <- FALSE
162+
with_temp_loop({
163+
cancel_1 <- later(function() { cancel_2(); cancel_3() })
164+
cancel_2 <- later(function() { ran_2 <<- TRUE })
165+
cancel_3 <- later(function() { ran_3 <<- TRUE })
166+
later(function() { ran_4 <<- TRUE })
167+
run_now()
168+
})
169+
170+
expect_false(ran_2)
171+
expect_false(ran_3)
172+
expect_true(ran_4)
173+
})

0 commit comments

Comments
 (0)