Skip to content

Closing a StreamController in addTearDown freezes test #2074

@ngoc-quoc-huynh

Description

@ngoc-quoc-huynh

Using a StreamController with an addTearDown to close the stream within withClock freezes the test.
If I remove the addTeardown the test finishes.

Here is a small example:

  testWidgets('description', (tester) async {
    await withClock(Clock.fixed(DateTime(2025)), () async {
      final controller = StreamController<DateTime?>();
      addTearDown(() async {
        print('Before');
        await controller.close();
        print('After');
      });

      final widget = MaterialApp(home: Text(clock.now().toString()));
      await tester.pumpWidget(widget);

      expect(find.text('2025-01-01 00:00:00.000'), findsOneWidget);
      print('End');
    });
  });

Output:

End
Before

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions