diff --git a/crates/forge-core/src/config.rs b/crates/forge-core/src/config.rs index cc4dec2..069c3e7 100644 --- a/crates/forge-core/src/config.rs +++ b/crates/forge-core/src/config.rs @@ -342,6 +342,8 @@ pub struct CiConfig { pub artifact_retention_days: u64, /// Hours a runner registration token stays valid. pub registration_token_hours: u32, + /// Milliseconds a runner claim waits for work before it is answered with no job. + pub claim_wait_ms: u64, } impl Default for CiConfig { @@ -354,6 +356,7 @@ impl Default for CiConfig { log_retention_days: 90, artifact_retention_days: 30, registration_token_hours: 24, + claim_wait_ms: 25_000, } } } diff --git a/crates/forge-web/src/ci/api.rs b/crates/forge-web/src/ci/api.rs index 638eb58..55068a7 100644 --- a/crates/forge-web/src/ci/api.rs +++ b/crates/forge-web/src/ci/api.rs @@ -42,11 +42,6 @@ const LOG_SIZE_HEADER: &str = "x-forge-log-size"; const MAX_LOG_CHUNK: usize = 1024 * 1024; /// Largest JSON body on the runner routes, in bytes. const MAX_JSON_BODY: usize = 64 * 1024; -/// How long a claim waits for work before answering 204. -#[cfg(not(test))] -const CLAIM_WAIT: std::time::Duration = std::time::Duration::from_secs(25); -#[cfg(test)] -const CLAIM_WAIT: std::time::Duration = std::time::Duration::from_millis(50); /// What the forge assumes a runner reserves for a job with no `memory`, when the runner did not say. const DEFAULT_RESERVE: u64 = 1 << 30; /// Longest runner name or label, in bytes. @@ -343,9 +338,10 @@ async fn claim_work( ) -> topcoat::Result { // Arm the wake-up before each attempt, so a job queued during the attempt is not missed. // Producers use notify_waiters, which wakes every claimer, so a runner whose labels - // don't match a job re-arms and keeps waiting for the rest of CLAIM_WAIT. + // don't match a job re-arms and keeps waiting for the rest of the claim wait. let waker = state.ci_waker.clone(); - let deadline = tokio::time::Instant::now() + CLAIM_WAIT; + let wait = std::time::Duration::from_millis(state.config.ci.claim_wait_ms); + let deadline = tokio::time::Instant::now() + wait; loop { let mut woken = std::pin::pin!(waker.notified()); woken.as_mut().enable(); diff --git a/crates/forge-web/src/test_support.rs b/crates/forge-web/src/test_support.rs index 15cd0e7..a0ebf23 100644 --- a/crates/forge-web/src/test_support.rs +++ b/crates/forge-web/src/test_support.rs @@ -7,6 +7,7 @@ use std::path::Path; use std::sync::Arc; use std::time::Duration; +use forge_core::config::CiConfig; use forge_core::time::stamp_after; use forge_core::token::hash_token; use forge_core::{Config, Registration, Store}; @@ -48,7 +49,11 @@ pub fn test_config(dir: &Path) -> Config { storage: Default::default(), replication: Default::default(), lfs: Default::default(), - ci: Default::default(), + ci: CiConfig { + // Unit tests that claim on an empty queue should not wait out the production long poll. + claim_wait_ms: 50, + ..Default::default() + }, advisories: Default::default(), mail: Default::default(), orgs: Default::default(), diff --git a/crates/forge-web/tests/ci_e2e.rs b/crates/forge-web/tests/ci_e2e.rs index 4206138..00e0dfe 100644 --- a/crates/forge-web/tests/ci_e2e.rs +++ b/crates/forge-web/tests/ci_e2e.rs @@ -11,7 +11,7 @@ //! - a live log: `log.txt` streams while a job runs and ends when the job finishes. mod common; -use common::{BIN, Server, launch, pick_ports, write_config}; +use common::{BIN, FAST_CLAIMS, Server, launch_with, pick_ports, write_config}; use forge_ci::proto::{JobAssignment, Outcome, RegisterRequest}; use forge_runner::client::ForgeClient; use forge_runner::config::{Limits, RunnerConfig}; @@ -398,7 +398,7 @@ async fn boot() -> Booted { run_admin(&config, "tahinli", &["--admin"], TAHINLI_PW); run_admin(&config, "bob", &[], BOB_PW); - let (server, ports) = launch(&dir, ports).await; + let (server, ports) = launch_with(&dir, ports, FAST_CLAIMS).await; Booted { _server: server, _root: root, @@ -791,18 +791,24 @@ async fn live_log_streams_until_the_job_finishes() { create_repo(&b.http, &tahinli, "r").await; let worker = register_runner(&b, &tahinli, "runner-a").await; - let slow = r#"[on] + // The job holds until the test drops a release file, so the first line is seen while it runs. + // The loop is capped, so a test that dies early does not leave the job spinning. + let release = b.dir.join("release"); + let slow = format!( + r#"[on] push = ["main"] [jobs.slow] image = "docker.io/library/alpine:3" -run = "echo started; sleep 5; echo done" -"#; +run = "echo started; i=0; while [ ! -f {} ] && [ $i -lt 100 ]; do sleep 0.1; i=$((i+1)); done; echo done" +"#, + release.display() + ); let work = clone( &remote(b.port, "tahinli", &token, "tahinli", "r"), &b.dir.join("tahinli-work"), ); - let sha = push_ci_config(&work, slow); + let sha = push_ci_config(&work, &slow); let found = wait_statuses(&b.http, &token, &sha, &["ci/slow"]).await; let job_id = job_id_of(&found["ci/slow"]); @@ -826,6 +832,7 @@ run = "echo started; sleep 5; echo done" !handle.is_finished(), "the first line arrived only after the job had finished" ); + fs::write(&release, "").expect("release the job"); } } }; diff --git a/crates/forge-web/tests/common/mod.rs b/crates/forge-web/tests/common/mod.rs index ac0ab4f..04c262b 100644 --- a/crates/forge-web/tests/common/mod.rs +++ b/crates/forge-web/tests/common/mod.rs @@ -104,6 +104,10 @@ pub fn write_config_with(dir: &Path, ports: Ports, extra: &str) -> PathBuf { path } +/// `[ci]` settings for tests that drain the queue. A claim that finds nothing waits `claim_wait_ms` +/// before it answers 204, and the production default is 25 s, so the test server answers at once. +pub const FAST_CLAIMS: &str = "[ci]\nclaim_wait_ms = 50\n"; + pub fn describe(out: &Output) -> String { format!( "status: {}\nstdout: {}\nstderr: {}", diff --git a/crates/forge-web/tests/merge_queue_e2e.rs b/crates/forge-web/tests/merge_queue_e2e.rs index 5b0da37..b1701db 100644 --- a/crates/forge-web/tests/merge_queue_e2e.rs +++ b/crates/forge-web/tests/merge_queue_e2e.rs @@ -10,7 +10,7 @@ #![allow(dead_code)] mod common; -use common::{BIN, Server, launch, pick_ports, tool_on_path, write_config}; +use common::{BIN, FAST_CLAIMS, Server, launch_with, pick_ports, tool_on_path, write_config}; use forge_ci::proto::{JobAssignment, RegisterRequest}; use forge_runner::client::ForgeClient; use forge_runner::config::{Limits, RunnerConfig}; @@ -339,7 +339,7 @@ async fn boot() -> Booted { run_admin(&config, "tahinli", &["--admin"], TAHINLI_PW); run_admin(&config, "bob", &[], BOB_PW); - let (server, ports) = launch(&dir, ports).await; + let (server, ports) = launch_with(&dir, ports, FAST_CLAIMS).await; Booted { _server: server, _root: root,