Remove EventSink from TestContext to simplify it

This replaces the EventSink callbacks with simple channel senders.
This simplifies the TestContext a lot as that is much simpler to
handle.  It then also removes the special-casing of the LogSink since
it now is another even sender, only injected at the very start.
This commit is contained in:
Floris Bruynooghe
2021-12-12 21:31:25 +01:00
parent f0ca50ba27
commit 147f5c1e0d
4 changed files with 117 additions and 142 deletions

View File

@@ -942,7 +942,6 @@ mod tests {
use crate::chat::ProtectionStatus;
use crate::chatlist::Chatlist;
use crate::constants::Chattype;
use crate::events::Event;
use crate::peerstate::Peerstate;
use crate::test_utils::TestContext;
use std::time::Duration;
@@ -955,16 +954,8 @@ mod tests {
assert_eq!(Chatlist::try_load(&bob, 0, None, None).await?.len(), 0);
// Setup JoinerProgress sinks.
let (joiner_progress_tx, joiner_progress_rx) = async_std::channel::bounded(100);
bob.add_event_sink(move |event: Event| {
let joiner_progress_tx = joiner_progress_tx.clone();
async move {
if let EventType::SecurejoinJoinerProgress { .. } = event.typ {
joiner_progress_tx.try_send(event).unwrap();
}
}
})
.await;
let (joiner_progress_tx, joiner_progress_rx) = async_std::channel::unbounded();
bob.add_event_sender(joiner_progress_tx).await;
// Step 1: Generate QR-code, ChatId(0) indicates setup-contact
let qr = dc_get_securejoin_qr(&alice.ctx, None).await?;
@@ -997,29 +988,33 @@ mod tests {
bob.recv_msg(&sent).await;
// Check Bob emitted the JoinerProgress event.
{
let evt = joiner_progress_rx
.recv()
.timeout(Duration::from_secs(10))
.await
.expect("timeout waiting for JoinerProgress event")
.expect("missing JoinerProgress event");
match evt.typ {
EventType::SecurejoinJoinerProgress {
contact_id,
progress,
} => {
let alice_contact_id =
Contact::lookup_id_by_addr(&bob.ctx, "alice@example.org", Origin::Unknown)
.await
.expect("Error looking up contact")
.expect("Contact not found");
assert_eq!(contact_id, alice_contact_id);
assert_eq!(progress, 400);
async {
loop {
let event = joiner_progress_rx.recv().await.unwrap();
match event.typ {
EventType::SecurejoinJoinerProgress {
contact_id,
progress,
} => {
let alice_contact_id = Contact::lookup_id_by_addr(
&bob.ctx,
"alice@example.org",
Origin::Unknown,
)
.await
.expect("Error looking up contact")
.expect("Contact not found");
assert_eq!(contact_id, alice_contact_id);
assert_eq!(progress, 400);
break;
}
_ => {}
}
_ => panic!("Wrong event type"),
}
}
.timeout(Duration::from_secs(10))
.await
.expect("timeout waiting for JoinerProgress event");
// Check Bob sent the right message.
let sent = bob.pop_sent_msg().await;
@@ -1157,16 +1152,8 @@ mod tests {
let bob = TestContext::new_bob().await;
// Setup JoinerProgress sinks.
let (joiner_progress_tx, joiner_progress_rx) = async_std::channel::bounded(100);
bob.add_event_sink(move |event: Event| {
let joiner_progress_tx = joiner_progress_tx.clone();
async move {
if let EventType::SecurejoinJoinerProgress { .. } = event.typ {
joiner_progress_tx.try_send(event).unwrap();
}
}
})
.await;
let (joiner_progress_tx, joiner_progress_rx) = async_std::channel::unbounded();
bob.add_event_sender(joiner_progress_tx).await;
// Ensure Bob knows Alice_FP
let alice_pubkey = SignedPublicKey::load_self(&alice.ctx).await?;
@@ -1194,29 +1181,33 @@ mod tests {
dc_join_securejoin(&bob.ctx, &qr).await.unwrap();
// Check Bob emitted the JoinerProgress event.
{
let evt = joiner_progress_rx
.recv()
.timeout(Duration::from_secs(10))
.await
.expect("timeout waiting for JoinerProgress event")
.expect("missing JoinerProgress event");
match evt.typ {
EventType::SecurejoinJoinerProgress {
contact_id,
progress,
} => {
let alice_contact_id =
Contact::lookup_id_by_addr(&bob.ctx, "alice@example.org", Origin::Unknown)
.await
.expect("Error looking up contact")
.expect("Contact not found");
assert_eq!(contact_id, alice_contact_id);
assert_eq!(progress, 400);
async {
loop {
let event = joiner_progress_rx.recv().await.unwrap();
match event.typ {
EventType::SecurejoinJoinerProgress {
contact_id,
progress,
} => {
let alice_contact_id = Contact::lookup_id_by_addr(
&bob.ctx,
"alice@example.org",
Origin::Unknown,
)
.await
.expect("Error looking up contact")
.expect("Contact not found");
assert_eq!(contact_id, alice_contact_id);
assert_eq!(progress, 400);
break;
}
_ => {}
}
_ => panic!("Wrong event type"),
}
}
.timeout(Duration::from_secs(10))
.await
.expect("timeout waiting for JoinerProgress event");
assert!(!bob.ctx.has_ongoing().await);
// Check Bob sent the right handshake message.
@@ -1330,16 +1321,8 @@ mod tests {
assert_eq!(Chatlist::try_load(&bob, 0, None, None).await?.len(), 0);
// Setup JoinerProgress sinks.
let (joiner_progress_tx, joiner_progress_rx) = async_std::channel::bounded(100);
bob.add_event_sink(move |event: Event| {
let joiner_progress_tx = joiner_progress_tx.clone();
async move {
if let EventType::SecurejoinJoinerProgress { .. } = event.typ {
joiner_progress_tx.try_send(event).unwrap();
}
}
})
.await;
let (joiner_progress_tx, joiner_progress_rx) = async_std::channel::unbounded();
bob.add_event_sender(joiner_progress_tx).await;
let chatid =
chat::create_group_chat(&alice.ctx, ProtectionStatus::Protected, "the chat").await?;
@@ -1376,29 +1359,33 @@ mod tests {
let sent = bob.pop_sent_msg().await;
// Check Bob emitted the JoinerProgress event.
{
let evt = joiner_progress_rx
.recv()
.timeout(Duration::from_secs(10))
.await
.expect("timeout waiting for JoinerProgress event")
.expect("missing JoinerProgress event");
match evt.typ {
EventType::SecurejoinJoinerProgress {
contact_id,
progress,
} => {
let alice_contact_id =
Contact::lookup_id_by_addr(&bob.ctx, "alice@example.org", Origin::Unknown)
.await
.expect("Error looking up contact")
.expect("Contact not found");
assert_eq!(contact_id, alice_contact_id);
assert_eq!(progress, 400);
async {
loop {
let event = joiner_progress_rx.recv().await.unwrap();
match event.typ {
EventType::SecurejoinJoinerProgress {
contact_id,
progress,
} => {
let alice_contact_id = Contact::lookup_id_by_addr(
&bob.ctx,
"alice@example.org",
Origin::Unknown,
)
.await
.expect("Error looking up contact")
.expect("Contact not found");
assert_eq!(contact_id, alice_contact_id);
assert_eq!(progress, 400);
break;
}
_ => {}
}
_ => panic!("Wrong event type"),
}
}
.timeout(Duration::from_secs(10))
.await
.expect("timeout waiting for JoinerProgress event");
// Check Bob sent the right handshake message.
let msg = alice.parse_msg(&sent).await;