tests/testthat/test-coverage-checkpoint-lock.R

coverage_acquire_run_lock <- freqTLS:::coverage_acquire_run_lock
coverage_canonical_output_path <- freqTLS:::coverage_canonical_output_path
coverage_write_checkpoint <- freqTLS:::coverage_write_checkpoint
coverage_checkpoint_metadata_matches <-
  freqTLS:::coverage_checkpoint_metadata_matches
coverage_failure_counts <- freqTLS:::coverage_failure_counts
coverage_interval_available <- freqTLS:::coverage_interval_available
coverage_bootstrap_counts_valid <- freqTLS:::coverage_bootstrap_counts_valid
coverage_shard_grid <- freqTLS:::coverage_shard_grid
coverage_parse_shard_args <- freqTLS:::coverage_parse_shard_args
coverage_shard_output_path <- freqTLS:::coverage_shard_output_path
coverage_merge_shard_rows <- freqTLS:::coverage_merge_shard_rows
coverage_checkpoint_rows_valid <- freqTLS:::coverage_checkpoint_rows_valid
coverage_summarize_results <- freqTLS:::coverage_summarize_results
coverage_prebuild_receipt_valid <- freqTLS:::coverage_prebuild_receipt_valid

test_that("coverage output locks have one explicit owner", {
  out <- file.path(tempdir(), paste0("coverage-lock-", Sys.getpid(), ".rds"))
  lock <- paste0(out, ".lock")
  on.exit(unlink(lock, recursive = TRUE), add = TRUE)
  unlink(lock, recursive = TRUE)
  release <- coverage_acquire_run_lock(out)
  expect_true(dir.exists(lock))
  expect_match(paste(readLines(file.path(lock, "owner")), collapse = " "),
               paste0("pid=", Sys.getpid()))
  expect_error(coverage_acquire_run_lock(out), "lock already exists")
  release()
  expect_false(dir.exists(lock))
  release_again <- coverage_acquire_run_lock(out)
  release_again()
})

test_that("coverage checkpoints replace through a unique temporary file", {
  skip_on_os("windows")
  out <- file.path(tempdir(), paste0("coverage-checkpoint-", Sys.getpid(), ".rds"))
  on.exit(unlink(out), add = TRUE)
  coverage_write_checkpoint(list(value = 1L), out)
  coverage_write_checkpoint(list(value = 2L), out)
  expect_identical(readRDS(out), list(value = 2L))
  expect_length(list.files(dirname(out),
                           pattern = paste0("^", basename(out), "[.]tmp-")), 0L)
})

test_that("coverage output aliases resolve to one canonical path", {
  root <- tempfile("coverage-path-")
  dir.create(root)
  on.exit(unlink(root, recursive = TRUE), add = TRUE)
  nested <- file.path(root, "nested")
  dir.create(nested)
  direct <- file.path(nested, "output.rds")
  aliased <- file.path(root, "nested", "..", "nested", "output.rds")
  expect_identical(coverage_canonical_output_path(direct),
                   coverage_canonical_output_path(aliased))

  skip_on_os("windows")
  target <- file.path(nested, "target.rds")
  link <- file.path(nested, "output-link.rds")
  saveRDS(list(value = 1L), target)
  expect_true(file.symlink(target, link))
  expect_identical(coverage_canonical_output_path(link),
                   coverage_canonical_output_path(target))
})

test_that("coverage output rejects dangling symbolic links", {
  skip_on_os("windows")
  root <- tempfile("coverage-dangling-link-")
  dir.create(root)
  on.exit(unlink(root, recursive = TRUE), add = TRUE)
  target <- file.path(root, "future-output.rds")
  link <- file.path(root, "output-link.rds")
  expect_true(file.symlink(basename(target), link))
  expect_error(coverage_canonical_output_path(link), "dangling symbolic link")
})

test_that("coverage resume checks source and runtime fingerprints", {
  expected <- list(
    runner_sha256 = "runner", coverage_helper_sha256 = "helper",
    candidate_base_sha = "candidate", r_version = "4.6.0",
    r_platform = "aarch64-apple-darwin23", rng_kind = "Mersenne-Twister/Inversion/Rejection"
  )
  expect_true(coverage_checkpoint_metadata_matches(expected, expected))
  changed_helper <- expected
  changed_helper$coverage_helper_sha256 <- "changed-helper"
  expect_false(coverage_checkpoint_metadata_matches(expected, changed_helper))
  missing_helper <- expected[names(expected) != "coverage_helper_sha256"]
  expect_false(coverage_checkpoint_metadata_matches(missing_helper, expected))
  changed_rng <- expected
  changed_rng$rng_kind <- "different-rng"
  expect_false(coverage_checkpoint_metadata_matches(expected, changed_rng))
  expect_false(coverage_checkpoint_metadata_matches(NULL, expected))
})

test_that("coverage array shards partition deterministic replicate datasets", {
  groups <- c(3L, 5L)
  nsim <- 3L
  shard_count <- 4L
  shards <- lapply(seq_len(shard_count), function(index) {
    coverage_shard_grid(groups, nsim, index, shard_count)
  })
  observed <- do.call(rbind, shards)
  expected <- do.call(rbind, lapply(groups, function(group) {
    data.frame(n_groups = group, replicate = seq_len(nsim))
  }))
  expect_equal(
    observed[order(observed$n_groups, observed$replicate), , drop = FALSE],
    expected
  )
  expect_error(coverage_shard_grid(groups, nsim, 0L, shard_count),
               "shard_index")
  expect_error(coverage_shard_grid(groups, nsim, 1L, length(groups) * nsim + 1L),
               "cannot exceed")
})

test_that("coverage shard arguments and output names are validated", {
  parsed <- coverage_parse_shard_args(c(
    "full", "--approved", "--shard-index", "7", "--shard-count", "48"
  ))
  expect_identical(parsed$shard_index, 7L)
  expect_identical(parsed$shard_count, 48L)
  expect_identical(parsed$remaining_args, c("full", "--approved"))
  expect_error(coverage_parse_shard_args(c("full", "--shard-index", "2")),
               "exactly once as a pair")
  expect_error(coverage_parse_shard_args(c(
    "full", "--shard-index", "x", "--shard-count", "4"
  )), "positive integer")
  expect_identical(
    coverage_shard_output_path("/private/coverage.rds", 1L, 48L),
    "/private/coverage.shard-001-of-048.rds"
  )
})

test_that("coverage shard merges and checkpoints reject missing or foreign rows", {
  grid <- coverage_shard_grid(c(3L, 5L), 2L, 1L, 2L)
  methods <- list(CTmax = c("wald", "profile", "bootstrap"),
                  sigma_CTmax = c("wald", "bootstrap"))
  rows <- do.call(rbind, lapply(seq_len(nrow(grid)), function(i) {
    do.call(rbind, lapply(names(methods), function(target) {
      data.frame(n_groups = grid$n_groups[[i]],
                 replicate = grid$replicate[[i]], target = target,
                 method_requested = methods[[target]],
                 stringsAsFactors = FALSE)
    }))
  }))
  expect_true(coverage_checkpoint_rows_valid(rows, grid, methods))
  expect_false(coverage_checkpoint_rows_valid(rows[-1L, , drop = FALSE],
                                               grid, methods))
  foreign <- rows
  foreign$n_groups[[1L]] <- 99L
  expect_false(coverage_checkpoint_rows_valid(foreign, grid, methods))

  other_grid <- coverage_shard_grid(c(3L, 5L), 2L, 2L, 2L)
  other_rows <- do.call(rbind, lapply(seq_len(nrow(other_grid)), function(i) {
    do.call(rbind, lapply(names(methods), function(target) {
      data.frame(n_groups = other_grid$n_groups[[i]],
                 replicate = other_grid$replicate[[i]], target = target,
                 method_requested = methods[[target]],
                 stringsAsFactors = FALSE)
    }))
  }))
  merged <- coverage_merge_shard_rows(
    list(rows, other_rows), group_counts = c(3L, 5L), nsim = 2L,
    methods = methods
  )
  expect_equal(nrow(merged), 20L)
  expect_error(coverage_merge_shard_rows(
    list(rows, other_rows[-1L, , drop = FALSE]), c(3L, 5L), 2L,
    methods = methods
  ), "complete")
})

test_that("coverage summaries keep failures and the requested denominator", {
  rows <- data.frame(
    n_groups = rep(3L, 3L), target = rep("CTmax", 3L),
    method_requested = rep("wald", 3L),
    fit_converged = c(TRUE, TRUE, FALSE), pd_hess = c(TRUE, FALSE, FALSE),
    interval_available = c(TRUE, FALSE, FALSE), covered = c(TRUE, FALSE, FALSE),
    width = c(1, NA_real_, NA_real_), estimate = c(36.1, 35.9, NA_real_),
    error_estimate = c(0.1, -0.1, NA_real_),
    method_returned = c("wald", NA_character_, NA_character_)
  )
  summary <- coverage_summarize_results(rows)
  expect_equal(summary$n_requested, 3L)
  expect_equal(summary$n_fit_failed, 1L)
  expect_equal(summary$n_pd_hess_failed, 1L)
  expect_equal(summary$n_interval_unavailable, 2L)
  expect_equal(summary$interval_rate, 1 / 3)
  expect_equal(summary$coverage_all_requested, 1 / 3)
})

test_that("coverage prebuild receipts must match candidate, root, and DLL", {
  receipt <- c("candidate_sha=abc", "dll_sha256=0123", "root=/project/freqTLS")
  expect_true(coverage_prebuild_receipt_valid(
    receipt, "abc", "/project/freqTLS", "0123"
  ))
  expect_false(coverage_prebuild_receipt_valid(
    receipt, "abc", "/project/freqTLS", "different-dll"
  ))
  expect_false(coverage_prebuild_receipt_valid(
    receipt, "other", "/project/freqTLS", "0123"
  ))
  expect_false(coverage_prebuild_receipt_valid(
    c(receipt, receipt[[1L]]), "abc", "/project/freqTLS", "0123"
  ))
})

Try the freqTLS package in your browser

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

freqTLS documentation built on Dec. 4, 2026, 5:11 p.m.