Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stream.periodic usage causes test to hang #40074

Open
kentcb opened this issue Sep 9, 2019 · 3 comments
Open

Stream.periodic usage causes test to hang #40074

kentcb opened this issue Sep 9, 2019 · 3 comments
Labels
a: tests "flutter test", flutter_test, or one of our tests dependency: dart Dart team may need to help us found in release: 3.3 Found to occur in 3.3 found in release: 3.6 Found to occur in 3.6 framework flutter/packages/flutter repository. See also f: labels. has reproducible steps The issue has been confirmed reproducible and is ready to work on P3 Issues that are less important to the Flutter project team-framework Owned by Framework team triaged-framework Triaged by Framework team

Comments

@kentcb
Copy link
Contributor

kentcb commented Sep 9, 2019

I've been trying to get to the bottom of this issue and have honed in on Stream.periodic. In trying to diagnose, I've hit a (probably related) issue, per below.

Steps to Reproduce

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.
@darshankawar
Copy link
Member

The test repro with testWidgets (hangs)' still hangs on latest master after completing execution, while other two tests passed without any issues.

Screenshot 2020-09-17 at 6 18 53 PM

Screenshot 2020-09-17 at 6 19 08 PM

flutter doctor -v
[✓] Flutter (Channel master, 1.22.0-10.0.pre.221, on Mac OS X 10.15.4 19E2269
    x86_64, locale en-IN)
    • Flutter version 1.22.0-10.0.pre.221 at
      /Users/dhs/documents/Fluttersdk/flutter
    • Framework revision fa646339d6 (8 hours ago), 2020-09-16 17:47:04 -0700
    • Engine revision 933f811d94
    • Dart version 2.10.0 (build 2.10.0-135.0.dev)

 
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.0)
    • Android SDK at /Users/dhs/Library/Android/sdk
    • Platform android-30, build-tools 30.0.0
    • Java binary at: /Applications/Android
      Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build
      1.8.0_242-release-1644-b3-6222593)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 11.5)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 11.5, Build version 11E608c
    • CocoaPods version 1.9.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 4.0)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 46.0.2
    • Dart plugin version 193.7361
    • Java version OpenJDK Runtime Environment (build
      1.8.0_242-release-1644-b3-6222593)

[✓] VS Code (version 1.46.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.11.0

[✓] Connected device (4 available)
    • SM G975F (mobile)                   • RZ8M802WY0X
      • android-arm64  • Android 10 (API 29)
    • iPhone SE (2nd generation) (mobile) • 312977A7-2DB1-4737-AEBB-35FA6522A309
      • ios            • com.apple.CoreSimulator.SimRuntime.iOS-13-5 (simulator)
    • Web Server (web)                    • web-server
      • web-javascript • Flutter Tools
    • Chrome (web)                        • chrome
      • web-javascript • Google Chrome 85.0.4183.102

• No issues found!

@darshankawar darshankawar added found in release: 1.22 Found to occur in 1.22 has reproducible steps The issue has been confirmed reproducible and is ready to work on passed first triage labels Sep 17, 2020
@maheshj01
Copy link
Member

maheshj01 commented Nov 23, 2022

The issue still reproduces as of master 3.6 and stable 3.3

image

test
import 'dart:async';

import 'package:flutter_test/flutter_test.dart';

void main() {
  testWidgets('repro with testWidgets (hangs)', (tester) async {
    final periodic = Stream<int>.periodic(const Duration(seconds: 10),(count)=>count);
    final subscription = periodic.listen((x){
      print('x: $x');
    });
    await tester.pump(const Duration(seconds: 3));
    await subscription.cancel();

    print('done');
  });
}
flutter doctor -v (mac)
[✓] Flutter (Channel master, 3.6.0-6.0.pre.33, on macOS 12.6 21G115 darwin-arm64,
    locale en-IN)
    • Flutter version 3.6.0-6.0.pre.33 on channel master at
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision a9858ec524 (72 minutes ago), 2022-11-21 10:58:11 -0500
    • Engine revision 46a6b54295
    • Dart version 2.19.0 (build 2.19.0-406.0.dev)
    • DevTools version 2.19.0
    • If those were intentional, you can disregard the above warnings; however it is
      recommended to use "git" directly to perform update checks and upgrades.

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0-rc4)
    • Android SDK at /Users/mahesh/Library/Android/sdk
    • Platform android-33, build-tools 33.0.0-rc4
    • ANDROID_HOME = /Users/mahesh/Library/Android/sdk
    • Java binary at: /Applications/Android
      Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.0.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14A400
    • CocoaPods version 1.11.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)

[✓] IntelliJ IDEA Community Edition (version 2021.2.1)
    • IntelliJ at /Applications/IntelliJ IDEA CE.app
    • Flutter plugin version 61.2.4
    • Dart plugin version 212.5080.8

[✓] VS Code (version 1.70.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.53.20221101

[✓] Connected device (3 available)
    • iPhone 12 Pro (mobile) • 026D5789-9E78-4AD5-B1B2-3F8D4E7F65E4 • ios
      • com.apple.CoreSimulator.SimRuntime.iOS-14-5 (simulator)
    • macOS (desktop)        • macos                                • darwin-arm64
      • macOS 12.6 21G115 darwin-arm64
    • Chrome (web)           • chrome                               • web-javascript
      • Google Chrome 107.0.5304.110

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!
[✓] Flutter (Channel stable, 3.3.9, on macOS 12.6 21G115 darwin-arm, locale en-IN)
    • Flutter version 3.3.9 on channel stable at /Users/mahesh/Development/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision b8f7f1f986 (24 hours ago), 2022-11-23 06:43:51 +0900
    • Engine revision 8f2221fbef
    • Dart version 2.18.5
    • DevTools version 2.15.0

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0-rc4)
    • Android SDK at /Users/mahesh/Library/Android/sdk
    • Platform android-33, build-tools 33.0.0-rc4
    • ANDROID_HOME = /Users/mahesh/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.0.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14A400
    • CocoaPods version 1.11.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.12+0-b1504.28-7817840)

[✓] IntelliJ IDEA Community Edition (version 2021.2.1)
    • IntelliJ at /Applications/IntelliJ IDEA CE.app
    • Flutter plugin version 61.2.4
    • Dart plugin version 212.5080.8

[✓] VS Code (version 1.70.2)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.53.20221101

[✓] Connected device (3 available)
    • iPhone 12 Pro (mobile) • 026D5789-9E78-4AD5-B1B2-3F8D4E7F65E4 • ios            •
      com.apple.CoreSimulator.SimRuntime.iOS-14-5 (simulator)
    • macOS (desktop)        • macos                                • darwin-arm64   • macOS 12.6 21G115 darwin-arm
    • Chrome (web)           • chrome                               • web-javascript • Google Chrome 107.0.5304.110

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!

@maheshj01 maheshj01 added found in release: 3.3 Found to occur in 3.3 found in release: 3.6 Found to occur in 3.6 and removed found in release: 1.22 Found to occur in 1.22 labels Nov 23, 2022
@goderbauer goderbauer added the P6 label Jan 17, 2023
@flutter-triage-bot flutter-triage-bot bot added P3 Issues that are less important to the Flutter project and removed P6 labels Jun 28, 2023
@flutter-triage-bot flutter-triage-bot bot added team-framework Owned by Framework team triaged-framework Triaged by Framework team labels Jul 8, 2023
@MelbourneDeveloper
Copy link

MelbourneDeveloper commented Dec 11, 2023

@kentcb the issue has nothing to do with Stream.periodic. The issue happens because the test awaits the cancellation of the subscription. If you don't await the cancellation, Stream.periodic works fine in tests. You can prove this yourself with this sample

import 'dart:async';
import 'package:flutter_test/flutter_test.dart';

void main() {
  testWidgets('Periodic Stream - Working Fine (No Await)', (tester) async {
    final periodic =
        Stream<int>.periodic(const Duration(seconds: 30), (i) => i);
    final subscription = periodic.listen(null);

    await tester.pump(const Duration(seconds: 3));

    //Note here that we don't await and the test passes fine
    subscription.cancel();
  });

    testWidgets('Periodic Stream - Test Hangs (Await Cancel Subscription)', (tester) async {
    final periodic =
        Stream<int>.periodic(const Duration(seconds: 30), (i) => i);
    final subscription = periodic.listen(null);

    await tester.pump(const Duration(seconds: 3));

    //Note here that you can step right over this line and it looks like it executes fine, but it will cause the test to hang
    await subscription.cancel();
  });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a: tests "flutter test", flutter_test, or one of our tests dependency: dart Dart team may need to help us found in release: 3.3 Found to occur in 3.3 found in release: 3.6 Found to occur in 3.6 framework flutter/packages/flutter repository. See also f: labels. has reproducible steps The issue has been confirmed reproducible and is ready to work on P3 Issues that are less important to the Flutter project team-framework Owned by Framework team triaged-framework Triaged by Framework team
Projects
None yet
Development

No branches or pull requests

8 participants