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

Regression when switchMap is used together with asynchronous generator #764

Open
ky1vstar opened this issue Aug 26, 2024 · 4 comments
Open
Assignees

Comments

@ky1vstar
Copy link

ky1vstar commented Aug 26, 2024

Here is minimal reproducible example for regression which I found during migration from rxdart 0.27.7 to 0.28.0.

Dart SDK version: 3.5.1

Source code

import 'package:rxdart/rxdart.dart';

void main(List<String> arguments) async {
  getStream().listen(print);
}

Stream<String> getStream() {
  final stream1 = Future.delayed(Duration(seconds: 1), () => "1").asStream();
  final stream2 = stream1.startWith("2");

  final stream3 = stream2.switchMap((value) async* {
    await for (final innerValue in BehaviorSubject.seeded(value)) {
      yield innerValue;
    }
  });

  return stream3;
}

Expected result (rxdart 0.27.7):

2
1

Actual result (rxdart 0.28.0):

2

@ReactiveX ReactiveX deleted a comment Aug 26, 2024
@ReactiveX ReactiveX deleted a comment Aug 26, 2024
@ReactiveX ReactiveX deleted a comment Aug 26, 2024
@hoc081098
Copy link
Collaborator

hoc081098 commented Aug 28, 2024

possibly related to dart-lang/sdk#42717 (comment), dart-lang/sdk#56619

@drstranges
Copy link

The issue is not only with the asynchronous generator, but also with someFuture.asStream too.
Here is the test that will fail on 0.28.0 but succeed on 0.27.7

  testWidgets('switchMap regression test', (tester) async {

    const delay = Duration(seconds: 1);

    Future<String> someFuture(String value) async {
      await Future<void>.delayed(delay);
      return value;
    }

    final subject = PublishSubject<String>();
    final beforeSwitchMap = <String>[];
    final afterSwitchMap = <String>[];

    subject
        .doOnData(beforeSwitchMap.add)
        .switchMap(
          (value) => someFuture(value).asStream(),
        )
        .doOnData(afterSwitchMap.add)
        .listen((event) {
      debugPrint('event: $event');
    });

    await tester.pump(const Duration(seconds: 2));
    subject.add('1');
    await tester.pump(const Duration(seconds: 2));
    subject.add('2');
    // Delay before next value < [delay] will fail the test in 0.28.0
    await tester.pump(delay - const Duration(microseconds: 1));
    subject.add('3');
    await tester.pump(const Duration(seconds: 2));
    subject.add('4');
    await tester.pump(const Duration(seconds: 2));

    expect(beforeSwitchMap, ['1', '2', '3', '4']);

    // Next line will succeed in 0.27.7 but fail in 0.28.0
    // with the message:
    // Expected: ['1', '3', '4']
    // Actual: ['1']
    expect(afterSwitchMap, ['1', '3', '4']);
  });

I can't reproduce the same behavior in runtime. Only widget tests seem to be affected (26 of our tests are red now ).

@duduBaiao
Copy link

duduBaiao commented Jan 8, 2025

After upgrading to 0.28.0, all code that uses BehaviorSubject.switchMap started to fail when I run the tests.
The production code works fine.

It only emits data for the first stream.

When I add a new value to the BehaviorSubject, I can see it's reprocessing the new stream, but it doesn't emit the value on time to be detected by the test. I'm using fake_async in these tests.

It seems to be a side effect of this change:

Screenshot 2025-01-08 at 15 43 18

Flutter 3.24.5 • channel [user-branch] • unknown source
Framework • revision dec2ee5c1f (8 weeks ago) • 2024-11-13 11:13:06 -0800
Engine • revision a18df97ca5
Tools • Dart 3.5.4 • DevTools 2.37.3

@hoc081098
Copy link
Collaborator

After upgrading to 0.28.0, all code that uses BehaviorSubject.switchMap started to fail when I run the tests. The production code works fine.

It only emits data for the first stream.

When I add a new value to the BehaviorSubject, I can see it's reprocessing the new stream, but it doesn't emit the value on time to be detected by the test. I'm using fake_async in these tests.

It seems to be a side effect of this change:

Screenshot 2025-01-08 at 15 43 18

Flutter 3.24.5 • channel [user-branch] • unknown source
Framework • revision dec2ee5c1f (8 weeks ago) • 2024-11-13 11:13:06 -0800
Engine • revision a18df97ca5
Tools • Dart 3.5.4 • DevTools 2.37.3

Maybe related: #395 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants
@duduBaiao @drstranges @ky1vstar @hoc081098 and others