Improve test_migration_flags, add EvTracker to test_utils

This commit is contained in:
Hocuri
2021-06-11 16:28:39 +02:00
committed by bjoern
parent 991d15615e
commit d12d9d94d6
2 changed files with 55 additions and 1 deletions

View File

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

View File

@@ -9,6 +9,7 @@ use std::{collections::BTreeMap, panic};
use std::{fmt, thread};
use ansi_term::Color;
use async_std::channel::Receiver;
use async_std::path::PathBuf;
use async_std::sync::{Arc, RwLock};
use async_std::{channel, pin::Pin};
@@ -183,6 +184,21 @@ impl TestContext {
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.
///
/// The context will be fake-configured as the alice user, with a pre-generated secret
@@ -568,6 +584,28 @@ pub fn bob_keypair() -> key::KeyPair {
}
}
pub struct EvTracker(Receiver<EventType>);
impl EvTracker {
pub async fn get_info_contains(&self, s: &str) -> EventType {
loop {
let event = self.0.recv().await.unwrap();
if let EventType::Info(i) = &event {
if i.contains(s) {
return event;
}
}
}
}
}
impl Deref for EvTracker {
type Target = Receiver<EventType>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
/// Gets a specific message from a chat and asserts that the chat has a specific length.
///
/// Panics if the length of the chat is not `asserted_msgs_count` or if the chat item at `index` is not a Message.