| 1 |
library(later) |
| 2 |
|
| 3 |
on_ci <- isTRUE(as.logical(Sys.getenv("CI"))) |
| 4 |
|
| 5 |
# Create a promise that can be EXTernally resolved/rejected/inspected |
| 6 |
ext_promise <- function() { |
| 7 |
res <- NULL |
| 8 |
p <- promise(function(resolve, reject) { |
| 9 |
res <<- list(resolve = resolve, reject = reject) |
| 10 |
}) |
| 11 |
|
| 12 |
list( |
| 13 |
promise = p, |
| 14 |
resolve = res$resolve, |
| 15 |
reject = res$reject, |
| 16 |
status = attr(p, "promise_impl", exact = TRUE)$status |
| 17 |
) |
| 18 |
} |
| 19 |
|
| 20 |
# Block until all pending later tasks have executed |
| 21 |
# wait_for_it <- function(timeout = if (on_ci) 60 else 30) { |
| 22 |
wait_for_it <- function(p = NULL, timeout = if (on_ci) 60 else 30) { |
| 23 |
start <- Sys.time() |
| 24 |
|
| 25 |
err <- NULL |
| 26 |
if (!is.null(p)) { |
| 27 |
p |> catch(\(reason) err <<- reason) |
| 28 |
} |
| 29 |
|
| 30 |
while (!later::loop_empty()) { |
| 31 |
if (difftime(Sys.time(), start, units = "secs") > timeout) { |
| 32 |
stop("Waited too long") |
| 33 |
} |
| 34 |
later::run_now(0.1) |
| 35 |
} |
| 36 |
|
| 37 |
if (!is.null(err)) { |
| 38 |
withRestarts( |
| 39 |
stop(err), |
| 40 |
continue_test = function(e) NULL |
| 41 |
) |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
# Block until the promise is resolved/rejected. If resolved, return the value. |
| 46 |
# If rejected, throw (yes throw, not return) the error. |
| 47 |
extract <- function(promise) { |
| 48 |
promise_value <- NULL |
| 49 |
|
| 50 |
promise |> |
| 51 |
then(\(value) promise_value <<- value) |> |
| 52 |
wait_for_it() |
| 53 |
|
| 54 |
promise_value |
| 55 |
} |
| 56 |
|
| 57 |
resolve_later <- function(value, delaySecs) { |
| 58 |
force(value) |
| 59 |
promise(\(resolve, reject) later::later(\() resolve(value), delaySecs)) |
| 60 |
} |
| 61 |
|
| 62 |
# Prevent "Unhandled promise error" warning that happens if you don't handle the |
| 63 |
# rejection of a promise |
| 64 |
squelch_unhandled_promise_error <- function(promise) { |
| 65 |
promise |> |
| 66 |
catch(\(reason) { |
| 67 |
if (inherits(reason, "failure")) { |
| 68 |
# Don't squelch test failures |
| 69 |
stop(reason) |
| 70 |
} |
| 71 |
}) |
| 72 |
} |
| 73 |
|
| 74 |
.GlobalEnv$.Last <- function() { |
| 75 |
# Detect unexpected "Unhandled promise error" warnings. |
| 76 |
wait_for_it() |
| 77 |
} |
| 78 |
|
| 79 |
create_counting_domain <- function(trackFinally = FALSE) { |
| 80 |
counts <- list2env( |
| 81 |
parent = emptyenv(), |
| 82 |
list( |
| 83 |
onFulfilledBound = 0L, |
| 84 |
onFulfilledCalled = 0L, |
| 85 |
onFulfilledActive = 0L, |
| 86 |
onRejectedBound = 0L, |
| 87 |
onRejectedCalled = 0L, |
| 88 |
onRejectedActive = 0L |
| 89 |
) |
| 90 |
) |
| 91 |
|
| 92 |
incr <- function(field) { |
| 93 |
field <- as.character(substitute(field)) |
| 94 |
counts[[field]] <- counts[[field]] + 1L |
| 95 |
} |
| 96 |
|
| 97 |
decr <- function(field) { |
| 98 |
field <- as.character(substitute(field)) |
| 99 |
counts[[field]] <- counts[[field]] - 1L |
| 100 |
} |
| 101 |
|
| 102 |
pd <- new_promise_domain( |
| 103 |
wrapOnFulfilled = function(onFulfilled) { |
| 104 |
incr(onFulfilledBound) |
| 105 |
function(...) { |
| 106 |
incr(onFulfilledCalled) |
| 107 |
incr(onFulfilledActive) |
| 108 |
on.exit(decr(onFulfilledActive)) |
| 109 |
|
| 110 |
onFulfilled(...) |
| 111 |
} |
| 112 |
}, |
| 113 |
wrapOnRejected = function(onRejected) { |
| 114 |
incr(onRejectedBound) |
| 115 |
function(...) { |
| 116 |
incr(onRejectedCalled) |
| 117 |
incr(onRejectedActive) |
| 118 |
on.exit(decr(onRejectedActive)) |
| 119 |
|
| 120 |
onRejected(...) |
| 121 |
} |
| 122 |
}, |
| 123 |
counts = counts |
| 124 |
) |
| 125 |
|
| 126 |
if (trackFinally) { |
| 127 |
counts$onFinallyBound <- 0L |
| 128 |
counts$onFinallyCalled <- 0L |
| 129 |
counts$onFinallyActive <- 0L |
| 130 |
|
| 131 |
pd$wrapOnFinally <- function(onFinally) { |
| 132 |
incr(onFinallyBound) |
| 133 |
function() { |
| 134 |
incr(onFinallyCalled) |
| 135 |
incr(onFinallyActive) |
| 136 |
on.exit(incr(onFinallyActive)) |
| 137 |
|
| 138 |
onFinally() |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
pd |
| 144 |
} |
| 145 |
|
| 146 |
expect_assertions <- function(code, n_expected) { |
| 147 |
n <- 0 |
| 148 |
|
| 149 |
withCallingHandlers( |
| 150 |
code, |
| 151 |
expectation_success = \(e) n <<- n + 1L, |
| 152 |
expectation_failure = \(e) n <<- n + 1L |
| 153 |
) |
| 154 |
|
| 155 |
expect_equal(n, n_expected, label = "Expected assertion count") |
| 156 |
} |