Skip to content

Commit dd0336e

Browse files
committed
test(ui): Update test_sync_indicator and make it non-flaky.
This patch updates `test_sync_indicator` since the `SyncIndicator` is shown for the `SettingUp` state. Also, this patch makes this test non-flaky entirely by changing the `sync::mpsc::channel` to a `sync::Barrier`.
1 parent 0095912 commit dd0336e

File tree

2 files changed

+47
-58
lines changed

2 files changed

+47
-58
lines changed

crates/matrix-sdk-ui/tests/integration/room_list_service.rs

Lines changed: 45 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::ops::Not;
1+
use std::{ops::Not, sync::Arc};
22

33
use assert_matches::assert_matches;
44
use eyeball_im::VectorDiff;
@@ -34,7 +34,7 @@ use ruma::{
3434
use serde_json::json;
3535
use stream_assert::{assert_next_matches, assert_pending};
3636
use tempfile::TempDir;
37-
use tokio::{spawn, sync::mpsc::channel, task::yield_now, time::sleep};
37+
use tokio::{spawn, sync::Barrier, task::yield_now, time::sleep};
3838
use wiremock::{
3939
matchers::{header, method, path},
4040
Mock, MockServer, ResponseTemplate,
@@ -2647,12 +2647,11 @@ async fn test_room_latest_event() -> Result<(), Error> {
26472647
Ok(())
26482648
}
26492649

2650-
// #[ignore = "Flaky"]
26512650
#[async_test]
26522651
async fn test_sync_indicator() -> Result<(), Error> {
26532652
let (_, server, room_list) = new_room_list_service().await?;
26542653

2655-
const DELAY_BEFORE_SHOWING: Duration = Duration::from_millis(20);
2654+
const DELAY_BEFORE_SHOWING: Duration = Duration::from_millis(100);
26562655
const DELAY_BEFORE_HIDING: Duration = Duration::from_millis(0);
26572656

26582657
let sync = room_list.sync();
@@ -2661,13 +2660,10 @@ async fn test_sync_indicator() -> Result<(), Error> {
26612660
let sync_indicator = room_list.sync_indicator(DELAY_BEFORE_SHOWING, DELAY_BEFORE_HIDING);
26622661

26632662
let request_margin = Duration::from_millis(100);
2664-
let request_1_delay = DELAY_BEFORE_SHOWING * 2;
2665-
let request_2_delay = DELAY_BEFORE_SHOWING * 3;
2666-
let request_4_delay = DELAY_BEFORE_SHOWING * 2;
2667-
let request_5_delay = DELAY_BEFORE_SHOWING * 2;
2663+
let request_delay = DELAY_BEFORE_SHOWING * 2;
26682664

2669-
let (in_between_requests_synchronizer_sender, mut in_between_requests_synchronizer) =
2670-
channel(1);
2665+
let barrier = Arc::new(Barrier::new(2));
2666+
let barrier_sync_indicator = barrier.clone();
26712667

26722668
macro_rules! assert_next_sync_indicator {
26732669
($sync_indicator:ident, $pattern:pat, now $(,)?) => {
@@ -2684,82 +2680,76 @@ async fn test_sync_indicator() -> Result<(), Error> {
26842680
let sync_indicator_task = spawn(async move {
26852681
pin_mut!(sync_indicator);
26862682

2683+
let barrier = barrier_sync_indicator;
2684+
26872685
// `SyncIndicator` is forced to be hidden to begin with.
26882686
assert_next_sync_indicator!(sync_indicator, SyncIndicator::Hide, now);
26892687

2688+
barrier.wait().await;
2689+
26902690
// Request 1.
26912691
{
2692-
// Sync has started, the `SyncIndicator` must be shown… but not immediately!
2692+
// The state transitions into `Init`. The `SyncIndicator` must be `Show`.
26932693
assert_next_sync_indicator!(
26942694
sync_indicator,
26952695
SyncIndicator::Show,
26962696
under DELAY_BEFORE_SHOWING + request_margin,
26972697
);
2698-
2699-
// Then, once the sync is done, the `SyncIndicator` must be hidden.
2700-
assert_next_sync_indicator!(
2701-
sync_indicator,
2702-
SyncIndicator::Hide,
2703-
under request_1_delay - DELAY_BEFORE_SHOWING
2704-
+ DELAY_BEFORE_HIDING
2705-
+ request_margin,
2706-
);
27072698
}
27082699

2709-
in_between_requests_synchronizer.recv().await.unwrap();
2710-
assert_pending!(sync_indicator);
2700+
barrier.wait().await;
27112701

27122702
// Request 2.
27132703
{
2714-
// Nothing happens, as the state transitions from `SettingUp` to
2715-
// `Running`, no `SyncIndicator` must be shown.
2704+
// The state transitions into `SettingUp`. The `SyncIndicator` stays in `Show`.
2705+
assert_next_sync_indicator!(
2706+
sync_indicator,
2707+
SyncIndicator::Show,
2708+
under DELAY_BEFORE_SHOWING + request_margin,
2709+
);
27162710
}
27172711

2718-
in_between_requests_synchronizer.recv().await.unwrap();
2719-
assert_pending!(sync_indicator);
2712+
barrier.wait().await;
27202713

27212714
// Request 3.
27222715
{
2723-
// Sync has errored, the `SyncIndicator` should be show. Fortunately
2724-
// for us (fictional situation), the sync is restarted
2725-
// immediately, and `SyncIndicator` doesn't have time to
2726-
// be shown for this particular state update.
2716+
// The state transitions into `Running`. The `SyncIndicator` must be `Hide`.
2717+
assert_next_sync_indicator!(
2718+
sync_indicator,
2719+
SyncIndicator::Hide,
2720+
under DELAY_BEFORE_HIDING + request_margin,
2721+
);
27272722
}
27282723

2729-
in_between_requests_synchronizer.recv().await.unwrap();
2730-
assert_pending!(sync_indicator);
2724+
barrier.wait().await;
27312725

27322726
// Request 4.
27332727
{
2734-
// The system is recovering, It takes times (fictional situation)!
2735-
// `SyncIndicator` has time to show (off).
2728+
// The state transitions into `Error`. The `SyncIndicator` must be `Show`.
27362729
assert_next_sync_indicator!(
27372730
sync_indicator,
27382731
SyncIndicator::Show,
27392732
under DELAY_BEFORE_SHOWING + request_margin,
27402733
);
2734+
}
2735+
2736+
barrier.wait().await;
27412737

2742-
// Then, once the sync is done, the `SyncIndicator` must be hidden.
2738+
// Request 5.
2739+
{
2740+
// The state transitions into `Recovering`. The `SyncIndicator` must be `Hide`.
27432741
assert_next_sync_indicator!(
27442742
sync_indicator,
27452743
SyncIndicator::Hide,
2746-
under request_4_delay - DELAY_BEFORE_SHOWING
2747-
+ DELAY_BEFORE_HIDING
2748-
+ request_margin,
2744+
under DELAY_BEFORE_HIDING + request_margin,
27492745
);
27502746
}
27512747

2752-
in_between_requests_synchronizer.recv().await.unwrap();
2753-
assert_pending!(sync_indicator);
2754-
2755-
// Request 5.
2756-
2757-
in_between_requests_synchronizer.recv().await.unwrap();
2758-
2759-
// Even though request 5 took a while, the `SyncIndicator` shouldn't show.
27602748
assert_pending!(sync_indicator);
27612749
});
27622750

2751+
barrier.wait().await;
2752+
27632753
// Request 1.
27642754
sync_then_assert_request_and_fake_response! {
27652755
[server, room_list, sync]
@@ -2768,10 +2758,10 @@ async fn test_sync_indicator() -> Result<(), Error> {
27682758
respond with = {
27692759
"pos": "0",
27702760
},
2771-
after delay = request_1_delay, // Slow request!
2761+
after delay = request_delay,
27722762
};
27732763

2774-
in_between_requests_synchronizer_sender.send(()).await.unwrap();
2764+
barrier.wait().await;
27752765

27762766
// Request 2.
27772767
sync_then_assert_request_and_fake_response! {
@@ -2781,10 +2771,10 @@ async fn test_sync_indicator() -> Result<(), Error> {
27812771
respond with = {
27822772
"pos": "1",
27832773
},
2784-
after delay = request_2_delay, // Slow request!
2774+
after delay = request_delay,
27852775
};
27862776

2787-
in_between_requests_synchronizer_sender.send(()).await.unwrap();
2777+
barrier.wait().await;
27882778

27892779
// Request 3.
27902780
sync_then_assert_request_and_fake_response! {
@@ -2796,12 +2786,13 @@ async fn test_sync_indicator() -> Result<(), Error> {
27962786
"error": "foo",
27972787
"errcode": "M_UNKNOWN",
27982788
},
2789+
after delay = request_delay,
27992790
};
28002791

28012792
let sync = room_list.sync();
28022793
pin_mut!(sync);
28032794

2804-
in_between_requests_synchronizer_sender.send(()).await.unwrap();
2795+
barrier.wait().await;
28052796

28062797
// Request 4.
28072798
sync_then_assert_request_and_fake_response! {
@@ -2811,10 +2802,10 @@ async fn test_sync_indicator() -> Result<(), Error> {
28112802
respond with = {
28122803
"pos": "2",
28132804
},
2814-
after delay = request_4_delay, // Slow request!
2805+
after delay = request_delay,
28152806
};
28162807

2817-
in_between_requests_synchronizer_sender.send(()).await.unwrap();
2808+
barrier.wait().await;
28182809

28192810
// Request 5.
28202811
sync_then_assert_request_and_fake_response! {
@@ -2824,11 +2815,9 @@ async fn test_sync_indicator() -> Result<(), Error> {
28242815
respond with = {
28252816
"pos": "3",
28262817
},
2827-
after delay = request_5_delay, // Slow request!
2818+
after delay = request_delay,
28282819
};
28292820

2830-
in_between_requests_synchronizer_sender.send(()).await.unwrap();
2831-
28322821
sync_indicator_task.await.unwrap();
28332822

28342823
Ok(())

crates/matrix-sdk-ui/tests/integration/sliding_sync.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ macro_rules! sliding_sync_then_assert_request_and_fake_response {
102102
$( $response_json )*
103103
})
104104
)
105-
$( .set_delay($response_delay) )?
105+
$( .set_delay( $response_delay ) )?
106106
})
107-
.mount_as_scoped(&$server)
107+
.mount_as_scoped(& $server )
108108
.await;
109109

110110
let next = $stream.next().await;

0 commit comments

Comments
 (0)