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

StreamSubscription.cancel hangs up #2316

Open
mercsst opened this issue Feb 13, 2024 · 0 comments
Open

StreamSubscription.cancel hangs up #2316

mercsst opened this issue Feb 13, 2024 · 0 comments

Comments

@mercsst
Copy link

mercsst commented Feb 13, 2024

I have a test that passes normally if run without FakeAsync, but freezes mid execution under FakeAsync.
After some time debugging, it looks like the problem is somehow related to StreamSubscription's cancel method.

Here is a sample to demonstrate:

import 'dart:async';

import 'package:fake_async/fake_async.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_test/flutter_test.dart';

class ServiceUnderTest {
  ServiceUnderTest(this.stream);

  Stream<bool> stream;
  StreamSubscription<dynamic>? _subscription;

  Future<void> start() async {
    _subscription = stream.listen((event) {
      debugPrintSynchronously('event: $event');
    });
  }

  Future<void> stop() async {
    debugPrintSynchronously('stop begun');
    await _subscription?.cancel();
    _subscription = null;
    debugPrintSynchronously('stop 1');
    await _subscription?.cancel();
    debugPrintSynchronously('stop finished');
  }
}

void main() {
  late ServiceUnderTest serviceUnderTest;

  setUp(() {
    final controller = StreamController<bool>();
    controller.add(true);

    serviceUnderTest = ServiceUnderTest(controller.stream);
  });

  test('No fakeAsync - Passes normally', () async {
    await serviceUnderTest.start();
    await expectLater(serviceUnderTest.stop(), completes);
  });

  test('With fakeAsync - Hangs up', () async {
    FakeAsync().run((async) {
      serviceUnderTest.start();
      async.flushMicrotasks();
      async.elapse(const Duration(seconds: 2));

      expectLater(serviceUnderTest.stop(), completes);
      async.flushMicrotasks();
      async.elapse(const Duration(seconds: 2));
    });
  });
}

First test would pass with no problems, but second one will output:

event: true
stop begun
stop 1

and hang up.

Is this an expected behaviour ? If so, how could i remedy it.

@mosuem mosuem transferred this issue from dart-lang/fake_async Oct 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants