cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 1 | use std::{sync::Arc, time::Duration}; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 2 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 3 | use matrix_sdk::{ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 4 | Client, Room, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 5 | ruma::{api::client::sync::sync_events::v5, events::AnySyncTimelineEvent, room_id, serde::Raw}, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 6 | sliding_sync::{SlidingSyncList, SlidingSyncMode}, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 7 | test_utils::mocks::MatrixMockServer, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 8 | }; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 9 | use serde_json::{Value, json}; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 10 | use tokio::sync::{Notify, Semaphore}; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 11 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 12 | async fn saved_position(client: &Client) -> Option<String> { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 13 | let machine = client.olm_machine_for_testing().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 14 | let key = format!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 15 | "sliding_sync_store::mentions-spike::{}::instance", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 16 | client.user_id().unwrap() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 17 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 18 | let saved = machine |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 19 | .as_ref() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 20 | .unwrap() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 21 | .store() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 22 | .get_custom_value(&key) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 23 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 24 | .unwrap()?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 25 | serde_json::from_slice::<Value>(&saved).unwrap()["pos"] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 26 | .as_str() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 27 | .map(ToOwned::to_owned) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 28 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 29 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 30 | async fn server_with_mention() -> (MatrixMockServer, Client) { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 31 | let server = MatrixMockServer::new().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 32 | let client = server.client_builder().build().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 33 | let mut response = v5::Response::new("position-1".to_owned()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 34 | response.rooms.insert( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 35 | room_id!("!checkpoint:localhost").to_owned(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 36 | serde_json::from_value(json!({ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 37 | "initial": true, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 38 | "required_state": [], |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 39 | "timeline": [{ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 40 | "type": "m.room.message", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 41 | "event_id": "$checkpoint-mention", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 42 | "sender": "@sender:localhost", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 43 | "origin_server_ts": 1, |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 44 | "content": { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 45 | "msgtype": "m.text", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 46 | "body": "checkpoint candidate", |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 47 | "m.mentions": { "user_ids": [client.user_id().unwrap()] } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 48 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 49 | }] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 50 | })) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 51 | .unwrap(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 52 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 53 | server.mock_sliding_sync().ok(response).mount().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 54 | (server, client) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 55 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 56 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 57 | async fn new_sync(client: &Client) -> matrix_sdk::sliding_sync::SlidingSync { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 58 | client |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 59 | .sliding_sync("mentions-spike") |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 60 | .unwrap() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 61 | .share_pos() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 62 | .add_list( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 63 | SlidingSyncList::builder("mentions") |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 64 | .sync_mode(SlidingSyncMode::new_selective().add_range(0..=0)), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 65 | ) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 66 | .build() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 67 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 68 | .unwrap() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 69 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 70 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 71 | #[tokio::test] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 72 | async fn awaited_event_handler_fences_position_even_after_caller_cancellation() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 73 | let (server, client) = server_with_mention().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 74 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 75 | let entered = Arc::new(Notify::new()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 76 | let committed = Arc::new(Semaphore::new(0)); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 77 | let handler = client.add_event_handler({ |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 78 | let entered = entered.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 79 | let committed = committed.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 80 | move |event: Raw<AnySyncTimelineEvent>, room: Room| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 81 | let entered = entered.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 82 | let committed = committed.clone(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 83 | async move { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 84 | assert_eq!(room.room_id().as_str(), "!checkpoint:localhost"); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 85 | assert_eq!( |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 86 | event.get_field::<String>("event_id").unwrap().as_deref(), |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 87 | Some("$checkpoint-mention") |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 88 | ); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 89 | entered.notify_one(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 90 | committed.acquire().await.unwrap().forget(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 91 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 92 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 93 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 94 | let sync = new_sync(&client).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 95 | let processing = tokio::spawn(async move { sync.sync_once().await }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 96 | tokio::time::timeout(Duration::from_secs(5), entered.notified()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 97 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 98 | .unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 99 | assert!(!processing.is_finished()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 100 | assert!(saved_position(&client).await.is_none()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 101 | processing.abort(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 102 | assert!(processing.await.unwrap_err().is_cancelled()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 103 | assert!(saved_position(&client).await.is_none()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 104 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 105 | committed.add_permits(1); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 106 | tokio::time::timeout(Duration::from_secs(5), async { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 107 | while saved_position(&client).await.as_deref() != Some("position-1") { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 108 | tokio::task::yield_now().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 109 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 110 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 111 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 112 | .unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 113 | client.remove_event_handler(handler); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 114 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 115 | let restored = new_sync(&client).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 116 | tokio::time::timeout(Duration::from_secs(5), restored.sync_once()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 117 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 118 | .unwrap() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 119 | .unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 120 | let requests: Vec<_> = server |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 121 | .received_requests() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 122 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 123 | .unwrap() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 124 | .into_iter() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 125 | .filter_map(|request| { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 126 | let body = serde_json::from_slice::<Value>(&request.body).ok()?; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 127 | let pos = request |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 128 | .url |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 129 | .query_pairs() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 130 | .find(|(key, _)| key == "pos") |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 131 | .map(|(_, value)| value.into_owned()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 132 | Some((body, pos)) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 133 | }) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 134 | .filter(|(body, _)| body["conn_id"] == "mentions-spike") |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 135 | .collect(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 136 | assert_eq!(requests.len(), 2); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 137 | assert!(requests[0].1.is_none()); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 138 | assert_eq!(requests[1].1.as_deref(), Some("position-1")); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 139 | } |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 140 | |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 141 | #[tokio::test] |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 142 | async fn event_handler_error_does_not_prevent_position_persistence() { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 143 | let (_server, client) = server_with_mention().await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 144 | let handler = |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 145 | client.add_event_handler(|_event: Raw<AnySyncTimelineEvent>, _room: Room| async { |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 146 | Err::<(), _>("the candidate transaction failed") |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 147 | }); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 148 | let sync = new_sync(&client).await; |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 149 | tokio::time::timeout(Duration::from_secs(5), sync.sync_once()) |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 150 | .await |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 151 | .unwrap() |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 152 | .unwrap(); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 153 | assert_eq!(saved_position(&client).await.as_deref(), Some("position-1")); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 154 | client.remove_event_handler(handler); |
cbb715e11963 | atagen | 2026-10-24 23:24 UTC | 155 | } |