4
4
5
5
import 'dart:async' ;
6
6
7
+ import 'package:fake_async/fake_async.dart' ;
7
8
import 'package:stream_transform/stream_transform.dart' ;
8
9
import 'package:test/test.dart' ;
9
10
@@ -21,7 +22,7 @@ void main() {
21
22
late StreamSubscription <int > subscription;
22
23
23
24
group ('audit' , () {
24
- setUp (() async {
25
+ setUp (() {
25
26
valuesCanceled = false ;
26
27
values = createController (streamType)
27
28
..onCancel = () {
@@ -31,73 +32,106 @@ void main() {
31
32
errors = [];
32
33
isDone = false ;
33
34
transformed = values.stream.audit (const Duration (milliseconds: 6 ));
35
+ });
36
+
37
+ void listen () {
34
38
subscription = transformed
35
39
.listen (emittedValues.add, onError: errors.add, onDone: () {
36
40
isDone = true ;
37
41
});
38
- });
42
+ }
39
43
40
44
test ('cancels values' , () async {
45
+ listen ();
41
46
await subscription.cancel ();
42
47
expect (valuesCanceled, true );
43
48
});
44
49
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
+ });
52
60
});
53
61
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
+ });
60
71
});
61
72
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
+ });
68
83
});
69
84
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
+ });
78
99
});
79
100
80
101
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
+ });
89
113
});
90
114
91
115
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
+ });
101
135
});
102
136
}
103
137
});
0 commit comments