Skip to content

Commit 07108ad

Browse files
committed
Add a universal method for synchronizing in tests
Some bors commands do not produce comments, so `expect_comments`/`get_comment` cannot be used for them.
1 parent 763b7f9 commit 07108ad

File tree

2 files changed

+57
-17
lines changed

2 files changed

+57
-17
lines changed

src/bors/handlers/review.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -547,11 +547,12 @@ approve = ["+approved"]
547547
async fn set_priority(pool: sqlx::PgPool) {
548548
run_test(pool, |mut tester| async {
549549
tester.post_comment("@bors p=5").await?;
550-
// Wait for db update.
551-
tester.expect_comments(1).await;
552-
let pr = tester.get_default_pr().await?;
553-
554-
assert_eq!(pr.priority, Some(5));
550+
tester
551+
.wait_for(|| async {
552+
let pr = tester.get_default_pr().await?;
553+
Ok(pr.priority == Some(5))
554+
})
555+
.await?;
555556
Ok(tester)
556557
})
557558
.await;
@@ -561,10 +562,12 @@ approve = ["+approved"]
561562
async fn priority_preserved_after_approve(pool: sqlx::PgPool) {
562563
run_test(pool, |mut tester| async {
563564
tester.post_comment("@bors p=5").await?;
564-
tester.expect_comments(1).await;
565-
566-
let pr = tester.get_default_pr().await?;
567-
assert_eq!(pr.priority, Some(5));
565+
tester
566+
.wait_for(|| async {
567+
let pr = tester.get_default_pr().await?;
568+
Ok(pr.priority == Some(5))
569+
})
570+
.await?;
568571

569572
tester.post_comment("@bors r+").await?;
570573
tester.expect_comments(1).await;
@@ -582,10 +585,12 @@ approve = ["+approved"]
582585
async fn priority_overridden_on_approve_with_priority(pool: sqlx::PgPool) {
583586
run_test(pool, |mut tester| async {
584587
tester.post_comment("@bors p=5").await?;
585-
tester.expect_comments(1).await;
586-
587-
let pr = tester.get_default_pr().await?;
588-
assert_eq!(pr.priority, Some(5));
588+
tester
589+
.wait_for(|| async {
590+
let pr = tester.get_default_pr().await?;
591+
Ok(pr.priority == Some(5))
592+
})
593+
.await?;
589594

590595
tester.post_comment("@bors r+ p=10").await?;
591596
tester.expect_comments(1).await;

src/tests/mocks/bors.rs

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use anyhow::Context;
2-
use std::collections::HashMap;
3-
use std::future::Future;
4-
use std::sync::Arc;
5-
62
use axum::Router;
73
use parking_lot::lock_api::MappedMutexGuard;
84
use parking_lot::{Mutex, MutexGuard, RawMutex};
95
use serde::Serialize;
106
use sqlx::PgPool;
7+
use std::collections::HashMap;
8+
use std::future::Future;
9+
use std::sync::Arc;
10+
use std::time::Duration;
1111
use tokio::sync::mpsc::Sender;
1212
use tokio::task::JoinHandle;
1313
use tower::Service;
@@ -351,6 +351,41 @@ impl BorsTester {
351351
Ok(())
352352
}
353353

354+
/// Wait until the given condition is true.
355+
/// Checks the condition every 500ms.
356+
/// Times out if it takes too long (more than 5s).
357+
///
358+
/// This method is useful if you execute a command that produces no comment as an output
359+
/// and you need to wait until it has been processed by bors.
360+
/// Prefer using [BorsTester::expect_comments] or [BorsTester::get_comment] to synchronize
361+
/// if you are waiting for a comment to be posted to a PR.
362+
pub async fn wait_for<F, Fut>(&self, condition: F) -> anyhow::Result<()>
363+
where
364+
F: Fn() -> Fut,
365+
Fut: Future<Output = anyhow::Result<bool>>,
366+
{
367+
let wait_fut = async move {
368+
loop {
369+
let fut = condition();
370+
match fut.await {
371+
Ok(res) => {
372+
if res {
373+
return Ok(());
374+
} else {
375+
tokio::time::sleep(Duration::from_millis(500)).await;
376+
}
377+
}
378+
Err(error) => {
379+
return Err(error);
380+
}
381+
}
382+
}
383+
};
384+
tokio::time::timeout(Duration::from_secs(5), wait_fut)
385+
.await
386+
.unwrap_or_else(|_| Err(anyhow::anyhow!("Timed out waiting for condition")))
387+
}
388+
354389
pub async fn finish(self, bors: JoinHandle<()>) -> World {
355390
// Make sure that the event channel senders are closed
356391
drop(self.app);

0 commit comments

Comments
 (0)