Skip to content

Commit

Permalink
test: get rid of EventBuilder (#4560)
Browse files Browse the repository at this point in the history
This gets rid of `EventBuilder`, and makes more usage of the
`EventFactory`, which is more ergonomic to create test events.

A large part of
#3716.
  • Loading branch information
bnjbvr authored Jan 21, 2025
1 parent dbadfe1 commit 0c74abb
Show file tree
Hide file tree
Showing 21 changed files with 567 additions and 827 deletions.
38 changes: 12 additions & 26 deletions benchmarks/benches/room_bench.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
use std::{sync::Arc, time::Duration};

use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion, Throughput};
use matrix_sdk::{
config::SyncSettings, test_utils::logged_in_client_with_server, utils::IntoRawStateEventContent,
};
use matrix_sdk::{config::SyncSettings, test_utils::logged_in_client_with_server};
use matrix_sdk_base::{
store::StoreConfig, BaseClient, RoomInfo, RoomState, SessionMeta, StateChanges, StateStore,
};
use matrix_sdk_sqlite::SqliteStateStore;
use matrix_sdk_test::{
event_factory::EventFactory, EventBuilder, JoinedRoomBuilder, StateTestEvent,
SyncResponseBuilder,
event_factory::EventFactory, JoinedRoomBuilder, StateTestEvent, SyncResponseBuilder,
};
use matrix_sdk_ui::{timeline::TimelineFocus, Timeline};
use ruma::{
api::client::membership::get_member_events,
device_id,
events::room::member::{RoomMemberEvent, RoomMemberEventContent},
owned_room_id, owned_user_id,
events::room::member::{MembershipState, RoomMemberEvent},
mxc_uri, owned_room_id, owned_user_id,
serde::Raw,
user_id, EventId, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedUserId,
};
Expand All @@ -35,28 +32,17 @@ pub fn receive_all_members_benchmark(c: &mut Criterion) {
let runtime = Builder::new_multi_thread().build().expect("Can't create runtime");
let room_id = owned_room_id!("!room:example.com");

let ev_builder = EventBuilder::new();
let f = EventFactory::new().room(&room_id);
let mut member_events: Vec<Raw<RoomMemberEvent>> = Vec::with_capacity(MEMBERS_IN_ROOM);
let member_content_json = json!({
"avatar_url": "mxc://example.org/SEsfnsuifSDFSSEF",
"displayname": "Alice Margatroid",
"membership": "join",
"reason": "Looking for support",
});
let member_content: Raw<RoomMemberEventContent> =
member_content_json.into_raw_state_event_content().cast();
for i in 0..MEMBERS_IN_ROOM {
let user_id = OwnedUserId::try_from(format!("@user_{}:matrix.org", i)).unwrap();
let state_key = user_id.to_string();
let event: Raw<RoomMemberEvent> = ev_builder
.make_state_event(
&user_id,
&room_id,
&state_key,
member_content.deserialize().unwrap(),
None,
)
.cast();
let event = f
.member(&user_id)
.membership(MembershipState::Join)
.avatar_url(mxc_uri!("mxc://example.org/SEsfnsuifSDFSSEF"))
.display_name("Alice Margatroid")
.reason("Looking for support")
.into_raw();
member_events.push(event);
}

Expand Down
178 changes: 82 additions & 96 deletions crates/matrix-sdk-base/src/read_receipts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ mod tests {

use eyeball_im::Vector;
use matrix_sdk_common::{deserialized_responses::SyncTimelineEvent, ring_buffer::RingBuffer};
use matrix_sdk_test::{sync_timeline_event, EventBuilder};
use matrix_sdk_test::{event_factory::EventFactory, sync_timeline_event};
use ruma::{
event_id,
events::receipt::{ReceiptThread, ReceiptType},
Expand Down Expand Up @@ -975,12 +975,12 @@ mod tests {
let ev1 = sync_timeline_message(other_user_id, receipt_event_id, "A");
let ev2 = sync_timeline_message(other_user_id, "$2", "A");

let receipt_event = EventBuilder::new().make_receipt_event_content([(
receipt_event_id.to_owned(),
ReceiptType::Read,
user_id.to_owned(),
ReceiptThread::Unthreaded,
)]);
let f = EventFactory::new();

let receipt_event = f
.read_receipts()
.add(receipt_event_id, user_id, ReceiptType::Read, ReceiptThread::Unthreaded)
.build();

let mut read_receipts = Default::default();
compute_unread_counts(
Expand Down Expand Up @@ -1035,30 +1035,32 @@ mod tests {

// Given a receipt event marking events 1-3 as read using a combination of
// different thread and privacy types,
let f = EventFactory::new();
for receipt_type_1 in &[ReceiptType::Read, ReceiptType::ReadPrivate] {
for receipt_thread_1 in &[ReceiptThread::Unthreaded, ReceiptThread::Main] {
for receipt_type_2 in &[ReceiptType::Read, ReceiptType::ReadPrivate] {
for receipt_thread_2 in &[ReceiptThread::Unthreaded, ReceiptThread::Main] {
let receipt_event = EventBuilder::new().make_receipt_event_content([
(
owned_event_id!("$2"),
let receipt_event = f
.read_receipts()
.add(
event_id!("$2"),
user_id,
receipt_type_1.clone(),
user_id.to_owned(),
receipt_thread_1.clone(),
),
(
owned_event_id!("$3"),
)
.add(
event_id!("$3"),
user_id,
receipt_type_2.clone(),
user_id.to_owned(),
receipt_thread_2.clone(),
),
(
owned_event_id!("$1"),
)
.add(
event_id!("$1"),
user_id,
receipt_type_1.clone(),
user_id.to_owned(),
receipt_thread_2.clone(),
),
]);
)
.build();

// When I compute the notifications for this room (with no new events),
let mut read_receipts = RoomReadReceipts::default();
Expand Down Expand Up @@ -1118,12 +1120,11 @@ mod tests {

let events = make_test_events(user_id!("@bob:example.org"));

let receipt_event = EventBuilder::new().make_receipt_event_content([(
owned_event_id!("$6"),
ReceiptType::Read,
user_id.clone(),
ReceiptThread::Unthreaded,
)]);
let f = EventFactory::new();
let receipt_event = f
.read_receipts()
.add(event_id!("$6"), &user_id, ReceiptType::Read, ReceiptThread::Unthreaded)
.build();

let mut read_receipts = RoomReadReceipts::default();
assert!(read_receipts.pending.is_empty());
Expand Down Expand Up @@ -1154,12 +1155,11 @@ mod tests {

let events = make_test_events(user_id!("@bob:example.org"));

let receipt_event = EventBuilder::new().make_receipt_event_content([(
owned_event_id!("$1"),
ReceiptType::Read,
user_id.clone(),
ReceiptThread::Unthreaded,
)]);
let f = EventFactory::new();
let receipt_event = f
.read_receipts()
.add(event_id!("$1"), &user_id, ReceiptType::Read, ReceiptThread::Unthreaded)
.build();

// Sync with a read receipt *and* a single event that was already known: in that
// case, only consider the new events in isolation, and compute the
Expand Down Expand Up @@ -1412,21 +1412,25 @@ mod tests {

#[test]
fn test_receipt_selector_handle_new_receipt() {
let myself = owned_user_id!("@alice:example.org");
let myself = user_id!("@alice:example.org");
let events = make_test_events(user_id!("@bob:example.org"));

let f = EventFactory::new();
{
// Thread receipts are ignored.
let mut selector = ReceiptSelector::new(&events, None);

let receipt_event = EventBuilder::new().make_receipt_event_content([(
owned_event_id!("$5"),
ReceiptType::Read,
myself.clone(),
ReceiptThread::Thread(owned_event_id!("$2")),
)]);

let pending = selector.handle_new_receipt(&myself, &receipt_event);
let receipt_event = f
.read_receipts()
.add(
event_id!("$5"),
myself,
ReceiptType::Read,
ReceiptThread::Thread(owned_event_id!("$2")),
)
.build();

let pending = selector.handle_new_receipt(myself, &receipt_event);
assert!(pending.is_empty());

let best_receipt = selector.select();
Expand All @@ -1440,14 +1444,12 @@ mod tests {
// receipt.
let mut selector = ReceiptSelector::new(&events, None);

let receipt_event = EventBuilder::new().make_receipt_event_content([(
owned_event_id!("$6"),
receipt_type.clone(),
myself.clone(),
receipt_thread.clone(),
)]);
let receipt_event = f
.read_receipts()
.add(event_id!("$6"), myself, receipt_type.clone(), receipt_thread.clone())
.build();

let pending = selector.handle_new_receipt(&myself, &receipt_event);
let pending = selector.handle_new_receipt(myself, &receipt_event);
assert_eq!(pending[0], event_id!("$6"));
assert_eq!(pending.len(), 1);

Expand All @@ -1460,14 +1462,12 @@ mod tests {
// receipt.
let mut selector = ReceiptSelector::new(&events, None);

let receipt_event = EventBuilder::new().make_receipt_event_content([(
owned_event_id!("$3"),
receipt_type.clone(),
myself.clone(),
receipt_thread.clone(),
)]);
let receipt_event = f
.read_receipts()
.add(event_id!("$3"), myself, receipt_type.clone(), receipt_thread.clone())
.build();

let pending = selector.handle_new_receipt(&myself, &receipt_event);
let pending = selector.handle_new_receipt(myself, &receipt_event);
assert!(pending.is_empty());

let best_receipt = selector.select();
Expand All @@ -1479,14 +1479,12 @@ mod tests {
// better receipt.
let mut selector = ReceiptSelector::new(&events, Some(event_id!("$4")));

let receipt_event = EventBuilder::new().make_receipt_event_content([(
owned_event_id!("$3"),
receipt_type.clone(),
myself.clone(),
receipt_thread.clone(),
)]);
let receipt_event = f
.read_receipts()
.add(event_id!("$3"), myself, receipt_type.clone(), receipt_thread.clone())
.build();

let pending = selector.handle_new_receipt(&myself, &receipt_event);
let pending = selector.handle_new_receipt(myself, &receipt_event);
assert!(pending.is_empty());

let best_receipt = selector.select();
Expand All @@ -1498,14 +1496,12 @@ mod tests {
// new better receipt.
let mut selector = ReceiptSelector::new(&events, Some(event_id!("$2")));

let receipt_event = EventBuilder::new().make_receipt_event_content([(
owned_event_id!("$3"),
receipt_type.clone(),
myself.clone(),
receipt_thread.clone(),
)]);
let receipt_event = f
.read_receipts()
.add(event_id!("$3"), myself, receipt_type.clone(), receipt_thread.clone())
.build();

let pending = selector.handle_new_receipt(&myself, &receipt_event);
let pending = selector.handle_new_receipt(myself, &receipt_event);
assert!(pending.is_empty());

let best_receipt = selector.select();
Expand All @@ -1519,23 +1515,14 @@ mod tests {
// new better receipt.
let mut selector = ReceiptSelector::new(&events, Some(event_id!("$2")));

let receipt_event = EventBuilder::new().make_receipt_event_content([
(
owned_event_id!("$4"),
ReceiptType::ReadPrivate,
myself.clone(),
ReceiptThread::Unthreaded,
),
(
owned_event_id!("$6"),
ReceiptType::ReadPrivate,
myself.clone(),
ReceiptThread::Main,
),
(owned_event_id!("$3"), ReceiptType::Read, myself.clone(), ReceiptThread::Main),
]);

let pending = selector.handle_new_receipt(&myself, &receipt_event);
let receipt_event = f
.read_receipts()
.add(event_id!("$4"), myself, ReceiptType::ReadPrivate, ReceiptThread::Unthreaded)
.add(event_id!("$6"), myself, ReceiptType::ReadPrivate, ReceiptThread::Main)
.add(event_id!("$3"), myself, ReceiptType::Read, ReceiptThread::Main)
.build();

let pending = selector.handle_new_receipt(myself, &receipt_event);
assert_eq!(pending.len(), 1);
assert_eq!(pending[0], event_id!("$6"));

Expand Down Expand Up @@ -1573,15 +1560,15 @@ mod tests {

#[test]
fn test_compute_unread_counts_with_implicit_receipt() {
let user_id = owned_user_id!("@alice:example.org");
let user_id = user_id!("@alice:example.org");
let bob = user_id!("@bob:example.org");
let room_id = room_id!("!room:example.org");

// Given a set of events sent by Bob,
let mut events = make_test_events(bob);

// One by me,
events.push_back(sync_timeline_message(&user_id, "$6", "A mulatto, an albino"));
events.push_back(sync_timeline_message(user_id, "$6", "A mulatto, an albino"));

// And others by Bob,
events.push_back(sync_timeline_message(bob, "$7", "A mosquito, my libido"));
Expand All @@ -1590,19 +1577,18 @@ mod tests {
let events: Vec<_> = events.into_iter().collect();

// I have a read receipt attached to one of Bob's event sent before my message,
let receipt_event = EventBuilder::new().make_receipt_event_content([(
owned_event_id!("$3"),
ReceiptType::Read,
user_id.clone(),
ReceiptThread::Unthreaded,
)]);
let f = EventFactory::new();
let receipt_event = f
.read_receipts()
.add(event_id!("$3"), user_id, ReceiptType::Read, ReceiptThread::Unthreaded)
.build();

let mut read_receipts = RoomReadReceipts::default();

// And I compute the unread counts for all those new events (no previous events
// in that room),
compute_unread_counts(
&user_id,
user_id,
room_id,
Some(&receipt_event),
Vector::new(),
Expand Down
Loading

0 comments on commit 0c74abb

Please sign in to comment.