| 1 |
test_that("later C++ BackgroundTask class works with promises", { |
| 2 |
|
| 3 |
# This test was originally in the promises package but moved here |
| 4 |
# to consolidate everything that requires compilation |
| 5 |
skip_if_not_installed("promises") |
| 6 |
|
| 7 |
# compile test |
| 8 |
env <- new.env() |
| 9 |
Rcpp::sourceCpp( |
| 10 |
system.file("promise_task.cpp", package = "later"), |
| 11 |
env = env |
| 12 |
) |
| 13 |
|
| 14 |
# test that resolve works |
| 15 |
result <- 0 |
| 16 |
promises::promise(function(resolve, reject) { |
| 17 |
env$asyncFib(resolve, reject, 3) |
| 18 |
}) |> |
| 19 |
promises::then(\(x) { |
| 20 |
result <<- x |
| 21 |
}) |
| 22 |
|
| 23 |
expect_identical(result, 0) |
| 24 |
run_now(1) |
| 25 |
while (!loop_empty()) { |
| 26 |
run_now(0.1) |
| 27 |
} |
| 28 |
expect_identical(result, 2) |
| 29 |
# test that reject works (swap resolve/reject) |
| 30 |
err_result <- 0 |
| 31 |
promises::promise(function(resolve, reject) { |
| 32 |
env$asyncFib(reject, resolve, 6) |
| 33 |
}) |> |
| 34 |
promises::catch(\(x) { |
| 35 |
err_result <<- x |
| 36 |
}) |
| 37 |
|
| 38 |
expect_identical(err_result, 0) |
| 39 |
run_now(1) |
| 40 |
while (!loop_empty()) { |
| 41 |
run_now(0.1) |
| 42 |
} |
| 43 |
expect_identical(err_result, 8) |
| 44 |
}) |