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 {
2395 ));2395 ));
2396 }2396 }
2397 2397
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+
2398 fn mem_job(name: &str, memory: Option<u64>) -> Job {2421 fn mem_job(name: &str, memory: Option<u64>) -> Job {
2399 let mut spec = serde_json::json!({2422 let mut spec = serde_json::json!({
2400 "image": "alpine", "run": "true", "needs": [], "runs_on": [],2423 "image": "alpine", "run": "true", "needs": [], "runs_on": [],
@@ -2420,7 +2443,7 @@ mod tests {
2420 #[tokio::test]2443 #[tokio::test]
2421 async fn claim_picks_the_first_job_that_fits_the_free_memory() {2444 async fn claim_picks_the_first_job_that_fits_the_free_memory() {
2422 let fx = fixture().await;2445 let fx = fixture().await;
2423- create(2446+ create_ordered(
2424 &fx.store,2447 &fx.store,
2425 &fx.repo.id,2448 &fx.repo.id,
2426 &fx.tahinli.id,2449 &fx.tahinli.id,
@@ -2470,7 +2493,7 @@ mod tests {
2470 #[tokio::test]2493 #[tokio::test]
2471 async fn a_runner_without_an_offer_gets_the_oldest_job_as_before() {2494 async fn a_runner_without_an_offer_gets_the_oldest_job_as_before() {
2472 let fx = fixture().await;2495 let fx = fixture().await;
2473- create(2496+ create_ordered(
2474 &fx.store,2497 &fx.store,
2475 &fx.repo.id,2498 &fx.repo.id,
2476 &fx.tahinli.id,2499 &fx.tahinli.id,
@@ -2490,7 +2513,7 @@ mod tests {
2490 #[tokio::test]2513 #[tokio::test]
2491 async fn a_job_bigger_than_the_budget_runs_when_the_runner_is_idle() {2514 async fn a_job_bigger_than_the_budget_runs_when_the_runner_is_idle() {
2492 let fx = fixture().await;2515 let fx = fixture().await;
2493- create(2516+ create_ordered(
2494 &fx.store,2517 &fx.store,
2495 &fx.repo.id,2518 &fx.repo.id,
2496 &fx.tahinli.id,2519 &fx.tahinli.id,
@@ -2519,7 +2542,7 @@ mod tests {
2519 #[tokio::test]2542 #[tokio::test]
2520 async fn a_job_that_waited_too_long_stops_smaller_jobs_passing_it() {2543 async fn a_job_that_waited_too_long_stops_smaller_jobs_passing_it() {
2521 let fx = fixture().await;2544 let fx = fixture().await;
2522- create(2545+ create_ordered(
2523 &fx.store,2546 &fx.store,
2524 &fx.repo.id,2547 &fx.repo.id,
2525 &fx.tahinli.id,2548 &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() {
604 ],604 ],
605 )605 )
606 .await;606 .await;
607- // An old runner sends a body with labels only, or nothing parseable at all.607+ // An old runner sends a body with labels only, or nothing parseable at all. Both jobs are
608- let sent = claim(&env.router, &secret).await;608+ // handed out whatever their size, in the queue's order (which ties here), and the one without
609- assert_eq!(sent.status, 200);609+ // memory carries no memory field, which an old runner ignores anyway.
610- assert_eq!(sent.json()["job_name"], "big");610+ let first = claim(&env.router, &secret).await;
611- // The assignment of a job without memory has no memory field, which an old runner ignores.611+ let second = claim(&env.router, &secret).await;
612- let sent = claim(&env.router, &secret).await;612+ assert_eq!((first.status, second.status), (200, 200));
613- assert_eq!(sent.json()["job_name"], "plain");613+ let mut names = [first.json(), second.json()].map(|j| {
614- assert!(sent.json().get("memory_bytes").is_none());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+ );
615 // And the stored figures stay empty.624 // And the stored figures stay empty.
616 let listed = env.store.list_runners().await.unwrap().remove(0);625 let listed = env.store.list_runners().await.unwrap().remove(0);
617 assert_eq!(listed.reported.memory_budget, None);626 assert_eq!(listed.reported.memory_budget, None);