Skip to content
This repository was archived by the owner on Feb 10, 2025. It is now read-only.

Commit 905caed

Browse files
authored
Use fake_async over waiting for timers with delays (#166)
On the web the timers may have some variance. Replace all usages of `waitForTimer` utility to use `fakeAsync`. Separate out the call to `Stream.listen` from `setUp` so it happens in the fake async zone.
1 parent bc9f537 commit 905caed

File tree

5 files changed

+361
-239
lines changed

5 files changed

+361
-239
lines changed

pubspec.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ environment:
88

99
dev_dependencies:
1010
async: ^2.5.0
11+
fake_async: ^1.3.0
1112
lints: ^2.0.0
1213
test: ^1.16.0

test/audit_test.dart

Lines changed: 80 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'dart:async';
66

7+
import 'package:fake_async/fake_async.dart';
78
import 'package:stream_transform/stream_transform.dart';
89
import 'package:test/test.dart';
910

@@ -21,7 +22,7 @@ void main() {
2122
late StreamSubscription<int> subscription;
2223

2324
group('audit', () {
24-
setUp(() async {
25+
setUp(() {
2526
valuesCanceled = false;
2627
values = createController(streamType)
2728
..onCancel = () {
@@ -31,73 +32,106 @@ void main() {
3132
errors = [];
3233
isDone = false;
3334
transformed = values.stream.audit(const Duration(milliseconds: 6));
35+
});
36+
37+
void listen() {
3438
subscription = transformed
3539
.listen(emittedValues.add, onError: errors.add, onDone: () {
3640
isDone = true;
3741
});
38-
});
42+
}
3943

4044
test('cancels values', () async {
45+
listen();
4146
await subscription.cancel();
4247
expect(valuesCanceled, true);
4348
});
4449

45-
test('swallows values that come faster than duration', () async {
46-
values
47-
..add(1)
48-
..add(2);
49-
await values.close();
50-
await waitForTimer(5);
51-
expect(emittedValues, [2]);
50+
test('swallows values that come faster than duration', () {
51+
fakeAsync((async) {
52+
listen();
53+
values
54+
..add(1)
55+
..add(2)
56+
..close();
57+
async.elapse(const Duration(milliseconds: 6));
58+
expect(emittedValues, [2]);
59+
});
5260
});
5361

54-
test('outputs multiple values spaced further than duration', () async {
55-
values.add(1);
56-
await waitForTimer(5);
57-
values.add(2);
58-
await waitForTimer(5);
59-
expect(emittedValues, [1, 2]);
62+
test('outputs multiple values spaced further than duration', () {
63+
fakeAsync((async) {
64+
listen();
65+
values.add(1);
66+
async.elapse(const Duration(milliseconds: 6));
67+
values.add(2);
68+
async.elapse(const Duration(milliseconds: 6));
69+
expect(emittedValues, [1, 2]);
70+
});
6071
});
6172

62-
test('waits for pending value to close', () async {
63-
values.add(1);
64-
await values.close();
65-
expect(isDone, false);
66-
await waitForTimer(5);
67-
expect(isDone, true);
73+
test('waits for pending value to close', () {
74+
fakeAsync((async) {
75+
listen();
76+
values
77+
..add(1)
78+
..close();
79+
expect(isDone, false);
80+
async.elapse(const Duration(milliseconds: 6));
81+
expect(isDone, true);
82+
});
6883
});
6984

70-
test('closes output if there are no pending values', () async {
71-
values.add(1);
72-
await waitForTimer(5);
73-
values.add(2);
74-
await values.close();
75-
expect(isDone, false);
76-
await waitForTimer(5);
77-
expect(isDone, true);
85+
test('closes output if there are no pending values', () {
86+
fakeAsync((async) {
87+
listen();
88+
values.add(1);
89+
async.elapse(const Duration(milliseconds: 6));
90+
values
91+
..add(2)
92+
..close();
93+
expect(isDone, false);
94+
expect(emittedValues, [1]);
95+
async.elapse(const Duration(milliseconds: 6));
96+
expect(isDone, true);
97+
expect(emittedValues, [1, 2]);
98+
});
7899
});
79100

80101
test('does not starve output if many values come closer than duration',
81-
() async {
82-
values.add(1);
83-
await Future.delayed(const Duration(milliseconds: 4));
84-
values.add(2);
85-
await Future.delayed(const Duration(milliseconds: 4));
86-
values.add(3);
87-
await waitForTimer(6);
88-
expect(emittedValues, [2, 3]);
102+
() {
103+
fakeAsync((async) {
104+
listen();
105+
values.add(1);
106+
async.elapse(const Duration(milliseconds: 3));
107+
values.add(2);
108+
async.elapse(const Duration(milliseconds: 3));
109+
values.add(3);
110+
async.elapse(const Duration(milliseconds: 6));
111+
expect(emittedValues, [2, 3]);
112+
});
89113
});
90114

91115
if (streamType == 'broadcast') {
92-
test('multiple listeners all get values', () async {
93-
var otherValues = [];
94-
transformed.listen(otherValues.add);
95-
values
96-
..add(1)
97-
..add(2);
98-
await waitForTimer(5);
99-
expect(emittedValues, [2]);
100-
expect(otherValues, [2]);
116+
test('multiple listeners all get the values', () {
117+
fakeAsync((async) {
118+
listen();
119+
values.add(1);
120+
async.elapse(const Duration(milliseconds: 3));
121+
values.add(2);
122+
var otherValues = [];
123+
transformed.listen(otherValues.add);
124+
values.add(3);
125+
async.elapse(const Duration(milliseconds: 3));
126+
values.add(4);
127+
async.elapse(const Duration(milliseconds: 3));
128+
values
129+
..add(5)
130+
..close();
131+
async.elapse(const Duration(milliseconds: 6));
132+
expect(emittedValues, [3, 5]);
133+
expect(otherValues, [3, 5]);
134+
});
101135
});
102136
}
103137
});

0 commit comments

Comments
 (0)