Debian Codegraph @

tests/testthat/test-cancel.R

raw
1 test_that("Cancelling callbacks", {
2 # Cancel with zero delay
3 x <- 0
4 cancel <- later(function() {
5 x <<- x + 1
6 })
7 later(function() {
8 x <<- x + 2
9 })
10 expect_true(cancel())
11 run_now()
12 expect_identical(x, 2)
13
14 # Cancel with zero delay
15 x <- 0
16 cancel <- later(
17 function() {
18 x <<- x + 1
19 },
20 1
21 )
22 run_now(0.25)
23 expect_true(cancel())
24 run_now(1)
25 expect_identical(x, 0)
26
27 # Make sure a cancelled callback doesn't interfere with others
28 x <- 0
29 later(
30 function() {
31 x <<- x + 1
32 },
33 1
34 )
35 cancel <- later(
36 function() {
37 x <<- x + 2
38 },
39 0.5
40 )
41 run_now()
42 expect_true(cancel())
43 run_now(2)
44 expect_identical(x, 1)
45 })
46
47
48 test_that("Cancelled functions will be GC'd", {
49 x <- 0
50 cancel <- later(
51 local({
52 reg.finalizer(environment(), function(e) x <<- x + 1)
53 function() message("foo")
54 })
55 )
56 expect_true(cancel())
57 gc()
58 expect_identical(x, 1)
59 })
60
61
62 test_that("Cancelling executed or cancelled callbacks has no effect", {
63 # Cancelling an executed callback
64 x <- 0
65 cancel <- later(function() {
66 x <<- x + 1
67 })
68 run_now()
69 expect_false(cancel())
70 run_now()
71 expect_identical(x, 1)
72
73 # Cancelling twice
74 x <- 0
75 cancel <- later(function() {
76 x <<- x + 1
77 })
78 expect_true(cancel())
79 expect_false(cancel())
80 run_now()
81 expect_identical(x, 0)
82 })
83
84
85 test_that("Cancelling callbacks on temporary event loops", {
86 with_temp_loop({
87 # Cancelling an executed callback
88 x <- 0
89 cancel <- later(function() {
90 x <<- x + 1
91 })
92 run_now()
93 expect_false(cancel())
94 run_now()
95 expect_identical(x, 1)
96 })
97
98 with_temp_loop({
99 # Cancelling twice
100 x <- 0
101 cancel <- later(function() {
102 x <<- x + 1
103 })
104 expect_true(cancel())
105 expect_false(cancel())
106 run_now()
107 expect_identical(x, 0)
108 })
109
110 with_temp_loop({
111 # Make sure a cancelled callback doesn't interfere with others
112 x <- 0
113 later(
114 function() {
115 x <<- x + 1
116 },
117 1
118 )
119 cancel <- later(
120 function() {
121 x <<- x + 2
122 },
123 0.5
124 )
125 run_now()
126 expect_true(cancel())
127 run_now(2)
128 expect_identical(x, 1)
129 })
130
131 # Canceling after an event loop handle has been destroyed: the underlying
132 # data structure (in C++) will be deleted, along with the callbacks. This is
133 # true because the loop does not have a parent.
134 cancel <- NULL
135 x <- 0
136 with_temp_loop({
137 cancel <- later(function() {
138 x <<- x + 1
139 })
140 })
141 expect_false(cancel())
142 expect_identical(x, 0)
143 })
144
145
146 test_that("Cancelling callbacks on persistent private loops without parent", {
147 l1 <- create_loop(parent = NULL)
148 l2 <- create_loop(parent = NULL)
149
150 # Cancel from outside with_loop
151 cancel <- NULL
152 x <- 0
153 with_loop(l1, {
154 cancel <- later(function() {
155 x <<- x + 1
156 })
157 })
158 expect_true(cancel())
159 expect_false(cancel())
160 with_loop(l1, run_now())
161 expect_false(cancel())
162 expect_identical(x, 0)
163
164 # Make sure it doesn't interfere with other event loops
165 with_loop(l1, {
166 cancel <- later(function() {
167 x <<- x + 1
168 })
169 })
170 with_loop(l2, {
171 later(function() {
172 x <<- x + 2
173 })
174 })
175 later(function() {
176 x <<- x + 4
177 })
178 expect_true(cancel())
179 with_loop(l1, run_now())
180 with_loop(l2, run_now())
181 run_now()
182 expect_identical(x, 6)
183
184 # Cancelling on an explicitly destroyed loop returns FALSE
185 l3 <- create_loop(parent = NULL)
186 cancel <- NULL
187 x <- 0
188 with_loop(l3, {
189 cancel <- later(function() {
190 x <<- x + 1
191 })
192 })
193 destroy_loop(l3)
194 expect_false(cancel())
195 expect_identical(x, 0)
196 })
197
198 test_that("Cancelling callbacks on persistent private loops with parent", {
199 # If the loop handle is GC'd but the loop _does have_ a parent, then the
200 # underlying objects will not be destroyed right away, so the cancel() will
201 # work.
202 cancel <- NULL
203 x <- 0
204 local({
205 l1 <- create_loop(parent = current_loop())
206 cancel <<- later(
207 function() {
208 x <<- x + 1
209 },
210 loop = l1
211 )
212 })
213 expect_true(cancel())
214 expect_false(cancel())
215 expect_identical(x, 0)
216 })
217
218 test_that("A canceler will not keep loop alive", {
219 l <- create_loop(parent = NULL)
220 finalized <- FALSE
221
222 reg.finalizer(l, function(x) finalized <<- TRUE)
223 cancel <- later(function() 1, loop = l)
224 rm(l)
225 gc()
226 expect_true(finalized)
227 })
228
229 test_that("Canceling a callback from another a callback", {
230 # Canceling a callback from another callback should work. Additionally, the
231 # altered ordering of callbacks shouldn't prevent other callbacks from
232 # running. In this test, #1 cancels 2 and 3, but we still expect 4 to run. If
233 # we used the wrong algorithm for traversing the queue and canceling
234 # callbacks, it would be possible for the cancellation of 2 and 3 to cause 4
235 # to not run. This test ensures that we do it the right way.
236 ran_2 <- FALSE
237 ran_3 <- FALSE
238 ran_4 <- FALSE
239 with_temp_loop({
240 cancel_1 <- later(function() {
241 cancel_2()
242 cancel_3()
243 })
244 cancel_2 <- later(function() {
245 ran_2 <<- TRUE
246 })
247 cancel_3 <- later(function() {
248 ran_3 <<- TRUE
249 })
250 later(function() {
251 ran_4 <<- TRUE
252 })
253 run_now()
254 })
255
256 expect_false(ran_2)
257 expect_false(ran_3)
258 expect_true(ran_4)
259 })