1
- use std:: ops:: Not ;
1
+ use std:: { ops:: Not , sync :: Arc } ;
2
2
3
3
use assert_matches:: assert_matches;
4
4
use eyeball_im:: VectorDiff ;
@@ -34,7 +34,7 @@ use ruma::{
34
34
use serde_json:: json;
35
35
use stream_assert:: { assert_next_matches, assert_pending} ;
36
36
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} ;
38
38
use wiremock:: {
39
39
matchers:: { header, method, path} ,
40
40
Mock , MockServer , ResponseTemplate ,
@@ -2647,12 +2647,11 @@ async fn test_room_latest_event() -> Result<(), Error> {
2647
2647
Ok ( ( ) )
2648
2648
}
2649
2649
2650
- // #[ignore = "Flaky"]
2651
2650
#[ async_test]
2652
2651
async fn test_sync_indicator ( ) -> Result < ( ) , Error > {
2653
2652
let ( _, server, room_list) = new_room_list_service ( ) . await ?;
2654
2653
2655
- const DELAY_BEFORE_SHOWING : Duration = Duration :: from_millis ( 20 ) ;
2654
+ const DELAY_BEFORE_SHOWING : Duration = Duration :: from_millis ( 100 ) ;
2656
2655
const DELAY_BEFORE_HIDING : Duration = Duration :: from_millis ( 0 ) ;
2657
2656
2658
2657
let sync = room_list. sync ( ) ;
@@ -2661,13 +2660,10 @@ async fn test_sync_indicator() -> Result<(), Error> {
2661
2660
let sync_indicator = room_list. sync_indicator ( DELAY_BEFORE_SHOWING , DELAY_BEFORE_HIDING ) ;
2662
2661
2663
2662
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 ;
2668
2664
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 ( ) ;
2671
2667
2672
2668
macro_rules! assert_next_sync_indicator {
2673
2669
( $sync_indicator: ident, $pattern: pat, now $( , ) ?) => {
@@ -2684,82 +2680,76 @@ async fn test_sync_indicator() -> Result<(), Error> {
2684
2680
let sync_indicator_task = spawn ( async move {
2685
2681
pin_mut ! ( sync_indicator) ;
2686
2682
2683
+ let barrier = barrier_sync_indicator;
2684
+
2687
2685
// `SyncIndicator` is forced to be hidden to begin with.
2688
2686
assert_next_sync_indicator ! ( sync_indicator, SyncIndicator :: Hide , now) ;
2689
2687
2688
+ barrier. wait ( ) . await ;
2689
+
2690
2690
// Request 1.
2691
2691
{
2692
- // Sync has started, the ` SyncIndicator` must be shown… but not immediately!
2692
+ // The state transitions into `Init`. The ` SyncIndicator` must be `Show`.
2693
2693
assert_next_sync_indicator ! (
2694
2694
sync_indicator,
2695
2695
SyncIndicator :: Show ,
2696
2696
under DELAY_BEFORE_SHOWING + request_margin,
2697
2697
) ;
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
- ) ;
2707
2698
}
2708
2699
2709
- in_between_requests_synchronizer. recv ( ) . await . unwrap ( ) ;
2710
- assert_pending ! ( sync_indicator) ;
2700
+ barrier. wait ( ) . await ;
2711
2701
2712
2702
// Request 2.
2713
2703
{
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
+ ) ;
2716
2710
}
2717
2711
2718
- in_between_requests_synchronizer. recv ( ) . await . unwrap ( ) ;
2719
- assert_pending ! ( sync_indicator) ;
2712
+ barrier. wait ( ) . await ;
2720
2713
2721
2714
// Request 3.
2722
2715
{
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
+ ) ;
2727
2722
}
2728
2723
2729
- in_between_requests_synchronizer. recv ( ) . await . unwrap ( ) ;
2730
- assert_pending ! ( sync_indicator) ;
2724
+ barrier. wait ( ) . await ;
2731
2725
2732
2726
// Request 4.
2733
2727
{
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`.
2736
2729
assert_next_sync_indicator ! (
2737
2730
sync_indicator,
2738
2731
SyncIndicator :: Show ,
2739
2732
under DELAY_BEFORE_SHOWING + request_margin,
2740
2733
) ;
2734
+ }
2735
+
2736
+ barrier. wait ( ) . await ;
2741
2737
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`.
2743
2741
assert_next_sync_indicator ! (
2744
2742
sync_indicator,
2745
2743
SyncIndicator :: Hide ,
2746
- under request_4_delay - DELAY_BEFORE_SHOWING
2747
- + DELAY_BEFORE_HIDING
2748
- + request_margin,
2744
+ under DELAY_BEFORE_HIDING + request_margin,
2749
2745
) ;
2750
2746
}
2751
2747
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.
2760
2748
assert_pending ! ( sync_indicator) ;
2761
2749
} ) ;
2762
2750
2751
+ barrier. wait ( ) . await ;
2752
+
2763
2753
// Request 1.
2764
2754
sync_then_assert_request_and_fake_response ! {
2765
2755
[ server, room_list, sync]
@@ -2768,10 +2758,10 @@ async fn test_sync_indicator() -> Result<(), Error> {
2768
2758
respond with = {
2769
2759
"pos" : "0" ,
2770
2760
} ,
2771
- after delay = request_1_delay , // Slow request!
2761
+ after delay = request_delay ,
2772
2762
} ;
2773
2763
2774
- in_between_requests_synchronizer_sender . send ( ( ) ) . await . unwrap ( ) ;
2764
+ barrier . wait ( ) . await ;
2775
2765
2776
2766
// Request 2.
2777
2767
sync_then_assert_request_and_fake_response ! {
@@ -2781,10 +2771,10 @@ async fn test_sync_indicator() -> Result<(), Error> {
2781
2771
respond with = {
2782
2772
"pos" : "1" ,
2783
2773
} ,
2784
- after delay = request_2_delay , // Slow request!
2774
+ after delay = request_delay ,
2785
2775
} ;
2786
2776
2787
- in_between_requests_synchronizer_sender . send ( ( ) ) . await . unwrap ( ) ;
2777
+ barrier . wait ( ) . await ;
2788
2778
2789
2779
// Request 3.
2790
2780
sync_then_assert_request_and_fake_response ! {
@@ -2796,12 +2786,13 @@ async fn test_sync_indicator() -> Result<(), Error> {
2796
2786
"error" : "foo" ,
2797
2787
"errcode" : "M_UNKNOWN" ,
2798
2788
} ,
2789
+ after delay = request_delay,
2799
2790
} ;
2800
2791
2801
2792
let sync = room_list. sync ( ) ;
2802
2793
pin_mut ! ( sync) ;
2803
2794
2804
- in_between_requests_synchronizer_sender . send ( ( ) ) . await . unwrap ( ) ;
2795
+ barrier . wait ( ) . await ;
2805
2796
2806
2797
// Request 4.
2807
2798
sync_then_assert_request_and_fake_response ! {
@@ -2811,10 +2802,10 @@ async fn test_sync_indicator() -> Result<(), Error> {
2811
2802
respond with = {
2812
2803
"pos" : "2" ,
2813
2804
} ,
2814
- after delay = request_4_delay , // Slow request!
2805
+ after delay = request_delay ,
2815
2806
} ;
2816
2807
2817
- in_between_requests_synchronizer_sender . send ( ( ) ) . await . unwrap ( ) ;
2808
+ barrier . wait ( ) . await ;
2818
2809
2819
2810
// Request 5.
2820
2811
sync_then_assert_request_and_fake_response ! {
@@ -2824,11 +2815,9 @@ async fn test_sync_indicator() -> Result<(), Error> {
2824
2815
respond with = {
2825
2816
"pos" : "3" ,
2826
2817
} ,
2827
- after delay = request_5_delay , // Slow request!
2818
+ after delay = request_delay ,
2828
2819
} ;
2829
2820
2830
- in_between_requests_synchronizer_sender. send ( ( ) ) . await . unwrap ( ) ;
2831
-
2832
2821
sync_indicator_task. await . unwrap ( ) ;
2833
2822
2834
2823
Ok ( ( ) )
0 commit comments