Skip to content

Commit 496f827

Browse files
authored
Improve snapshotting cleanup in parallel tests (#1800)
We cannot forward all work of the snapshot reporter to the main process, because `expect_snapshot_helper()` uses the return value of the `take_snapshot()` method. So this must be called in the subprocess. This is also good for performance, because the snapshot comparison code still runs in the subprocess, and we don't copy potentially large objects between processes. The disadvantage is that we need slightly more complicated reporters, both in the subprocess and in the main process, to make sure that both processes are doing the right type of snapshot cleanup. Closes #1797. Simplifies and improves #1788 and #1793.
1 parent 3ac8a78 commit 496f827

14 files changed

+271
-45
lines changed

R/parallel.R

+58-23
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@ test_files_parallel <- function(
4545
load_package = c("none", "installed", "source")
4646
) {
4747

48-
49-
reporters <- test_files_reporter(reporter, parallel = TRUE)
50-
5148
# TODO: support timeouts. 20-30s for each file by default?
5249

5350
num_workers <- min(default_num_cpus(), length(test_paths))
@@ -71,6 +68,7 @@ test_files_parallel <- function(
7168
)
7269

7370
withr::with_dir(test_dir, {
71+
reporters <- test_files_reporter_parallel(reporter)
7472
with_reporter(reporters$multi, {
7573
parallel_updates <- reporter$capabilities$parallel_updates
7674
if (parallel_updates) {
@@ -79,11 +77,29 @@ test_files_parallel <- function(
7977
parallel_event_loop_chunky(queue, reporters, ".")
8078
}
8179
})
80+
81+
test_files_check(reporters$list$get_results(),
82+
stop_on_failure = stop_on_failure,
83+
stop_on_warning = stop_on_warning
84+
)
8285
})
86+
}
8387

84-
test_files_check(reporters$list$get_results(),
85-
stop_on_failure = stop_on_failure,
86-
stop_on_warning = stop_on_warning
88+
test_files_reporter_parallel <- function(reporter, .env = parent.frame()) {
89+
lister <- ListReporter$new()
90+
snapshotter <- MainprocessSnapshotReporter$new("_snaps", fail_on_new = FALSE)
91+
reporters <- list(
92+
find_reporter(reporter),
93+
lister, # track data
94+
snapshotter
95+
)
96+
withr::local_options(
97+
"testthat.snapshotter" = snapshotter,
98+
.local_envir = .env
99+
)
100+
list(
101+
multi = MultiReporter$new(reporters = compact(reporters)),
102+
list = lister
87103
)
88104
}
89105

@@ -133,21 +149,21 @@ parallel_event_loop_smooth <- function(queue, reporters, test_dir) {
133149
}
134150

135151
if (m$cmd != "DONE") {
136-
# Set working directory so expect_location() generates correct links
137-
withr::with_dir(test_dir, {
138-
reporters$multi$start_file(m$filename)
152+
reporters$multi$start_file(m$filename)
153+
reporters$multi$start_test(m$context, m$test)
154+
if (m$type == "snapshotter") {
155+
snapshotter <- getOption("testthat.snapshotter")
156+
do.call(snapshotter[[m$cmd]], m$args)
157+
} else {
139158
do.call(reporters$multi[[m$cmd]], m$args)
140159
updated <- TRUE
141-
})
160+
}
142161
}
143162
}
144163

145164
# We need to spin, even if there were no events
146165
if (!updated) {
147-
# Set working directory so expect_location() generates correct links
148-
withr::with_dir(test_dir, {
149-
reporters$multi$update()
150-
})
166+
reporters$multi$update()
151167
}
152168
}
153169
}
@@ -172,20 +188,22 @@ parallel_event_loop_chunky <- function(queue, reporters, test_dir) {
172188
if (m$cmd != "DONE") {
173189
files[[m$filename]] <- append(files[[m$filename]], list(m))
174190
} else {
175-
# Set working directory so expect_location() generates correct links
176-
withr::with_dir(test_dir, {
177-
replay_events(reporters$multi, files[[m$filename]])
178-
reporters$multi$end_context_if_started()
179-
})
191+
replay_events(reporters$multi, files[[m$filename]])
192+
reporters$multi$end_context_if_started()
180193
files[[m$filename]] <- NULL
181194
}
182195
}
183196
}
184197
}
185198

186199
replay_events <- function(reporter, events) {
187-
for (event in events) {
188-
do.call(reporter[[event$cmd]], event$args)
200+
snapshotter <- getOption("testthat.snapshotter")
201+
for (m in events) {
202+
if (m$type == "snapshotter") {
203+
do.call(snapshotter[[m$cmd]], m$args)
204+
} else {
205+
do.call(reporter[[m$cmd]], m$args)
206+
}
189207
}
190208
}
191209

@@ -254,8 +272,17 @@ queue_task <- function(path) {
254272
env <- .GlobalEnv$.test_env
255273

256274
withr::local_envvar("TESTTHAT_IS_PARALLEL" = "true")
257-
reporters <- test_files_reporter(SubprocessReporter$new())
258-
with_reporter(reporters$multi, test_one_file(path, env = env))
275+
snapshotter <- SubprocessSnapshotReporter$new(
276+
snap_dir = "_snaps",
277+
fail_on_new = FALSE
278+
)
279+
withr::local_options(testthat.snapshotter = snapshotter)
280+
reporters <- list(
281+
SubprocessReporter$new(),
282+
snapshotter
283+
)
284+
multi <- MultiReporter$new(reporters = reporters)
285+
with_reporter(multi, test_one_file(path, env = env))
259286
NULL
260287
}
261288

@@ -326,9 +353,12 @@ SubprocessReporter <- R6::R6Class("SubprocessReporter",
326353
private$event("start_file", filename)
327354
},
328355
start_test = function(context, test) {
356+
private$context <- context
357+
private$test <- test
329358
private$event("start_test", context, test)
330359
},
331360
start_context = function(context) {
361+
private$context <- context
332362
private$event("start_context", context)
333363
},
334364
add_result = function(context, test, result) {
@@ -355,11 +385,16 @@ SubprocessReporter <- R6::R6Class("SubprocessReporter",
355385

356386
private = list(
357387
filename = NULL,
388+
context = NULL,
389+
test = NULL,
358390
event = function(cmd, ...) {
359391
msg <- list(
360392
code = PROCESS_MSG,
393+
type = "reporter",
361394
cmd = cmd,
362395
filename = private$filename,
396+
context = private$context,
397+
test = private$test,
363398
time = proc.time()[[3]],
364399
args = list(...)
365400
)

R/snapshot-reporter-parallel.R

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
2+
MainprocessSnapshotReporter <- R6::R6Class("MainprocessSnapshotReporter",
3+
inherit = SnapshotReporter,
4+
public = list(
5+
end_file = function() {
6+
# No thing to do, this is done in the subprocess
7+
}
8+
)
9+
)
10+
11+
SubprocessSnapshotReporter <- R6::R6Class("SubprocessSnapshotReporter",
12+
inherit = SnapshotReporter,
13+
public = list(
14+
start_file = function(path, test = NULL) {
15+
private$filename <- path
16+
private$test <- test
17+
super$start_file(path, test)
18+
},
19+
20+
end_file = function() {
21+
private$filename <- NULL
22+
super$end_file()
23+
},
24+
25+
end_context = function(context) {
26+
private$context <- NULL
27+
super$end_context()
28+
},
29+
30+
end_test = function(context, test) {
31+
private$context <- NULL
32+
private$test <- NULL
33+
super$end_test(context, test)
34+
},
35+
36+
start_test = function(context, test) {
37+
private$context <- context
38+
private$test <- test
39+
super$start_test(context, test)
40+
},
41+
42+
announce_file_snapshot = function(name) {
43+
private$event("announce_file_snapshot", name)
44+
super$announce_file_snapshot(name)
45+
},
46+
47+
end_reporter = function() {
48+
# do not delete unused snapshots, that's up to the main process
49+
}
50+
),
51+
private = list(
52+
filename = NULL,
53+
context = NULL,
54+
test = NULL,
55+
event = function(cmd, ...) {
56+
msg <- list(
57+
code = PROCESS_MSG,
58+
type = "snapshotter",
59+
cmd = cmd,
60+
filename = private$filename,
61+
context = context,
62+
test = test,
63+
time = NULL,
64+
args = list(...)
65+
)
66+
class(msg) <- c("testthat_message", "callr_message", "condition")
67+
signalCondition(msg)
68+
}
69+
)
70+
)

R/snapshot-reporter.R

+3-14
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,14 @@ SnapshotReporter <- R6::R6Class("SnapshotReporter",
99
snap_file_seen = character(),
1010
variants_changed = FALSE,
1111
fail_on_new = FALSE,
12-
parallel = FALSE,
1312

1413
old_snaps = NULL,
1514
cur_snaps = NULL,
1615
new_snaps = NULL,
1716

18-
initialize = function(snap_dir = "_snaps", fail_on_new = FALSE, parallel = FALSE) {
17+
initialize = function(snap_dir = "_snaps", fail_on_new = FALSE) {
1918
self$snap_dir <- normalizePath(snap_dir, mustWork = FALSE)
2019
self$fail_on_new <- fail_on_new
21-
self$parallel <- parallel
2220
},
2321

2422
start_file = function(path, test = NULL) {
@@ -129,11 +127,6 @@ SnapshotReporter <- R6::R6Class("SnapshotReporter",
129127
},
130128

131129
end_file = function() {
132-
if (self$parallel) {
133-
# This is only needs to be done in the child threads.
134-
return()
135-
}
136-
137130
dir.create(self$snap_dir, showWarnings = FALSE)
138131

139132
self$cur_snaps$write()
@@ -200,13 +193,9 @@ get_snapshotter <- function() {
200193
#'
201194
#' @export
202195
#' @keywords internal
203-
local_snapshotter <- function(snap_dir = NULL, cleanup = FALSE, fail_on_new = FALSE, parallel = FALSE, .env = parent.frame()) {
196+
local_snapshotter <- function(snap_dir = NULL, cleanup = FALSE, fail_on_new = FALSE, .env = parent.frame()) {
204197
snap_dir <- snap_dir %||% withr::local_tempdir(.local_envir = .env)
205-
reporter <- SnapshotReporter$new(
206-
snap_dir = snap_dir,
207-
fail_on_new = fail_on_new,
208-
parallel = parallel
209-
)
198+
reporter <- SnapshotReporter$new(snap_dir = snap_dir, fail_on_new = fail_on_new)
210199
if (!identical(cleanup, FALSE)) {
211200
warn("`cleanup` is deprecated")
212201
}

R/test-files.R

+2-7
Original file line numberDiff line numberDiff line change
@@ -277,17 +277,12 @@ test_files_setup_state <- function(
277277
withr::defer(source_test_teardown(".", env), frame) # old school
278278
}
279279

280-
test_files_reporter <- function(reporter, .env = parent.frame(), parallel = FALSE) {
280+
test_files_reporter <- function(reporter, .env = parent.frame()) {
281281
lister <- ListReporter$new()
282282
reporters <- list(
283283
find_reporter(reporter),
284284
lister, # track data
285-
local_snapshotter(
286-
"_snaps",
287-
fail_on_new = FALSE,
288-
parallel = parallel,
289-
.env = .env
290-
)
285+
local_snapshotter("_snaps", fail_on_new = FALSE, .env = .env)
291286
)
292287
list(
293288
multi = MultiReporter$new(reporters = compact(reporters)),

man/local_snapshotter.Rd

-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-parallel.R

+83
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,86 @@ test_that("ok", {
3434
expect_equal(tdf$failed, c(0,1,0))
3535
expect_equal(tdf$skipped, c(FALSE, FALSE, TRUE))
3636
})
37+
38+
test_that("snapshots", {
39+
withr::local_envvar(c(TESTTHAT_PARALLEL = "TRUE"))
40+
on.exit(unlink(tmp, recursive = TRUE), add = TRUE)
41+
dir.create(tmp <- tempfile("testthat-snap-"))
42+
file.copy(test_path("test-parallel", "snap"), tmp, recursive = TRUE)
43+
suppressMessages(ret <- test_local(
44+
file.path(tmp, "snap"),
45+
reporter = "silent",
46+
stop_on_failure = FALSE
47+
))
48+
tdf <- as.data.frame(ret)
49+
tdf <- tdf[order(tdf$file), ]
50+
expect_equal(tdf$failed, c(0,0,1))
51+
snaps <- file.path(tmp, "snap", "tests", "testthat", "_snaps")
52+
expect_true(file.exists(file.path(snaps, "snap-1.md")))
53+
expect_true(file.exists(file.path(snaps, "snap-2.md")))
54+
expect_true(file.exists(file.path(snaps, "snap-3.md")))
55+
})
56+
57+
test_that("new snapshots are added", {
58+
withr::local_envvar(c(TESTTHAT_PARALLEL = "TRUE"))
59+
on.exit(unlink(tmp, recursive = TRUE), add = TRUE)
60+
dir.create(tmp <- tempfile("testthat-snap-"))
61+
file.copy(test_path("test-parallel", "snap"), tmp, recursive = TRUE)
62+
unlink(file.path(tmp, "snap", "tests", "testthat", "_snaps", "snap-2.md"))
63+
suppressMessages(ret <- test_local(
64+
file.path(tmp, "snap"),
65+
reporter = "silent",
66+
stop_on_failure = FALSE
67+
))
68+
tdf <- as.data.frame(ret)
69+
tdf <- tdf[order(tdf$file), ]
70+
expect_equal(tdf$failed, c(0,0,1))
71+
snaps <- file.path(tmp, "snap", "tests", "testthat", "_snaps")
72+
expect_true(file.exists(file.path(snaps, "snap-1.md")))
73+
expect_true(file.exists(file.path(snaps, "snap-2.md")))
74+
expect_true(file.exists(file.path(snaps, "snap-3.md")))
75+
})
76+
77+
test_that("snapshots are removed if test file has no snapshots", {
78+
withr::local_envvar(c(TESTTHAT_PARALLEL = "TRUE"))
79+
on.exit(unlink(tmp, recursive = TRUE), add = TRUE)
80+
dir.create(tmp <- tempfile("testthat-snap-"))
81+
file.copy(test_path("test-parallel", "snap"), tmp, recursive = TRUE)
82+
writeLines(
83+
"test_that(\"2\", { expect_true(TRUE) })",
84+
file.path(tmp, "snap", "tests", "testthat", "test-snap-2.R")
85+
)
86+
suppressMessages(ret <- test_local(
87+
file.path(tmp, "snap"),
88+
reporter = "silent",
89+
stop_on_failure = FALSE
90+
))
91+
tdf <- as.data.frame(ret)
92+
tdf <- tdf[order(tdf$file), ]
93+
expect_equal(tdf$failed, c(0,0,1))
94+
snaps <- file.path(tmp, "snap", "tests", "testthat", "_snaps")
95+
expect_true(file.exists(file.path(snaps, "snap-1.md")))
96+
expect_false(file.exists(file.path(snaps, "snap-2.md")))
97+
expect_true(file.exists(file.path(snaps, "snap-3.md")))
98+
})
99+
100+
test_that("snapshots are removed if test file is removed", {
101+
withr::local_envvar(c(TESTTHAT_PARALLEL = "TRUE"))
102+
on.exit(unlink(tmp, recursive = TRUE), add = TRUE)
103+
dir.create(tmp <- tempfile("testthat-snap-"))
104+
file.copy(test_path("test-parallel", "snap"), tmp, recursive = TRUE)
105+
unlink(file.path(tmp, "snap", "tests", "testthat", "test-snap-2.R"))
106+
withr::local_envvar(CI = NA_character_)
107+
suppressMessages(ret <- test_local(
108+
file.path(tmp, "snap"),
109+
reporter = "silent",
110+
stop_on_failure = FALSE
111+
))
112+
tdf <- as.data.frame(ret)
113+
tdf <- tdf[order(tdf$file), ]
114+
expect_equal(tdf$failed, c(0,1))
115+
snaps <- file.path(tmp, "snap", "tests", "testthat", "_snaps")
116+
expect_true(file.exists(file.path(snaps, "snap-1.md")))
117+
expect_false(file.exists(file.path(snaps, "snap-2.md")))
118+
expect_true(file.exists(file.path(snaps, "snap-3.md")))
119+
})

0 commit comments

Comments
 (0)