Debian Codegraph @

inst/testme/test-isNodeAlive.R

raw
1 library(parallelly)
2
3 message("*** isNodeAlive() ...")
4
5 ## Test default isNodeAlive()
6 res <- isNodeAlive(list())
7 print(res)
8 stopifnot(is.na(res))
9
10 ## Test on a cluster object
11 cl <- makeClusterPSOCK(1L)
12 print(cl)
13 res <- isNodeAlive(cl)
14 print(res)
15 stopifnot(is.logical(res), length(res) == 1L)
16 parallel::stopCluster(cl)
17
18 ## Test isNodeAlive() for RichSOCKnode with missing PID
19 cl <- makeClusterPSOCK(1L)
20 node <- cl[[1]]
21 node$session_info$process$pid <- NULL
22 res <- isNodeAlive(node)
23 print(res)
24 stopifnot(is.na(res))
25 parallel::stopCluster(cl)
26
27 ## Test isNodeAlive() for RichSOCKnode with missing hostname
28 cl <- makeClusterPSOCK(1L)
29 node <- cl[[1]]
30 node$session_info$system$nodename <- NULL
31 res <- isNodeAlive(node)
32 print(res)
33 stopifnot(is.na(res))
34 parallel::stopCluster(cl)
35
36 ## Test isNodeAlive() for RichSOCKnode with timeout
37 cl <- makeClusterPSOCK(1L)
38 node <- cl[[1]]
39 res <- isNodeAlive(node, timeout = 1.0)
40 print(res)
41 stopifnot(is.logical(res), length(res) == 1L)
42 parallel::stopCluster(cl)
43 message("- isNodeAlive.RichSOCKnode (with timeout) ... DONE")
44
45 ## Test isNodeAlive() for RichSOCKnode with invalid PID (triggers error handler)
46 message("- isNodeAlive.RichSOCKnode (with invalid PID) ...")
47 cl <- makeClusterPSOCK(1L)
48 node <- cl[[1]]
49 ## Set an invalid PID to trigger error in pid_exists()
50 node$session_info$process$pid <- -1L
51 res <- tryCatch({
52 isNodeAlive(node)
53 }, warning = function(w) {
54 message(" Caught expected warning: ", conditionMessage(w))
55 NA
56 })
57 print(res)
58 stopifnot(is.na(res))
59 parallel::stopCluster(cl)
60 message("- isNodeAlive.RichSOCKnode (with invalid PID) ... DONE")
61
62 ## Remote host tests use 'echo' as a fake rshcmd, which behaves differently
63 ## on Windows vs Unix. Run these tests only on Unix/macOS.
64 if (.Platform[["OS.type"]] != "windows") {
65
66 ## Test isNodeAlive() for RichSOCKnode on "remote" host (triggers remote code path)
67 message("- isNodeAlive.RichSOCKnode (remote host path) ...")
68 cl <- makeClusterPSOCK(1L)
69 node <- cl[[1]]
70 ## Set a fake remote hostname to trigger the remote host code path
71 node$session_info$system$nodename <- "fake-remote-host.invalid"
72 ## Set up rshcmd so the system() call runs but returns invalid output
73 ## This exercises the result parsing code (lines 135-156)
74 options <- attr(node, "options")
75 options$rshcmd <- "echo" ## 'echo' will just print the command, not execute it
76 options$rscript_sh <- c("sh", "sh")
77 attr(node, "options") <- options
78 ## Use a short timeout to avoid long waits
79 res <- tryCatch({
80 suppressWarnings(isNodeAlive(node, timeout = 1.0))
81 }, warning = function(w) {
82 message(" Caught expected warning: ", conditionMessage(w))
83 NA
84 }, error = function(e) {
85 message(" Caught error: ", conditionMessage(e))
86 NA
87 })
88 print(res)
89 stopifnot(is.na(res))
90 parallel::stopCluster(cl)
91 message("- isNodeAlive.RichSOCKnode (remote host path) ... DONE")
92
93 ## Test remote host path with user option (covers line 108)
94 message("- isNodeAlive.RichSOCKnode (remote host with user) ...")
95 cl <- makeClusterPSOCK(1L)
96 node <- cl[[1]]
97 node$session_info$system$nodename <- "fake-remote-host.invalid"
98 options <- attr(node, "options")
99 options$rshcmd <- "echo"
100 options$rscript_sh <- c("sh", "sh")
101 options$arguments$user <- "testuser" ## Triggers line 108
102 attr(node, "options") <- options
103 res <- tryCatch({
104 suppressWarnings(isNodeAlive(node, timeout = 1.0))
105 }, warning = function(w) NA, error = function(e) NA)
106 print(res)
107 stopifnot(is.na(res))
108 parallel::stopCluster(cl)
109 message("- isNodeAlive.RichSOCKnode (remote host with user) ... DONE")
110
111 ## Test remote host path with small timeout (covers line 119)
112 message("- isNodeAlive.RichSOCKnode (remote host with small timeout) ...")
113 cl <- makeClusterPSOCK(1L)
114 node <- cl[[1]]
115 node$session_info$system$nodename <- "fake-remote-host.invalid"
116 options <- attr(node, "options")
117 options$rshcmd <- "echo"
118 options$rscript_sh <- c("sh", "sh")
119 attr(node, "options") <- options
120 ## Use a timeout less than 1 second to trigger line 119
121 res <- tryCatch({
122 suppressWarnings(isNodeAlive(node, timeout = 0.5))
123 }, warning = function(w) NA, error = function(e) NA)
124 print(res)
125 stopifnot(is.na(res))
126 parallel::stopCluster(cl)
127 message("- isNodeAlive.RichSOCKnode (remote host with small timeout) ... DONE")
128
129 ## Test remote host path with debug mode (covers debug lines)
130 message("- isNodeAlive.RichSOCKnode (remote host with debug) ...")
131 cl <- makeClusterPSOCK(1L)
132 node <- cl[[1]]
133 node$session_info$system$nodename <- "fake-remote-host.invalid"
134 options <- attr(node, "options")
135 options$rshcmd <- "echo"
136 options$rscript_sh <- c("sh", "sh")
137 attr(node, "options") <- options
138 ## Enable debug mode to cover debug lines
139 opts <- options(parallelly.debug = TRUE)
140 res <- tryCatch({
141 suppressWarnings(isNodeAlive(node, timeout = 1.0))
142 }, warning = function(w) NA, error = function(e) NA)
143 options(opts)
144 print(res)
145 stopifnot(is.na(res))
146 parallel::stopCluster(cl)
147 message("- isNodeAlive.RichSOCKnode (remote host with debug) ... DONE")
148
149 } ## if (.Platform[["OS.type"]] != "windows")
150
151
152 message("*** isNodeAlive() ... DONE")