@@ -1311,108 +1311,157 @@ async fn test_sync_resumes_from_terminated() -> Result<(), Error> {
1311
1311
1312
1312
#[ async_test]
1313
1313
async fn test_loading_states ( ) -> Result < ( ) , Error > {
1314
- let ( _, server, room_list) = new_room_list_service ( ) . await ?;
1314
+ // Test with an empty client, so no cache.
1315
+ let ( client, server) = {
1316
+ let ( client, server, room_list) = new_room_list_service ( ) . await ?;
1315
1317
1316
- let sync = room_list. sync ( ) ;
1317
- pin_mut ! ( sync) ;
1318
+ let sync = room_list. sync ( ) ;
1319
+ pin_mut ! ( sync) ;
1318
1320
1319
- let all_rooms = room_list. all_rooms ( ) . await ?;
1320
- let mut all_rooms_loading_state = all_rooms. loading_state ( ) ;
1321
+ let all_rooms = room_list. all_rooms ( ) . await ?;
1322
+ let mut all_rooms_loading_state = all_rooms. loading_state ( ) ;
1321
1323
1322
- // The loading is not loaded.
1323
- assert_matches ! ( all_rooms_loading_state. get( ) , RoomListLoadingState :: NotLoaded ) ;
1324
- assert_pending ! ( all_rooms_loading_state) ;
1324
+ // The loading is not loaded.
1325
+ assert_matches ! ( all_rooms_loading_state. get( ) , RoomListLoadingState :: NotLoaded ) ;
1326
+ assert_pending ! ( all_rooms_loading_state) ;
1325
1327
1326
- sync_then_assert_request_and_fake_response ! {
1327
- [ server, room_list, sync]
1328
- states = Init => SettingUp ,
1329
- assert request >= {
1330
- "lists" : {
1331
- ALL_ROOMS : {
1332
- "ranges" : [ [ 0 , 19 ] ] ,
1328
+ sync_then_assert_request_and_fake_response ! {
1329
+ [ server, room_list, sync]
1330
+ states = Init => SettingUp ,
1331
+ assert request >= {
1332
+ "lists" : {
1333
+ ALL_ROOMS : {
1334
+ "ranges" : [ [ 0 , 19 ] ] ,
1335
+ } ,
1333
1336
} ,
1334
1337
} ,
1335
- } ,
1336
- respond with = {
1337
- "pos ": "0" ,
1338
- "lists" : {
1339
- ALL_ROOMS : {
1340
- "count" : 10 ,
1338
+ respond with = {
1339
+ "pos" : "0" ,
1340
+ "lists ": {
1341
+ ALL_ROOMS : {
1342
+ "count" : 10 ,
1343
+ } ,
1341
1344
} ,
1345
+ "rooms" : { } ,
1342
1346
} ,
1343
- "rooms" : { } ,
1344
- } ,
1345
- } ;
1347
+ } ;
1346
1348
1347
- // Wait on Tokio to run all the tasks. Necessary only when testing.
1348
- yield_now ( ) . await ;
1349
+ // Wait on Tokio to run all the tasks. Necessary only when testing.
1350
+ yield_now ( ) . await ;
1349
1351
1350
- // There is a loading state update, it's loaded now!
1351
- assert_next_matches ! (
1352
- all_rooms_loading_state,
1353
- RoomListLoadingState :: Loaded { maximum_number_of_rooms } => {
1354
- assert_eq!( maximum_number_of_rooms, Some ( 10 ) ) ;
1355
- }
1356
- ) ;
1352
+ // There is a loading state update, it's loaded now!
1353
+ assert_next_matches ! (
1354
+ all_rooms_loading_state,
1355
+ RoomListLoadingState :: Loaded { maximum_number_of_rooms: Some ( 10 ) }
1356
+ ) ;
1357
1357
1358
- sync_then_assert_request_and_fake_response ! {
1359
- [ server, room_list, sync]
1360
- states = SettingUp => Running ,
1361
- assert request >= {
1362
- "lists" : {
1363
- ALL_ROOMS : {
1364
- "ranges" : [ [ 0 , 9 ] ] ,
1358
+ sync_then_assert_request_and_fake_response ! {
1359
+ [ server, room_list, sync]
1360
+ states = SettingUp => Running ,
1361
+ assert request >= {
1362
+ "lists" : {
1363
+ ALL_ROOMS : {
1364
+ "ranges" : [ [ 0 , 9 ] ] ,
1365
+ } ,
1365
1366
} ,
1366
1367
} ,
1367
- } ,
1368
- respond with = {
1369
- "pos ": "1" ,
1370
- "lists" : {
1371
- ALL_ROOMS : {
1372
- "count" : 12 , // 2 more rooms
1368
+ respond with = {
1369
+ "pos" : "1" ,
1370
+ "lists ": {
1371
+ ALL_ROOMS : {
1372
+ "count" : 12 , // 2 more rooms
1373
+ } ,
1373
1374
} ,
1375
+ "rooms" : { } ,
1374
1376
} ,
1375
- "rooms" : { } ,
1376
- } ,
1377
- } ;
1377
+ } ;
1378
1378
1379
- // Wait on Tokio to run all the tasks. Necessary only when testing.
1380
- yield_now ( ) . await ;
1379
+ // Wait on Tokio to run all the tasks. Necessary only when testing.
1380
+ yield_now ( ) . await ;
1381
1381
1382
- // There is a loading state update because the number of rooms has been updated.
1383
- assert_next_matches ! (
1384
- all_rooms_loading_state,
1385
- RoomListLoadingState :: Loaded { maximum_number_of_rooms } => {
1386
- assert_eq!( maximum_number_of_rooms, Some ( 12 ) ) ;
1387
- }
1388
- ) ;
1382
+ // There is a loading state update because the number of rooms has been updated.
1383
+ assert_next_matches ! (
1384
+ all_rooms_loading_state,
1385
+ RoomListLoadingState :: Loaded { maximum_number_of_rooms: Some ( 12 ) }
1386
+ ) ;
1389
1387
1390
- sync_then_assert_request_and_fake_response ! {
1391
- [ server, room_list, sync]
1392
- states = Running => Running ,
1393
- assert request >= {
1394
- "lists" : {
1395
- ALL_ROOMS : {
1396
- "ranges" : [ [ 0 , 11 ] ] ,
1388
+ sync_then_assert_request_and_fake_response ! {
1389
+ [ server, room_list, sync]
1390
+ states = Running => Running ,
1391
+ assert request >= {
1392
+ "lists" : {
1393
+ ALL_ROOMS : {
1394
+ "ranges" : [ [ 0 , 11 ] ] ,
1395
+ } ,
1397
1396
} ,
1398
1397
} ,
1399
- } ,
1400
- respond with = {
1401
- "pos ": "2" ,
1402
- "lists" : {
1403
- ALL_ROOMS : {
1404
- "count" : 12 , // no more rooms
1398
+ respond with = {
1399
+ "pos" : "2" ,
1400
+ "lists ": {
1401
+ ALL_ROOMS : {
1402
+ "count" : 12 , // no more rooms
1403
+ } ,
1405
1404
} ,
1405
+ "rooms" : { } ,
1406
1406
} ,
1407
- "rooms" : { } ,
1408
- } ,
1407
+ } ;
1408
+
1409
+ // Wait on Tokio to run all the tasks. Necessary only when testing.
1410
+ yield_now ( ) . await ;
1411
+
1412
+ // No loading state update.
1413
+ assert_pending ! ( all_rooms_loading_state) ;
1414
+
1415
+ ( client, server)
1409
1416
} ;
1410
1417
1411
- // Wait on Tokio to run all the tasks. Necessary only when testing.
1412
- yield_now ( ) . await ;
1418
+ // Now, let's try with a cache!
1419
+ {
1420
+ let room_list = RoomListService :: new ( client) . await ?;
1421
+
1422
+ let all_rooms = room_list. all_rooms ( ) . await ?;
1423
+ let mut all_rooms_loading_state = all_rooms. loading_state ( ) ;
1424
+
1425
+ let sync = room_list. sync ( ) ;
1426
+ pin_mut ! ( sync) ;
1427
+
1428
+ // The loading state is loaded! Indeed, there is data loaded from the cache.
1429
+ assert_matches ! (
1430
+ all_rooms_loading_state. get( ) ,
1431
+ RoomListLoadingState :: Loaded { maximum_number_of_rooms: Some ( 12 ) }
1432
+ ) ;
1433
+ assert_pending ! ( all_rooms_loading_state) ;
1434
+
1435
+ sync_then_assert_request_and_fake_response ! {
1436
+ [ server, room_list, sync]
1437
+ states = Init => SettingUp ,
1438
+ assert request >= {
1439
+ "lists" : {
1440
+ ALL_ROOMS : {
1441
+ "ranges" : [ [ 0 , 19 ] ] ,
1442
+ } ,
1443
+ } ,
1444
+ } ,
1445
+ respond with = {
1446
+ "pos" : "0" ,
1447
+ "lists" : {
1448
+ ALL_ROOMS : {
1449
+ "count" : 13 , // 1 more room
1450
+ } ,
1451
+ } ,
1452
+ "rooms" : { } ,
1453
+ } ,
1454
+ } ;
1455
+
1456
+ // Wait on Tokio to run all the tasks. Necessary only when testing.
1457
+ yield_now ( ) . await ;
1413
1458
1414
- // No loading state update.
1415
- assert_pending ! ( all_rooms_loading_state) ;
1459
+ // The loading state has been updated.
1460
+ assert_next_matches ! (
1461
+ all_rooms_loading_state,
1462
+ RoomListLoadingState :: Loaded { maximum_number_of_rooms: Some ( 13 ) }
1463
+ ) ;
1464
+ }
1416
1465
1417
1466
Ok ( ( ) )
1418
1467
}
0 commit comments