Skip to content

Commit 61001ca

Browse files
committed
store test [nfc]: Use async.elapse instead of await Future.delayed
This lets us simplify by cutting `async.flushMicrotasks` calls. Saying `await Future.delayed` creates a timer, and then waits for that timer. Waiting for the timer starts by flushing microtasks, then runs any other timers due before the scheduled time (which can't exist when the delay is Duration.zero), then moves on to timers scheduled for right at the given delay. But for those timers with the exact same scheduled time, it runs only the timers that were previously scheduled. Any timers created by those microtasks or intermediate timers, and scheduled for the exact time, remain in the queue behind the timer created by that `Future.delayed`. That's why these `await Future.delayed` lines had to be preceded by `async.flushMicrotasks` calls: the timers they were meant to wait for hadn't yet been created, but there were pending microtasks that would create them. By saying `async.elapse`, we step outside the timers. Because the `elapse` call doesn't itself have a place in the timer queue, it can keep waiting until every timer with the scheduled time has run -- no matter how long a chain of microtasks and previous timers is involved in creating that timer.
1 parent 0d0e576 commit 61001ca

File tree

1 file changed

+6
-10
lines changed

1 file changed

+6
-10
lines changed

test/model/store_test.dart

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ void main() {
611611
updateMachine.debugAdvanceLoop();
612612
async.flushMicrotasks();
613613
checkLastRequest(lastEventId: 1);
614-
await Future<void>.delayed(Duration.zero);
614+
async.elapse(Duration.zero);
615615
check(updateMachine.lastEventId).equals(2);
616616

617617
// Loop makes second request, and processes result.
@@ -621,7 +621,7 @@ void main() {
621621
updateMachine.debugAdvanceLoop();
622622
async.flushMicrotasks();
623623
checkLastRequest(lastEventId: 2);
624-
await Future<void>.delayed(Duration.zero);
624+
async.elapse(Duration.zero);
625625
check(updateMachine.lastEventId).equals(3);
626626
}));
627627

@@ -635,8 +635,7 @@ void main() {
635635
property: UserSettingName.twentyFourHourTime, value: true),
636636
], queueId: null).toJson());
637637
updateMachine.debugAdvanceLoop();
638-
async.flushMicrotasks();
639-
await Future<void>.delayed(Duration.zero);
638+
async.elapse(Duration.zero);
640639
check(store.userSettings!.twentyFourHourTime).isTrue();
641640
}));
642641

@@ -647,8 +646,7 @@ void main() {
647646

648647
prepareError();
649648
updateMachine.debugAdvanceLoop();
650-
async.flushMicrotasks();
651-
await Future<void>.delayed(Duration.zero);
649+
async.elapse(Duration.zero);
652650
check(store).isLoading.isTrue();
653651

654652
// The global store has a new store.
@@ -665,8 +663,7 @@ void main() {
665663
property: UserSettingName.twentyFourHourTime, value: true),
666664
], queueId: null).toJson());
667665
updateMachine.debugAdvanceLoop();
668-
async.flushMicrotasks();
669-
await Future<void>.delayed(Duration.zero);
666+
async.elapse(Duration.zero);
670667
check(store.userSettings!.twentyFourHourTime).isTrue();
671668
});
672669
}
@@ -805,8 +802,7 @@ void main() {
805802
// Let the server expire the event queue.
806803
prepareExpiredEventQueue();
807804
updateMachine.debugAdvanceLoop();
808-
async.flushMicrotasks();
809-
await Future<void>.delayed(Duration.zero);
805+
async.elapse(Duration.zero);
810806

811807
// The old store's [MessageListView]s have been disposed.
812808
// (And no exception was thrown; that was #810.)

0 commit comments

Comments
 (0)