Skip to content

Commit

Permalink
Get back to Recovering syncing when we haven't sync for a while
Browse files Browse the repository at this point in the history
  • Loading branch information
MatMaul committed Sep 13, 2024
1 parent 72cc2bd commit 0b79ccb
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 45 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ growable-bloom-filter = "2.1.0"
http = "1.1.0"
imbl = "3.0.0"
itertools = "0.12.0"
mock_instant = "0.5.1"
once_cell = "1.16.0"
pin-project-lite = "0.2.9"
rand = "0.8.5"
Expand Down
2 changes: 1 addition & 1 deletion bindings/matrix-sdk-ffi/src/room_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ impl From<matrix_sdk_ui::room_list_service::State> for RoomListServiceState {
S::Init => Self::Initial,
S::SettingUp => Self::SettingUp,
S::Recovering => Self::Recovering,
S::Running => Self::Running,
S::Running { .. } => Self::Running,
S::Error { .. } => Self::Error,
S::Terminated { .. } => Self::Terminated,
}
Expand Down
8 changes: 8 additions & 0 deletions crates/matrix-sdk-ui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ rustls-tls = ["matrix-sdk/rustls-tls"]

uniffi = ["dep:uniffi", "matrix-sdk/uniffi", "matrix-sdk-base/uniffi"]

testing = ["dep:mock_instant", "matrix-sdk/testing", "matrix-sdk-base/testing"]

# Add support for encrypted extensible events.
unstable-msc3956 = ["ruma/unstable-msc3956"]

Expand All @@ -38,6 +40,7 @@ itertools = { workspace = true }
matrix-sdk = { workspace = true, features = ["experimental-sliding-sync", "e2e-encryption"] }
matrix-sdk-base = { workspace = true }
mime = "0.3.16"
mock_instant = { workspace = true, optional = true }
once_cell = { workspace = true }
pin-project-lite = { workspace = true }
ruma = { workspace = true, features = ["html", "unstable-msc3381"] }
Expand All @@ -57,9 +60,14 @@ assert_matches2 = { workspace = true }
eyeball-im-util = { workspace = true }
matrix-sdk = { workspace = true, features = ["testing"] }
matrix-sdk-test = { workspace = true }
mock_instant = { workspace = true }
stream_assert = { workspace = true }
tempfile = "3.3.0"
wiremock = { workspace = true }

[[test]]
name = "integration"
required-features = ["testing"]

[lints]
workspace = true
10 changes: 7 additions & 3 deletions crates/matrix-sdk-ui/src/room_list_service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ impl RoomListService {
(SyncIndicator::Show, delay_before_showing)
}

State::SettingUp | State::Recovering | State::Running | State::Terminated { .. } => {
State::SettingUp | State::Recovering | State::Running {..} | State::Terminated { .. } => {
(SyncIndicator::Hide, delay_before_hiding)
}
};
Expand Down Expand Up @@ -610,13 +610,17 @@ mod tests {
let _ = sync.next().await;

// State is `Terminated`, as expected!
assert_eq!(room_list.state.get(), State::Terminated { from: Box::new(State::Running) });
assert_matches!(room_list.state.get(), State::Terminated { from } => {
assert_matches!(from.as_ref(), State::Running { .. });
});

// Now, let's make the sliding sync session to expire.
room_list.expire_sync_session().await;

// State is `Error`, as a regular session expiration would generate!
assert_eq!(room_list.state.get(), State::Error { from: Box::new(State::Running) });
assert_matches!(room_list.state.get(), State::Error { from } => {
assert_matches!(from.as_ref(), State::Running { .. });
});

Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion crates/matrix-sdk-ui/src/room_list_service/room_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl RoomList {

match state {
Terminated { .. } | Error { .. } | Init => (),
SettingUp | Recovering | Running => break,
SettingUp | Recovering | Running { .. } => break,
}
}

Expand Down
27 changes: 20 additions & 7 deletions crates/matrix-sdk-ui/src/room_list_service/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
//! States and actions for the `RoomList` state machine.

use std::future::ready;
#[cfg(not(any(test, feature = "testing")))]
use std::time::Instant;

use matrix_sdk::{sliding_sync::Range, SlidingSync, SlidingSyncMode};
#[cfg(any(test, feature = "testing"))]
use mock_instant::global::Instant;

use super::Error;

Expand All @@ -37,7 +41,7 @@ pub enum State {
Recovering,

/// At this state, all rooms are syncing.
Running,
Running { last_time: Instant },

/// At this state, the sync has been stopped because an error happened.
Error { from: Box<State> },
Expand All @@ -57,10 +61,18 @@ impl State {

SettingUp | Recovering => {
set_all_rooms_to_growing_sync_mode(sliding_sync).await?;
Running
Running { last_time: Instant::now() }
}

Running => Running,
Running { last_time } => {
// We haven't sync for a while so we should go back to recovering
if last_time.elapsed().as_secs() > 1800 {
set_all_rooms_to_selective_sync_mode(sliding_sync).await?;
Recovering
} else {
Running { last_time: Instant::now() }
}
}

Error { from: previous_state } | Terminated { from: previous_state } => {
match previous_state.as_ref() {
Expand All @@ -72,7 +84,7 @@ impl State {
}

// If the previous state was `Running`, we enter the `Recovering` state.
Running => {
Running { .. } => {
set_all_rooms_to_selective_sync_mode(sliding_sync).await?;
Recovering
}
Expand Down Expand Up @@ -121,6 +133,7 @@ pub const ALL_ROOMS_DEFAULT_GROWING_BATCH_SIZE: u32 = 100;

#[cfg(test)]
mod tests {
use assert_matches::assert_matches;
use matrix_sdk_test::async_test;

use super::{super::tests::new_room_list, *};
Expand Down Expand Up @@ -173,7 +186,7 @@ mod tests {

// Next state.
let state = state.next(sliding_sync).await?;
assert_eq!(state, State::Running);
assert_matches!(state, State::Running { .. });

// Hypothetical error.
{
Expand All @@ -185,7 +198,7 @@ mod tests {
let state = state.next(sliding_sync).await?;

// Now, back to the previous state.
assert_eq!(state, State::Running);
assert_matches!(state, State::Running { .. });
}

// Hypothetical termination.
Expand All @@ -199,7 +212,7 @@ mod tests {
let state = state.next(sliding_sync).await?;

// Now, back to the previous state.
assert_eq!(state, State::Running);
assert_matches!(state, State::Running { .. });
}

// Hypothetical error when recovering.
Expand Down
Loading

0 comments on commit 0b79ccb

Please sign in to comment.