Skip to content

Some flutter_tools tests using FakeAsync are always passing #60675

@DanTup

Description

@DanTup

There are some tests that use FakeAsync.run() that are not awaiting the call, for example:

FakeAsync().run((FakeAsync time) async {
final FakePollingDeviceDiscovery pollingDeviceDiscovery = FakePollingDeviceDiscovery();
await pollingDeviceDiscovery.startPolling();
time.elapse(const Duration(milliseconds: 4001));

It seems like this is resulted in the tests always passing immediately. For example adding throw 'fail'; as the first line inside the callback does nothing - the test still passes.

I think these tests need await in front of the FakeAsync().run, however doing so right now causes them to hang - which I think is because the implementation uses await which queues a microtask but the queue is never flushed (since the code stops on the await).

Based on what @Hixie said, I think flutter_test is handling this by constantly pumping the microtask queue, however the flutter_tools tests don't run with flutter_test so don't have an equivilent. I think the relevant code in flutter_test is this bit:

return Future<void>.microtask(() async {
// testBodyResult is a Future that was created in the Zone of the
// fakeAsync. This means that if we await it here, it will register a
// microtask to handle the future _in the fake async zone_. We avoid this
// by calling '.then' in the current zone. While flushing the microtasks
// of the fake-zone below, the new future will be completed and can then
// be used without fakeAsync.
final Future<void> resultFuture = testBodyResult.then<void>((_) {
// Do nothing.
});
// Resolve interplay between fake async and real async calls.
fakeAsync.flushMicrotasks();
while (_pendingAsyncTasks != null) {
await _pendingAsyncTasks.future;
fakeAsync.flushMicrotasks();
}
return resultFuture;
});

cc @Hixie @jonahwilliams

Metadata

Metadata

Assignees

Labels

P2Important issues not at the top of the work lista: tests"flutter test", flutter_test, or one of our teststeam-toolOwned by Flutter Tool teamtoolAffects the "flutter" command-line tool. See also t: labels.triaged-toolTriaged by Flutter Tool team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions