rstudio / shinytest

Automated testing for shiny apps
https://rstudio.github.io/shinytest/
Other
225 stars 55 forks source link

More robust approach to wait for flush cycle #349

Open hadley opened 4 years ago

hadley commented 4 years ago

One approach from @jcheng5:

waitForEventLoop <- function(code, timeout) {
  # recorded by js handler that counts number of times busy event has been fired
  runs <- element$private$event_count()

  code

  # wait until busy cycles increased by one; timing out in case nothing
  # happened
  timeout_by <- proc.time()[[3]] + timeout
  while(proc.time()[[3]] < timeout_by) {
    if (element$private$event_count() > runs) {
      return()
    }
    Sys.sleep(0.1)
  }
  stop("Timed out")
}

This has the advantage of not needing the name of an output you expect to be updated.

Still suffers from the downside that if the reactive code runs (e.g.) insertUI(), another independent run of the event loop will trigger, and this isn't guaranteed to wait for it.

hadley commented 4 years ago

This is important for widget$click(), widget$sendKeys() and app$setWindowSize() since these can trigger reactive updates, but don't currently automatically wait for the event loop to complete. app$setInputs() already has a custom pathway.