ReactiveX / rxdart

The Reactive Extensions for Dart
http://reactivex.io
Apache License 2.0
3.36k stars 271 forks source link

Cannot await stream after upgrading to rxdart 0.27.1 to 0.27.2 in Flutter tests #691

Open voidsp opened 2 years ago

voidsp commented 2 years ago

Hello! After I have upgraded from rxdart 0.27.1 to 0.27.5, my tests have started failing. I found the test starts failing from rxdart 0.27.2:

import 'package:flutter_test/flutter_test.dart';
import 'package:rxdart/subjects.dart';

void main() {
  group('subject test', () {
    testWidgets('wait first', (WidgetTester tester) async {
      final subject = BehaviorSubject<String>();
      subject.add('my-value');

      final Stream<String> stream = subject.stream;

      expect(await stream.first, 'my-value');

      subject.close();
    });
  });
}

This test only fails when using flutter_test instead of test (however, in the actual test case I need to use flutter_test as there are some widgets being tested), so the interesting thing is this test will work:

import 'package:flutter_test/flutter_test.dart';
import 'package:rxdart/subjects.dart';

void main() {
  group('subject test', () {
    test('wait first', () async {
      final subject = BehaviorSubject<String>();
      subject.add('my-value');

      final Stream<String> stream = subject.stream;

      expect(await stream.first, 'my-value');

      subject.close();
    });
  });
}

The first code snippet test (with flutter_test) now just hangs, but previously it ran correctly in rxdart 0.27.1. The second code snippet (with test) works on all version of rxdart.

hoc081098 commented 2 years ago

I wrap await stream.first in tester.runAsync and it works.

  group('subject test', () {
    testWidgets('wait first', (tester) async {
      final subject = BehaviorSubject<String>();
      subject.add('my-value');

      final Stream<String> stream = subject.stream;

      await tester.runAsync(() async {
        expect(await stream.first, 'my-value');
      });

      await subject.close();
    });
  });
voidsp commented 2 years ago

Thank you @hoc081098, is this a regression in rxdart? I'm not sure why wrapping it in tester.runAsync is now required.

CaleyD commented 1 year ago

I'm still running into this - unfortunately wrapping our entire widget test in runAsync causes it to fail for other reasons so its not a reasonable workaround for us.

I've found that this for some reason fixes the tests await stream.doOnData((_) {}).first but seems like an ugly hack.