Skip to content

Some flutter_tools tests using FakeAsync are always passing #60675

Closed

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

Activity

  1. added
    a: tests"flutter test", flutter_test, or one of our tests
    on Jul 1, 2020
  2. DanTup commented on Jul 1, 2020

    @DanTup
    ContributorAuthor

    For my tests, I just made a small loop that flushes the queue:

    https://github.com/flutter/flutter/pull/55376/files#diff-1a0ff66910e803e1ddabc1968f7d9e8bR358-R371

    I don't know if this may be suitable to use elsewhere too (it may also be worth having a lint or something to catch these - perhaps the existing unawaited futures one would work?).

  3. added
    frameworkflutter/packages/flutter repository. See also f: labels.
    on Jul 8, 2020
  4. added
    toolAffects the "flutter" command-line tool. See also t: labels.
    and removed
    frameworkflutter/packages/flutter repository. See also f: labels.
    on Jul 8, 2020
  5. zanderso commented on Aug 5, 2020

    @zanderso
    Member

    Bumping to P2 due to lost test coverage of currently unknown scope, and to force to surface in the next triage meeting to discuss.

  6. jonahwilliams commented on Aug 13, 2020

    @jonahwilliams
    Contributor

    FakeAsync cases are now updated where feasible and skips added for the existing failures. Since we can't regress further, I'm bumping to a P3

  7. added
    P1High-priority issues at the top of the work list
    and removed on Aug 13, 2020
  8. zanderso commented on Aug 13, 2020

    @zanderso
    Member

    What's the rough number of tests that are skipped?

  9. jonahwilliams commented on Aug 13, 2020

    @jonahwilliams
    Contributor

    5 test cases

  10. jonahwilliams commented on Dec 2, 2020

    @jonahwilliams
    Contributor

    3 test cases left

  11. jonahwilliams commented on Dec 4, 2020

    @jonahwilliams
    Contributor

    removing assignment because I will be OOO

  12. removed their assignment
    on Dec 4, 2020
  13. added
    P2Important issues not at the top of the work list
    and removed
    P1High-priority issues at the top of the work list
    on May 4, 2021
  14. self-assigned this
    on Nov 5, 2024
  15. github-actions commented on Nov 20, 2024

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  16. locked as resolved and limited conversation to collaborators on Nov 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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