From a10b699c278889356b45b85525d7f6384ae4516c Mon Sep 17 00:00:00 2001 From: "Matthew T. Warkentin" Date: Wed, 8 Jan 2025 11:33:19 -0700 Subject: [PATCH] Improved testing suite --- tests/testthat/helper-oms.R | 25 +++++++++++++++++++++---- tests/testthat/setup.R | 8 ++++++-- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/tests/testthat/helper-oms.R b/tests/testthat/helper-oms.R index 44076cf..b80489c 100644 --- a/tests/testthat/helper-oms.R +++ b/tests/testthat/helper-oms.R @@ -1,11 +1,28 @@ -local_initiate_oms <- function(path) { - cmd <- if (Sys.info()['sysname'] == 'Windows') './bin/oms.exe' else './bin/oms' +local_initiate_oms <- function(path, env = rlang::caller_env()) { + cmd <- if (Sys.info()['sysname'] == 'Windows') '.\\bin\\oms.exe' else 'bin/oms' pid <- withr::with_dir( new = path, code = sys::exec_background(cmd = cmd, std_out = FALSE) ) - Sys.sleep(0.5) - withr::defer_parent(tools::pskill(pid)) + withr::defer(tools::pskill(pid), envir = env) + ensure_oms_running(pid) + allow_startup() pid } + +check_oms_running <- function(pid) { + status <- sys::exec_status(pid, wait = FALSE) + if (rlang::is_na(status)) return(TRUE) + rlang::abort('OpenM++ Web Service (OMS) is not running.') +} + +ensure_oms_running <- + purrr::insistently( + f = \(x) check_oms_running(x), + rate = purrr::rate_delay(pause = 0.1, max_times = 10) + ) + +allow_startup <- function(time = 1L) { + Sys.sleep(time) +} diff --git a/tests/testthat/setup.R b/tests/testthat/setup.R index 21a206c..21cea6b 100644 --- a/tests/testthat/setup.R +++ b/tests/testthat/setup.R @@ -5,7 +5,7 @@ local_install_openmpp <- function() { if (os == 'Darwin' & type == 'arm64') { url <- 'https://github.com/openmpp/main/releases/latest/download/openmpp_mac_arm64_20241226.tar.gz' - } else if (os == 'Darwin' & type == 'intel') { + } else if (os == 'Darwin' & type == 'x86_64') { url <- 'https://github.com/openmpp/main/releases/latest/download/openmpp_mac_x86_64_20241226.tar.gz' } else if (os == 'Linux') { url <- 'https://github.com/openmpp/main/releases/latest/download/openmpp_debian-11_20241226.tar.gz' @@ -14,9 +14,14 @@ local_install_openmpp <- function() { } else { rlang::abort('No compatible OS detected.') } + file <- basename(url) + dir <- tempdir() + path <- paste0(dir, '/', file) + withr::defer(unlink(path)) + download.file(url, path, quiet = TRUE) if (os == 'Windows') { @@ -25,7 +30,6 @@ local_install_openmpp <- function() { untar(path, exdir = dir) } - unlink(path) new_path <- tools::file_path_sans_ext(path, compression = TRUE) new_path }