Skip to content

Commit c322f4d

Browse files
committed
Add tests
1 parent 2a45040 commit c322f4d

File tree

2 files changed

+185
-109
lines changed

2 files changed

+185
-109
lines changed

database/src/pool.rs

Lines changed: 142 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ mod tests {
312312
use std::str::FromStr;
313313

314314
use super::*;
315-
use crate::{tests::run_db_test, Commit, CommitType, Date};
315+
use crate::{tests::run_db_test, Commit, CommitJobState, CommitJobType, CommitType, Date};
316316

317317
/// Create a Commit
318318
fn create_commit(commit_sha: &str, time: chrono::DateTime<Utc>, r#type: CommitType) -> Commit {
@@ -323,6 +323,29 @@ mod tests {
323323
}
324324
}
325325

326+
/// Create a CommitJob
327+
fn create_commit_job(
328+
sha: &str,
329+
parent_sha: &str,
330+
commit_time: chrono::DateTime<Utc>,
331+
target: Target,
332+
job_type: CommitJobType,
333+
state: CommitJobState,
334+
) -> CommitJob {
335+
CommitJob {
336+
sha: sha.to_string(),
337+
parent_sha: parent_sha.to_string(),
338+
commit_time: Date(commit_time),
339+
target,
340+
include: None,
341+
exclude: None,
342+
runs: None,
343+
backends: None,
344+
job_type,
345+
state,
346+
}
347+
}
348+
326349
#[tokio::test]
327350
async fn pstat_returns_empty_vector_when_empty() {
328351
run_db_test(|ctx| async {
@@ -381,4 +404,122 @@ mod tests {
381404
})
382405
.await;
383406
}
407+
408+
#[tokio::test]
409+
async fn take_commit_job() {
410+
run_db_test(|ctx| async {
411+
// ORDER:
412+
// Releases first
413+
// Master commits second, order by oldest PR ascending
414+
// Try commits last, order by oldest PR ascending
415+
416+
let db = ctx.db_client().connection().await;
417+
let time = chrono::DateTime::from_str("2021-09-01T00:00:00.000Z").unwrap();
418+
419+
// Try commits
420+
let try_job_1 = create_commit_job(
421+
"sha1",
422+
"p1",
423+
time,
424+
Target::X86_64UnknownLinuxGnu,
425+
CommitJobType::Try { pr: 1 },
426+
CommitJobState::Queued,
427+
);
428+
let try_job_2 = create_commit_job(
429+
"sha2",
430+
"p2",
431+
time,
432+
Target::X86_64UnknownLinuxGnu,
433+
CommitJobType::Try { pr: 2 },
434+
CommitJobState::Queued,
435+
);
436+
437+
// Master commits
438+
let master_job_1 = create_commit_job(
439+
"sha3",
440+
"p3",
441+
time,
442+
Target::X86_64UnknownLinuxGnu,
443+
CommitJobType::Master { pr: 3 },
444+
CommitJobState::Queued,
445+
);
446+
let master_job_2 = create_commit_job(
447+
"sha4",
448+
"p4",
449+
time,
450+
Target::X86_64UnknownLinuxGnu,
451+
CommitJobType::Master { pr: 4 },
452+
CommitJobState::Queued,
453+
);
454+
455+
// Release commits
456+
let release_job_1 = create_commit_job(
457+
"sha5",
458+
"p5",
459+
time,
460+
Target::X86_64UnknownLinuxGnu,
461+
CommitJobType::Release { tag: "tag1".into() },
462+
CommitJobState::Queued,
463+
);
464+
let release_job_2 = create_commit_job(
465+
"sha6",
466+
"p6",
467+
time,
468+
Target::X86_64UnknownLinuxGnu,
469+
CommitJobType::Release { tag: "tag2".into() },
470+
CommitJobState::Queued,
471+
);
472+
473+
// Shuffle the insert order a bit
474+
let all_commits = vec![
475+
release_job_1,
476+
master_job_2,
477+
try_job_1,
478+
release_job_2,
479+
master_job_1,
480+
try_job_2,
481+
];
482+
483+
// queue all the jobs
484+
for commit in all_commits {
485+
db.enqueue_commit_job(&commit).await;
486+
}
487+
488+
// Now we test the ordering: after each dequeue we immediately mark
489+
// the job as finished for the sake of testing so it can't be
490+
// returned again in the test.
491+
//
492+
// The priority should be;
493+
//
494+
// 1. Release commits (oldest tag first)
495+
// 2. Master commits (oldest PR first)
496+
// 3. Try commits (oldest PR first)
497+
//
498+
// Given the data we inserted above the expected SHA order is:
499+
// sha5, sha6, sha3, sha4, sha1, sha2.
500+
501+
let machine = "machine-1";
502+
let target = Target::X86_64UnknownLinuxGnu;
503+
let expected = ["sha5", "sha6", "sha3", "sha4", "sha1", "sha2"];
504+
505+
for &sha in &expected {
506+
let job = db.take_commit_job(machine, target).await;
507+
assert!(job.is_some(), "expected a job for sha {sha}");
508+
let job = job.unwrap();
509+
assert_eq!(job.sha, sha, "jobs dequeued out of priority order");
510+
511+
// Mark the job finished so it is not returned again.
512+
db.finish_commit_job(machine, target, sha.to_string()).await;
513+
}
514+
515+
// After all six jobs have been taken, the queue should be empty.
516+
assert!(
517+
db.take_commit_job(machine, target).await.is_none(),
518+
"queue should be empty after draining all jobs"
519+
);
520+
521+
Ok(ctx)
522+
})
523+
.await;
524+
}
384525
}

0 commit comments

Comments
 (0)