Skip to content

Commit b70199c

Browse files
committed
Merge PR #216
2 parents 9bc5b7f + 07108ad commit b70199c

File tree

2 files changed

+74
-27
lines changed

2 files changed

+74
-27
lines changed

src/bors/handlers/review.rs

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -558,10 +558,12 @@ approve = ["+approved"]
558558
async fn set_priority(pool: sqlx::PgPool) {
559559
run_test(pool, |mut tester| async {
560560
tester.post_comment("@bors p=5").await?;
561-
tester.expect_comments(1).await;
562-
let pr = tester.get_default_pr().await?;
563-
564-
assert_eq!(pr.priority, Some(5));
561+
tester
562+
.wait_for(|| async {
563+
let pr = tester.get_default_pr().await?;
564+
Ok(pr.priority == Some(5))
565+
})
566+
.await?;
565567
Ok(tester)
566568
})
567569
.await;
@@ -571,10 +573,12 @@ approve = ["+approved"]
571573
async fn priority_preserved_after_approve(pool: sqlx::PgPool) {
572574
run_test(pool, |mut tester| async {
573575
tester.post_comment("@bors p=5").await?;
574-
tester.expect_comments(1).await;
575-
576-
let pr = tester.get_default_pr().await?;
577-
assert_eq!(pr.priority, Some(5));
576+
tester
577+
.wait_for(|| async {
578+
let pr = tester.get_default_pr().await?;
579+
Ok(pr.priority == Some(5))
580+
})
581+
.await?;
578582

579583
tester.post_comment("@bors r+").await?;
580584
tester.expect_comments(1).await;
@@ -592,10 +596,12 @@ approve = ["+approved"]
592596
async fn priority_overridden_on_approve_with_priority(pool: sqlx::PgPool) {
593597
run_test(pool, |mut tester| async {
594598
tester.post_comment("@bors p=5").await?;
595-
tester.expect_comments(1).await;
596-
597-
let pr = tester.get_default_pr().await?;
598-
assert_eq!(pr.priority, Some(5));
599+
tester
600+
.wait_for(|| async {
601+
let pr = tester.get_default_pr().await?;
602+
Ok(pr.priority == Some(5))
603+
})
604+
.await?;
599605

600606
tester.post_comment("@bors r+ p=10").await?;
601607
tester.expect_comments(1).await;
@@ -734,10 +740,12 @@ approve = ["+approved"]
734740
assert!(pr.delegated);
735741

736742
tester.post_comment(as_reviewer("@bors delegate-")).await?;
737-
tester.expect_comments(1).await;
738-
739-
let pr = tester.get_default_pr().await?;
740-
assert!(!pr.delegated);
743+
tester
744+
.wait_for(|| async {
745+
let pr = tester.get_default_pr().await?;
746+
Ok(!pr.delegated)
747+
})
748+
.await?;
741749
Ok(tester)
742750
})
743751
.await;
@@ -752,10 +760,12 @@ approve = ["+approved"]
752760
tester.expect_comments(1).await;
753761

754762
tester.post_comment("@bors delegate-").await?;
755-
tester.expect_comments(1).await;
756-
757-
let pr = tester.get_default_pr().await?;
758-
assert!(!pr.delegated);
763+
tester
764+
.wait_for(|| async {
765+
let pr = tester.get_default_pr().await?;
766+
Ok(!pr.delegated)
767+
})
768+
.await?;
759769
Ok(tester)
760770
})
761771
.await;

src/tests/mocks/bors.rs

Lines changed: 44 additions & 7 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;
@@ -66,7 +66,7 @@ impl BorsBuilder {
6666
Ok(tester) => tester.finish(bors).await,
6767
Err(error) => {
6868
let result = bors.await;
69-
panic!("Error in run_test\n{result:?}\n{error:?}");
69+
panic!("Error in test:\n{error:?}\n\nBors service result:\n{result:?}");
7070
}
7171
}
7272
}
@@ -207,8 +207,10 @@ impl BorsTester {
207207

208208
/// Expect that `count` comments will be received, without checking their contents.
209209
pub async fn expect_comments(&mut self, count: u64) {
210-
for _ in 0..count {
211-
let _ = self.get_comment().await;
210+
for i in 0..count {
211+
self.get_comment()
212+
.await
213+
.expect(&format!("Failed to get comment #{i}"));
212214
}
213215
}
214216

@@ -349,6 +351,41 @@ impl BorsTester {
349351
Ok(())
350352
}
351353

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+
352389
pub async fn finish(self, bors: JoinHandle<()>) -> World {
353390
// Make sure that the event channel senders are closed
354391
drop(self.app);

0 commit comments

Comments
 (0)