Skip to content

Commit 349ad8a

Browse files
TESTS: Increase test coverage further
1 parent a3e71b7 commit 349ad8a

29 files changed

+321
-5
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Package: future.mirai
2-
Version: 0.10.1-9010
2+
Version: 0.10.1-9011
33
Depends:
44
future (>= 1.49.0)
55
Imports:

R/001.import_future_functions.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ sQuoteLabel <- NULL
1111

1212
## Import private functions from 'future'
1313
import_future_functions <- function() {
14+
## Already done?
15+
if (is.function(readImmediateConditions)) return()
16+
1417
readImmediateConditions <<- import_future("readImmediateConditions")
1518
signalEarly <<- import_future("signalEarly")
1619
FutureRegistry <<- import_future("FutureRegistry")

inst/testme/_prologue/030.imports.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@
22
mdebugf <- future.mirai:::mdebugf
33
mprint <- future.mirai:::mprint
44
commaq <- future.mirai:::commaq
5+
mirai_daemons_nworkers <- future.mirai:::mirai_daemons_nworkers
6+
mirai_version <- future.mirai:::mirai_version
7+
get_mirai_daemons <- future.mirai:::get_mirai_daemons
58

69

inst/testme/test-01-daemons.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
mirai::daemons(2)
55
print(mirai::status())
66

7+
mirai::daemons(0)
8+
79
## Give daemons a chance to shutdown
810
Sys.sleep(10)
911

Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
#' @tags MiraiFutureBackend
2+
#' @tags detritus-files
3+
#' @tags mirai_cluster mirai_multisession
4+
5+
library(future)
6+
library(future.mirai)
7+
8+
options(future.mirai.debug = TRUE)
9+
options(future.debug = TRUE)
10+
11+
message("*** MiraiFutureBackend ...")
12+
13+
14+
## ---------------------------------------------------------
15+
## mirai_version()
16+
## ---------------------------------------------------------
17+
message("- mirai_version() ...")
18+
19+
v <- mirai_version()
20+
stopifnot(inherits(v, "package_version"))
21+
message(" mirai version: ", v)
22+
23+
## Second call should use cached value
24+
v2 <- mirai_version()
25+
stopifnot(identical(v, v2))
26+
27+
message("- mirai_version() ... DONE")
28+
29+
30+
## ---------------------------------------------------------
31+
## get_mirai_daemons() and mirai_daemons_nworkers()
32+
## ---------------------------------------------------------
33+
message("- get_mirai_daemons() and mirai_daemons_nworkers() ...")
34+
35+
## Setup daemons
36+
mirai::daemons(2)
37+
38+
daemons <- get_mirai_daemons()
39+
message(" daemons class: ", class(daemons)[1])
40+
stopifnot(is.data.frame(daemons) || is.numeric(daemons))
41+
42+
nworkers <- mirai_daemons_nworkers()
43+
message(" nworkers: ", nworkers)
44+
stopifnot(is.numeric(nworkers), nworkers >= 1)
45+
46+
mirai::daemons(0)
47+
48+
message("- get_mirai_daemons() and mirai_daemons_nworkers() ... DONE")
49+
50+
51+
## ---------------------------------------------------------
52+
## tweak.mirai_cluster()
53+
## ---------------------------------------------------------
54+
message("- tweak.mirai_cluster() ...")
55+
56+
plan(future.mirai::mirai_multisession, workers = 2)
57+
stopifnot(nbrOfWorkers() == 2L)
58+
59+
## Create a future to verify backend works after tweak
60+
f <- future(42)
61+
v <- value(f)
62+
stopifnot(v == 42)
63+
64+
r <- resolved(f)
65+
message("Resolved: ", r)
66+
67+
r <- result(f)
68+
print(r)
69+
70+
plan(sequential)
71+
72+
message("- tweak.mirai_cluster() ... DONE")
73+
74+
75+
## ---------------------------------------------------------
76+
## Global variables that clash with mirai::mirai() formals
77+
## ---------------------------------------------------------
78+
message("- globals that clash with mirai::mirai() formals ...")
79+
80+
plan(future.mirai::mirai_multisession, workers = 2)
81+
82+
## '.args' is a formal argument of mirai::mirai()
83+
## This should produce an error
84+
res <- tryCatch({
85+
.args <- 42
86+
f <- future(.args)
87+
value(f)
88+
}, error = identity)
89+
print(res)
90+
stopifnot(inherits(res, "FutureError"))
91+
stopifnot(grepl("clash with argument names of mirai::mirai", conditionMessage(res)))
92+
93+
plan(sequential)
94+
95+
message("- globals that clash with mirai::mirai() formals ... DONE")
96+
97+
98+
## ---------------------------------------------------------
99+
## interruptFuture.MiraiFutureBackend() via cancel()
100+
## ---------------------------------------------------------
101+
message("- interruptFuture.MiraiFutureBackend() via cancel() ...")
102+
103+
plan(future.mirai::mirai_multisession, workers = 2)
104+
105+
## Create a long-running future
106+
f <- future({
107+
Sys.sleep(60)
108+
42
109+
})
110+
111+
r <- resolved(f)
112+
message("Resolved: ", r)
113+
stopifnot(!isTRUE(r))
114+
115+
## Cancel/interrupt the future
116+
f <- cancel(f, interrupt = TRUE)
117+
stopifnot(f[["state"]] == "canceled")
118+
119+
r <- resolved(f)
120+
message("Resolved: ", r)
121+
122+
## Trying to get the value should produce a FutureInterruptError
123+
res <- tryCatch(value(f), error = identity)
124+
print(res)
125+
stopifnot(inherits(res, "FutureInterruptError"))
126+
127+
plan(sequential)
128+
129+
message("- interruptFuture.MiraiFutureBackend() via cancel() ... DONE")
130+
131+
132+
message("- stopWorkers.MiraiFutureBackend() with active futures ...")
133+
134+
plan(future.mirai::mirai_multisession, workers = 2, interrupts = FALSE)
135+
136+
message("nbrOfWorkers(): ", nbrOfWorkers())
137+
message("nbrOfFreeWorkers(): ", nbrOfFreeWorkers())
138+
139+
## Create a long-running future
140+
f <- future({
141+
Sys.sleep(60)
142+
42
143+
})
144+
145+
r <- resolved(f)
146+
message("Resolved: ", r)
147+
stopifnot(!isTRUE(r))
148+
149+
plan(sequential)
150+
151+
message("- stopWorkers.MiraiFutureBackend() with active futures ... DONE")
152+
153+
154+
## ---------------------------------------------------------
155+
## Error when no mirai daemons are available
156+
## ---------------------------------------------------------
157+
message("- Error when no mirai daemons ...")
158+
159+
## Make sure no daemons are running
160+
mirai::daemons(0)
161+
162+
## Try to create a MiraiFutureBackend without daemons
163+
MiraiFutureBackend <- future.mirai:::MiraiFutureBackend
164+
res <- tryCatch({
165+
MiraiFutureBackend()
166+
}, error = identity)
167+
print(res)
168+
stopifnot(inherits(res, "FutureError"))
169+
stopifnot(grepl("at least one mirai daemon", conditionMessage(res)))
170+
171+
message("- Error when no mirai daemons ... DONE")
172+
173+
174+
## ---------------------------------------------------------
175+
## mirai_cluster() function should always error when called directly
176+
## ---------------------------------------------------------
177+
message("- mirai_cluster() direct call ...")
178+
179+
res <- tryCatch({
180+
future.mirai::mirai_cluster()
181+
}, error = identity)
182+
print(res)
183+
stopifnot(inherits(res, "error"))
184+
stopifnot(grepl("must never be called directly", conditionMessage(res)))
185+
186+
message("- mirai_cluster() direct call ... DONE")
187+
188+
189+
## ---------------------------------------------------------
190+
## resolved.MiraiFuture() with lazy future (state = "created")
191+
## ---------------------------------------------------------
192+
message("- resolved.MiraiFuture() for lazy future ...")
193+
194+
plan(future.mirai::mirai_multisession, workers = 2)
195+
196+
## Create a lazy future - it stays in "created" state until resolved
197+
f <- future({ 42 }, lazy = TRUE)
198+
stopifnot(f[["state"]] == "created")
199+
200+
## resolved() should launch the future
201+
r <- resolved(f)
202+
message(" resolved: ", r)
203+
## After resolved() is called on a lazy future, it should be submitted
204+
stopifnot(f[["state"]] %in% c("running", "finished"))
205+
206+
## Get the value
207+
v <- value(f)
208+
stopifnot(v == 42)
209+
210+
plan(sequential)
211+
212+
message("- resolved.MiraiFuture() for lazy future ... DONE")
213+
214+
215+
## ---------------------------------------------------------
216+
## nbrOfFreeWorkers.MiraiFutureBackend()
217+
## ---------------------------------------------------------
218+
message("- nbrOfFreeWorkers.MiraiFutureBackend() ...")
219+
220+
plan(future.mirai::mirai_multisession, workers = 2)
221+
222+
free <- nbrOfFreeWorkers()
223+
message(" free workers: ", free)
224+
stopifnot(is.numeric(free), free >= 0, free <= nbrOfWorkers())
225+
226+
## Create a future that takes some time
227+
f <- future({ Sys.sleep(2); 42 })
228+
229+
## Check free workers while future is running
230+
Sys.sleep(0.5)
231+
free_during <- nbrOfFreeWorkers()
232+
message(" free workers during execution: ", free_during)
233+
234+
## Wait for future to complete
235+
v <- value(f)
236+
stopifnot(v == 42)
237+
238+
## Check free workers after completion
239+
free_after <- nbrOfFreeWorkers()
240+
message(" free workers after: ", free_after)
241+
stopifnot(free_after == nbrOfWorkers())
242+
243+
plan(sequential)
244+
245+
message("- nbrOfFreeWorkers.MiraiFutureBackend() ... DONE")
246+
247+
248+
message("*** MiraiFutureBackend ... DONE")

inst/testme/test-internals.R

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ message("Internal import functions ...")
44

55
import_from <- future.mirai:::import_from
66
import_future <- future.mirai:::import_future
7-
import_future_functions <- future.mirai:::import_future_functions
7+
with_assert <- future.mirai:::with_assert
8+
.onLoad <- future.mirai:::.onLoad
89

910
void <- import_future("future")
1011
stopifnot(is.function(void))
@@ -13,5 +14,10 @@ stopifnot(is.na(void))
1314
void <- tryCatch(import_future("non-existing"), error = identity)
1415
stopifnot(inherits(void, "error"))
1516

17+
with_assert(TRUE)
1618

17-
19+
options(future.mirai.debug = NULL)
20+
options(future.mirai.queue = NULL)
21+
Sys.setenv(R_FUTURE_MIRAI_DEBUG = "FALSE")
22+
Sys.setenv(R_FUTURE_MIRAI_QUEUE = "FALSE")
23+
.onLoad("future.mirai", "future.mirai")

inst/testme/test-nbrOfWorkers.R

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,42 @@
44

55
library(future)
66

7-
message("*** nbrOfWorkers() ...")
7+
message("*** mirai_multisession - nbrOfWorkers() ...")
88

99
ncores <- availableCores()
10-
1110
plan(future.mirai::mirai_multisession)
1211

1312
n <- nbrOfWorkers()
1413
message("Number of workers: ", n)
1514
stopifnot(n == ncores)
1615

16+
n <- nbrOfFreeWorkers()
17+
message("Number of free workers: ", n)
18+
stopifnot(n == ncores)
19+
20+
plan(sequential)
21+
22+
23+
message("*** mirai_cluster - nbrOfWorkers() ...")
24+
25+
mirai::daemons(2)
26+
print(mirai::status())
27+
28+
plan(future.mirai::mirai_cluster)
29+
print(c(nbrOfWorkers = nbrOfWorkers(), nbrOfFreeWorkers = nbrOfFreeWorkers()))
30+
31+
f <- future({
32+
Sys.sleep(2)
33+
42
34+
})
35+
print(c(
36+
nbrOfWorkers = nbrOfWorkers(),
37+
nbrOfFreeWorkers = nbrOfFreeWorkers(),
38+
resolved = resolved(f)
39+
))
40+
41+
print(value(f))
42+
1743
plan(sequential)
1844

1945
message("*** nbrOfWorkers() ... DONE")

tests/test-01-daemons.R

100644100755
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#! /usr/bin/env Rscript
12
## This runs testme test script inst/testme/test-01-daemons.R
23
## Don't edit - it was autogenerated by inst/testme/deploy.R
34
future.mirai:::testme("01-daemons")

tests/test-02-daemons-shutdown.R

100644100755
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#! /usr/bin/env Rscript
12
## This runs testme test script inst/testme/test-02-daemons-shutdown.R
23
## Don't edit - it was autogenerated by inst/testme/deploy.R
34
future.mirai:::testme("02-daemons-shutdown")

tests/test-MiraiFutureBackend.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#! /usr/bin/env Rscript
2+
## This runs testme test script inst/testme/test-MiraiFutureBackend.R
3+
## Don't edit - it was autogenerated by inst/testme/deploy.R
4+
future.mirai:::testme("MiraiFutureBackend")

0 commit comments

Comments
 (0)