Skip to content

Commit 8d27b0c

Browse files
committed
test: Simplify some tests using the assert_next_with_timeout macro
1 parent 3707d2f commit 8d27b0c

File tree

1 file changed

+69
-78
lines changed

1 file changed

+69
-78
lines changed

crates/matrix-sdk/src/room/identity_status_changes.rs

Lines changed: 69 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -194,17 +194,14 @@ fn wrap_room_member_events(
194194

195195
#[cfg(test)]
196196
mod tests {
197-
use std::{
198-
pin::{pin, Pin},
199-
time::Duration,
200-
};
201-
202-
use futures_core::Stream;
203-
use futures_util::FutureExt;
204-
use matrix_sdk_base::crypto::{IdentityState, IdentityStatusChange};
197+
use std::time::Duration;
198+
199+
use futures_util::{pin_mut, FutureExt as _, StreamExt as _};
200+
use matrix_sdk_base::crypto::IdentityState;
205201
use matrix_sdk_test::{async_test, test_json::keys_query_sets::IdentityChangeDataSet};
206202
use test_setup::TestSetup;
207-
use tokio_stream::{StreamExt, Timeout};
203+
204+
use crate::assert_next_with_timeout;
208205

209206
#[async_test]
210207
async fn test_when_user_becomes_unpinned_we_report_it() {
@@ -215,13 +212,14 @@ mod tests {
215212
t.pin_bob().await;
216213

217214
// And we are listening for identity changes
218-
let changes = t.subscribe_to_identity_status_changes().await;
215+
let stream = t.subscribe_to_identity_status_changes().await;
216+
pin_mut!(stream);
219217

220218
// When Bob becomes unpinned
221219
t.unpin_bob().await;
222220

223221
// Then we were notified about it
224-
let change = next_change(&mut pin!(changes)).await;
222+
let change = assert_next_with_timeout!(stream);
225223
assert_eq!(change[0].user_id, t.bob_user_id());
226224
assert_eq!(change[0].changed_to, IdentityState::PinViolation);
227225
assert_eq!(change.len(), 1);
@@ -236,13 +234,14 @@ mod tests {
236234
t.verify_bob().await;
237235

238236
// And we are listening for identity changes
239-
let changes = t.subscribe_to_identity_status_changes().await;
237+
let stream = t.subscribe_to_identity_status_changes().await;
238+
pin_mut!(stream);
240239

241240
// When Bob's identity changes
242241
t.unpin_bob().await;
243242

244243
// Then we were notified about a verification violation
245-
let change = next_change(&mut pin!(changes)).await;
244+
let change = assert_next_with_timeout!(stream);
246245
assert_eq!(change[0].user_id, t.bob_user_id());
247246
assert_eq!(change[0].changed_to, IdentityState::VerificationViolation);
248247
assert_eq!(change.len(), 1);
@@ -257,20 +256,20 @@ mod tests {
257256
t.unpin_bob().await;
258257

259258
// And we are listening for identity changes
260-
let changes = t.subscribe_to_identity_status_changes().await;
261-
let mut changes = pin!(changes);
259+
let stream = t.subscribe_to_identity_status_changes().await;
260+
pin_mut!(stream);
262261

263262
// When Bob becomes pinned
264263
t.pin_bob().await;
265264

266265
// Then we were notified about the initial state of the room
267-
let change1 = next_change(&mut changes).await;
266+
let change1 = assert_next_with_timeout!(stream);
268267
assert_eq!(change1[0].user_id, t.bob_user_id());
269268
assert_eq!(change1[0].changed_to, IdentityState::PinViolation);
270269
assert_eq!(change1.len(), 1);
271270

272271
// And the change when Bob became pinned
273-
let change2 = next_change(&mut changes).await;
272+
let change2 = assert_next_with_timeout!(stream);
274273
assert_eq!(change2[0].user_id, t.bob_user_id());
275274
assert_eq!(change2[0].changed_to, IdentityState::Pinned);
276275
assert_eq!(change2.len(), 1);
@@ -282,8 +281,8 @@ mod tests {
282281
let t = TestSetup::new_room_with_other_bob().await;
283282

284283
// And we are listening for identity changes
285-
let changes = t.subscribe_to_identity_status_changes().await;
286-
let mut changes = pin!(changes);
284+
let stream = t.subscribe_to_identity_status_changes().await;
285+
pin_mut!(stream);
287286

288287
// When Bob becomes verified
289288
t.verify_bob().await;
@@ -292,10 +291,10 @@ mod tests {
292291
t.unpin_bob().await;
293292

294293
// Then we are only notified about the unpinning part
295-
let change2 = next_change(&mut changes).await;
296-
assert_eq!(change2[0].user_id, t.bob_user_id());
297-
assert_eq!(change2[0].changed_to, IdentityState::VerificationViolation);
298-
assert_eq!(change2.len(), 1);
294+
let change = assert_next_with_timeout!(stream);
295+
assert_eq!(change[0].user_id, t.bob_user_id());
296+
assert_eq!(change[0].changed_to, IdentityState::VerificationViolation);
297+
assert_eq!(change.len(), 1);
299298
}
300299

301300
#[async_test]
@@ -307,20 +306,20 @@ mod tests {
307306
t.unpin_bob_with(IdentityChangeDataSet::key_query_with_identity_a()).await;
308307

309308
// And we are listening for identity changes
310-
let changes = t.subscribe_to_identity_status_changes().await;
311-
let mut changes = pin!(changes);
309+
let stream = t.subscribe_to_identity_status_changes().await;
310+
pin_mut!(stream);
312311

313312
// When Bob becomes verified
314313
t.verify_bob().await;
315314

316315
// Then we were notified about the initial state of the room
317-
let change1 = next_change(&mut changes).await;
316+
let change1 = assert_next_with_timeout!(stream);
318317
assert_eq!(change1[0].user_id, t.bob_user_id());
319318
assert_eq!(change1[0].changed_to, IdentityState::PinViolation);
320319
assert_eq!(change1.len(), 1);
321320

322321
// And the change when Bob became verified
323-
let change2 = next_change(&mut changes).await;
322+
let change2 = assert_next_with_timeout!(stream);
324323
assert_eq!(change2[0].user_id, t.bob_user_id());
325324
assert_eq!(change2[0].changed_to, IdentityState::Verified);
326325
assert_eq!(change2.len(), 1);
@@ -341,20 +340,20 @@ mod tests {
341340
t.unpin_bob().await;
342341

343342
// And we are listening for identity changes
344-
let changes = t.subscribe_to_identity_status_changes().await;
345-
let mut changes = pin!(changes);
343+
let stream = t.subscribe_to_identity_status_changes().await;
344+
pin_mut!(stream);
346345

347346
// When Bob becomes verified
348347
t.verify_bob().await;
349348

350349
// Then we were notified about the initial state of the room
351-
let change1 = next_change(&mut changes).await;
350+
let change1 = assert_next_with_timeout!(stream);
352351
assert_eq!(change1[0].user_id, t.bob_user_id());
353352
assert_eq!(change1[0].changed_to, IdentityState::VerificationViolation);
354353
assert_eq!(change1.len(), 1);
355354

356355
// And the change when Bob became verified
357-
let change2 = next_change(&mut changes).await;
356+
let change2 = assert_next_with_timeout!(stream);
358357
assert_eq!(change2[0].user_id, t.bob_user_id());
359358
assert_eq!(change2[0].changed_to, IdentityState::Verified);
360359
assert_eq!(change2.len(), 1);
@@ -369,13 +368,14 @@ mod tests {
369368
t.unpin_bob().await;
370369

371370
// And we are listening for identity changes
372-
let changes = t.subscribe_to_identity_status_changes().await;
371+
let stream = t.subscribe_to_identity_status_changes().await;
372+
pin_mut!(stream);
373373

374374
// When Bob joins the room
375375
t.bob_joins().await;
376376

377377
// Then we were notified about it
378-
let change = next_change(&mut pin!(changes)).await;
378+
let change = assert_next_with_timeout!(stream);
379379
assert_eq!(change[0].user_id, t.bob_user_id());
380380
assert_eq!(change[0].changed_to, IdentityState::PinViolation);
381381
assert_eq!(change.len(), 1);
@@ -391,13 +391,14 @@ mod tests {
391391
t.unpin_bob().await;
392392

393393
// And we are listening for identity changes
394-
let changes = t.subscribe_to_identity_status_changes().await;
394+
let stream = t.subscribe_to_identity_status_changes().await;
395+
pin_mut!(stream);
395396

396397
// When Bob joins the room
397398
t.bob_joins().await;
398399

399400
// Then we were notified about it
400-
let change = next_change(&mut pin!(changes)).await;
401+
let change = assert_next_with_timeout!(stream);
401402
assert_eq!(change[0].user_id, t.bob_user_id());
402403
assert_eq!(change[0].changed_to, IdentityState::VerificationViolation);
403404
assert_eq!(change.len(), 1);
@@ -412,7 +413,8 @@ mod tests {
412413
t.verify_bob().await;
413414

414415
// And we are listening for identity changes
415-
let changes = t.subscribe_to_identity_status_changes().await;
416+
let stream = t.subscribe_to_identity_status_changes().await;
417+
pin_mut!(stream);
416418

417419
// When Bob joins the room
418420
t.bob_joins().await;
@@ -421,8 +423,7 @@ mod tests {
421423
t.unpin_bob().await;
422424

423425
//// Then we were only notified about the unpin
424-
let mut changes = pin!(changes);
425-
let change = next_change(&mut changes).await;
426+
let change = assert_next_with_timeout!(stream);
426427
assert_eq!(change[0].user_id, t.bob_user_id());
427428
assert_eq!(change[0].changed_to, IdentityState::VerificationViolation);
428429
assert_eq!(change.len(), 1);
@@ -437,15 +438,15 @@ mod tests {
437438
t.pin_bob().await;
438439

439440
// And we are listening for identity changes
440-
let changes = t.subscribe_to_identity_status_changes().await;
441-
let mut changes = pin!(changes);
441+
let stream = t.subscribe_to_identity_status_changes().await;
442+
pin_mut!(stream);
442443

443444
// When Bob joins the room
444445
t.bob_joins().await;
445446

446447
// Then there is no notification
447448
tokio::time::sleep(Duration::from_millis(200)).await;
448-
let change = changes.next().now_or_never();
449+
let change = stream.next().now_or_never();
449450
assert!(change.is_none());
450451
}
451452

@@ -458,20 +459,20 @@ mod tests {
458459
t.unpin_bob().await;
459460

460461
// And we are listening for identity changes
461-
let changes = t.subscribe_to_identity_status_changes().await;
462-
let mut changes = pin!(changes);
462+
let stream = t.subscribe_to_identity_status_changes().await;
463+
pin_mut!(stream);
463464

464465
// When Bob leaves the room
465466
t.bob_leaves().await;
466467

467468
// Then we were notified about the initial state of the room
468-
let change1 = next_change(&mut changes).await;
469+
let change1 = assert_next_with_timeout!(stream);
469470
assert_eq!(change1[0].user_id, t.bob_user_id());
470471
assert_eq!(change1[0].changed_to, IdentityState::PinViolation);
471472
assert_eq!(change1.len(), 1);
472473

473474
// And we were notified about the change when the user left
474-
let change2 = next_change(&mut changes).await;
475+
let change2 = assert_next_with_timeout!(stream);
475476
// Note: the user left the room, but we see that as them "becoming pinned" i.e.
476477
// "you no longer need to notify about this user".
477478
assert_eq!(change2[0].user_id, t.bob_user_id());
@@ -488,36 +489,35 @@ mod tests {
488489
t.unpin_bob().await;
489490

490491
// And we are listening for identity changes
491-
let changes = t.subscribe_to_identity_status_changes().await;
492-
let mut changes = pin!(changes);
492+
let stream = t.subscribe_to_identity_status_changes().await;
493+
pin_mut!(stream);
493494

494495
// NOTE: below we pull the changes out of the subscription after each action.
495-
// This makes sure that the identity changes and membership changes are
496-
// properly ordered. If we pull them out later, the identity changes get
497-
// shifted forward because they rely on less-complex async stuff under
498-
// the hood. Calling next_change ends up winding the async
499-
// machinery sufficiently that the membership change and any subsequent events
500-
// have fully completed.
496+
// This makes sure that the identity changes and membership changes are properly
497+
// ordered. If we pull them out later, the identity changes get shifted forward
498+
// because they rely on less-complex async stuff under the hood. Calling
499+
// next_change ends up winding the async machinery sufficiently that the
500+
// membership change and any subsequent events have fully completed.
501501

502502
// When Bob joins the room ...
503503
t.bob_joins().await;
504-
let change1 = next_change(&mut changes).await;
504+
let change1 = assert_next_with_timeout!(stream);
505505

506506
// ... becomes pinned ...
507507
t.pin_bob().await;
508-
let change2 = next_change(&mut changes).await;
508+
let change2 = assert_next_with_timeout!(stream);
509509

510510
// ... leaves and joins again (ignored since they stay pinned) ...
511511
t.bob_leaves().await;
512512
t.bob_joins().await;
513513

514514
// ... becomes unpinned ...
515515
t.unpin_bob().await;
516-
let change3 = next_change(&mut changes).await;
516+
let change3 = assert_next_with_timeout!(stream);
517517

518518
// ... and leaves.
519519
t.bob_leaves().await;
520-
let change4 = next_change(&mut changes).await;
520+
let change4 = assert_next_with_timeout!(stream);
521521

522522
assert_eq!(change1[0].user_id, t.bob_user_id());
523523
assert_eq!(change2[0].user_id, t.bob_user_id());
@@ -542,10 +542,11 @@ mod tests {
542542
t.unpin_bob().await;
543543

544544
// When we start listening for identity changes
545-
let changes = t.subscribe_to_identity_status_changes().await;
545+
let stream = t.subscribe_to_identity_status_changes().await;
546+
pin_mut!(stream);
546547

547548
// Then we were immediately notified about Bob being unpinned
548-
let change = next_change(&mut pin!(changes)).await;
549+
let change = assert_next_with_timeout!(stream);
549550
assert_eq!(change[0].user_id, t.bob_user_id());
550551
assert_eq!(change[0].changed_to, IdentityState::PinViolation);
551552
assert_eq!(change.len(), 1);
@@ -558,34 +559,26 @@ mod tests {
558559
t.verify_bob().await;
559560

560561
// When we start listening for identity changes
561-
let changes = t.subscribe_to_identity_status_changes().await;
562+
let stream = t.subscribe_to_identity_status_changes().await;
563+
pin_mut!(stream);
562564

563565
// (And we unpin so that something is available in the changes stream)
564566
t.unpin_bob().await;
565567

566568
// Then we were only notified about the unpin, not being verified
567-
let change = next_change(&mut pin!(changes)).await;
568-
assert_eq!(change[0].user_id, t.bob_user_id());
569-
assert_eq!(change[0].changed_to, IdentityState::VerificationViolation);
570-
assert_eq!(change.len(), 1);
569+
let next_change = assert_next_with_timeout!(stream);
570+
571+
assert_eq!(next_change[0].user_id, t.bob_user_id());
572+
assert_eq!(next_change[0].changed_to, IdentityState::VerificationViolation);
573+
assert_eq!(next_change.len(), 1);
571574
}
572575

573576
// TODO: I (andyb) haven't figured out how to test room membership changes that
574577
// affect our own user (they should not be shown). Specifically, I haven't
575578
// figure out how to get out own user into a non-pinned state.
576579

577-
async fn next_change(
578-
changes: &mut Pin<&mut Timeout<impl Stream<Item = Vec<IdentityStatusChange>>>>,
579-
) -> Vec<IdentityStatusChange> {
580-
changes
581-
.next()
582-
.await
583-
.expect("Should not reach end of changes stream")
584-
.expect("Should not time out waiting for a change")
585-
}
586-
587580
mod test_setup {
588-
use std::time::{Duration, SystemTime, UNIX_EPOCH};
581+
use std::time::{SystemTime, UNIX_EPOCH};
589582

590583
use futures_core::Stream;
591584
use matrix_sdk_base::{
@@ -605,7 +598,6 @@ mod tests {
605598
owned_user_id, OwnedUserId, TransactionId, UserId,
606599
};
607600
use serde_json::json;
608-
use tokio_stream::{StreamExt as _, Timeout};
609601
use wiremock::{
610602
matchers::{header, method, path_regex},
611603
Mock, MockServer, ResponseTemplate,
@@ -786,12 +778,11 @@ mod tests {
786778

787779
pub(super) async fn subscribe_to_identity_status_changes(
788780
&self,
789-
) -> Timeout<impl Stream<Item = Vec<IdentityStatusChange>>> {
781+
) -> impl Stream<Item = Vec<IdentityStatusChange>> {
790782
self.room
791783
.subscribe_to_identity_status_changes()
792784
.await
793785
.expect("Should be able to subscribe")
794-
.timeout(Duration::from_secs(5))
795786
}
796787

797788
async fn init() -> (Client, OwnedUserId, SyncResponseBuilder) {

0 commit comments

Comments
 (0)