38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 1 | use std::time::Duration; |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 2 | |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 3 | use macltok::{Engine, EngineConfig}; |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 4 | use matrix_sdk::{ |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 5 | ruma::{api::client::sync::sync_events::v5, room_id, user_id}, |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 6 | test_utils::mocks::MatrixMockServer, |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 7 | }; |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 8 | use matrix_sdk_test::{JoinedRoomBuilder, event_factory::EventFactory}; |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 9 | use wiremock::ResponseTemplate; |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 10 | |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 11 | async fn prepare_twice(encrypted: bool, member_syncs: u64) { |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 12 | let server = MatrixMockServer::new().await; |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 13 | server |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 14 | .mock_sliding_sync() |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 15 | .ok(v5::Response::new("1".to_owned())) |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 16 | .mount() |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 17 | .await; |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 18 | let client = server.client_builder().build().await; |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 19 | let room_id = room_id!("!prepare:localhost"); |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 20 | let factory = EventFactory::new().room(room_id); |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 21 | let mut room = JoinedRoomBuilder::new(room_id).add_state_event( |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 22 | factory |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 23 | .member(user_id!("@example:localhost")) |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 24 | .into_raw_sync_state(), |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 25 | ); |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 26 | if encrypted { |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 27 | room = room.add_state_event( |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 28 | factory |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 29 | .room_encryption() |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 30 | .sender(user_id!("@example:localhost")) |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 31 | .into_raw_sync_state(), |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 32 | ); |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 33 | } |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 34 | server.sync_room(&client, room).await; |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 35 | let member: matrix_sdk::ruma::serde::Raw< |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 36 | matrix_sdk::ruma::events::room::member::RoomMemberEvent, |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 37 | > = factory.member(user_id!("@example:localhost")).into_raw(); |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 38 | server |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 39 | .mock_get_members() |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 40 | .respond_with( |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 41 | ResponseTemplate::new(200) |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 42 | .set_body_json(serde_json::json!({ "chunk": [member] })) |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 43 | .set_delay(Duration::from_millis(300)), |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 44 | ) |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 45 | .expect(member_syncs) |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 46 | .mount() |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 47 | .await; |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 48 | let config = EngineConfig::default() |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 49 | .with_initial_history_limit(0) |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 50 | .with_prepare_to_send(false); |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 51 | let (engine, handle, mut events) = Engine::from_client(client, config).await.unwrap(); |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 52 | let running = tokio::spawn(engine.run()); |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 53 | let draining = tokio::spawn(async move { while events.recv().await.is_some() {} }); |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 54 | let (first, second) = tokio::join!( |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 55 | handle.prepare_to_send(room_id.to_owned()), |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 56 | handle.prepare_to_send(room_id.to_owned()), |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 57 | ); |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 58 | first.unwrap(); |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 59 | second.unwrap(); |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 60 | handle.shutdown().await.unwrap(); |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 61 | running.await.unwrap().unwrap(); |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 62 | draining.await.unwrap(); |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 63 | server.server().verify().await; |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 64 | } |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 65 | |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 66 | #[tokio::test] |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 67 | async fn two_prepares_at_once_sync_members_once() { |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 68 | prepare_twice(true, 1).await; |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 69 | } |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 70 | |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 71 | #[tokio::test] |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 72 | async fn a_plain_room_needs_no_preparing() { |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 73 | prepare_twice(false, 0).await; |
38c4caecdb3d | atagen | 2026-09-26 13:35 UTC | 74 | } |