Debian Codegraph @

tests/testthat/test_parallel.R

raw
1 for (strategy in c(
2 "sequential",
3 "multicore"
4 # "multisession",
5 # "cluster"
6 )){
7 skip_if_not_installed("future")
8
9 context(sprintf("future plan = '%s'", strategy))
10
11 if (!future::availableCores("multicore") > 1L){
12 skip("'multicore' not supported on system")
13 }
14
15
16 test_that(paste0(strategy, ": Logging works"), {
17 skip_if_not_installed("future")
18 skip_if_not_installed("future.apply")
19 future::plan(strategy)
20
21 tf <- tempfile()
22 on.exit(unlink(tf))
23
24 lr <- get_logger("par_root")
25 lr$
26 set_appenders(AppenderFile$new(tf))$
27 set_propagate(FALSE)
28
29
30 lg <- get_logger("par_root/par_child")
31
32 x <- future::future(lr$info("root_logger"))
33 expect_silent(x <- future::value(x))
34 expect_match(readLines(tf)[[1]], "root_logger")
35
36 y <- future::future(lg$info("child_logger"))
37 expect_silent(y <- future::value(y))
38 expect_match(readLines(tf)[[2]], "child_logger")
39
40 future.apply::future_lapply(
41 c("flapply 1", "flapply 2"),
42 function(.x) lr$info("root %s", .x, pid = Sys.getpid())
43 )
44
45 res <- readLines(tf)[3:4]
46 expect_true(any(grepl("root.*flapply 1", res)))
47 expect_true(any(grepl("root.*flapply 2", res)))
48
49 future.apply::future_lapply(
50 c("flapply 1", "flapply 2"),
51 function(.x) lr$info("child %s", .x)
52 )
53
54 res <- readLines(tf)[5:6]
55 expect_true(any(grepl("child.*flapply 1", res)))
56 expect_true(any(grepl("child.*flapply 2", res)))
57
58 unlink(tf)
59 })
60
61
62 }
63