tests/testthat/helper.R

mlr3misc::walk(
  list.files(system.file("testthat", package = "rush"), pattern = "^helper.*\\.[rR]", full.names = TRUE),
  source
)

# Poll `condition` until it evaluates to `TRUE` or `timeout` seconds elapse.
# Returns as soon as the condition holds and errors on timeout, replacing fixed `Sys.sleep()` calls
# used to wait for asynchronous worker state to settle.
wait_until = function(condition, timeout = 10, interval = 0.05) {
  condition = substitute(condition)
  env = parent.frame()
  start_time = Sys.time()
  while (difftime(Sys.time(), start_time, units = "secs") < timeout) {
    if (isTRUE(eval(condition, env))) {
      return(invisible(TRUE))
    }
    Sys.sleep(interval)
  }
  stop(sprintf(
    "`wait_until()` timed out after %g seconds waiting for: %s",
    timeout, paste(deparse(condition), collapse = " ")
  ))
}


wl_default = function(rush) {
  while (TRUE) {
    xs = list(x1 = runif(1, 0, 10), x2 = runif(1, 0, 10))
    keys = rush$push_running_tasks(list(xs))
    ys = list(y = xs$x1 + xs$x2)
    rush$finish_tasks(keys, yss = list(ys))
    Sys.sleep(1)
  }

  NULL
}

wl_finished = function(rush) {
  xss = list(list(x1 = 1, x2 = 2))
  keys = rush$push_running_tasks(xss)
  rush$finish_tasks(keys, yss = list(list(y = 3)))
  NULL
}

# pops tasks from the queue and finishes them
wl_queue = function(rush) {
  while (!rush$terminated) {
    task = rush$pop_task(fields = c("xs"))
    if (!is.null(task)) {
      fun = function(x1, x2) list(y = x1 + x2)
      ys = mlr3misc::invoke(fun, .args = task$xs)
      rush$finish_tasks(task$key, yss = list(ys))
    }
  }

  NULL
}

# fails a task with an R error if x1 < 1
wl_fail = function(rush) {
  while (!rush$terminated) {
    task = rush$pop_task(fields = c("xs"))

    if (!is.null(task)) {
      if (task$xs$x1 < 1) {
        condition = list(message = "Test error")
        rush$fail_tasks(task$key, conditions = list(condition))
      } else {
        fun = function(x1, x2, ...) {
          list(y = x1 + x2)
        }
        ys = mlr3misc::invoke(fun, .args = task$xs)
        rush$finish_tasks(task$key, yss = list(ys))
      }
    }
  }

  NULL
}

# simulates a segfault by killing the worker process after adding a running task
wl_segfault = function(rush) {
  xs = list(x1 = 1, x2 = 2)
  rush$push_running_tasks(list(xs))
  Sys.sleep(1)
  tools::pskill(Sys.getpid(), tools::SIGKILL)
}

# simulates a segfault on the first worker and runs the queue loop on its restarted successor
wl_segfault_once = function(rush) {
  restarted_from = rush$connector$HGET(sprintf("%s:%s", rush$network_id, rush$worker_id), "restarted_from")
  if (!nzchar(restarted_from)) {
    tools::pskill(Sys.getpid(), tools::SIGKILL)
  }
  while (!rush$terminated) {
    task = rush$pop_task(fields = c("xs"))
    if (!is.null(task)) {
      rush$finish_tasks(task$key, yss = list(list(y = task$xs$x1 + task$xs$x2)))
    }
  }

  NULL
}

# writes more than the OS pipe buffer (~64 KiB) to stderr before finishing a task
wl_big_stderr = function(rush) {
  keys = rush$push_running_tasks(list(list(x1 = 1, x2 = 2)))
  message(strrep("x", 1e6))
  rush$finish_tasks(keys, yss = list(list(y = 3)))
  NULL
}

# pops tasks from the queue and reports the compute profile the worker runs on
wl_queue_profile = function(rush, profile = NULL) {
  while (!rush$terminated) {
    task = rush$pop_task(fields = "xs")
    if (!is.null(task)) {
      rush$finish_tasks(task$key, yss = list(list(y = if (is.null(profile)) "default" else profile)))
    }
  }

  NULL
}

# reports the compute profile the worker runs on
wl_profile = function(rush, profile = NULL) {
  keys = rush$push_running_tasks(list(list(x1 = 1)))
  rush$finish_tasks(keys, yss = list(list(y = if (is.null(profile)) "default" else profile)))
  NULL
}

wl_nop = function(rush) {
  while (!rush$terminated) {
    Sys.sleep(1)
  }

  NULL
}

Try the rush package in your browser

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

rush documentation built on Nov. 20, 2026, 5:08 p.m.