Skip to content

Commit

Permalink
Add dynamic AppDriver$stop(timeout=) support for {covr} (#259)
Browse files Browse the repository at this point in the history
  • Loading branch information
schloerke authored Sep 13, 2022
1 parent 995f977 commit cca51ed
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@

* Fix documentation on on `AppDriver`'s `delay` parameter being in **seconds**, not *milli*seconds (#255)

* Added support for `AppDriver$stop(timeout=)`. The default timeout when sending `SIGINT`, `SIGTERM`, and `SIGKILL` signals to the `{shiny}` process is now 500ms. However, if `{covr}` is running, the default timeout is 20,000 ms to allow time for the report to generate. (#259)


# shinytest2 0.1.1

Expand Down
28 changes: 24 additions & 4 deletions R/app-driver-stop.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@

app_stop <- function(self, private) {
app_stop <- function(self, private, timeout = missing_arg()) {
"!DEBUG AppDriver$stop"
ckm8_assert_app_driver(self, private)

timeout <- rlang::maybe_missing(timeout, {
# Increased the timeout for packages like covr to upload their results:
# https://github.com/rstudio/shinytest2/issues/250
# Taken from `covr::in_covr()`
in_covr <- identical(Sys.getenv("R_COVR"), "true")
if (in_covr) {
20 * 1000
} else {
500
}
})
ckm8_assert_single_number(timeout, lower = 0, finite = TRUE)

if (private$state == "stopped") {
return(invisible(private$shiny_proc_value))
}
Expand All @@ -27,10 +40,17 @@ app_stop <- function(self, private) {
# SIGINT quits the Shiny application, SIGTERM tells R to quit.
# Unfortunately, SIGTERM isn't quite the same as `q()`, because
# finalizers with onexit=TRUE don't seem to run.
private$shiny_process$signal(tools::SIGINT)
private$shiny_process$wait(500)

# Using private$shiny_process$interrupt() to send SIGNAL 2 (SIGINT) to the process
# https://github.com/r-lib/processx/blob/84301784382296217e7f5d11e1116dc4e24da809/R/process.R#L276-L283
# https://github.com/r-lib/covr/issues/277#issuecomment-555502769
# https://github.com/rstudio/shinytest2/issues/250
private$shiny_process$interrupt()
private$shiny_process$wait(timeout)

private$shiny_process$signal(tools::SIGTERM)
private$shiny_process$wait(250)
private$shiny_process$wait(timeout)

private$shiny_process$kill()
},
error = function(e) {
Expand Down
8 changes: 6 additions & 2 deletions R/app-driver.R
Original file line number Diff line number Diff line change
Expand Up @@ -1662,6 +1662,10 @@ AppDriver <- R6Class( # nolint
#' Typically, this can be paired with a button that when clicked will call
#' `shiny::stopApp(info)` to return `info` from the test app back to the
#' main R session.
#' @param timeout Milliseconds to wait between sending a SIGINT, SIGTERM,
#' and SIGKILL to the Shiny process. Defaults to 500ms. However, if
#' \pkg{covr} is currently executing, then the `timeout` is set to
#' 20,000ms to allow for the coverage report to be generated.
#' @return The result of the background process if the Shiny application has
#' already been terminated.
#' @examples
Expand Down Expand Up @@ -1711,8 +1715,8 @@ AppDriver <- R6Class( # nolint
#' #> ..$ session: chr "bdc7417f2fc8c84fc05c9518e36fdc44"
#' #> ..$ time : num 1.65e+09
#' }
stop = function() {
app_stop(self, private)
stop = function(timeout = missing_arg()) {
app_stop(self, private, timeout = timeout)
}
)
)
12 changes: 11 additions & 1 deletion man/AppDriver.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cca51ed

Please sign in to comment.