tests/testthat/test-plot_worker.R

skip_if_no_daemons <- function() {
  testthat::skip_on_cran()
  testthat::skip_if_not_installed("mirai")
}

settle <- function(root, done, seconds = 60) {
  start <- Sys.time()
  deadline <- start + seconds
  while (!done() && Sys.time() < deadline) {
    later::run_now(0.1)
    if (!is.null(root)) root$flushReact()
  }
  if (!done()) {
    profile <- NACHO:::plot_pool$profile
    cat(
      "\nThe wait ended after",
      format(round(difftime(Sys.time(), start, units = "secs"), 1)),
      "and the state of the plot pool",
      if (is.null(profile)) "(no pool)" else profile,
      "is:\n"
    )
    if (!is.null(profile)) print(mirai::status(.compute = profile))
  }
  done()
}

idle <- function(root, seconds) {
  deadline <- Sys.time() + seconds
  while (Sys.time() < deadline) {
    later::run_now(0.1)
    if (!is.null(root)) root$flushReact()
  }
}

connected <- function(profile, count) {
  function() mirai::status(.compute = profile)$connections >= count
}

test_that("plot_cache_key() changes with any input", {
  size <- c(width = 5, height = 3.5)
  key <- function(type = "BD", options = list(size = 1), dark = FALSE) {
    NACHO:::plot_cache_key(GSE74821, type, options, dark, size)
  }
  base <- key()
  expect_type(base, "character")
  expect_identical(base, key())
  expect_false(identical(base, key(type = "FoV")))
  expect_false(identical(base, key(options = list(size = 2))))
  expect_false(identical(base, key(dark = TRUE)))
  expect_false(identical(
    base,
    NACHO:::plot_cache_key(
      GSE74821,
      "BD",
      list(size = 1),
      FALSE,
      c(width = 6, height = 3.5)
    )
  ))
})

test_that("plot_worker_count() keeps a core free and uses at most four", {
  expect_identical(NACHO:::plot_worker_count(10L, NULL), 4L)
  expect_identical(NACHO:::plot_worker_count(3L, NULL), 2L)
  expect_identical(NACHO:::plot_worker_count(1L, NULL), 1L)
  expect_identical(NACHO:::plot_worker_count(NA_integer_, NULL), 1L)
})

test_that("the nacho.plot_workers option caps or turns off the workers", {
  expect_identical(NACHO:::plot_worker_count(10L, 2), 2L)
  expect_identical(NACHO:::plot_worker_count(3L, 8), 2L)
  expect_identical(NACHO:::plot_worker_count(10L, 0), 0L)
  withr::local_options(nacho.plot_workers = 0)
  expect_null(NACHO:::plot_workers_start())
})

test_that("build_card() gives a ggplot for a static card", {
  plot <- NACHO:::build_card(
    GSE74821,
    "BD",
    list(colour = "CartridgeID"),
    FALSE,
    FALSE,
    c(width = 5, height = 3.5)
  )
  expect_s3_class(plot, "ggplot")
})

test_that("build_card() gives the same widget in a worker and in process", {
  skip_if_not_installed("ggiraph")
  skip_if_no_daemons()
  args <- list(
    GSE74821,
    "BD",
    list(colour = "CartridgeID", show_legend = TRUE, size = 1),
    FALSE,
    TRUE,
    c(width = 5, height = 3.5)
  )
  local <- withr::with_seed(
    1,
    do.call(NACHO:::build_card, args),
    .rng_kind = "Mersenne-Twister",
    .rng_normal_kind = "Inversion",
    .rng_sample_kind = "Rejection"
  )
  mirai::daemons(1, .compute = "nacho-test")
  on.exit(mirai::daemons(0, .compute = "nacho-test"), add = TRUE)
  remote <- mirai::mirai(
    {
      .libPaths(libs)
      set.seed(1, "Mersenne-Twister", "Inversion", "Rejection")
      do.call(NACHO:::build_card, args)
    },
    args = args,
    libs = .libPaths(),
    .compute = "nacho-test"
  )[]
  strip_ids <- function(html) gsub("svg_[0-9a-f]+", "svg", html)
  expect_identical(strip_ids(remote$x$html), strip_ids(local$x$html))
})

test_that("one worker pool serves the app until it stops", {
  skip_if_no_daemons()
  skip_if_not_installed("later")
  withr::defer(NACHO:::plot_workers_stop())
  withr::local_options(nacho.plot_workers = 2)
  profile <- NACHO:::plot_workers_start()
  expect_identical(NACHO:::plot_workers_start(), profile)
  expect_true(mirai::mirai(TRUE, .compute = profile)[])
  expect_true(settle(NULL, connected(profile, 2L), seconds = 30))
  NACHO:::plot_workers_stop()
  later::run_now(0)
  expect_identical(mirai::status(.compute = profile)$connections, 0L)
})

test_that("callbacks of a stopped pool do nothing to the next pool", {
  skip_if_no_daemons()
  skip_if_not_installed("later")
  withr::defer(NACHO:::plot_workers_stop())
  withr::local_options(nacho.plot_workers = 2, nacho.quiet = TRUE)
  warmed <- 0L
  local_mocked_bindings(
    everywhere = function(...) {
      warmed <<- warmed + 1L
      invisible(NULL)
    },
    .package = "mirai"
  )
  with_mocked_bindings(
    NACHO:::plot_workers_start(),
    launch_local = function(...) invisible(NULL),
    .package = "mirai"
  )
  NACHO:::plot_workers_stop()
  profile <- NACHO:::plot_workers_start()
  expect_true(settle(NULL, function() warmed > 0L, seconds = 30))
  idle(NULL, 1)
  expect_identical(warmed, 1L)
  expect_false(isTRUE(NACHO:::plot_pool$failed))
  expect_identical(NACHO:::plot_pool$profile, profile)
})

test_that("without mirai the app starts no workers", {
  local_mocked_bindings(has_package = function(package) FALSE)
  expect_null(NACHO:::plot_workers_start())
})

card_args <- function(active, workers) {
  list(
    id = "BD",
    object = shiny::reactiveVal(NACHO::GSE74821),
    qc = shiny::reactive(NACHO::nacho_qc(NACHO::GSE74821)),
    type = "BD",
    dark = shiny::reactive(FALSE),
    interactive = TRUE,
    active = active,
    workers = workers
  )
}

card_session <- function() {
  root <- shiny::MockShinySession$new()
  root$clientData <- shiny::reactiveValues(
    `output_BD-girafe_width` = 600,
    `output_BD-girafe_height` = 350
  )
  root
}

test_that("a card builds in a worker only while its page shows", {
  skip_if_not_installed("ggiraph")
  skip_if_no_daemons()
  skip_if_not_installed("later")
  withr::defer(NACHO:::plot_workers_stop())
  withr::local_options(nacho.plot_workers = 2)
  root <- card_session()
  on.exit(root$close(), add = TRUE)
  active <- shiny::reactiveVal(FALSE)
  profile <- NACHO:::plot_workers_start()
  expect_true(settle(NULL, connected(profile, 2L), seconds = 30))
  shiny::testServer(
    NACHO:::mod_qc_plot_server,
    args = card_args(active, profile),
    session = root,
    {
      cache <- shiny::getShinyOption("cache", default = session$cache)
      cache$reset()
      keys <- function() length(cache$keys())
      drawn <- function() {
        !inherits(try(output$girafe, silent = TRUE), "try-error")
      }
      session$setInputs(colour = "CartridgeID")
      session$elapse(300)
      idle(root, 1)
      expect_identical(keys(), 0L)
      active(TRUE)
      expect_true(settle(root, function() keys() == 1L))
      expect_true(settle(root, drawn))
      widget <- jsonlite::fromJSON(output$girafe, simplifyVector = FALSE)
      expect_match(widget$x$html, "viewBox='0 0 450 262.5'", fixed = TRUE)
      session$setInputs(size = 2)
      active(FALSE)
      idle(root, 3)
      expect_identical(keys(), 1L)
      session$setInputs(size = 3)
      active(TRUE)
      root$flushReact()
      active(FALSE)
      root$flushReact()
      active(TRUE)
      expect_true(settle(root, function() keys() == 2L))
      expect_true(settle(root, drawn))
      expect_false(isTRUE(NACHO:::plot_pool$failed))
    }
  )
})

test_that("a card builds in the app process when the workers do not start", {
  skip_if_not_installed("ggiraph")
  skip_if_no_daemons()
  skip_if_not_installed("later")
  withr::defer(NACHO:::plot_workers_stop())
  timeout <- NACHO:::plot_pool$timeout
  withr::defer(assign("timeout", timeout, envir = NACHO:::plot_pool))
  assign("timeout", 1000, envir = NACHO:::plot_pool)
  local_mocked_bindings(
    launch_local = function(...) invisible(NULL),
    .package = "mirai"
  )
  withr::local_options(nacho.plot_workers = 2, nacho.quiet = TRUE)
  root <- card_session()
  on.exit(root$close(), add = TRUE)
  shiny::testServer(
    NACHO:::mod_qc_plot_server,
    args = card_args(shiny::reactive(TRUE), NACHO:::plot_workers_start()),
    session = root,
    {
      cache <- shiny::getShinyOption("cache", default = session$cache)
      cache$reset()
      drawn <- function() {
        !inherits(try(output$girafe, silent = TRUE), "try-error")
      }
      session$setInputs(colour = "CartridgeID")
      session$elapse(300)
      expect_true(settle(root, function() length(cache$keys()) == 1L))
      expect_true(settle(root, drawn))
      expect_true(NACHO:::plot_pool$failed)
      expect_null(NACHO:::plot_workers_start())
      widget <- jsonlite::fromJSON(output$girafe, simplifyVector = FALSE)
      expect_match(widget$x$html, "viewBox='0 0 450 262.5'", fixed = TRUE)
    }
  )
})

test_that("a busy pool that answers is not taken as failed", {
  skip_if_not_installed("ggiraph")
  skip_if_no_daemons()
  skip_if_not_installed("later")
  withr::defer(NACHO:::plot_workers_stop())
  timeout <- NACHO:::plot_pool$timeout
  withr::defer(assign("timeout", timeout, envir = NACHO:::plot_pool))
  assign("timeout", 3000, envir = NACHO:::plot_pool)
  withr::local_options(nacho.plot_workers = 1)
  profile <- NACHO:::plot_workers_start()
  session <- shiny::MockShinySession$new()
  on.exit(session$close(), add = TRUE)
  cache <- session$cache
  results <- list()
  shiny::isolate(
    for (i in 1:6) {
      jobs <- new.env(parent = emptyenv())
      jobs$build <- 1L
      task <- NACHO:::card_task(session, jobs, cache)
      args <- list(
        object = NACHO::GSE74821,
        type = "BD",
        options = list(size = i),
        dark = FALSE,
        interactive = TRUE,
        size = c(width = 5, height = 3.5)
      )
      task$invoke(1L, paste0("key", i), args)
      results[[i]] <- task
    }
  )
  deadline <- Sys.time() + 120
  done <- function() {
    all(vapply(
      results,
      function(task) shiny::isolate(task$status()) == "success",
      logical(1)
    ))
  }
  while (!done() && Sys.time() < deadline) {
    later::run_now(0.1)
  }
  expect_true(done())
  expect_false(isTRUE(NACHO:::plot_pool$failed))
  expect_length(cache$keys(), 6L)
})

test_that("a pool that fails to start leaves the plots in the app process", {
  skip_if_no_daemons()
  withr::defer(NACHO:::plot_workers_stop())
  local_mocked_bindings(
    launch_local = function(...) stop("no daemons here"),
    .package = "mirai"
  )
  withr::local_options(nacho.plot_workers = 1, nacho.quiet = TRUE)
  expect_null(NACHO:::plot_workers_start())
  expect_true(NACHO:::plot_pool$failed)
  expect_null(NACHO:::plot_workers_start())
})

static_args <- function() {
  list(
    object = NACHO::GSE74821,
    type = "BD",
    options = list(),
    dark = FALSE,
    interactive = FALSE,
    size = c(width = 5, height = 3.5)
  )
}

run_task <- function(task, args) {
  shiny::isolate(task$invoke(1L, "key", args))
  finished <- function() shiny::isolate(task$status()) != "running"
  testthat::expect_true(settle(NULL, finished, seconds = 60))
  shiny::isolate(task$result())
}

test_that("a build whose daemon dies builds in the app process", {
  skip_if_no_daemons()
  skip_if_not_installed("later")
  withr::defer(NACHO:::plot_workers_stop())
  withr::local_options(nacho.plot_workers = 1, nacho.quiet = TRUE)
  NACHO:::plot_workers_start()
  expect_true(settle(NULL, function() isTRUE(NACHO:::plot_pool$warm)))
  real_mirai <- mirai::mirai
  local_mocked_bindings(
    mirai = function(..., .compute) {
      real_mirai(quit(save = "no", status = 1L), .compute = .compute)
    },
    .package = "mirai"
  )
  session <- shiny::MockShinySession$new()
  on.exit(session$close(), add = TRUE)
  jobs <- new.env(parent = emptyenv())
  jobs$build <- 1L
  task <- NACHO:::card_task(session, jobs, session$cache)
  result <- run_task(task, static_args())
  expect_null(result$error)
  expect_true(ggplot2::is_ggplot(result$value))
  expect_null(NACHO:::plot_pool$failed)
  expect_identical(NACHO:::plot_pool$failures, 1L)
  expect_identical(session$cache$keys(), "key")
})

test_that("a crashed pool is replaced by one new pool", {
  skip_if_no_daemons()
  skip_if_not_installed("later")
  withr::defer(NACHO:::plot_workers_stop())
  withr::local_options(nacho.plot_workers = 1, nacho.quiet = TRUE)
  first <- NACHO:::plot_workers_start()
  expect_true(settle(NULL, function() isTRUE(NACHO:::plot_pool$warm)))
  mirai::mirai(quit(save = "no", status = 1L), .compute = first)
  expect_true(settle(NULL, function() {
    mirai::status(.compute = first)$connections == 0L
  }))
  session <- shiny::MockShinySession$new()
  on.exit(session$close(), add = TRUE)
  jobs <- new.env(parent = emptyenv())
  jobs$build <- 1L
  task <- NACHO:::card_task(session, jobs, session$cache)
  run_task(task, static_args())
  second <- NACHO:::plot_workers_start()
  expect_false(is.null(second))
  expect_false(identical(second, first))
  expect_true(settle(NULL, connected(second, 1L), seconds = 30))
  expect_null(NACHO:::plot_pool$failed)
})

test_that("a second crash leaves the plots in the app process for good", {
  skip_if_no_daemons()
  skip_if_not_installed("later")
  withr::defer(NACHO:::plot_workers_stop())
  withr::local_options(nacho.plot_workers = 1, nacho.quiet = TRUE)
  crash <- function() {
    profile <- NACHO:::plot_workers_start()
    expect_true(settle(NULL, function() isTRUE(NACHO:::plot_pool$warm)))
    mirai::mirai(quit(save = "no", status = 1L), .compute = profile)
    expect_true(settle(NULL, function() {
      mirai::status(.compute = profile)$connections == 0L
    }))
    session <- shiny::MockShinySession$new()
    on.exit(session$close(), add = TRUE)
    jobs <- new.env(parent = emptyenv())
    jobs$build <- 1L
    result <- run_task(
      NACHO:::card_task(session, jobs, session$cache),
      static_args()
    )
    expect_true(ggplot2::is_ggplot(result$value))
  }
  crash()
  expect_null(NACHO:::plot_pool$failed)
  crash()
  expect_true(NACHO:::plot_pool$failed)
  expect_null(NACHO:::plot_workers_start())
})

test_that("builds that wait on a crashed pool all end in the app process", {
  skip_if_no_daemons()
  skip_if_not_installed("later")
  withr::defer(NACHO:::plot_workers_stop())
  withr::local_options(nacho.plot_workers = 2, nacho.quiet = TRUE)
  profile <- NACHO:::plot_workers_start()
  expect_true(settle(NULL, connected(profile, 2L), seconds = 30))
  real_mirai <- mirai::mirai
  calls <- 0L
  local_mocked_bindings(
    mirai = function(..., .compute) {
      calls <<- calls + 1L
      if (calls == 1L) {
        real_mirai(quit(save = "no", status = 1L), .compute = .compute)
      } else {
        real_mirai(..., .compute = .compute)
      }
    },
    .package = "mirai"
  )
  session <- shiny::MockShinySession$new()
  on.exit(session$close(), add = TRUE)
  tasks <- lapply(1:4, function(i) {
    jobs <- new.env(parent = emptyenv())
    jobs$build <- 1L
    task <- NACHO:::card_task(session, jobs, session$cache)
    shiny::isolate(task$invoke(1L, paste0("key", i), static_args()))
    task
  })
  finished <- function() {
    all(vapply(
      tasks,
      function(task) shiny::isolate(task$status()) != "running",
      logical(1)
    ))
  }
  expect_true(settle(NULL, finished, seconds = 120))
  for (task in tasks) {
    result <- shiny::isolate(task$result())
    expect_null(result$error)
    expect_true(ggplot2::is_ggplot(result$value))
  }
})

test_that("a pool whose daemons all left is replaced before the build", {
  skip_if_no_daemons()
  skip_if_not_installed("later")
  withr::defer(NACHO:::plot_workers_stop())
  withr::local_options(nacho.plot_workers = 1, nacho.quiet = TRUE)
  profile <- NACHO:::plot_workers_start()
  expect_true(settle(NULL, function() isTRUE(NACHO:::plot_pool$warm)))
  mirai::mirai(quit(save = "no", status = 1L), .compute = profile)
  expect_true(settle(NULL, function() {
    mirai::status(.compute = profile)$connections == 0L
  }))
  session <- shiny::MockShinySession$new()
  on.exit(session$close(), add = TRUE)
  jobs <- new.env(parent = emptyenv())
  jobs$build <- 1L
  task <- NACHO:::card_task(session, jobs, session$cache)
  result <- run_task(task, static_args())
  expect_null(result$error)
  expect_true(ggplot2::is_ggplot(result$value))
  expect_null(NACHO:::plot_pool$failed)
  expect_identical(NACHO:::plot_pool$failures, 1L)
  expect_false(identical(NACHO:::plot_pool$profile, profile))
  expect_false(is.null(NACHO:::plot_pool$profile))
})

test_that("a pool that never reached full strength is replaced once its daemon dies", {
  skip_if_no_daemons()
  skip_if_not_installed("later")
  withr::defer(NACHO:::plot_workers_stop())
  withr::local_options(nacho.plot_workers = 2, nacho.quiet = TRUE)
  real_launch <- mirai::launch_local
  local_mocked_bindings(
    launch_local = function(n, ...) real_launch(1L, ...),
    .package = "mirai"
  )
  profile <- NACHO:::plot_workers_start()
  expect_true(settle(NULL, function() isTRUE(NACHO:::plot_pool$warm)))
  mirai::mirai(quit(save = "no", status = 1L), .compute = profile)
  expect_true(settle(NULL, function() {
    mirai::status(.compute = profile)$connections == 0L
  }))
  session <- shiny::MockShinySession$new()
  on.exit(session$close(), add = TRUE)
  jobs <- new.env(parent = emptyenv())
  jobs$build <- 1L
  task <- NACHO:::card_task(session, jobs, session$cache)
  result <- run_task(task, static_args())
  expect_null(result$error)
  expect_true(ggplot2::is_ggplot(result$value))
  expect_null(NACHO:::plot_pool$failed)
  expect_identical(NACHO:::plot_pool$failures, 1L)
  expect_false(identical(NACHO:::plot_pool$profile, profile))
  expect_false(is.null(NACHO:::plot_pool$profile))
})

test_that("a pool whose dispatcher no longer answers is lost", {
  skip_if_no_daemons()
  withr::defer(NACHO:::plot_workers_stop())
  withr::local_options(nacho.plot_workers = 1, nacho.quiet = TRUE)
  profile <- NACHO:::plot_workers_start()
  expect_true(settle(NULL, function() isTRUE(NACHO:::plot_pool$warm)))
  expect_false(NACHO:::pool_lost(profile))
  local_mocked_bindings(
    status = function(...) stop("the dispatcher is gone"),
    .package = "mirai"
  )
  expect_true(NACHO:::pool_lost(profile))
})

test_that("an interrupted build ends its task without a result", {
  skip_if_no_daemons()
  skip_if_not_installed("later")
  withr::defer(NACHO:::plot_workers_stop())
  withr::local_options(nacho.plot_workers = 1, nacho.quiet = TRUE)
  NACHO:::plot_workers_start()
  real_mirai <- mirai::mirai
  local_mocked_bindings(
    mirai = function(..., .compute) {
      real_mirai(
        signalCondition(structure(
          list(message = "", call = NULL),
          class = c("interrupt", "condition")
        )),
        .compute = .compute
      )
    },
    .package = "mirai"
  )
  session <- shiny::MockShinySession$new()
  on.exit(session$close(), add = TRUE)
  jobs <- new.env(parent = emptyenv())
  jobs$build <- 1L
  task <- NACHO:::card_task(session, jobs, session$cache)
  shiny::isolate(task$invoke(1L, "key", list()))
  finished <- function() shiny::isolate(task$status()) != "running"
  expect_true(settle(NULL, finished, seconds = 30))
  expect_true(mirai::is_mirai_interrupt(jobs$running$data))
  expect_identical(shiny::isolate(task$status()), "success")
  expect_null(shiny::isolate(task$result()))
  expect_length(session$cache$keys(), 0L)
})

test_that("build_cancelled() knows a stopped or interrupted build", {
  error_value <- function(code) {
    structure(code, class = c("errorValue", "try-error"))
  }
  interrupt <- structure(
    "",
    class = c("miraiInterrupt", "errorValue", "try-error")
  )
  expect_true(NACHO:::build_cancelled(error_value(20L)))
  expect_true(NACHO:::build_cancelled(interrupt))
  expect_false(NACHO:::build_cancelled(error_value(5L)))
  expect_false(NACHO:::build_cancelled(error_value(7L)))
})

Try the NACHO package in your browser

Any scripts or data that you put into this service are public.

NACHO documentation built on Nov. 2, 2026, 5:09 p.m.