Debian Codegraph @

inst/testme/test-killNode.R

raw
1 #' @tags detrius-files
2
3 library(parallelly)
4
5 if (on_windows) {
6 killNode <- function(cl) {
7 if (is.null(cl)) {
8 parallelly::killNode(NULL)
9 } else {
10 parallel::stopCluster(cl)
11 rep(TRUE, times = length(cl))
12 }
13 }
14 }
15
16 options(parallelly.debug = TRUE)
17
18 message("*** killNode() and isNodeAlive() ...")
19
20 isNodeAliveSupported <- isTRUE(parallelly:::pid_exists(Sys.getpid()))
21 message("isNodeAlive() works: ", isNodeAliveSupported)
22
23 cl <- makeClusterPSOCK(2L, autoStop = FALSE)
24 names(cl) <- sprintf("Node %d", seq_along(cl))
25 print(cl)
26
27 ## WORKAROUND: On MS Windows, each R process creates a temporary Rscript<hexcode>
28 ## file. In this test we terminate the workers such that these temporary files
29 ## are not cleaned up, which will trigger a NOTE by 'R CMD check'. Because of
30 ## this, we have to make sure to remove such files manually in this test.
31 if (on_windows) {
32 files <- setdiff(dir(path = tempdir(), all.files = TRUE), c(".", ".."))
33 files <- file.path(tempdir(), files)
34 tmpfiles <- files
35 files <- parallel::clusterEvalQ(cl, {
36 files <- setdiff(dir(path = tempdir(), all.files = TRUE), c(".", ".."))
37 file.path(tempdir(), files)
38 })
39 files <- unlist(files)
40 tmpfiles <- unique(c(tmpfiles, files))
41 message(sprintf("- files: [n=%d] %s", length(tmpfiles),
42 paste(sQuote(tmpfiles), collapse = ", ")))
43 }
44
45 alive <- isNodeAlive(cl)
46 print(alive)
47 stopifnot(
48 length(alive) == length(cl),
49 is.logical(alive)
50 )
51 if (isNodeAliveSupported) {
52 stopifnot(
53 !anyNA(alive),
54 isTRUE(alive[[1]]), isTRUE(alive[[2]]),
55 all(alive)
56 )
57 }
58
59 message("- Terminate cluster nodes")
60 signaled <- killNode(cl)
61 print(signaled)
62 stopifnot(
63 length(signaled) == length(cl),
64 is.logical(signaled)
65 )
66 ## The value of tools::pskill() is incorrect in R (< 3.5.0)
67 if (getRversion() >= "3.5.0") {
68 stopifnot(
69 isTRUE(signaled[[1]]), isTRUE(signaled[[2]]),
70 all(signaled)
71 )
72 }
73
74 message("- Waiting for cluster nodes to terminate")
75 ## It might take a moment before the background
76 ## workers are shutdown after having been signaled
77 timeout <- Sys.time() + 30.0
78 repeat {
79 alive <- isNodeAlive(cl)
80 print(alive)
81 stopifnot(
82 length(alive) == length(cl),
83 is.logical(alive)
84 )
85 if (!any(alive, na.rm = TRUE)) break
86 if (Sys.time() > timeout) {
87 stop("One or more cluster nodes are still running after 30 seconds")
88 }
89 Sys.sleep(0.5)
90 }
91
92 ## Remove any stray Rscript<hexcode> files
93 if (on_windows) {
94 if (!isNodeAliveSupported) Sys.sleep(5.0)
95 tmpfiles <- tmpfiles[utils::file_test("-f", tmpfiles)]
96 if (length(tmpfiles) > 0L) {
97 warning(sprintf("Cleaning up temporary left-over files: [n=%d] %s",
98 length(tmpfiles),
99 paste(sQuote(tmpfiles), collapse = ", ")))
100 file.remove(tmpfiles)
101 tmpfiles <- tmpfiles[utils::file_test("-f", tmpfiles)]
102 if (length(tmpfiles) > 0L) {
103 stop(sprintf("Failed to remove some temporary left-over files: [n=%d] %s",
104 length(tmpfiles),
105 paste(sQuote(tmpfiles), collapse = ", ")))
106 }
107 }
108 }
109
110 cl <- NULL
111
112 message("- Exceptions")
113 res <- killNode(NULL)
114 print(res)
115 stopifnot(is.logical(res), length(res) == 1L, is.na(res))
116
117 res <- tryCatch(killNode(NULL), warning = identity)
118 print(res)
119 stopifnot(inherits(res, "warning"))
120
121 message("*** killNode() and isNodeAlive() ... done")