Compare commits

...

1 Commits

Author SHA1 Message Date
Garrick Aden-Buie
bd7e1a5203 Disable private seed in testmode
Fixes #3816

Enables deterministic tests when the `shiny.testmode` option
is TRUE or when an app is run with `test.mode = TRUE`.
2023-04-28 11:22:29 -04:00
2 changed files with 17 additions and 0 deletions

View File

@@ -52,6 +52,11 @@ repeatable <- function(rngfunc, seed = stats::runif(1, 0, .Machine$integer.max))
# Evaluate an expression using Shiny's own private stream of
# randomness (not affected by set.seed).
withPrivateSeed <- function(expr) {
# Don't use the private seed in testmode for deterministic tests
if (isTRUE(getOption("shiny.testmode", getShinyOption("testmode")))) {
return(expr)
}
# Save the old seed if present.
if (exists(".Random.seed", envir = .GlobalEnv, inherits = FALSE)) {
hasOrigSeed <- TRUE

View File

@@ -50,6 +50,18 @@ test_that("Setting the private seed explicitly results in identical values", {
expect_identical(id7, id8)
})
test_that("Private seed is disabled in testmode", {
oldopts <- options(shiny.testmode = TRUE)
on.exit(options(oldopts), add = TRUE)
set.seed(0)
id1 <- createUniqueId(4)
set.seed(0)
id2 <- createUniqueId(4)
expect_identical(id1, id2)
})
test_that("Private and 'public' random streams are independent and work the same", {
set.seed(0)
public <- c(runif(1), runif(1), runif(1))