Nothing
cli_fixture_request <- function(content = character(), timeout = 5) {
list(backend = list(id = "fixture"), executable = file.path(R.home("bin"), "Rscript"),
content = content, timeout = timeout, working_directory = tempdir(), env = NULL)
}
test_that("CLI runner streams exact stdin independently from stdout and stderr", {
# Starts subprocesses; too slow and timing-sensitive for CRAN machines.
skip_on_cran()
script <- withr::local_tempfile(fileext = ".R")
writeLines(c(
"input <- readBin(file('stdin', 'rb'), 'raw', n = 1e7)",
"cat(rawToChar(input))",
"cat(strrep('e', 100000), file = stderr())"
), script)
content <- c(paste0('"\U0001f600"', strrep("x", 100000)), "")
request <- cli_fixture_request(content)
result <- backend_cli_run(request, c("--vanilla", script))
expect_identical(result$status, 0L)
# R's text-mode stdout uses CRLF on Windows; stdin bytes remain unchanged.
expect_equal(gsub("\r\n", "\n", result$stdout, fixed = TRUE), paste(content, collapse = "\n"))
expect_equal(nchar(result$stderr), 100000)
})
test_that("CLI runner uses explicit cwd, environment, arguments and timeout", {
# Starts subprocesses; too slow and timing-sensitive for CRAN machines.
skip_on_cran()
directory <- withr::local_tempdir()
script <- withr::local_tempfile(fileext = ".R")
writeLines("cat(getwd(), Sys.getenv('LANGUAGESERVER_CLI_TEST'), commandArgs(TRUE), sep = '\\n')", script)
request <- cli_fixture_request()
request$working_directory <- directory
request$env <- c(LANGUAGESERVER_CLI_TEST = "value")
result <- backend_cli_run(request, c("--vanilla", script, "argument with spaces"))
expect_equal(strsplit(gsub("\r\n", "\n", result$stdout, fixed = TRUE), "\n", fixed = TRUE)[[1L]],
c(normalizePath(directory, winslash = "/"), "value", "argument with spaces"))
request$timeout <- 0.2
expect_error(backend_cli_run(request, c("--vanilla", "-e", "Sys.sleep(10)")), "timed out")
})
test_that("CLI runner drains fast-exiting stdout and stderr through partial reads to EOF", {
# Starts subprocesses; too slow and timing-sensitive for CRAN machines.
skip_on_cran()
script <- withr::local_tempfile(fileext = ".R")
writeLines(c(
"for (i in 1:32) {",
" cat(strrep('output', 16384))",
" cat(strrep('error', 16384), file = stderr())",
"}"
), script)
native_process <- processx::process
# A text read may return just a portion of the bytes buffered in a pipe.
partial_process <- R6::R6Class("PartialReadProcess", inherit = native_process,
public = list(read_output = function(n = -1) super$read_output(4096),
read_error = function(n = -1) super$read_error(4096)))
testthat::local_mocked_bindings(process = partial_process, .package = "processx")
result <- backend_cli_run(cli_fixture_request(timeout = 30), c("--vanilla", script))
expect_identical(result$status, 0L)
expect_identical(result$stdout, strrep("output", 16384 * 32))
expect_identical(result$stderr, strrep("error", 16384 * 32))
})
test_that("CLI timeout also bounds pipe draining after the process exits", {
state <- new.env(parent = emptyenv())
state$killed <- FALSE
process <- list(read_output = function() "", read_error = function() "",
is_alive = function() FALSE, is_incomplete_output = function() TRUE,
is_incomplete_error = function() FALSE,
poll_io = function(timeout) Sys.sleep(timeout / 1000),
kill = function(...) state$killed <- TRUE)
testthat::local_mocked_bindings(process = list(new = function(...) process), .package = "processx")
expect_error(backend_cli_run(cli_fixture_request(timeout = 0.05), character()), "timed out")
expect_true(state$killed)
})
test_that("slow descendant enumeration leaves the CLI time to drain its output", {
clock <- new.env(parent = emptyenv())
clock$elapsed <- 0
clock$output <- clock$error <- 500L
fixture_process <- list(
read_output = function() {
clock$elapsed <- clock$elapsed + 0.0001
if (!clock$output) return("")
clock$output <- clock$output - 1L
strrep("x", 4096)
},
read_error = function() {
clock$elapsed <- clock$elapsed + 0.0001
if (!clock$error) return("")
clock$error <- clock$error - 1L
strrep("e", 4096)
},
is_alive = function() clock$output > 0L || clock$error > 0L,
is_incomplete_output = function() clock$output > 0L,
is_incomplete_error = function() clock$error > 0L,
poll_io = function(timeout) clock$elapsed <- clock$elapsed + timeout / 1000,
get_exit_status = function() 0L,
kill = function(...) NULL)
runner <- backend_cli_run
stub(runner, "proc.time", function() c(0, 0, clock$elapsed))
stub(runner, "task_process_descendants", function(process, previous) {
# Model an expensive process-table query without depending on machine speed.
clock$elapsed <- clock$elapsed + 0.02
previous
})
testthat::local_mocked_bindings(process = list(new = function(...) fixture_process), .package = "processx")
result <- runner(cli_fixture_request(timeout = 1), character())
expect_identical(result$status, 0L)
expect_identical(result$stdout, strrep("x", 4096 * 500))
expect_identical(result$stderr, strrep("e", 4096 * 500))
expect_lt(clock$elapsed, 1)
})
test_that("CLI descendants stop on cancellation, supersession, shutdown, worker death and timeout", {
skip_on_cran()
skip_if(identical(Sys.getenv("R_COVR"), "true"))
skip_if_not_installed("ps")
script <- withr::local_tempfile(fileext = ".R")
writeLines(c(
"child <- processx::process$new(file.path(R.home('bin'), 'Rscript'),",
" c('--vanilla', '-e', \"writeLines('ready', commandArgs(TRUE)[[1L]]); Sys.sleep(30)\",",
" paste0(commandArgs(TRUE)[[1L]], '.child')), cleanup = FALSE)",
"while (!file.exists(paste0(commandArgs(TRUE)[[1L]], '.child'))) Sys.sleep(0.01)",
"saveRDS(c(parent = Sys.getpid(), child = child$get_pid()), commandArgs(TRUE)[[1L]])",
"Sys.sleep(30)"
), script)
for (mode in c("cancel", "supersede", "stop", "worker_death", "timeout")) {
marker <- withr::local_tempfile()
withr::defer(unlink(paste0(marker, ".child")))
request <- cli_fixture_request(timeout = if (mode == "timeout") 1.5 else 30)
manager <- TaskManager$new(paste("CLI", mode), use_session = TRUE,
min_idle_sessions = 0, max_running_tasks = 1)
withr::defer(manager$stop())
manager$add_task("cli", create_task(package_call(backend_cli_run),
list(request = request, arguments = c("--vanilla", script, marker)),
track_descendants = TRUE))
deadline <- Sys.time() + 10
while (!file.exists(marker) && Sys.time() < deadline) {
manager$run_tasks()
manager$check_tasks()
Sys.sleep(0.01)
}
expect_true(file.exists(marker), info = mode)
if (!file.exists(marker)) next
handles <- lapply(readRDS(marker), ps::ps_handle)
withr::defer(for (handle in handles) try(ps::ps_kill(handle), silent = TRUE))
# The parent has observed a fully started process tree before failure.
Sys.sleep(0.05)
manager$check_tasks()
switch(mode,
cancel = manager$cancel("cli"),
supersede = manager$add_task("cli", create_task(function() list(), list())),
stop = manager$stop(),
worker_death = manager$.__enclos_env__$private$running_tasks$get("cli")$
.__enclos_env__$private$session$kill(grace = 0, close_connections = FALSE),
timeout = NULL)
alive <- function(handle) {
tryCatch(ps::ps_is_running(handle) && !ps::ps_status(handle) %in% c("zombie", "dead"),
no_such_process = function(e) FALSE)
}
deadline <- Sys.time() + 5
while (any(vapply(handles, alive, logical(1L))) && Sys.time() < deadline) {
manager$check_tasks()
Sys.sleep(0.02)
}
expect_false(any(vapply(handles, alive, logical(1L))), info = mode)
manager$stop()
}
})
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.