| 1 |
|
| 2 |
test_that("renv_socket_accept times out when no client connects", { |
| 3 |
|
| 4 |
skip_on_cran() |
| 5 |
|
| 6 |
server <- tryCatch(renv_socket_server(), error = skip) |
| 7 |
defer(close(server$socket)) |
| 8 |
|
| 9 |
# a short timeout should produce an error, not block indefinitely |
| 10 |
expect_error( |
| 11 |
renv_socket_accept(server$socket, open = "rb", timeout = 1), |
| 12 |
"timed out" |
| 13 |
) |
| 14 |
|
| 15 |
}) |
| 16 |
|
| 17 |
test_that("persistent socket connections detect results and worker death", { |
| 18 |
|
| 19 |
skip_on_cran() |
| 20 |
|
| 21 |
server <- tryCatch(renv_socket_server(), error = skip) |
| 22 |
defer(close(server$socket)) |
| 23 |
|
| 24 |
port <- server$port |
| 25 |
|
| 26 |
# launch two workers that follow the persistent connection protocol: |
| 27 |
# 1. connect, 2. send package name (hello), 3. send result, 4. close |
| 28 |
for (pkg in c("pkgA", "pkgB")) { |
| 29 |
script <- renv_test_code({ |
| 30 |
conn <- socketConnection( |
| 31 |
host = "127.0.0.1", port = port, |
| 32 |
open = "wb", blocking = TRUE, timeout = 10 |
| 33 |
) |
| 34 |
serialize(pkg, conn) |
| 35 |
serialize(list(package = pkg, status = 0L, output = "ok"), conn) |
| 36 |
close(conn) |
| 37 |
}, list(port = port, pkg = pkg)) |
| 38 |
|
| 39 |
system2( |
| 40 |
command = R(), |
| 41 |
args = c("--vanilla", "-s", "-f", renv_shell_path(script)), |
| 42 |
stdout = FALSE, |
| 43 |
stderr = FALSE, |
| 44 |
wait = FALSE |
| 45 |
) |
| 46 |
} |
| 47 |
|
| 48 |
# launch a worker that connects and sends hello, then crashes (no result) |
| 49 |
script <- renv_test_code({ |
| 50 |
conn <- socketConnection( |
| 51 |
host = "127.0.0.1", port = port, |
| 52 |
open = "wb", blocking = TRUE, timeout = 10 |
| 53 |
) |
| 54 |
serialize("pkgC", conn) |
| 55 |
close(conn) |
| 56 |
}, list(port = port)) |
| 57 |
|
| 58 |
system2( |
| 59 |
command = R(), |
| 60 |
args = c("--vanilla", "-s", "-f", renv_shell_path(script)), |
| 61 |
stdout = FALSE, |
| 62 |
stderr = FALSE, |
| 63 |
wait = FALSE |
| 64 |
) |
| 65 |
|
| 66 |
# accept all three hello connections via socketSelect on the server socket |
| 67 |
conns <- list() |
| 68 |
for (i in 1:3) { |
| 69 |
socketSelect(list(server$socket), write = FALSE, timeout = 10) |
| 70 |
conn <- socketAccept(server$socket, open = "rb", blocking = TRUE, timeout = 10) |
| 71 |
pkg <- unserialize(conn) |
| 72 |
conns[[pkg]] <- conn |
| 73 |
} |
| 74 |
|
| 75 |
expect_equal(sort(names(conns)), c("pkgA", "pkgB", "pkgC")) |
| 76 |
|
| 77 |
# now use socketSelect on all three worker connections to read results |
| 78 |
results <- list() |
| 79 |
remaining <- names(conns) |
| 80 |
|
| 81 |
while (length(remaining) > 0L) { |
| 82 |
active_conns <- conns[remaining] |
| 83 |
flags <- socketSelect(active_conns, write = FALSE, timeout = 10) |
| 84 |
for (i in which(flags)) { |
| 85 |
pkg <- remaining[i] |
| 86 |
data <- tryCatch(unserialize(conns[[pkg]]), error = function(e) NULL) |
| 87 |
close(conns[[pkg]]) |
| 88 |
if (is.null(data)) { |
| 89 |
results[[pkg]] <- list(success = FALSE, output = "worker died") |
| 90 |
} else { |
| 91 |
results[[pkg]] <- list( |
| 92 |
success = identical(as.integer(data$status), 0L), |
| 93 |
output = data$output |
| 94 |
) |
| 95 |
} |
| 96 |
} |
| 97 |
remaining <- setdiff(remaining, remaining[flags]) |
| 98 |
} |
| 99 |
|
| 100 |
# pkgA and pkgB sent results |
| 101 |
expect_true(results$pkgA$success) |
| 102 |
expect_true(results$pkgB$success) |
| 103 |
|
| 104 |
# pkgC connected but sent no result (EOF detected) |
| 105 |
expect_false(results$pkgC$success) |
| 106 |
expect_equal(results$pkgC$output, "worker died") |
| 107 |
|
| 108 |
}) |
| 109 |
|
| 110 |
test_that("we can communicate with a large number of child processes", { |
| 111 |
|
| 112 |
skip_on_cran() |
| 113 |
skip_on_ci() |
| 114 |
n <- 20L |
| 115 |
|
| 116 |
server <- tryCatch(renv_socket_server(), error = skip) |
| 117 |
defer(close(server$socket)) |
| 118 |
|
| 119 |
port <- server$port |
| 120 |
script <- renv_test_code({ |
| 121 |
renv:::summon() |
| 122 |
conn <- renv_socket_connect(port, open = "wb") |
| 123 |
defer(close(conn)) |
| 124 |
serialize(Sys.getpid(), connection = conn) |
| 125 |
}, list(port = port)) |
| 126 |
|
| 127 |
for (i in seq_len(n)) { |
| 128 |
system2( |
| 129 |
command = R(), |
| 130 |
args = c("--vanilla", "-s", "-f", renv_shell_path(script)), |
| 131 |
stdout = FALSE, |
| 132 |
stderr = FALSE, |
| 133 |
wait = FALSE |
| 134 |
) |
| 135 |
} |
| 136 |
|
| 137 |
stk <- stack() |
| 138 |
for (i in seq_len(n)) local({ |
| 139 |
conn <- renv_socket_accept(server$socket, open = "rb", timeout = 10) |
| 140 |
defer(close(conn)) |
| 141 |
stk$push(unserialize(conn)) |
| 142 |
}) |
| 143 |
|
| 144 |
expect_length(stk$data(), n) |
| 145 |
|
| 146 |
}) |