31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 1 | mod support; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 2 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 3 | use std::time::Duration; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 4 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 5 | use macltok::{ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 6 | Command, Engine, EngineConfig, ErrorKind, Event, MediaSize, MessageContent, TimelineCommand, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 7 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 8 | use matrix_sdk::{ |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 9 | ruma::{api::client::sync::sync_events::v5, mxc_uri, room_id}, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 10 | test_utils::mocks::MatrixMockServer, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 11 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 12 | use matrix_sdk_test::{ALICE, JoinedRoomBuilder, event_factory::EventFactory}; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 13 | use support::{event_messages, receive_until, shut_down}; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 14 | use wiremock::ResponseTemplate; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 15 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 16 | #[tokio::test] |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 17 | async fn delayed_media_fetch_does_not_block_timeline_command() { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 18 | let server = MatrixMockServer::new().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 19 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 20 | .mock_sliding_sync() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 21 | .ok(v5::Response::new("1".to_owned())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 22 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 23 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 24 | let client = server.client_builder().build().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 25 | let sync_client = client.clone(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 26 | let room_id = room_id!("!nonblocking:localhost"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 27 | server.mock_room_state_encryption().plain().mount().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 28 | server.sync_joined_room(&client, room_id).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 29 | let config = EngineConfig::default().with_initial_history_limit(0); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 30 | let (engine, handle, mut events) = Engine::from_client(client, config).await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 31 | let running = tokio::spawn(engine.run()); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 32 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 33 | handle.open_room(room_id.to_owned()).await.unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 34 | receive_until( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 35 | &mut events, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 36 | |event| matches!(event, Event::TimelineReset { timeline_id: reset, .. } if reset.room_id() == room_id), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 37 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 38 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 39 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 40 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 41 | .sync_room( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 42 | &sync_client, |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 43 | JoinedRoomBuilder::new(room_id).add_timeline_event( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 44 | EventFactory::new() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 45 | .image( |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 46 | "image.jpg".into(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 47 | mxc_uri!("mxc://localhost/image").to_owned(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 48 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 49 | .sender(&ALICE), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 50 | ), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 51 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 52 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 53 | let media_event = receive_until(&mut events, |event| { |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 54 | event_messages(event) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 55 | .into_iter() |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 56 | .any(|message| message.media.is_some()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 57 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 58 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 59 | let media_id = event_messages(&media_event) |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 60 | .into_iter() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 61 | .find_map(|message| message.media.as_ref().map(|media| media.id.clone())) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 62 | .unwrap(); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 63 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 64 | let delayed_image = || { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 65 | ResponseTemplate::new(200) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 66 | .set_body_raw(b"binaryjpegfullimagedata", "image/jpeg") |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 67 | .set_delay(Duration::from_millis(500)) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 68 | }; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 69 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 70 | .mock_media_download() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 71 | .respond_with(delayed_image()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 72 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 73 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 74 | server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 75 | .mock_authed_media_download() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 76 | .respond_with(delayed_image()) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 77 | .mount() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 78 | .await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 79 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 80 | // media no longer travels through the command queue, so driving it on its |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 81 | // own task is what puts the request in flight concurrently with the send below |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 82 | let media_task = tokio::spawn({ |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 83 | let handle = handle.clone(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 84 | async move { handle.media(&media_id, MediaSize::Full).await } |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 85 | }); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 86 | wait_for_media_request(&server).await; |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 87 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 88 | let error = tokio::time::timeout( |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 89 | Duration::from_millis(300), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 90 | handle.execute(Command::Timeline(TimelineCommand::Send { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 91 | timeline_id: room_id.to_owned().into(), |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 92 | content: MessageContent::text(String::new()), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 93 | })), |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 94 | ) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 95 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 96 | .expect("timeline command was blocked by media fetch") |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 97 | .expect_err("empty body command unexpectedly succeeded"); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 98 | assert_eq!(error.kind, ErrorKind::EmptyBody); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 99 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 100 | let media = tokio::time::timeout(Duration::from_secs(2), media_task) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 101 | .await |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 102 | .expect("media fetch did not complete") |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 103 | .unwrap() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 104 | .unwrap(); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 105 | assert!(!media.bytes.is_empty()); |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 106 | |
31ce2453c82f | atagen | 2026-09-24 13:07 UTC | 107 | shut_down(&handle, &mut events, running).await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 108 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 109 | |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 110 | async fn wait_for_media_request(server: &MatrixMockServer) { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 111 | tokio::time::timeout(Duration::from_secs(2), async { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 112 | loop { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 113 | if server |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 114 | .received_requests() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 115 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 116 | .unwrap() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 117 | .iter() |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 118 | .any(|request| request.url.path().ends_with("/download/localhost/image")) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 119 | { |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 120 | break; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 121 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 122 | tokio::task::yield_now().await; |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 123 | } |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 124 | }) |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 125 | .await |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 126 | .expect("media request did not reach the homeserver"); |
96fdf90aacd6 | atagen | 2026-09-08 08:24 UTC | 127 | } |