diff --git a/crates/forge-core/src/store/ci.rs b/crates/forge-core/src/store/ci.rs index 0cf06cc..6c0aef9 100644 --- a/crates/forge-core/src/store/ci.rs +++ b/crates/forge-core/src/store/ci.rs @@ -2395,6 +2395,29 @@ mod tests { )); } + /// Like `create`, then gives the jobs distinct, increasing `queued_at` in the order listed, + /// because jobs created together tie and then sort by random id. + async fn create_ordered( + store: &Store, + repo_id: &str, + actor: &str, + jobs: &[Job], + approval: bool, + ) -> CiRun { + let run = create(store, repo_id, actor, jobs, approval).await; + let conn = store.inner.conn.lock().await; + for (i, j) in jobs.iter().enumerate() { + let at = crate::time::stamp_before(Duration::from_secs(10 - i as u64)); + conn.execute( + "UPDATE ci_jobs SET queued_at = ?1 WHERE name = ?2", + (at, j.name.clone()), + ) + .await + .unwrap(); + } + run + } + fn mem_job(name: &str, memory: Option) -> Job { let mut spec = serde_json::json!({ "image": "alpine", "run": "true", "needs": [], "runs_on": [], @@ -2420,7 +2443,7 @@ mod tests { #[tokio::test] async fn claim_picks_the_first_job_that_fits_the_free_memory() { let fx = fixture().await; - create( + create_ordered( &fx.store, &fx.repo.id, &fx.tahinli.id, @@ -2470,7 +2493,7 @@ mod tests { #[tokio::test] async fn a_runner_without_an_offer_gets_the_oldest_job_as_before() { let fx = fixture().await; - create( + create_ordered( &fx.store, &fx.repo.id, &fx.tahinli.id, @@ -2490,7 +2513,7 @@ mod tests { #[tokio::test] async fn a_job_bigger_than_the_budget_runs_when_the_runner_is_idle() { let fx = fixture().await; - create( + create_ordered( &fx.store, &fx.repo.id, &fx.tahinli.id, @@ -2519,7 +2542,7 @@ mod tests { #[tokio::test] async fn a_job_that_waited_too_long_stops_smaller_jobs_passing_it() { let fx = fixture().await; - create( + create_ordered( &fx.store, &fx.repo.id, &fx.tahinli.id, diff --git a/crates/forge-web/src/ci/api_tests.rs b/crates/forge-web/src/ci/api_tests.rs index c6da520..1688a21 100644 --- a/crates/forge-web/src/ci/api_tests.rs +++ b/crates/forge-web/src/ci/api_tests.rs @@ -604,14 +604,23 @@ async fn an_old_runner_without_memory_fields_still_gets_jobs() { ], ) .await; - // An old runner sends a body with labels only, or nothing parseable at all. - let sent = claim(&env.router, &secret).await; - assert_eq!(sent.status, 200); - assert_eq!(sent.json()["job_name"], "big"); - // The assignment of a job without memory has no memory field, which an old runner ignores. - let sent = claim(&env.router, &secret).await; - assert_eq!(sent.json()["job_name"], "plain"); - assert!(sent.json().get("memory_bytes").is_none()); + // An old runner sends a body with labels only, or nothing parseable at all. Both jobs are + // handed out whatever their size, in the queue's order (which ties here), and the one without + // memory carries no memory field, which an old runner ignores anyway. + let first = claim(&env.router, &secret).await; + let second = claim(&env.router, &secret).await; + assert_eq!((first.status, second.status), (200, 200)); + let mut names = [first.json(), second.json()].map(|j| { + ( + j["job_name"].as_str().unwrap().to_owned(), + j.get("memory_bytes").is_none(), + ) + }); + names.sort(); + assert_eq!( + names, + [("big".to_owned(), false), ("plain".to_owned(), true)] + ); // And the stored figures stay empty. let listed = env.store.list_runners().await.unwrap().remove(0); assert_eq!(listed.reported.memory_budget, None);