Debian Codegraph @

tests/testthat/test-zzz-future_promise.R

raw
1 skip_on_cran()
2 skip_on_os("mac") # TODO: Remove this skip once paralely is fixed on macOS in GHA; https://github.com/futureverse/doFuture/issues/87#issuecomment-3001858683
3 skip_on_os("windows") # timing is not consistent on Windows GHA
4
5 skip_if_not_installed("future", "1.21.0")
6 skip_if_not_installed("fastmap", "1.1.0")
7
8 local({
9 ## Setup ##
10
11 n_workers <- 2
12 # Set up a plan with 2 future workers
13 with_test_workers <- function(code) {
14 # (Can not use a variable for workers if in a local({}))
15 old_plan <- future::plan(future::multisession, workers = 2)
16 on.exit(
17 {
18 future::plan(old_plan)
19 },
20 add = TRUE
21 )
22
23 force(code)
24 }
25
26 start <- Sys.time()
27 time_diffs <- c()
28
29 reset_baselines <- function() {
30 start <<- Sys.time()
31 time_diffs <<- c()
32 }
33
34 time_diff <- function() {
35 difftime(Sys.time(), start, units = "secs")
36 }
37
38 # This function will print every `delay` seconds
39 # The thing to notes is that the function will not execute unless the main R session is free
40 # We should expect to see `run_every()` statements interleaved with `future_promise()` calls
41 # We should NOT expect to see `run_every()` statements while `future::future()` is blocking the main R session
42 run_every <- function(i = 0, max = 2 / delay, delay = 0.1) {
43 if (i > max) {
44 return()
45 }
46 time_diffs <<- c(time_diffs, time_diff())
47 # Do it again, later
48 later::later(
49 function() {
50 run_every(i + 1, max = max, delay = delay)
51 },
52 delay = delay
53 )
54 }
55
56 worker_jobs <- 8
57 # allow for more time on CI (4s on CI vs 1s locally)
58 worker_job_time <- if (on_ci) 4 else 1
59 expected_total_time <- worker_jobs * worker_job_time / n_workers
60
61 do_future_test <- function(
62 prom_fn = future_promise,
63 # Introduce extra future workers mid execution
64 block_mid_session = FALSE,
65 # expect that the average finish lag time is less than 2 * n_time
66 expect_reasonable_exec_lag_time = TRUE,
67 # expect the lapply to finish in less than 1s
68 expect_immediate_lapply = TRUE,
69 # expect `run_every()` delay to be < 1s (Expected 0.1s)
70 expect_no_main_blocking = TRUE
71 ) {
72 with_test_workers({
73 # prep future sessions
74 f1 <- future::future({
75 1
76 })
77 f2 <- future::future({
78 2
79 })
80 c(future::value(future::resolve(f1)), future::value(future::resolve(f2)))
81
82 expect_true(future_worker_is_free())
83 expect_equal(future::nbrOfWorkers(), 2)
84 expect_equal(future::nbrOfFreeWorkers(), 2)
85
86 reset_baselines()
87 run_every()
88
89 future_exec_times <- c()
90 if (block_mid_session) {
91 # Have `future` block the main R session 1 second into execution
92 lapply(seq_len(worker_jobs), function(i) {
93 later::later(
94 function() {
95 future::future({
96 Sys.sleep(worker_job_time)
97 time_diff()
98 }) |>
99 then(\(x) {
100 future_exec_times <<- c(future_exec_times, x)
101 })
102 },
103 delay = 1
104 )
105 })
106 }
107
108 exec_times <- NA
109 proms <-
110 lapply(seq_len(worker_jobs), function(i) {
111 prom_fn({
112 Sys.sleep(worker_job_time)
113 time_diff()
114 })
115 })
116
117 p <-
118 promise_all(.list = proms) |>
119 then(\(x) {
120 exec_times <<- unlist(x)
121 })
122 post_lapply_time_diff <- time_diff()
123
124 p |> wait_for_it()
125
126 # expect that the average time is less than the expected total time
127 expect_equal(median(exec_times) < expected_total_time, !block_mid_session)
128
129 # expect prom_fn to take a reasonable amount of time to finish
130 exec_times_lag <- exec_times[-1] - exec_times[-length(exec_times)]
131 expect_equal(
132 all(exec_times_lag < (2 * worker_job_time)),
133 expect_reasonable_exec_lag_time
134 )
135
136 # post_lapply_time_diff should be ~ 0s
137 expect_equal(
138 post_lapply_time_diff <
139 (worker_job_time * ((worker_jobs - n_workers) / n_workers)),
140 expect_immediate_lapply
141 )
142
143 # time_diffs should never grow by more than 1s; (Expected 0.1)
144 time_diffs_lag <- time_diffs[-1] - time_diffs[-length(time_diffs)]
145 expect_equal(
146 all(time_diffs_lag < worker_job_time),
147 expect_no_main_blocking
148 )
149 })
150 }
151
152 test_that("future_promise() allows the main thread to keep the main R process open", {
153 do_future_test(
154 prom_fn = future_promise,
155 # expect that the average finish lag time is less than 2 * n_time
156 expect_reasonable_exec_lag_time = TRUE,
157 # expect the lapply to finish in less than 1s
158 expect_immediate_lapply = TRUE,
159 # expect `run_every()` delay to be < 1s (Expected 0.1s)
160 expect_no_main_blocking = TRUE
161 )
162 })
163
164 test_that("future::future() does not keep the main process open when all workers are busy", {
165 do_future_test(
166 prom_fn = future::future,
167 # expect that the average finish lag time is less than 2 * n_time
168 expect_reasonable_exec_lag_time = TRUE,
169 # expect the lapply to finish after 1s
170 expect_immediate_lapply = FALSE,
171 # expect one `run_every()` delay to be >= 1s (Expected 0.1s)
172 expect_no_main_blocking = FALSE
173 )
174 })
175
176 test_that("future_promise() recovers from losing all future workers", {
177 do_future_test(
178 prom_fn = future_promise,
179 block_mid_session = TRUE,
180 # expect that the average finish lag time is less than 2 * n_time
181 # b/c `future::future()` blocks the main R session, expect >=1 unreasonable lag time
182 expect_reasonable_exec_lag_time = FALSE,
183 # expect the lapply to finish in less than 1s
184 expect_immediate_lapply = TRUE,
185 # expect one `run_every()` delay to be >= 1s (Expected 0.1s)
186 # b/c `future::future()` blocks the main R session, expect >=1 unreasonable lag time
187 expect_no_main_blocking = FALSE
188 )
189 })
190
191 test_that("future_promise reports unhandled errors", {
192 with_test_workers({
193 err <- capture.output(type = "message", {
194 future_promise(stop("boom1"))
195 wait_for_it()
196 })
197 expect_match(err, "boom1")
198 })
199 })
200
201 test_that("future_promise doesn't report errors that have been handled", {
202 with_test_workers({
203 err <- capture.output(type = "message", {
204 future_promise(stop("boom1")) |>
205 then(
206 onRejected = \(err) {}
207 ) |>
208 wait_for_it()
209 })
210 expect_equal(err, character(0))
211 })
212 })
213 })