1
2context("pid reuse")
3
4test_that("pid reuse", {
5  ## This is simulated, because it is quite some work to force a pid
6  ## reuse on some systems. So we create a handle with the pid of a
7  ## running process, but wrong (earlier) create time stamp.
8
9  z <- processx::process$new(px(), c("sleep", "600"))
10  on.exit(z$kill(), add = TRUE)
11  zpid <- z$get_pid()
12
13  ctime <- Sys.time() - 60
14  attr(ctime, "tzone") <- "GMT"
15  p <- ps_handle(zpid, ctime)
16
17  expect_match(format(p), format_regexp())
18  expect_output(print(p), format_regexp())
19
20  expect_equal(ps_pid(p), zpid)
21  expect_equal(ps_create_time(p), ctime)
22  expect_false(ps_is_running(p))
23
24  chk <- function(expr) {
25    err <- tryCatch(expr, error = function(e) e)
26    expect_s3_class(err, "no_such_process")
27    expect_s3_class(err, "ps_error")
28    expect_equal(err$pid, zpid)
29  }
30
31  ## All these error out with "no_such_process"
32  chk(ps_status(p))
33  chk(ps_ppid(p))
34  chk(ps_parent(p))
35  chk(ps_name(p))
36  if (ps_os_type()[["POSIX"]]) chk(ps_uids(p))
37  chk(ps_username(p))
38  if (ps_os_type()[["POSIX"]]) chk(ps_gids(p))
39  chk(ps_terminal(p))
40
41  if (ps_os_type()[["POSIX"]]) chk(ps_send_signal(p, signals()$SIGINT))
42  chk(ps_suspend(p))
43  chk(ps_resume(p))
44  if (ps_os_type()[["POSIX"]]) chk(ps_terminate(p))
45  chk(ps_kill(p))
46
47  chk(ps_exe(p))
48  chk(ps_cmdline(p))
49  chk(ps_environ(p))
50  chk(ps_cwd(p))
51  chk(ps_memory_info(p))
52  chk(ps_cpu_times(p))
53  chk(ps_num_threads(p))
54  chk(ps_num_fds(p))
55  chk(ps_open_files(p))
56  chk(ps_connections(p))
57})
58