Debian Codegraph @

tests/testthat/test-legacy-domains.R

raw
1 async_true <- function() {
2 promise_resolve(TRUE)
3 }
4
5 describe("Promise domains", {
6 it("are reentered during handlers", {
7 cd <- create_counting_domain(trackFinally = TRUE)
8 p <- with_promise_domain(cd, {
9 async_true() %...>%
10 {
11 expect_identical(cd$counts$onFulfilledCalled, 1L)
12 expect_identical(cd$counts$onFulfilledActive, 1L)
13 10 # sync result
14 } %...>%
15 {
16 expect_identical(cd$counts$onFulfilledCalled, 2L)
17 expect_identical(cd$counts$onFulfilledActive, 1L)
18 promise_resolve(20) # async result
19 }
20 })
21
22 expect_identical(cd$counts$onFulfilledBound, 2L)
23
24 p <- p %...>%
25 {
26 expect_identical(cd$counts$onFulfilledCalled, 2L)
27 expect_identical(cd$counts$onFulfilledActive, 0L)
28 }
29
30 expect_identical(cd$counts$onFulfilledBound, 2L)
31 p %>% wait_for_it()
32
33 with_promise_domain(cd, {
34 p <- async_true() %>%
35 finally(
36 ~ {
37 expect_identical(cd$counts$onFinallyCalled, 1L)
38 expect_identical(cd$counts$onFinallyActive, 1L)
39 }
40 )
41 expect_identical(cd$counts$onFinallyBound, 1L)
42
43 expect_identical(cd$counts$onFulfilledBound, 2L)
44 expect_identical(cd$counts$onRejectedBound, 0L)
45
46 p %>% wait_for_it()
47 })
48
49 expect_identical(cd$counts$onFulfilledBound, 2L)
50
51 with_promise_domain(cd, {
52 p <- async_true() %...>%
53 {
54 expect_identical(., TRUE)
55 expect_identical(cd$counts$onFulfilledCalled, 3L)
56 ten <- 10
57 # This tests if promise domain membership infects subscriptions made
58 # in handlers.
59 promise_resolve(invisible(ten)) %...>%
60 (function(value, .visible) {
61 expect_identical(value, 10)
62 expect_false(.visible)
63 expect_true(!is.null(current_promise_domain()))
64 expect_identical(cd$counts$onFulfilledCalled, 4L)
65 })
66 }
67 })
68
69 expect_true(is.null(current_promise_domain()))
70 expect_identical(cd$counts$onFulfilledCalled, 2L)
71
72 p %>% wait_for_it()
73 })
74
75 it("pass finally binding to fulfill/reject by default", {
76 cd1 <- create_counting_domain(trackFinally = FALSE)
77
78 with_promise_domain(cd1, {
79 p1 <- async_true() %>%
80 finally(
81 ~ {
82 expect_identical(cd1$counts$onFulfilledActive, 1L)
83 expect_identical(cd1$counts$onRejectedActive, 0L)
84 }
85 )
86 expect_identical(cd1$counts$onFulfilledBound, 1L)
87 expect_identical(cd1$counts$onRejectedBound, 1L)
88 p1 %>% wait_for_it()
89 expect_identical(cd1$counts$onFulfilledCalled, 1L)
90 expect_identical(cd1$counts$onRejectedCalled, 0L)
91 })
92
93 cd2 <- create_counting_domain(trackFinally = FALSE)
94
95 p2 <- with_promise_domain(cd2, {
96 promise_reject("a problem") %>%
97 finally(
98 ~ {
99 expect_identical(cd2$counts$onFulfilledActive, 0L)
100 expect_identical(cd2$counts$onRejectedActive, 1L)
101 }
102 )
103 }) %>%
104 squelch_unhandled_promise_error()
105
106 expect_identical(cd2$counts$onFulfilledBound, 1L)
107 expect_identical(cd2$counts$onRejectedBound, 1L)
108 p2 %>% wait_for_it()
109 expect_identical(cd2$counts$onFulfilledCalled, 0L)
110 expect_identical(cd2$counts$onRejectedCalled, 1L)
111 })
112
113 it("doesn't intercept fulfill/reject on finally, if finally is explicitly intercepted", {
114 cd1 <- create_counting_domain(trackFinally = TRUE)
115
116 with_promise_domain(cd1, {
117 p1 <- async_true() %>%
118 finally(
119 ~ {
120 expect_identical(cd1$counts$onFinallyActive, 1L)
121 expect_identical(cd1$counts$onFulfilledActive, 0L)
122 expect_identical(cd1$counts$onRejectedActive, 0L)
123 }
124 )
125 expect_identical(cd1$counts$onFinallyBound, 1L)
126 expect_identical(cd1$counts$onFulfilledBound, 0L)
127 expect_identical(cd1$counts$onRejectedBound, 0L)
128 p1 %>% wait_for_it()
129 expect_identical(cd1$counts$onFinallyCalled, 1L)
130 expect_identical(cd1$counts$onFulfilledCalled, 0L)
131 expect_identical(cd1$counts$onRejectedCalled, 0L)
132 })
133
134 cd2 <- create_counting_domain(trackFinally = TRUE)
135
136 p2 <- with_promise_domain(cd2, {
137 promise_reject(TRUE) %>%
138 finally(
139 ~ {
140 expect_identical(cd2$counts$onFinallyActive, 1L)
141 expect_identical(cd2$counts$onFulfilledActive, 0L)
142 expect_identical(cd2$counts$onRejectedActive, 0L)
143 }
144 )
145 }) %>%
146 squelch_unhandled_promise_error()
147
148 expect_identical(cd2$counts$onFinallyBound, 1L)
149 expect_identical(cd2$counts$onFulfilledBound, 0L)
150 expect_identical(cd2$counts$onRejectedBound, 0L)
151 p2 %>% wait_for_it()
152 expect_identical(cd2$counts$onFinallyCalled, 1L)
153 expect_identical(cd2$counts$onFulfilledCalled, 0L)
154 expect_identical(cd2$counts$onRejectedCalled, 0L)
155 })
156
157 it("handles weird edge case relating to symbols", {
158 # This test resulted from a bug in wrap_callback_reenter() where do.call()'s
159 # default behavior of quote=FALSE would cause a resolved value that happens
160 # to be a symbol, to be evaluated. This would only happen when a promise
161 # domain was in effect, and the symbol was passed to an onFulfilled. Fixed
162 # by using rlang::exec() instead of do.call().
163 cd <- create_counting_domain()
164 with_promise_domain(cd, {
165 promise_resolve(as.symbol("foo")) %...>%
166 {
167 expect_identical(., as.symbol("foo"))
168 } %>%
169 wait_for_it()
170 })
171 })
172
173 it("executes wrap_callback_reenter handlers in the right lexical environment", {
174 # No reason this shouldn't work, I haven't seen it fail, just making sure.
175 cd <- create_counting_domain()
176 x <- NULL
177 with_promise_domain(cd, {
178 promise_resolve(NULL) %...>%
179 {
180 x <<- 1L
181 } %...>%
182 {
183 x <<- x + 1L
184 } %>%
185 wait_for_it()
186 })
187 expect_identical(x, 2L)
188 })
189
190 it("doesn't grow the call stack", {
191 # See https://github.com/rstudio/promises/issues/114
192 # and also https://github.com/jcheng5/shinychat/issues/16
193
194 recursive_promise <- function(n, .last_callstack_depth = NULL) {
195 if (n == 0) {
196 return(0)
197 } else {
198 promise_resolve(TRUE) %...>%
199 {
200 current_callstack_depth <- length(sys.calls())
201 if (!is.null(.last_callstack_depth)) {
202 expect_identical(current_callstack_depth, .last_callstack_depth)
203 }
204
205 recursive_promise(
206 n - 1,
207 .last_callstack_depth = current_callstack_depth
208 )
209 }
210 }
211 }
212
213 cd <- create_counting_domain()
214 with_promise_domain(cd, {
215 recursive_promise(5) %...>%
216 {
217 # 5 (from inside recursive_promise) + 1 (for the current handler)
218 expect_identical(cd$counts$onFulfilledCalled, 6L)
219 } %>%
220 wait_for_it()
221 })
222
223 cd2 <- create_counting_domain()
224 p <- recursive_promise(5)
225 with_promise_domain(cd2, {
226 p %...>%
227 {
228 # This time, none of the ones inside recursive_promise count, since
229 # they were bound outside of the influence of cd2 (even though they
230 # are resolved within the influence of cd2, thanks to wait_for_it()).
231 expect_identical(cd2$counts$onFulfilledCalled, 1L)
232 } %>%
233 wait_for_it()
234 })
235 })
236 })