1
2test_that("external_process", {
3  px <- asNamespace("processx")$get_tool("px")
4  pxgen <- function(...) {
5    processx::process$new(
6      px,
7      c("outln", "foo", "errln", "bar"),
8      stdout = tempfile(),
9      stderr = tempfile(),
10      ...
11    )
12  }
13
14  afun <- function() {
15    when_all(external_process(pxgen), async_constant(13))
16  }
17
18  res <- synchronise(afun())
19  expect_equal(res[[1]]$status, 0L)
20  expect_match(res[[1]]$stdout, "foo\r?\n")
21  expect_match(res[[1]]$stderr, "bar\r?\n")
22  expect_false(res[[1]]$timeout)
23  expect_equal(res[[2]], 13)
24})
25
26test_that("cancel external_process", {
27  px <- asNamespace("processx")$get_tool("px")
28  proc <- NULL
29  pxgen <- function(...) {
30    proc <<- processx::process$new(
31      px,
32      c("sleep", "5"),
33      stdout = tempfile(),
34      stderr = tempfile(),
35      ...
36    )
37  }
38
39  running <- NULL
40  afun <- function() {
41    when_all(
42      external_process(pxgen),
43      delay(0.001)$
44        then(function() {
45          limit <- Sys.time() + as.difftime(2, units = "secs")
46          while (Sys.time() < limit && !proc$is_alive()) Sys.sleep(0.1)
47          running <<- proc$is_alive()
48        })$
49        then(function() stop("failed"))
50    )
51  }
52
53  expect_error(synchronise(afun()))
54  expect_true(running)
55
56  limit <- Sys.time() + as.difftime(2, units = "secs")
57  while (Sys.time() < limit && proc$is_alive()) Sys.sleep(0.1)
58  expect_false(proc$is_alive())
59})
60
61test_that("discarding stdout/stderr works", {
62  px <- asNamespace("processx")$get_tool("px")
63  pxgen <- function(...) {
64    processx::process$new(
65      px,
66      c("outln", "foo", "errln", "bar"),
67      stdout = NULL,
68      stderr = NULL,
69      ...
70    )
71  }
72
73  afun <- function() external_process(pxgen)
74
75  res <- synchronise(afun())
76  expect_equal(res$status, 0L)
77  expect_null(res$stdout)
78  expect_null(res$stderr)
79  expect_false(res$timeout)
80})
81