about summary refs log tree commit diff
path: root/tests
diff options
context:
space:
mode:
authorPjotr <wrk@bosa.lan>2026-11-08 16:23:03 +0000
committerPjotr <wrk@bosa.lan>2026-11-08 16:23:14 +0000
commite934e755f14648fff6ebac07e9cf9e5b37cb3fe4 (patch)
treeb7b921fe7e83bed348d65f3ecda3cb284479cd19 /tests
parent0769a0ea0a8654e5e50e03365545c8ccf58d6b91 (diff)
downloadgenenetwork4-e934e755f14648fff6ebac07e9cf9e5b37cb3fe4.tar.gz
api: describe case attributes, and gate large computations only
Two things that came out of reading a real response rather than a test.

**Say what a case attribute means.** `GET /groups/{group}/case-attributes`
returned names and values, and the per-trait attribute endpoint returned
values, so a caller had to guess that `SeqCvge` is a sequencing depth.
MariaDB has had the answer all along in `CaseAttribute.Description`:

  SeqCvge   Depth of DNA sequencing for 1 male of each strain in 2016
  Status    Live= Available at JAX, Cryo=Cryopreserved only, Extinct
  Epoch     BXD family subgroups. Each number with common parents. ...
  Sex       F= female, M= male

Both endpoints now carry it -- a `descriptions` map on the group listing, a
`description` field on the per-trait one -- and the covariate notes quote it,
so `/compute/gemma` answers "Covariate SeqCvge (Depth of DNA sequencing for
1 male of each strain in 2016): categorical ..." instead of leaving the name
to be interpreted. It matters most for covariates: `SeqCvge` is a property
of the *genotype* data, its values include compound ones (`40+31L`, two
sequencing runs), and the server codes those as 20-odd levels rather than as
a depth -- a covariate fitted for the wrong reason, which the description is
the first hint against.

**Gate large computations, not every computation.** The gate was applied to
every job, so a 93-strain test request could be refused because somebody
else's 6 000-individual run held the slot -- which is how the test suite
started serialising itself behind a mutex, a workaround for the wrong
problem. What the gate is for is *large* work: a BXD panel of ~200 finishes
in seconds and two at once cost nothing, while UM-HET3's 6 382 write 207 MB
of kinship per chromosome. It now starts at `LARGE_PANEL` (1 000
individuals): below that no slot is taken, nothing waits and nothing is
refused; above it the old policy holds unchanged (1 slot for gemma, 1 for
ravanan, `RQTL_MAX_CONCURRENT` for rqtl, one large job per caller, 409
naming the run in the way).

The size is not known when the request arrives -- the panel is resolved
server-side -- so the decorator now makes a *light* claim and the handler
promotes it (`__gn4_claim.claim_slot_or_busy(individuals, rid)`) at the
point where it knows, then hands it to `Gate::run` around the call that
computes, so the slot still outlives a client disconnect. A light claim
registers nothing and its drop removes nothing: only a held claim may touch
the claimant map.

* src/compute_gate.rs: `LARGE_PANEL`/`is_large`, `Gate::claim_light`,
  `Claim::claim_slot_or_busy`/`is_held`, `busy_error` (the 409 policy, moved
  out of `claim_or_busy`), and `Gate::run` unchanged in spirit.
* gnauth-macros: binds the light claim; the body keeps one gate call.
* src/routes/{gemma,rqtl}.rs: promotion after the panel/sample count is
  known, and the compute call wrapped in `run`; error mapping moved inside
  the job so the future returns `ApiError`.
* src/rqtl.rs: `Sample` is `Clone` (the job owns its copy).
* tests: the gemma suite loses its lock entirely (the fixtures and BXD.4 are
  under the threshold); rqtl keeps its lock only for the machine's sake --
  concurrent `Rscript` scans would compete for cores and scratch, not for a
  slot. Four new gate tests pin the threshold, that a small job never takes
  the slot, that one caller cannot hold two large jobs, and that another
  caller waits rather than fails.
* README.md, docs/www/ai-instruction.md, docs/www/operator-checklist.md:
  what the threshold is and why it is 1 000.

Measured on :3001 after the change: two concurrent small-panel GEMMA
requests from one token both answer 200 (one was 409 before), while two
6 382-individual requests answer 502/409 -- the second gated with the usual
`compute_busy` naming the first.

277 tests pass; the same four as before do not.
Diffstat (limited to 'tests')
-rw-r--r--tests/case_attributes.rs3
-rw-r--r--tests/gemma.rs24
-rw-r--r--tests/group_case_attributes.rs17
-rw-r--r--tests/rqtl.rs16
4 files changed, 38 insertions, 22 deletions
diff --git a/tests/case_attributes.rs b/tests/case_attributes.rs
index f8ff456..00d6573 100644
--- a/tests/case_attributes.rs
+++ b/tests/case_attributes.rs
@@ -60,6 +60,9 @@ async fn a_traits_case_attribute_lines_up_with_its_values() {
     assert_eq!(data["group"], "BXD-Longevity");
     assert_eq!(data["trait_id"], 10001);
     assert_eq!(data["attribute"], "Sex");
+    // And what `Sex` means comes with it: "F= female, M= male".
+    let description = data["description"].as_str().expect("description");
+    assert!(description.contains("female"), "{description}");
 
     let rows = data["values"].as_array().expect("values");
     assert_eq!(
diff --git a/tests/gemma.rs b/tests/gemma.rs
index 6241e2a..ac01492 100644
--- a/tests/gemma.rs
+++ b/tests/gemma.rs
@@ -12,19 +12,6 @@ mod common;
 
 /// True only when gemma-wrapper is actually runnable. `-v` is cheap and
 /// does not touch the cache dir or invoke the real GEMMA binary.
-/// The tests that run a real GEMMA job take this lock first: the compute
-/// gate refuses a second job from the same caller with 409 instead of
-/// queueing it, and every test shares the one token.
-static COMPUTE_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
-
-/// Take [`COMPUTE_LOCK`], ignoring poisoning (one failing test must not
-/// fail the rest).
-fn serialised() -> std::sync::MutexGuard<'static, ()> {
-    COMPUTE_LOCK
-        .lock()
-        .unwrap_or_else(|poisoned| poisoned.into_inner())
-}
-
 fn gemma_wrapper_available() -> bool {
     std::process::Command::new("gemma-wrapper")
         .arg("-v")
@@ -65,7 +52,6 @@ fn bxd_trait_values() -> Vec<Option<f64>> {
 
 #[tokio::test]
 async fn compute_gemma_maps_a_qtl_from_the_bxd_fixtures() {
-    let _serial = serialised();
     if !gemma_wrapper_available() {
         eprintln!(
             "skipping compute_gemma_maps_a_qtl_from_the_bxd_fixtures: gemma-wrapper not on PATH"
@@ -477,7 +463,6 @@ async fn compute_gemma_rejects_a_run_id_already_in_use() {
 /// fitted as one ordinal number.
 #[tokio::test]
 async fn compute_gemma_fits_a_case_attribute_as_a_covariate() {
-    let _serial = serialised();
     if !gemma_wrapper_available() {
         eprintln!(
             "skipping compute_gemma_fits_a_case_attribute_as_a_covariate: gemma-wrapper not on PATH"
@@ -556,8 +541,14 @@ async fn compute_gemma_fits_a_case_attribute_as_a_covariate() {
         .collect();
     let covariate_note = notes
         .iter()
-        .find(|note| note.starts_with("Covariate Status:"))
+        .find(|note| note.starts_with("Covariate Status"))
         .expect("a note saying what the covariate was");
+    // The attribute's own description is quoted: `Status` alone does not
+    // say what its levels mean.
+    assert!(
+        covariate_note.contains("Cryopreserved"),
+        "the note says what the attribute means: {covariate_note}"
+    );
     assert!(covariate_note.contains("categorical"), "{covariate_note}");
     assert!(
         covariate_note.contains("coded"),
@@ -644,7 +635,6 @@ async fn compute_gemma_fits_a_case_attribute_as_a_covariate() {
 async fn compute_gemma_refuses_permutations() {
     // The gate answers before the handler does, so a job from another test
     // in this binary would come back 409 instead of the refusal under test.
-    let _serial = serialised();
     let Some(token) = common::auth_token().await else {
         eprintln!("skipping compute_gemma_refuses_permutations: no token (see docs/gn-auth.md)");
         return;
diff --git a/tests/group_case_attributes.rs b/tests/group_case_attributes.rs
index e4cf62b..41e83e0 100644
--- a/tests/group_case_attributes.rs
+++ b/tests/group_case_attributes.rs
@@ -49,6 +49,23 @@ async fn group_case_attributes_bxd_returns_names_and_values() {
         .collect();
     assert_eq!(names, vec!["Epoch", "RRID", "SeqCvge", "Status"]);
 
+    // Every name says what it means, which is the only way to know that
+    // SeqCvge is a sequencing depth rather than a phenotype.
+    let descriptions = case_attributes["descriptions"].as_object().unwrap();
+    assert_eq!(descriptions.len(), names.len());
+    let seq_cvge = descriptions["SeqCvge"].as_str().unwrap();
+    assert!(
+        seq_cvge.contains("Depth of DNA sequencing"),
+        "SeqCvge is explained: {seq_cvge}"
+    );
+    assert!(
+        descriptions["Status"]
+            .as_str()
+            .unwrap()
+            .contains("Cryopreserved"),
+        "Status explains its own levels"
+    );
+
     let values = case_attributes["values"].as_array().unwrap();
     assert!(!values.is_empty());
     let bxd1 = values
diff --git a/tests/rqtl.rs b/tests/rqtl.rs
index 20164e5..45157b2 100644
--- a/tests/rqtl.rs
+++ b/tests/rqtl.rs
@@ -14,10 +14,11 @@ use serde_json::Value;
 
 mod common;
 
-/// `/compute/rqtl` allows one job per caller, and every test in this file
-/// uses the same token, so two compute tests running at once would have one
-/// of them refused with `409 compute_busy`. Cargo runs a file's tests on
-/// several threads, so they take this lock first.
+/// These tests stay serialised for the machine's sake, not the gate's:
+/// small panels are exempt from gating (`compute_gate::LARGE_PANEL`), so
+/// nothing here is refused, but several Rscript scans at once would still
+/// compete for cores and the shared R/qtl scratch directory. Cargo runs a
+/// file's tests on several threads, so they take this lock first.
 static COMPUTE_LOCK: std::sync::Mutex<()> = std::sync::Mutex::new(());
 
 /// Take [`COMPUTE_LOCK`], ignoring poisoning (one failing test must not
@@ -431,8 +432,13 @@ async fn compute_rqtl_fits_a_case_attribute_as_a_covariate() {
         .collect();
     let covariate_note = notes
         .iter()
-        .find(|note| note.starts_with("Covariate Sex:"))
+        .find(|note| note.starts_with("Covariate Sex"))
         .expect("a note saying what the covariate was");
+    // The attribute's description comes with it: "F= female, M= male".
+    assert!(
+        covariate_note.contains("female"),
+        "the note says what the attribute means: {covariate_note}"
+    );
     assert!(covariate_note.contains("categorical"), "{covariate_note}");
     assert!(
         covariate_note.contains("coded"),