| 1 |
|
| 2 |
test_that("locks can be acquired, released", { |
| 3 |
|
| 4 |
skip_on_cran() |
| 5 |
|
| 6 |
renv_scope_options(renv.config.locking.enabled = TRUE) |
| 7 |
path <- renv_lock_path(renv_scope_tempfile()) |
| 8 |
|
| 9 |
renv_lock_acquire(path) |
| 10 |
expect_true(file.exists(path)) |
| 11 |
|
| 12 |
renv_lock_release(path) |
| 13 |
expect_false(file.exists(path)) |
| 14 |
|
| 15 |
}) |
| 16 |
|
| 17 |
test_that("scoped locks are released appropriately", { |
| 18 |
|
| 19 |
skip_on_cran() |
| 20 |
|
| 21 |
renv_scope_options(renv.config.locking.enabled = TRUE) |
| 22 |
path <- renv_lock_path(renv_scope_tempfile()) |
| 23 |
|
| 24 |
local({ |
| 25 |
renv_scope_lock(path) |
| 26 |
expect_true(file.exists(path)) |
| 27 |
}) |
| 28 |
|
| 29 |
expect_false(file.exists(path)) |
| 30 |
|
| 31 |
}) |
| 32 |
|
| 33 |
test_that("we can recursively acquire locks", { |
| 34 |
|
| 35 |
skip_on_cran() |
| 36 |
|
| 37 |
renv_scope_options(renv.config.locking.enabled = TRUE) |
| 38 |
path <- renv_lock_path(renv_scope_tempfile()) |
| 39 |
|
| 40 |
local({ |
| 41 |
|
| 42 |
renv_scope_lock(path) |
| 43 |
expect_true(file.exists(path)) |
| 44 |
|
| 45 |
local({ |
| 46 |
renv_scope_lock(path) |
| 47 |
expect_true(file.exists(path)) |
| 48 |
}) |
| 49 |
|
| 50 |
expect_true(file.exists(path)) |
| 51 |
|
| 52 |
}) |
| 53 |
|
| 54 |
expect_false(file.exists(path)) |
| 55 |
|
| 56 |
}) |
| 57 |
|
| 58 |
test_that("other processes cannot lock our owned locks", { |
| 59 |
|
| 60 |
skip_on_cran() |
| 61 |
|
| 62 |
skip_if( |
| 63 |
is.null(formals(system2)[["timeout"]]), |
| 64 |
"system2() lacks the timeout argument" |
| 65 |
) |
| 66 |
|
| 67 |
renv_scope_options(renv.config.locking.enabled = TRUE) |
| 68 |
path <- renv_lock_path(renv_scope_tempfile()) |
| 69 |
|
| 70 |
renv_lock_acquire(path) |
| 71 |
|
| 72 |
script <- renv_test_code( |
| 73 |
print(renv:::renv_lock_acquire(path)), |
| 74 |
list(path = path) |
| 75 |
) |
| 76 |
|
| 77 |
args <- c("--vanilla", "-s", "-f", shQuote(script)) |
| 78 |
output <- suppressWarnings( |
| 79 |
system2(R(), args, stdout = FALSE, stderr = FALSE, timeout = 5L) |
| 80 |
) |
| 81 |
|
| 82 |
expect_equal(output, 124L) |
| 83 |
|
| 84 |
}) |
| 85 |
|
| 86 |
test_that("locks are released on process exit", { |
| 87 |
|
| 88 |
skip_on_cran() |
| 89 |
|
| 90 |
renv_scope_options(renv.config.locking.enabled = TRUE) |
| 91 |
path <- renv_lock_path(renv_scope_tempfile()) |
| 92 |
|
| 93 |
code <- expr({ |
| 94 |
renv_lock_acquire(!!path) |
| 95 |
stopifnot(file.exists(!!path)) |
| 96 |
}) |
| 97 |
|
| 98 |
args <- c("--vanilla", "-s", "-e", shQuote(stringify(code))) |
| 99 |
status <- suppressWarnings( |
| 100 |
system2(R(), args, stdout = FALSE, stderr = FALSE, timeout = 5L) |
| 101 |
) |
| 102 |
|
| 103 |
expect_equal(status, 1L) |
| 104 |
expect_false(file.exists(path)) |
| 105 |
|
| 106 |
}) |
| 107 |
|
| 108 |
test_that("we can refresh locks", { |
| 109 |
|
| 110 |
skip_on_cran() |
| 111 |
|
| 112 |
# create a file |
| 113 |
path <- renv_scope_tempfile("renv-lock-") |
| 114 |
file.create(path) |
| 115 |
|
| 116 |
# get current info |
| 117 |
old <- file.info(path, extra_cols = FALSE) |
| 118 |
|
| 119 |
# wait a bit |
| 120 |
Sys.sleep(2) |
| 121 |
|
| 122 |
# refresh the 'lock' |
| 123 |
renv_lock_refresh(path) |
| 124 |
new <- file.info(path, extra_cols = FALSE) |
| 125 |
|
| 126 |
# check for updated time |
| 127 |
expect_gt(new$mtime, old$mtime) |
| 128 |
|
| 129 |
}) |
| 130 |
|
| 131 |
|
| 132 |
test_that("old locks are considered 'orphaned'", { |
| 133 |
|
| 134 |
skip_on_cran() |
| 135 |
|
| 136 |
renv_scope_options(renv.config.locking.enabled = TRUE) |
| 137 |
renv_scope_envvars(RENV_WATCHDOG_ENABLED = "FALSE") |
| 138 |
|
| 139 |
path <- renv_lock_path(renv_scope_tempfile()) |
| 140 |
|
| 141 |
renv_scope_options(renv.lock.timeout = -1L) |
| 142 |
renv_lock_acquire(path) |
| 143 |
|
| 144 |
expect_true(renv_lock_orphaned(path)) |
| 145 |
expect_true(file.exists(path)) |
| 146 |
|
| 147 |
script <- renv_test_code({ |
| 148 |
options(renv.config.locking.enabled = TRUE) |
| 149 |
options(renv.lock.timeout = -1L) |
| 150 |
stopifnot(renv:::renv_lock_acquire(path)) |
| 151 |
stopifnot(file.exists(path)) |
| 152 |
}, list(path = path)) |
| 153 |
|
| 154 |
output <- renv_system_exec( |
| 155 |
command = R(), |
| 156 |
args = c("--vanilla", "-s", "-f", shQuote(script)), |
| 157 |
action = "checking for orphaned locks", |
| 158 |
) |
| 159 |
|
| 160 |
expect_false(file.exists(path)) |
| 161 |
|
| 162 |
}) |
| 163 |
|
| 164 |
test_that("multiple renv processes successfully acquire, release locks", { |
| 165 |
|
| 166 |
skip_on_cran() |
| 167 |
skip_if(getRversion() < "4.0.0") |
| 168 |
skip_on_os("windows") |
| 169 |
skip_on_ci() |
| 170 |
|
| 171 |
renv_scope_options(renv.config.locking.enabled = TRUE) |
| 172 |
renv_scope_envvars(RENV_WATCHDOG_ENABLED = "FALSE") |
| 173 |
|
| 174 |
# initialize server |
| 175 |
server <- tryCatch(renv_socket_server(), error = skip) |
| 176 |
defer(close(server$socket)) |
| 177 |
|
| 178 |
# initialize state |
| 179 |
n <- 100 |
| 180 |
start <- tempfile("renv-start-") |
| 181 |
lockfile <- renv_lock_path(tempfile("renv-lock-")) |
| 182 |
|
| 183 |
# initialize shared file |
| 184 |
shared <- renv_scope_tempfile("renv-file-") |
| 185 |
writeLines("0", con = shared) |
| 186 |
|
| 187 |
# generate runner script |
| 188 |
script <- renv_test_code( |
| 189 |
|
| 190 |
code = { |
| 191 |
|
| 192 |
renv:::summon() |
| 193 |
|
| 194 |
# wait for start file |
| 195 |
wait_until(file.exists, start) |
| 196 |
|
| 197 |
# helper function |
| 198 |
increment <- function() { |
| 199 |
renv_lock_acquire(lockfile) |
| 200 |
stopifnot(file.exists(lockfile)) |
| 201 |
number <- as.integer(readLines(shared)) |
| 202 |
writeLines(as.character(number + 1L), con = shared) |
| 203 |
renv_lock_release(lockfile) |
| 204 |
number |
| 205 |
} |
| 206 |
|
| 207 |
# update shared file with lock acquired |
| 208 |
number <- catch(increment()) |
| 209 |
if (inherits(number, "error")) |
| 210 |
number <- -1L |
| 211 |
|
| 212 |
# notify parent |
| 213 |
conn <- renv_socket_connect(port = port, open = "wb") |
| 214 |
defer(close(conn)) |
| 215 |
serialize(number, connection = conn) |
| 216 |
|
| 217 |
# we're done |
| 218 |
invisible() |
| 219 |
|
| 220 |
}, |
| 221 |
|
| 222 |
data = list( |
| 223 |
start = start, |
| 224 |
lockfile = lockfile, |
| 225 |
shared = shared, |
| 226 |
port = server$port |
| 227 |
) |
| 228 |
|
| 229 |
) |
| 230 |
|
| 231 |
# create start file |
| 232 |
file.create(start) |
| 233 |
|
| 234 |
# create a bunch of processes that try to update the shared file |
| 235 |
for (i in 1:n) { |
| 236 |
system2( |
| 237 |
command = R(), |
| 238 |
args = c("--vanilla", "-s", "-f", renv_shell_path(script)), |
| 239 |
wait = FALSE |
| 240 |
) |
| 241 |
} |
| 242 |
|
| 243 |
# wait for all the processes to communicate |
| 244 |
responses <- stack() |
| 245 |
for (i in 1:n) local({ |
| 246 |
conn <- renv_socket_accept(server$socket, open = "rb", timeout = 60) |
| 247 |
defer(close(conn)) |
| 248 |
responses$push(unserialize(conn)) |
| 249 |
}) |
| 250 |
|
| 251 |
# check that the count is correct |
| 252 |
contents <- readLines(shared) |
| 253 |
expect_equal(contents, as.character(n)) |
| 254 |
|
| 255 |
# check that each process saw a unique value |
| 256 |
numbers <- unlist(responses$data()) |
| 257 |
expect_equal(sort(numbers), 0:(n - 1)) |
| 258 |
|
| 259 |
}) |