| 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 |
then(\(x) { |
| 11 |
expect_identical(cd$counts$onFulfilledCalled, 1L) |
| 12 |
expect_identical(cd$counts$onFulfilledActive, 1L) |
| 13 |
10 # sync result |
| 14 |
}) |> |
| 15 |
then(\(x) { |
| 16 |
expect_identical(x, 10) |
| 17 |
expect_identical(cd$counts$onFulfilledCalled, 2L) |
| 18 |
expect_identical(cd$counts$onFulfilledActive, 1L) |
| 19 |
promise_resolve(20) # async result |
| 20 |
}) |
| 21 |
}) |
| 22 |
|
| 23 |
expect_identical(cd$counts$onFulfilledBound, 2L) |
| 24 |
|
| 25 |
p <- p |> |
| 26 |
then(\(x) { |
| 27 |
expect_identical(cd$counts$onFulfilledCalled, 2L) |
| 28 |
expect_identical(cd$counts$onFulfilledActive, 0L) |
| 29 |
}) |
| 30 |
|
| 31 |
expect_identical(cd$counts$onFulfilledBound, 2L) |
| 32 |
p |> wait_for_it() |
| 33 |
|
| 34 |
with_promise_domain(cd, { |
| 35 |
p <- async_true() |> |
| 36 |
finally(\() { |
| 37 |
expect_identical(cd$counts$onFinallyCalled, 1L) |
| 38 |
expect_identical(cd$counts$onFinallyActive, 1L) |
| 39 |
}) |
| 40 |
expect_identical(cd$counts$onFinallyBound, 1L) |
| 41 |
|
| 42 |
expect_identical(cd$counts$onFulfilledBound, 2L) |
| 43 |
expect_identical(cd$counts$onRejectedBound, 0L) |
| 44 |
|
| 45 |
p |> wait_for_it() |
| 46 |
}) |
| 47 |
|
| 48 |
expect_identical(cd$counts$onFulfilledBound, 2L) |
| 49 |
|
| 50 |
with_promise_domain(cd, { |
| 51 |
p <- async_true() |> |
| 52 |
then(\(x) { |
| 53 |
expect_identical(x, TRUE) |
| 54 |
expect_identical(cd$counts$onFulfilledCalled, 3L) |
| 55 |
ten <- 10 |
| 56 |
# This tests if promise domain membership infects subscriptions made |
| 57 |
# in handlers. |
| 58 |
promise_resolve(invisible(ten)) |> |
| 59 |
then(\(value, .visible) { |
| 60 |
expect_identical(value, 10) |
| 61 |
expect_false(.visible) |
| 62 |
expect_true(!is.null(current_promise_domain())) |
| 63 |
expect_identical(cd$counts$onFulfilledCalled, 4L) |
| 64 |
}) |
| 65 |
}) |
| 66 |
}) |
| 67 |
expect_true(is.null(current_promise_domain())) |
| 68 |
expect_identical(cd$counts$onFulfilledCalled, 2L) |
| 69 |
|
| 70 |
p |> wait_for_it() |
| 71 |
}) |
| 72 |
|
| 73 |
it("pass finally binding to fulfill/reject by default", { |
| 74 |
cd1 <- create_counting_domain(trackFinally = FALSE) |
| 75 |
|
| 76 |
with_promise_domain(cd1, { |
| 77 |
p1 <- async_true() |> |
| 78 |
finally(\() { |
| 79 |
expect_identical(cd1$counts$onFulfilledActive, 1L) |
| 80 |
expect_identical(cd1$counts$onRejectedActive, 0L) |
| 81 |
}) |
| 82 |
|
| 83 |
expect_identical(cd1$counts$onFulfilledBound, 1L) |
| 84 |
expect_identical(cd1$counts$onRejectedBound, 1L) |
| 85 |
p1 |> wait_for_it() |
| 86 |
expect_identical(cd1$counts$onFulfilledCalled, 1L) |
| 87 |
expect_identical(cd1$counts$onRejectedCalled, 0L) |
| 88 |
}) |
| 89 |
|
| 90 |
cd2 <- create_counting_domain(trackFinally = FALSE) |
| 91 |
|
| 92 |
p2 <- with_promise_domain(cd2, { |
| 93 |
promise_reject("a problem") |> |
| 94 |
finally(\() { |
| 95 |
expect_identical(cd2$counts$onFulfilledActive, 0L) |
| 96 |
expect_identical(cd2$counts$onRejectedActive, 1L) |
| 97 |
}) |
| 98 |
}) |> |
| 99 |
squelch_unhandled_promise_error() |
| 100 |
|
| 101 |
expect_identical(cd2$counts$onFulfilledBound, 1L) |
| 102 |
expect_identical(cd2$counts$onRejectedBound, 1L) |
| 103 |
p2 |> wait_for_it() |
| 104 |
expect_identical(cd2$counts$onFulfilledCalled, 0L) |
| 105 |
expect_identical(cd2$counts$onRejectedCalled, 1L) |
| 106 |
}) |
| 107 |
|
| 108 |
it("doesn't intercept fulfill/reject on finally, if finally is explicitly intercepted", { |
| 109 |
cd1 <- create_counting_domain(trackFinally = TRUE) |
| 110 |
|
| 111 |
with_promise_domain(cd1, { |
| 112 |
p1 <- async_true() |> |
| 113 |
finally(\() { |
| 114 |
expect_identical(cd1$counts$onFinallyActive, 1L) |
| 115 |
expect_identical(cd1$counts$onFulfilledActive, 0L) |
| 116 |
expect_identical(cd1$counts$onRejectedActive, 0L) |
| 117 |
}) |
| 118 |
|
| 119 |
expect_identical(cd1$counts$onFinallyBound, 1L) |
| 120 |
expect_identical(cd1$counts$onFulfilledBound, 0L) |
| 121 |
expect_identical(cd1$counts$onRejectedBound, 0L) |
| 122 |
p1 |> wait_for_it() |
| 123 |
expect_identical(cd1$counts$onFinallyCalled, 1L) |
| 124 |
expect_identical(cd1$counts$onFulfilledCalled, 0L) |
| 125 |
expect_identical(cd1$counts$onRejectedCalled, 0L) |
| 126 |
}) |
| 127 |
|
| 128 |
cd2 <- create_counting_domain(trackFinally = TRUE) |
| 129 |
|
| 130 |
p2 <- with_promise_domain(cd2, { |
| 131 |
promise_reject(TRUE) |> |
| 132 |
finally(\() { |
| 133 |
expect_identical(cd2$counts$onFinallyActive, 1L) |
| 134 |
expect_identical(cd2$counts$onFulfilledActive, 0L) |
| 135 |
expect_identical(cd2$counts$onRejectedActive, 0L) |
| 136 |
}) |
| 137 |
}) |> |
| 138 |
squelch_unhandled_promise_error() |
| 139 |
|
| 140 |
expect_identical(cd2$counts$onFinallyBound, 1L) |
| 141 |
expect_identical(cd2$counts$onFulfilledBound, 0L) |
| 142 |
expect_identical(cd2$counts$onRejectedBound, 0L) |
| 143 |
p2 |> wait_for_it() |
| 144 |
expect_identical(cd2$counts$onFinallyCalled, 1L) |
| 145 |
expect_identical(cd2$counts$onFulfilledCalled, 0L) |
| 146 |
expect_identical(cd2$counts$onRejectedCalled, 0L) |
| 147 |
}) |
| 148 |
|
| 149 |
it("handles weird edge case relating to symbols", { |
| 150 |
# This test resulted from a bug in wrap_callback_reenter() where do.call()'s |
| 151 |
# default behavior of quote=FALSE would cause a resolved value that happens |
| 152 |
# to be a symbol, to be evaluated. This would only happen when a promise |
| 153 |
# domain was in effect, and the symbol was passed to an onFulfilled. Fixed |
| 154 |
# by using rlang::exec() instead of do.call(). |
| 155 |
cd <- create_counting_domain() |
| 156 |
with_promise_domain(cd, { |
| 157 |
promise_resolve(as.symbol("foo")) |> |
| 158 |
then(\(x) { |
| 159 |
expect_identical(x, as.symbol("foo")) |
| 160 |
}) |> |
| 161 |
wait_for_it() |
| 162 |
}) |
| 163 |
}) |
| 164 |
|
| 165 |
it("executes wrap_callback_reenter handlers in the right lexical environment", { |
| 166 |
# No reason this shouldn't work, I haven't seen it fail, just making sure. |
| 167 |
cd <- create_counting_domain() |
| 168 |
x <- NULL |
| 169 |
with_promise_domain(cd, { |
| 170 |
promise_resolve(NULL) |> |
| 171 |
then(\() { |
| 172 |
x <<- 1L |
| 173 |
}) |> |
| 174 |
then(\() { |
| 175 |
x <<- x + 1L |
| 176 |
}) |> |
| 177 |
wait_for_it() |
| 178 |
}) |
| 179 |
expect_identical(x, 2L) |
| 180 |
}) |
| 181 |
|
| 182 |
it("doesn't grow the call stack", { |
| 183 |
# See https://github.com/rstudio/promises/issues/114 |
| 184 |
# and also https://github.com/jcheng5/shinychat/issues/16 |
| 185 |
|
| 186 |
recursive_promise <- function(n, .last_callstack_depth = NULL) { |
| 187 |
if (n == 0) { |
| 188 |
return(0) |
| 189 |
} else { |
| 190 |
promise_resolve(TRUE) |> |
| 191 |
then(\() { |
| 192 |
current_callstack_depth <- length(sys.calls()) |
| 193 |
if (!is.null(.last_callstack_depth)) { |
| 194 |
expect_identical(current_callstack_depth, .last_callstack_depth) |
| 195 |
} |
| 196 |
|
| 197 |
recursive_promise( |
| 198 |
n - 1, |
| 199 |
.last_callstack_depth = current_callstack_depth |
| 200 |
) |
| 201 |
}) |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
cd <- create_counting_domain() |
| 206 |
with_promise_domain(cd, { |
| 207 |
recursive_promise(5) |> |
| 208 |
then(\() { |
| 209 |
# 5 (from inside recursive_promise) + 1 (for the current handler) |
| 210 |
expect_identical(cd$counts$onFulfilledCalled, 6L) |
| 211 |
}) |> |
| 212 |
wait_for_it() |
| 213 |
}) |
| 214 |
|
| 215 |
cd2 <- create_counting_domain() |
| 216 |
p <- recursive_promise(5) |
| 217 |
with_promise_domain(cd2, { |
| 218 |
p |> |
| 219 |
then(\() { |
| 220 |
# This time, none of the ones inside recursive_promise count, since |
| 221 |
# they were bound outside of the influence of cd2 (even though they |
| 222 |
# are resolved within the influence of cd2, thanks to wait_for_it()). |
| 223 |
expect_identical(cd2$counts$onFulfilledCalled, 1L) |
| 224 |
}) |> |
| 225 |
wait_for_it() |
| 226 |
}) |
| 227 |
}) |
| 228 |
|
| 229 |
it("apply nested domains in reverse order (innermost domain closest to callback)", { |
| 230 |
# Test that when nesting promise domains, the latest domain to be added |
| 231 |
# (innermost) should be the one that wraps the onFulfilled/onRejected most closely. |
| 232 |
|
| 233 |
# Create a test environment to track domain activation order |
| 234 |
test_env <- new.env() |
| 235 |
test_env$x <- "initial" |
| 236 |
test_env$activations <- character(0) |
| 237 |
|
| 238 |
createVarPromiseDomain <- function(env, name, value) { |
| 239 |
force(env) |
| 240 |
force(name) |
| 241 |
force(value) |
| 242 |
|
| 243 |
new_promise_domain( |
| 244 |
wrapOnFulfilled = function(onFulfilled) { |
| 245 |
function(...) { |
| 246 |
orig <- env[[name]] |
| 247 |
env[[name]] <- value |
| 248 |
env$activations <- c(env$activations, paste0(name, "=", value)) |
| 249 |
on.exit({ |
| 250 |
env[[name]] <- orig |
| 251 |
}) |
| 252 |
|
| 253 |
onFulfilled(...) |
| 254 |
} |
| 255 |
}, |
| 256 |
wrapSync = function(expr) { |
| 257 |
orig <- env[[name]] |
| 258 |
env[[name]] <- value |
| 259 |
on.exit(env[[name]] <- orig) |
| 260 |
|
| 261 |
force(expr) |
| 262 |
} |
| 263 |
) |
| 264 |
} |
| 265 |
|
| 266 |
domain1 <- createVarPromiseDomain(test_env, "x", 1) |
| 267 |
domain2 <- createVarPromiseDomain(test_env, "x", 2) |
| 268 |
|
| 269 |
# Test the nested domain behavior |
| 270 |
result <- NULL |
| 271 |
|
| 272 |
with_promise_domain(domain1, { |
| 273 |
# Outer domain should set x=1 |
| 274 |
expect_identical(test_env$x, 1) |
| 275 |
|
| 276 |
with_promise_domain(domain2, { |
| 277 |
# Inner domain should set x=2 |
| 278 |
expect_identical(test_env$x, 2) |
| 279 |
|
| 280 |
# Create and resolve a promise |
| 281 |
promise_resolve(TRUE) |> |
| 282 |
then(function(value) { |
| 283 |
# The inner domain (domain2) should be closest to this callback, |
| 284 |
# so x should be 2 when this executes |
| 285 |
result <<- test_env$x |
| 286 |
test_env$x |
| 287 |
}) |> |
| 288 |
wait_for_it() |
| 289 |
}) |
| 290 |
}) |
| 291 |
|
| 292 |
# Verify that the innermost domain took precedence |
| 293 |
expect_identical(result, 2) |
| 294 |
|
| 295 |
# Verify the activation order: domain1 (outer) activates first, |
| 296 |
# then domain2 (inner) activates and takes precedence |
| 297 |
expect_length(test_env$activations, 2) |
| 298 |
expect_identical(test_env$activations[1], "x=1") |
| 299 |
expect_identical(test_env$activations[2], "x=2") |
| 300 |
}) |
| 301 |
}) |