Skip to content

Commit d31dd02

Browse files
TESTS: Adjust unit tests for new with_progress() warning [#178]
1 parent caa80df commit d31dd02

File tree

5 files changed

+42
-32
lines changed

5 files changed

+42
-32
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Package: progressr
2-
Version: 0.16.0-9010
2+
Version: 0.16.0-9012
33
Title: An Inclusive, Unifying API for Progress Updates
44
Description: A minimal, unifying API for scripts and packages to report progress updates from anywhere including when using parallel processing. The package is designed such that the developer can to focus on what progress should be reported on without having to worry about how to present it. The end user has full control of how, where, and when to render these progress updates, e.g. in the terminal using utils::txtProgressBar(), cli::cli_progress_bar(), in a graphical user interface using utils::winProgressBar(), tcltk::tkProgressBar() or shiny::withProgress(), via the speakers using beepr::beep(), or on a file system via the size of a file. Anyone can add additional, customized, progression handlers. The 'progressr' package uses R's condition framework for signaling progress updated. Because of this, progress can be reported from almost anywhere in R, e.g. from classical for and while loops, from map-reduce API:s like the lapply() family of functions, 'purrr', 'plyr', and 'foreach'. It will also work with parallel processing via the 'future' framework, e.g. future.apply::future_lapply(), furrr::future_map(), and 'foreach' with 'doFuture'. The package is compatible with Shiny applications.
55
Authors@R: c(person("Henrik", "Bengtsson",

R/global_progression_handler.R

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -217,25 +217,7 @@ global_progression_handler <- local({
217217
}
218218
} else if (type == "update") {
219219
if (is.null(current_progressor_uuid)) {
220-
## We might receive zero-amount progress updates after the fact that the
221-
## progress has been completed
222-
amount <- progression$amount
223-
if (!is.numeric(amount) || amount > 0) {
224-
## But otherwise, it might be a coding mistake ...
225-
226-
## unless the 'progression' signaled within with_progress(), which
227-
## in case it might be re-signaled such that it reaches the global
228-
## 'progression' handler here
229-
is_with_progress <- function(call) {
230-
identical(call, quote(with_progress)) || identical(call, quote(progressr::with_progress))
231-
}
232-
calls <- lapply(sys.calls(), FUN = .subset2, 1L)
233-
if (!any(vapply(calls, FUN = is_with_progress, FUN.VALUE = FALSE))) {
234-
msg <- conditionMessage(progression)
235-
if (length(msg) == 0) msg <- "character(0)"
236-
warning(sprintf("Received a progression %s request (amount=%g; msg=%s) but is not listening to this progressor. This can happen when code signals more progress updates than it configured the progressor to do. When the progressor completes all steps, it shuts down resulting in the global progression handler to no longer listen to it. To troubleshoot this, try with progressr::handlers(\"debug\")", sQuote(type), amount, sQuote(msg)))
237-
}
238-
}
220+
warn_about_too_many_progressions(progression, global = TRUE)
239221
return()
240222
}
241223

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
warn_about_too_many_progressions <- function(progression, global = FALSE) {
2+
## We might receive progress updates after the fact that the progressor
3+
## has been completed
4+
5+
## This is okay if it's a zero-amount progression
6+
amount <- progression$amount
7+
if (is.numeric(amount) && amount == 0) return()
8+
9+
## This is okay if it's signaled from within with_progress() and then
10+
## caught by the global 'progression' handler
11+
if (global) {
12+
## ... unless the 'progression' signaled within with_progress(), which
13+
## in case it might be re-signaled such that it reaches the global
14+
## 'progression' handler here
15+
is_with_progress <- function(call) {
16+
identical(call, quote(with_progress)) || identical(call, quote(progressr::with_progress))
17+
}
18+
calls <- lapply(sys.calls(), FUN = .subset2, 1L)
19+
if (any(vapply(calls, FUN = is_with_progress, FUN.VALUE = FALSE))) {
20+
return()
21+
}
22+
}
23+
24+
## In all cases, this is a mistake that we should warn about
25+
m <- conditionMessage(progression)
26+
m <- if (length(m) == 0) "character(0)" else sQuote(m)
27+
info <- sprintf("amount=%g; msg=%s", amount, m)
28+
msg <- if (global) "The global progression handlers" else "with_progress()"
29+
msg <- sprintf("%s received a progression %s request (%s), but is no longer listening to this progressor. This can happen when code signals more progress updates than steps in the progressor. To troubleshoot this, retry with progressr::handlers(\"debug\")", msg, sQuote(progression$type), info)
30+
warning(msg, call. = FALSE)
31+
} ## warn_about_too_many_progressions()

R/with_progress.R

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -233,16 +233,8 @@ with_progress <- function(expr, handlers = progressr::handlers(), cleanup = TRUE
233233
if (debug) message(sprintf("- received a %s (n=%g)", sQuote(class(p)[1]), progression_counter))
234234

235235
if (finished) {
236-
## We might receive zero-amount progress updates after the fact that
237-
## the progress has been completed ...
238-
amount <- p$amount
239-
if (!is.numeric(amount) || amount > 0) {
240-
## ... but otherwise, it might be a coding mistake
241-
msg <- conditionMessage(p)
242-
if (length(msg) == 0) msg <- "character(0)"
243-
type <- p$type
244-
warning(sprintf("Received a progression %s request (amount=%g; msg=%s) but is not listening to this progressor. This can happen when code signals more progress updates than it configured the progressor to do. When the progressor completes all steps, it shuts down resulting in the global progression handler to no longer listen to it. To troubleshoot this, try with progressr::handlers(\"debug\")", sQuote(type), amount, sQuote(msg)))
245-
}
236+
warn_about_too_many_progressions(p)
237+
return()
246238
}
247239

248240
## Don't capture conditions that are produced by progression handlers

tests/with_progress,relay.R

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,14 @@ for (kk in seq_along(handlers)) {
4242
}
4343
})
4444
}, classes = type)
45+
output <- relay$stdout
46+
message <- gsub("\n$", "", relay$msgs)
47+
if (delta > 0) {
48+
message <- grep("no longer listening to this progressor", message, invert = TRUE, value = TRUE)
49+
}
4550
stopifnot(
46-
identical(relay$stdout, truth),
47-
identical(gsub("\n$", "", relay$msgs), truth)
51+
identical(output, truth),
52+
identical(message, truth)
4853
)
4954
} ## for (delta ...)
5055
} ## for (signal ...)

0 commit comments

Comments
 (0)