Debian Codegraph @

tests/testthat/test-watchdog.R

raw
1
2 test_that("the watchdog process acquires and releases locks as expected", {
3
4 skip_on_cran()
5 skip_if(getRversion() < "4.0.0")
6
7 renv_scope_envvars(RENV_WATCHDOG_ENABLED = TRUE)
8
9 script <- renv_test_code({
10
11 renv:::summon()
12
13 path <- tempfile()
14 renv_watchdog_notify("LockAcquired", list(path = path))
15 locks <- renv_watchdog_request("ListLocks")
16 stopifnot(path %in% locks)
17
18 renv_watchdog_notify("LockReleased", list(path = path))
19 locks <- renv_watchdog_request("ListLocks")
20 stopifnot(!path %in% locks)
21
22 })
23
24 renv_scope_envvars(RENV_WATCHDOG_ENABLED = "TRUE")
25 output <- expect_no_error(
26 system2(
27 command = R(),
28 args = c("--vanilla", "-s", "-f", renv_shell_path(script)),
29 stdout = FALSE,
30 stderr = FALSE,
31 timeout = 10
32 )
33 )
34
35 })
36
37 test_that("the watchdog process releases locks from killed processes", {
38
39 skip_on_cran()
40 skip_if(getRversion() < "4.0.0")
41 skip_on_ci()
42
43 renv_scope_envvars(RENV_WATCHDOG_ENABLED = TRUE)
44
45 # start a socket server
46 server <- tryCatch(renv_socket_server(), error = skip)
47 defer(close(server$socket))
48
49 # acquire lock in a background process
50 path <- tempfile("renv-lock-")
51 expect_false(file.exists(path))
52
53 script <- renv_test_code({
54
55 renv:::summon()
56
57 # acquire lock in child process
58 renv_scope_lock(path)
59
60 # let parent know we have the lock
61 conn <- renv_socket_connect(port, open = "wb")
62 defer(close(conn))
63 serialize(Sys.getpid(), connection = conn)
64
65 # commit seppuku
66 command <- paste("kill -TERM", Sys.getpid())
67 system(command)
68
69 }, list(path = path, port = server$port))
70
71 renv_scope_envvars(RENV_WATCHDOG_ENABLED = "TRUE")
72 system2(
73 command = R(),
74 args = c("--vanilla", "-s", "-f", renv_shell_path(script)),
75 stdout = FALSE,
76 stderr = FALSE,
77 wait = FALSE
78 )
79
80 # get PID of child process
81 pid <- local({
82 conn <- renv_socket_accept(server$socket, open = "rb")
83 defer(close(conn))
84 unserialize(conn)
85 })
86
87 # wait for watchdog to try and remove the process
88 clock <- timer()
89 wait_until(function() { !file.exists(path) || clock$elapsed() > 3 })
90
91 # check that the file no longer exists
92 expect_false(file.exists(path))
93
94 })