Tweak test

This commit is contained in:
Hocuri
2021-06-12 15:06:16 +02:00
committed by bjoern
parent d12d9d94d6
commit b16785bb62
2 changed files with 10 additions and 21 deletions

View File

@@ -806,8 +806,7 @@ mod test {
#[async_std::test] #[async_std::test]
async fn test_migration_flags() -> Result<()> { async fn test_migration_flags() -> Result<()> {
let t = TestContext::new().await; let t = TestContext::new().await;
let events = t.new_evtracker().await; t.evtracker.get_info_contains("Opened database").await;
events.get_info_contains("Opened database").await;
// as migrations::run() was already executed on context creation, // as migrations::run() was already executed on context creation,
// another call should not result in any action needed. // another call should not result in any action needed.
@@ -822,7 +821,7 @@ mod test {
info!(&t, "test_migration_flags: XXX"); info!(&t, "test_migration_flags: XXX");
loop { loop {
if let EventType::Info(info) = events.recv().await.unwrap() { if let EventType::Info(info) = t.evtracker.recv().await.unwrap() {
assert!( assert!(
!info.contains("[migration]"), !info.contains("[migration]"),
"Migrations were run twice, you probably forgot to update the db version" "Migrations were run twice, you probably forgot to update the db version"

View File

@@ -48,6 +48,7 @@ static CONTEXT_NAMES: Lazy<std::sync::RwLock<BTreeMap<u32, String>>> =
pub(crate) struct TestContext { pub(crate) struct TestContext {
pub ctx: Context, pub ctx: Context,
pub dir: TempDir, pub dir: TempDir,
pub evtracker: EvTracker,
/// Counter for fake IMAP UIDs in [recv_msg], for private use in that function only. /// Counter for fake IMAP UIDs in [recv_msg], for private use in that function only.
recv_idx: RwLock<u32>, recv_idx: RwLock<u32>,
/// Functions to call for events received. /// Functions to call for events received.
@@ -104,6 +105,8 @@ impl TestContext {
let event_sinks: Arc<RwLock<Vec<Box<EventSink>>>> = Arc::new(RwLock::new(Vec::new())); let event_sinks: Arc<RwLock<Vec<Box<EventSink>>>> = Arc::new(RwLock::new(Vec::new()));
let sinks = Arc::clone(&event_sinks); let sinks = Arc::clone(&event_sinks);
let (poison_sender, poison_receiver) = channel::bounded(1); let (poison_sender, poison_receiver) = channel::bounded(1);
let (evtracker_sender, evtracker_receiver) = channel::unbounded();
async_std::task::spawn(async move { async_std::task::spawn(async move {
// Make sure that the test fails if there is a panic on this thread here: // Make sure that the test fails if there is a panic on this thread here:
let current_id = task::current().id(); let current_id = task::current().id();
@@ -123,13 +126,15 @@ impl TestContext {
sink(event.clone()).await; sink(event.clone()).await;
} }
} }
receive_event(event); receive_event(&event);
evtracker_sender.send(event.typ).await.ok();
} }
}); });
Self { Self {
ctx, ctx,
dir, dir,
evtracker: EvTracker(evtracker_receiver),
recv_idx: RwLock::new(0), recv_idx: RwLock::new(0),
event_sinks, event_sinks,
poison_receiver, poison_receiver,
@@ -184,21 +189,6 @@ impl TestContext {
sinks.push(Box::new(move |evt| Box::pin(sink(evt)))); sinks.push(Box::new(move |evt| Box::pin(sink(evt))));
} }
pub async fn new_evtracker(&self) -> EvTracker {
let (sender, receiver) = channel::unbounded();
let sender = Arc::new(sender);
self.add_event_sink(move |event| {
let sender = sender.clone();
async move {
sender.send(event.typ).await.ok();
// If sending fails, probably the EvTracker was simply dropped,
// so we call ok() to ignore the error
}
})
.await;
EvTracker(receiver)
}
/// Configure with alice@example.com. /// Configure with alice@example.com.
/// ///
/// The context will be fake-configured as the alice user, with a pre-generated secret /// The context will be fake-configured as the alice user, with a pre-generated secret
@@ -629,12 +619,12 @@ pub(crate) async fn get_chat_msg(
/// Pretty-print an event to stdout /// Pretty-print an event to stdout
/// ///
/// Done during tests this is captured by `cargo test` and associated with the test itself. /// Done during tests this is captured by `cargo test` and associated with the test itself.
fn receive_event(event: Event) { fn receive_event(event: &Event) {
let green = Color::Green.normal(); let green = Color::Green.normal();
let yellow = Color::Yellow.normal(); let yellow = Color::Yellow.normal();
let red = Color::Red.normal(); let red = Color::Red.normal();
let msg = match event.typ { let msg = match &event.typ {
EventType::Info(msg) => format!("INFO: {}", msg), EventType::Info(msg) => format!("INFO: {}", msg),
EventType::SmtpConnected(msg) => format!("[SMTP_CONNECTED] {}", msg), EventType::SmtpConnected(msg) => format!("[SMTP_CONNECTED] {}", msg),
EventType::ImapConnected(msg) => format!("[IMAP_CONNECTED] {}", msg), EventType::ImapConnected(msg) => format!("[IMAP_CONNECTED] {}", msg),