Skip to content

Commit 4ec8181

Browse files
committed
add tests for isProcessForkedChild() and archSystemFile()
cover two 6.0.0 additions that previously had no automated tests: - isProcessForkedChild(): FALSE in the parent, TRUE in parallel::mclapply workers (skipped on Windows, which has no fork()) - archSystemFile(): delegates to system.file() with no arch subdir, and composes the arch-specific subdirectory when .Platform$r_arch is set
1 parent 2ff3695 commit 4ec8181

2 files changed

Lines changed: 63 additions & 0 deletions

File tree

tests/test-arch-system-file.R

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Unit tests for archSystemFile(), the arch-aware wrapper around system.file()
2+
# introduced when systemFile() was renamed (see #251, #252). It injects the
3+
# architecture-specific subdirectory (.Platform$r_arch) used on Windows, e.g.
4+
# archSystemFile("lib") -> 'lib/x64', while behaving like system.file()
5+
# elsewhere.
6+
7+
RcppParallel:::test_init()
8+
9+
arch <- .Platform$r_arch
10+
11+
# with no arch subdirectory (the usual case off Windows), archSystemFile()
12+
# should be equivalent to a plain system.file() call
13+
if (!nzchar(arch)) {
14+
assert(identical(
15+
archSystemFile("include"),
16+
system.file("include", package = "RcppParallel")
17+
))
18+
assert(identical(
19+
archSystemFile("include", "RcppParallel.h"),
20+
system.file("include/RcppParallel.h", package = "RcppParallel")
21+
))
22+
}
23+
24+
# the composed relative path must match what system.file() is handed: dir,
25+
# then the arch subdir when set, then the optional name -- joined with '/'
26+
expected <- function(dir, name = NULL) {
27+
parts <- c(dir, if (nzchar(arch)) arch, name)
28+
system.file(paste(parts, collapse = "/"), package = "RcppParallel")
29+
}
30+
31+
assert(identical(archSystemFile("lib"), expected("lib")))
32+
assert(identical(archSystemFile("lib", "libtbb.so"), expected("lib", "libtbb.so")))
33+
34+
# a real, shipped resource resolves to an existing path
35+
header <- archSystemFile("include", "RcppParallel.h")
36+
assert(nzchar(header))
37+
assert(file.exists(header))

tests/test-fork.R

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Unit tests for isProcessForkedChild().
2+
#
3+
# The contract: it returns FALSE in the process where RcppParallel was loaded,
4+
# and TRUE in a fork() of that process (e.g. a parallel::mclapply worker). On
5+
# Windows there is no fork(), so it always returns FALSE.
6+
7+
RcppParallel:::test_init()
8+
9+
# in the parent process, we are never a forked child
10+
assert(isProcessForkedChild() == FALSE)
11+
12+
# parallel::mclapply uses fork() on unix, so its workers must report TRUE.
13+
# on Windows there is no fork() (mclapply falls back to serial), so skip: the
14+
# workers there run in the parent process and would correctly report FALSE.
15+
if (!is_windows()) {
16+
17+
results <- parallel::mclapply(1:2, function(i) {
18+
RcppParallel::isProcessForkedChild()
19+
}, mc.cores = 2L)
20+
21+
assert(all(vapply(results, isTRUE, logical(1L))))
22+
23+
}
24+
25+
# forking must not disturb the parent's own view of itself
26+
assert(isProcessForkedChild() == FALSE)

0 commit comments

Comments
 (0)