@@ -759,13 +759,11 @@ pub enum RoomEventCacheUpdate {
759
759
760
760
#[ cfg( test) ]
761
761
mod tests {
762
- use std:: time:: { Duration , Instant } ;
763
762
764
763
use assert_matches2:: assert_matches;
765
764
use matrix_sdk_common:: executor:: spawn;
766
765
use matrix_sdk_test:: { async_test, sync_timeline_event} ;
767
766
use ruma:: room_id;
768
- use tokio:: time:: sleep;
769
767
770
768
use super :: { store:: TimelineEntry , EventCacheError } ;
771
769
use crate :: { event_cache:: store:: PaginationToken , test_utils:: logged_in_client} ;
@@ -787,97 +785,52 @@ mod tests {
787
785
}
788
786
789
787
#[ async_test]
790
- async fn test_wait_no_pagination_token ( ) {
788
+ async fn test_unknown_pagination_token ( ) {
791
789
let client = logged_in_client ( None ) . await ;
792
790
let room_id = room_id ! ( "!galette:saucisse.bzh" ) ;
793
791
client. base_client ( ) . get_or_create_room ( room_id, matrix_sdk_base:: RoomState :: Joined ) ;
794
792
795
793
client. event_cache ( ) . subscribe ( ) . unwrap ( ) ;
796
794
797
- // When I only have events in a room,
798
- client
799
- . event_cache ( )
800
- . inner
801
- . store
802
- . lock ( )
803
- . await
804
- . append_room_entries (
805
- room_id,
806
- vec ! [ TimelineEntry :: Event (
807
- sync_timeline_event!( {
808
-
809
- "type" : "m.room.message" ,
810
- "event_id" : "$ida" ,
811
- "origin_server_ts" : 12344446 ,
812
- "content" : { "body" : "yolo" , "msgtype" : "m.text" } ,
813
- } )
814
- . into( ) ,
815
- ) ] ,
816
- )
817
- . await
818
- . unwrap ( ) ;
819
-
820
- let ( room_event_cache, _drop_handlers) =
795
+ let ( room_event_cache, _drop_handles) =
821
796
client. event_cache ( ) . for_room ( room_id) . await . unwrap ( ) ;
822
797
let room_event_cache = room_event_cache. unwrap ( ) ;
823
798
824
- // If I don't wait for the backpagination token,
825
- let found = room_event_cache. earliest_backpagination_token ( None ) . await . unwrap ( ) ;
826
- // Then I don't find it.
827
- assert ! ( found. is_none( ) ) ;
799
+ // If I try to back-paginate with an unknown back-pagination token,
800
+ let token = PaginationToken ( "old" . to_owned ( ) ) ;
828
801
829
- // If I wait for a back-pagination token for 0 seconds,
830
- let before = Instant :: now ( ) ;
831
- let found = room_event_cache
832
- . earliest_backpagination_token ( Some ( Duration :: default ( ) ) )
833
- . await
834
- . unwrap ( ) ;
835
- let waited = before. elapsed ( ) ;
836
- // then I don't get any,
837
- assert ! ( found. is_none( ) ) ;
838
- // and I haven't waited long.
839
- assert ! ( waited. as_secs( ) < 1 ) ;
840
-
841
- // If I wait for a back-pagination token for 1 second,
842
- let before = Instant :: now ( ) ;
843
- let found = room_event_cache
844
- . earliest_backpagination_token ( Some ( Duration :: from_secs ( 1 ) ) )
845
- . await
846
- . unwrap ( ) ;
847
- let waited = before. elapsed ( ) ;
848
- // then I still don't get any.
849
- assert ! ( found. is_none( ) ) ;
850
- // and I've waited a bit.
851
- assert ! ( waited. as_secs( ) < 2 ) ;
852
- assert ! ( waited. as_secs( ) >= 1 ) ;
802
+ // Then I run into an error.
803
+ let res = room_event_cache. backpaginate_with_token ( 20 , Some ( token) ) . await ;
804
+ assert_matches ! ( res. unwrap_err( ) , EventCacheError :: UnknownBackpaginationToken ) ;
853
805
}
854
806
855
- #[ async_test]
856
- async fn test_wait_for_pagination_token_already_present ( ) {
857
- let client = logged_in_client ( None ) . await ;
858
- let room_id = room_id ! ( "!galette:saucisse.bzh" ) ;
859
- client. base_client ( ) . get_or_create_room ( room_id, matrix_sdk_base:: RoomState :: Joined ) ;
807
+ // Those tests require time to work, and it does not on wasm32.
808
+ #[ cfg( not( target_arch = "wasm32" ) ) ]
809
+ mod time_tests {
810
+ use std:: time:: { Duration , Instant } ;
860
811
861
- client . event_cache ( ) . subscribe ( ) . unwrap ( ) ;
812
+ use tokio :: time :: sleep ;
862
813
863
- let ( room_event_cache, _drop_handles) =
864
- client. event_cache ( ) . for_room ( room_id) . await . unwrap ( ) ;
865
- let room_event_cache = room_event_cache. unwrap ( ) ;
814
+ use super :: * ;
866
815
867
- let expected_token = PaginationToken ( "old" . to_owned ( ) ) ;
816
+ #[ async_test]
817
+ async fn test_wait_no_pagination_token ( ) {
818
+ let client = logged_in_client ( None ) . await ;
819
+ let room_id = room_id ! ( "!galette:saucisse.bzh" ) ;
820
+ client. base_client ( ) . get_or_create_room ( room_id, matrix_sdk_base:: RoomState :: Joined ) ;
868
821
869
- // When I have events and multiple gaps, in a room,
870
- client
871
- . event_cache ( )
872
- . inner
873
- . store
874
- . lock ( )
875
- . await
876
- . append_room_entries (
877
- room_id ,
878
- vec ! [
879
- TimelineEntry :: Gap { prev_token : expected_token . clone ( ) } ,
880
- TimelineEntry :: Event (
822
+ client . event_cache ( ) . subscribe ( ) . unwrap ( ) ;
823
+
824
+ // When I only have events in a room,
825
+ client
826
+ . event_cache ( )
827
+ . inner
828
+ . store
829
+ . lock ( )
830
+ . await
831
+ . append_room_entries (
832
+ room_id ,
833
+ vec ! [ TimelineEntry :: Event (
881
834
sync_timeline_event!( {
882
835
883
836
"type" : "m.room.message" ,
@@ -886,62 +839,61 @@ mod tests {
886
839
"content" : { "body" : "yolo" , "msgtype" : "m.text" } ,
887
840
} )
888
841
. into( ) ,
889
- ) ,
890
- ] ,
891
- )
892
- . await
893
- . unwrap ( ) ;
842
+ ) ] ,
843
+ )
844
+ . await
845
+ . unwrap ( ) ;
894
846
895
- // If I don't wait for a back-pagination token,
896
- let found = room_event_cache. earliest_backpagination_token ( None ) . await . unwrap ( ) ;
897
- // Then I get it.
898
- assert_eq ! ( found. as_ref( ) , Some ( & expected_token) ) ;
847
+ let ( room_event_cache, _drop_handlers) =
848
+ client. event_cache ( ) . for_room ( room_id) . await . unwrap ( ) ;
849
+ let room_event_cache = room_event_cache. unwrap ( ) ;
899
850
900
- // If I wait for a back-pagination token for 0 seconds,
901
- let before = Instant :: now ( ) ;
902
- let found = room_event_cache
903
- . earliest_backpagination_token ( Some ( Duration :: default ( ) ) )
904
- . await
905
- . unwrap ( ) ;
906
- let waited = before. elapsed ( ) ;
907
- // then I do get one.
908
- assert_eq ! ( found. as_ref( ) , Some ( & expected_token) ) ;
909
- // and I haven't waited long.
910
- assert ! ( waited. as_millis( ) < 100 ) ;
911
-
912
- // If I wait for a back-pagination token for 1 second,
913
- let before = Instant :: now ( ) ;
914
- let found = room_event_cache
915
- . earliest_backpagination_token ( Some ( Duration :: from_secs ( 1 ) ) )
916
- . await
917
- . unwrap ( ) ;
918
- let waited = before. elapsed ( ) ;
919
- // then I do get one.
920
- assert_eq ! ( found. as_ref( ) , Some ( & expected_token) ) ;
921
- // and I haven't waited long.
922
- assert ! ( waited. as_millis( ) < 100 ) ;
923
- }
851
+ // If I don't wait for the backpagination token,
852
+ let found = room_event_cache. earliest_backpagination_token ( None ) . await . unwrap ( ) ;
853
+ // Then I don't find it.
854
+ assert ! ( found. is_none( ) ) ;
924
855
925
- #[ async_test]
926
- async fn test_wait_for_late_pagination_token ( ) {
927
- let client = logged_in_client ( None ) . await ;
928
- let room_id = room_id ! ( "!galette:saucisse.bzh" ) ;
929
- client. base_client ( ) . get_or_create_room ( room_id, matrix_sdk_base:: RoomState :: Joined ) ;
856
+ // If I wait for a back-pagination token for 0 seconds,
857
+ let before = Instant :: now ( ) ;
858
+ let found = room_event_cache
859
+ . earliest_backpagination_token ( Some ( Duration :: default ( ) ) )
860
+ . await
861
+ . unwrap ( ) ;
862
+ let waited = before. elapsed ( ) ;
863
+ // then I don't get any,
864
+ assert ! ( found. is_none( ) ) ;
865
+ // and I haven't waited long.
866
+ assert ! ( waited. as_secs( ) < 1 ) ;
867
+
868
+ // If I wait for a back-pagination token for 1 second,
869
+ let before = Instant :: now ( ) ;
870
+ let found = room_event_cache
871
+ . earliest_backpagination_token ( Some ( Duration :: from_secs ( 1 ) ) )
872
+ . await
873
+ . unwrap ( ) ;
874
+ let waited = before. elapsed ( ) ;
875
+ // then I still don't get any.
876
+ assert ! ( found. is_none( ) ) ;
877
+ // and I've waited a bit.
878
+ assert ! ( waited. as_secs( ) < 2 ) ;
879
+ assert ! ( waited. as_secs( ) >= 1 ) ;
880
+ }
930
881
931
- client. event_cache ( ) . subscribe ( ) . unwrap ( ) ;
882
+ #[ async_test]
883
+ async fn test_wait_for_pagination_token_already_present ( ) {
884
+ let client = logged_in_client ( None ) . await ;
885
+ let room_id = room_id ! ( "!galette:saucisse.bzh" ) ;
886
+ client. base_client ( ) . get_or_create_room ( room_id, matrix_sdk_base:: RoomState :: Joined ) ;
932
887
933
- let ( room_event_cache, _drop_handles) =
934
- client. event_cache ( ) . for_room ( room_id) . await . unwrap ( ) ;
935
- let room_event_cache = room_event_cache. unwrap ( ) ;
888
+ client. event_cache ( ) . subscribe ( ) . unwrap ( ) ;
936
889
937
- let expected_token = PaginationToken ( "old" . to_owned ( ) ) ;
890
+ let ( room_event_cache, _drop_handles) =
891
+ client. event_cache ( ) . for_room ( room_id) . await . unwrap ( ) ;
892
+ let room_event_cache = room_event_cache. unwrap ( ) ;
938
893
939
- let before = Instant :: now ( ) ;
940
- let cloned_expected_token = expected_token. clone ( ) ;
941
- let insert_token_task = spawn ( async move {
942
- // If a backpagination token is inserted after 400 milliseconds,
943
- sleep ( Duration :: from_millis ( 400 ) ) . await ;
894
+ let expected_token = PaginationToken ( "old" . to_owned ( ) ) ;
944
895
896
+ // When I have events and multiple gaps, in a room,
945
897
client
946
898
. event_cache ( )
947
899
. inner
@@ -950,50 +902,106 @@ mod tests {
950
902
. await
951
903
. append_room_entries (
952
904
room_id,
953
- vec ! [ TimelineEntry :: Gap { prev_token: cloned_expected_token } ] ,
905
+ vec ! [
906
+ TimelineEntry :: Gap { prev_token: expected_token. clone( ) } ,
907
+ TimelineEntry :: Event (
908
+ sync_timeline_event!( {
909
+
910
+ "type" : "m.room.message" ,
911
+ "event_id" : "$ida" ,
912
+ "origin_server_ts" : 12344446 ,
913
+ "content" : { "body" : "yolo" , "msgtype" : "m.text" } ,
914
+ } )
915
+ . into( ) ,
916
+ ) ,
917
+ ] ,
954
918
)
955
919
. await
956
920
. unwrap ( ) ;
957
- } ) ;
958
921
959
- // Then first I don't get it (if I'm not waiting,)
960
- let found = room_event_cache. earliest_backpagination_token ( None ) . await . unwrap ( ) ;
961
- assert ! ( found. is_none( ) ) ;
922
+ // If I don't wait for a back-pagination token,
923
+ let found = room_event_cache. earliest_backpagination_token ( None ) . await . unwrap ( ) ;
924
+ // Then I get it.
925
+ assert_eq ! ( found. as_ref( ) , Some ( & expected_token) ) ;
962
926
963
- // And if I wait for the back-pagination token for 600ms ,
964
- let found = room_event_cache
965
- . earliest_backpagination_token ( Some ( Duration :: from_millis ( 600 ) ) )
966
- . await
967
- . unwrap ( ) ;
968
- let waited = before . elapsed ( ) ;
969
-
970
- // then I do get one eventually .
971
- assert_eq ! ( found. as_ref( ) , Some ( & expected_token) ) ;
972
- // and I have waited between ~400 and ~1000 milliseconds .
973
- assert ! ( waited. as_secs ( ) < 1 ) ;
974
- assert ! ( waited . as_millis ( ) >= 400 ) ;
975
-
976
- // The task succeeded.
977
- insert_token_task . await . unwrap ( ) ;
978
- }
979
-
980
- # [ async_test ]
981
- async fn test_unknown_pagination_token ( ) {
982
- let client = logged_in_client ( None ) . await ;
983
- let room_id = room_id ! ( "!galette:saucisse.bzh" ) ;
984
- client . base_client ( ) . get_or_create_room ( room_id , matrix_sdk_base :: RoomState :: Joined ) ;
985
-
986
- client . event_cache ( ) . subscribe ( ) . unwrap ( ) ;
927
+ // If I wait for a back-pagination token for 0 seconds ,
928
+ let before = Instant :: now ( ) ;
929
+ let found = room_event_cache
930
+ . earliest_backpagination_token ( Some ( Duration :: default ( ) ) )
931
+ . await
932
+ . unwrap ( ) ;
933
+ let waited = before . elapsed ( ) ;
934
+ // then I do get one.
935
+ assert_eq ! ( found. as_ref( ) , Some ( & expected_token) ) ;
936
+ // and I haven't waited long .
937
+ assert ! ( waited. as_millis ( ) < 100 ) ;
938
+
939
+ // If I wait for a back-pagination token for 1 second,
940
+ let before = Instant :: now ( ) ;
941
+ let found = room_event_cache
942
+ . earliest_backpagination_token ( Some ( Duration :: from_secs ( 1 ) ) )
943
+ . await
944
+ . unwrap ( ) ;
945
+ let waited = before . elapsed ( ) ;
946
+ // then I do get one.
947
+ assert_eq ! ( found . as_ref ( ) , Some ( & expected_token ) ) ;
948
+ // and I haven't waited long.
949
+ assert ! ( waited . as_millis ( ) < 100 ) ;
950
+ }
987
951
988
- let ( room_event_cache, _drop_handles) =
989
- client. event_cache ( ) . for_room ( room_id) . await . unwrap ( ) ;
990
- let room_event_cache = room_event_cache. unwrap ( ) ;
952
+ #[ async_test]
953
+ async fn test_wait_for_late_pagination_token ( ) {
954
+ let client = logged_in_client ( None ) . await ;
955
+ let room_id = room_id ! ( "!galette:saucisse.bzh" ) ;
956
+ client. base_client ( ) . get_or_create_room ( room_id, matrix_sdk_base:: RoomState :: Joined ) ;
957
+
958
+ client. event_cache ( ) . subscribe ( ) . unwrap ( ) ;
959
+
960
+ let ( room_event_cache, _drop_handles) =
961
+ client. event_cache ( ) . for_room ( room_id) . await . unwrap ( ) ;
962
+ let room_event_cache = room_event_cache. unwrap ( ) ;
963
+
964
+ let expected_token = PaginationToken ( "old" . to_owned ( ) ) ;
965
+
966
+ let before = Instant :: now ( ) ;
967
+ let cloned_expected_token = expected_token. clone ( ) ;
968
+ let insert_token_task = spawn ( async move {
969
+ // If a backpagination token is inserted after 400 milliseconds,
970
+ sleep ( Duration :: from_millis ( 400 ) ) . await ;
971
+
972
+ client
973
+ . event_cache ( )
974
+ . inner
975
+ . store
976
+ . lock ( )
977
+ . await
978
+ . append_room_entries (
979
+ room_id,
980
+ vec ! [ TimelineEntry :: Gap { prev_token: cloned_expected_token } ] ,
981
+ )
982
+ . await
983
+ . unwrap ( ) ;
984
+ } ) ;
985
+
986
+ // Then first I don't get it (if I'm not waiting,)
987
+ let found = room_event_cache. earliest_backpagination_token ( None ) . await . unwrap ( ) ;
988
+ assert ! ( found. is_none( ) ) ;
989
+
990
+ // And if I wait for the back-pagination token for 600ms,
991
+ let found = room_event_cache
992
+ . earliest_backpagination_token ( Some ( Duration :: from_millis ( 600 ) ) )
993
+ . await
994
+ . unwrap ( ) ;
995
+ let waited = before. elapsed ( ) ;
991
996
992
- // If I try to back-paginate with an unknown back-pagination token,
993
- let token = PaginationToken ( "old" . to_owned ( ) ) ;
997
+ // then I do get one eventually.
998
+ assert_eq ! ( found. as_ref( ) , Some ( & expected_token) ) ;
999
+ // and I have waited between ~400 and ~1000 milliseconds.
1000
+ assert ! ( waited. as_secs( ) < 1 ) ;
1001
+ assert ! ( waited. as_millis( ) >= 400 ) ;
994
1002
995
- // Then I run into an error .
996
- let res = room_event_cache . backpaginate_with_token ( 20 , Some ( token ) ) . await ;
997
- assert_matches ! ( res . unwrap_err ( ) , EventCacheError :: UnknownBackpaginationToken ) ;
1003
+ // The task succeeded .
1004
+ insert_token_task . await . unwrap ( ) ;
1005
+ }
998
1006
}
999
1007
}
0 commit comments