Repository: flutter
Issue: Stream.periodic usage causes test to hang
Labels: a: tests,framework,dependency: dart,has reproducible steps,P3,found in release: 3.3,found in release: 3.6,team-framework,triaged-framework

I've been trying to get to the bottom of [this issue](https://github.com/ReactiveX/rxdart/issues/322) and have honed in on `Stream.periodic`. In trying to diagnose, I've hit a (probably related) issue, per below.

## Steps to Reproduce

```dart
import 'dart:async';

import 'package:fake_async/fake_async.dart';
import 'package:flutter_test/flutter_test.dart';

void main() {
  testWidgets('repro with testWidgets (hangs)', (tester) async {
    final periodic = Stream<int>.periodic(const Duration(seconds: 30));
    final subscription = periodic.listen(null);

    await tester.pump(const Duration(seconds: 3));
    await subscription.cancel();

    print('done');
  });

  test('repro with quiver (works)', () async {
    final fakeAsync = FakeAsync();

    // Below I am mimicking the same calls that are made to FakeAsync when using testWidgets
    fakeAsync.flushMicrotasks();
    fakeAsync.flushMicrotasks();
    fakeAsync.flushMicrotasks();

    final periodic = Stream<int>.periodic(const Duration(seconds: 30));
    final subscription = periodic.listen(null);
    fakeAsync.elapse(const Duration(seconds: 3));
    fakeAsync.flushMicrotasks();

    await subscription.cancel();
    print('done');

    fakeAsync.flushMicrotasks();
    fakeAsync.flushMicrotasks();
    fakeAsync.flushMicrotasks();
  });

  // I have no idea why both quiver and fake_async implement a FakeAsync helper or why Flutter uses the former, but I thought I'd try this one as well.
  test('repro with fake_async package (works)', () async {
    fakeAsync((f) async {
      final periodic = Stream<int>.periodic(const Duration(seconds: 30));
      final subscription = periodic.listen(null);
      f.elapse(const Duration(seconds: 3));
      await subscription.cancel();

      print('done');
    });
  });
}
```

Running the `repro with quiver (works)` or `repro with fake_async package (works)` tests both work fine. But running the `repro with testWidgets (hangs)` test will hang after test execution.

## Flutter Doctor

```
[√] Flutter (Channel stable, v1.9.1+hotfix.4, on Microsoft Windows [Version 10.0.18362.418], locale en-AU)
    • Flutter version 1.9.1+hotfix.4 at C:\Users\Kent\Repository\flutter
    • Framework revision cc949a8e8b (3 weeks ago), 2019-09-27 15:04:59 -0700
    • Engine revision b863200c37
    • Dart version 2.5.0

[√] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    • Android SDK at C:\Users\Kent\AppData\Local\Android\Sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-29, build-tools 29.0.2
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b03)
    • All Android licenses accepted.

[√] Android Studio (version 3.5)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin version 39.0.3
    • Dart plugin version 191.8423
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b03)

[√] VS Code (version 1.39.2)
    • VS Code at C:\Users\Kent\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension version 3.5.1

[!] Connected device
    ! No devices available

! Doctor found issues in 1 category.
```