forge
Explore
Log in

Don't you dare go Hollow

Order the memory claim tests by queued_at, jobs created together tie

Browse files at this commit

b3beedbbd329021124e69e646d3e6acecf9087e6Ahmet Kaan GümüşParent: 69c5821
  • fix
    • order the memory claim tests by queued_at, jobs created together tie
Show full message
🐛 fix: order the memory claim tests by queued_at, jobs created together tie
crates/forge-core/src/store/ci.rsmodified+27 -4
@@ -2395,6 +2395,29 @@ mod tests {
23952395 ));
23962396 }
23972397
2398+ /// Like `create`, then gives the jobs distinct, increasing `queued_at` in the order listed,
2399+ /// because jobs created together tie and then sort by random id.
2400+ async fn create_ordered(
2401+ store: &Store,
2402+ repo_id: &str,
2403+ actor: &str,
2404+ jobs: &[Job],
2405+ approval: bool,
2406+ ) -> CiRun {
2407+ let run = create(store, repo_id, actor, jobs, approval).await;
2408+ let conn = store.inner.conn.lock().await;
2409+ for (i, j) in jobs.iter().enumerate() {
2410+ let at = crate::time::stamp_before(Duration::from_secs(10 - i as u64));
2411+ conn.execute(
2412+ "UPDATE ci_jobs SET queued_at = ?1 WHERE name = ?2",
2413+ (at, j.name.clone()),
2414+ )
2415+ .await
2416+ .unwrap();
2417+ }
2418+ run
2419+ }
2420+
23982421 fn mem_job(name: &str, memory: Option<u64>) -> Job {
23992422 let mut spec = serde_json::json!({
24002423 "image": "alpine", "run": "true", "needs": [], "runs_on": [],
@@ -2420,7 +2443,7 @@ mod tests {
24202443 #[tokio::test]
24212444 async fn claim_picks_the_first_job_that_fits_the_free_memory() {
24222445 let fx = fixture().await;
2423- create(
2446+ create_ordered(
24242447 &fx.store,
24252448 &fx.repo.id,
24262449 &fx.tahinli.id,
@@ -2470,7 +2493,7 @@ mod tests {
24702493 #[tokio::test]
24712494 async fn a_runner_without_an_offer_gets_the_oldest_job_as_before() {
24722495 let fx = fixture().await;
2473- create(
2496+ create_ordered(
24742497 &fx.store,
24752498 &fx.repo.id,
24762499 &fx.tahinli.id,
@@ -2490,7 +2513,7 @@ mod tests {
24902513 #[tokio::test]
24912514 async fn a_job_bigger_than_the_budget_runs_when_the_runner_is_idle() {
24922515 let fx = fixture().await;
2493- create(
2516+ create_ordered(
24942517 &fx.store,
24952518 &fx.repo.id,
24962519 &fx.tahinli.id,
@@ -2519,7 +2542,7 @@ mod tests {
25192542 #[tokio::test]
25202543 async fn a_job_that_waited_too_long_stops_smaller_jobs_passing_it() {
25212544 let fx = fixture().await;
2522- create(
2545+ create_ordered(
25232546 &fx.store,
25242547 &fx.repo.id,
25252548 &fx.tahinli.id,
crates/forge-web/src/ci/api_tests.rsmodified+17 -8
@@ -604,14 +604,23 @@ async fn an_old_runner_without_memory_fields_still_gets_jobs() {
604604 ],
605605 )
606606 .await;
607- // An old runner sends a body with labels only, or nothing parseable at all.
608- let sent = claim(&env.router, &secret).await;
609- assert_eq!(sent.status, 200);
610- assert_eq!(sent.json()["job_name"], "big");
611- // The assignment of a job without memory has no memory field, which an old runner ignores.
612- let sent = claim(&env.router, &secret).await;
613- assert_eq!(sent.json()["job_name"], "plain");
614- assert!(sent.json().get("memory_bytes").is_none());
607+ // An old runner sends a body with labels only, or nothing parseable at all. Both jobs are
608+ // handed out whatever their size, in the queue's order (which ties here), and the one without
609+ // memory carries no memory field, which an old runner ignores anyway.
610+ let first = claim(&env.router, &secret).await;
611+ let second = claim(&env.router, &secret).await;
612+ assert_eq!((first.status, second.status), (200, 200));
613+ let mut names = [first.json(), second.json()].map(|j| {
614+ (
615+ j["job_name"].as_str().unwrap().to_owned(),
616+ j.get("memory_bytes").is_none(),
617+ )
618+ });
619+ names.sort();
620+ assert_eq!(
621+ names,
622+ [("big".to_owned(), false), ("plain".to_owned(), true)]
623+ );
615624 // And the stored figures stay empty.
616625 let listed = env.store.list_runners().await.unwrap().remove(0);
617626 assert_eq!(listed.reported.memory_budget, None);