OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 // Test the basic StreamController and StreamController.broadcast. | 5 // Test the basic StreamController and StreamController.broadcast. |
6 library stream_controller_async_test; | 6 library stream_controller_async_test; |
7 | 7 |
8 import 'dart:async'; | 8 import 'dart:async'; |
9 import 'package:test/test.dart'; | 9 import "package:expect/expect.dart"; |
| 10 import 'package:unittest/unittest.dart'; |
10 import 'event_helper.dart'; | 11 import 'event_helper.dart'; |
11 import 'stream_state_helper.dart'; | 12 import 'stream_state_helper.dart'; |
12 | 13 |
13 void cancelSub(StreamSubscription sub) { sub.cancel(); } | 14 void cancelSub(StreamSubscription sub) { sub.cancel(); } |
14 | 15 |
15 testController() { | 16 testController() { |
16 // Test fold | 17 // Test fold |
17 test("StreamController.fold", () { | 18 test("StreamController.fold", () { |
18 StreamController c = new StreamController(); | 19 StreamController c = new StreamController(); |
19 Stream stream = c.stream.asBroadcastStream(onCancel: cancelSub); | 20 Stream stream = c.stream.asBroadcastStream(onCancel: cancelSub); |
20 stream.fold(0, (a,b) => a + b) | 21 stream.fold(0, (a,b) => a + b) |
21 .then(expectAsync((int v) { | 22 .then(expectAsync((int v) { |
22 expect(42, equals(v)); | 23 Expect.equals(42, v); |
23 })); | 24 })); |
24 c.add(10); | 25 c.add(10); |
25 c.add(32); | 26 c.add(32); |
26 c.close(); | 27 c.close(); |
27 }); | 28 }); |
28 | 29 |
29 test("StreamController.fold throws", () { | 30 test("StreamController.fold throws", () { |
30 StreamController c = new StreamController(); | 31 StreamController c = new StreamController(); |
31 Stream stream = c.stream.asBroadcastStream(onCancel: cancelSub); | 32 Stream stream = c.stream.asBroadcastStream(onCancel: cancelSub); |
32 stream.fold(0, (a,b) { throw "Fnyf!"; }) | 33 stream.fold(0, (a,b) { throw "Fnyf!"; }) |
33 .catchError(expectAsync((error) { expect("Fnyf!", equals(error)); })); | 34 .catchError(expectAsync((error) { Expect.equals("Fnyf!", error); })); |
34 c.add(42); | 35 c.add(42); |
35 }); | 36 }); |
36 } | 37 } |
37 | 38 |
38 testSingleController() { | 39 testSingleController() { |
39 test("Single-subscription StreamController.fold", () { | 40 test("Single-subscription StreamController.fold", () { |
40 StreamController c = new StreamController(); | 41 StreamController c = new StreamController(); |
41 Stream stream = c.stream; | 42 Stream stream = c.stream; |
42 stream.fold(0, (a,b) => a + b) | 43 stream.fold(0, (a,b) => a + b) |
43 .then(expectAsync((int v) { expect(42, equals(v)); })); | 44 .then(expectAsync((int v) { Expect.equals(42, v); })); |
44 c.add(10); | 45 c.add(10); |
45 c.add(32); | 46 c.add(32); |
46 c.close(); | 47 c.close(); |
47 }); | 48 }); |
48 | 49 |
49 test("Single-subscription StreamController.fold throws", () { | 50 test("Single-subscription StreamController.fold throws", () { |
50 StreamController c = new StreamController(); | 51 StreamController c = new StreamController(); |
51 Stream stream = c.stream; | 52 Stream stream = c.stream; |
52 stream.fold(0, (a,b) { throw "Fnyf!"; }) | 53 stream.fold(0, (a,b) { throw "Fnyf!"; }) |
53 .catchError(expectAsync((e) { expect("Fnyf!", equals(e)); })); | 54 .catchError(expectAsync((e) { Expect.equals("Fnyf!", e); })); |
54 c.add(42); | 55 c.add(42); |
55 }); | 56 }); |
56 | 57 |
57 test("Single-subscription StreamController events are buffered when" | 58 test("Single-subscription StreamController events are buffered when" |
58 " there is no subscriber", | 59 " there is no subscriber", |
59 () { | 60 () { |
60 StreamController c = new StreamController(); | 61 StreamController c = new StreamController(); |
61 EventSink sink = c.sink; | 62 EventSink sink = c.sink; |
62 Stream stream = c.stream; | 63 Stream stream = c.stream; |
63 int counter = 0; | 64 int counter = 0; |
64 sink.add(1); | 65 sink.add(1); |
65 sink.add(2); | 66 sink.add(2); |
66 sink.close(); | 67 sink.close(); |
67 stream.listen( | 68 stream.listen( |
68 (data) { | 69 (data) { |
69 counter += data; | 70 counter += data; |
70 }, | 71 }, |
71 onDone: expectAsync(() { | 72 onDone: expectAsync(() { |
72 expect(3, equals(counter)); | 73 Expect.equals(3, counter); |
73 })); | 74 })); |
74 }); | 75 }); |
75 } | 76 } |
76 | 77 |
77 testExtraMethods() { | 78 testExtraMethods() { |
78 Events sentEvents = new Events()..add(7)..add(9)..add(13)..add(87)..close(); | 79 Events sentEvents = new Events()..add(7)..add(9)..add(13)..add(87)..close(); |
79 | 80 |
80 test("forEach", () { | 81 test("forEach", () { |
81 StreamController c = new StreamController(); | 82 StreamController c = new StreamController(); |
82 Events actualEvents = new Events(); | 83 Events actualEvents = new Events(); |
83 Future f = c.stream.forEach(actualEvents.add); | 84 Future f = c.stream.forEach(actualEvents.add); |
84 f.then(expectAsync((_) { | 85 f.then(expectAsync((_) { |
85 actualEvents.close(); | 86 actualEvents.close(); |
86 expect(sentEvents.events, equals(actualEvents.events)); | 87 Expect.listEquals(sentEvents.events, actualEvents.events); |
87 })); | 88 })); |
88 sentEvents.replay(c); | 89 sentEvents.replay(c); |
89 }); | 90 }); |
90 | 91 |
91 test("forEachError", () { | 92 test("forEachError", () { |
92 Events sentEvents = new Events()..add(7)..error("bad")..add(87)..close(); | 93 Events sentEvents = new Events()..add(7)..error("bad")..add(87)..close(); |
93 StreamController c = new StreamController(); | 94 StreamController c = new StreamController(); |
94 Events actualEvents = new Events(); | 95 Events actualEvents = new Events(); |
95 Future f = c.stream.forEach(actualEvents.add); | 96 Future f = c.stream.forEach(actualEvents.add); |
96 f.catchError(expectAsync((error) { | 97 f.catchError(expectAsync((error) { |
97 expect("bad", equals(error)); | 98 Expect.equals("bad", error); |
98 expect((new Events()..add(7)).events, equals(actualEvents.events)); | 99 Expect.listEquals((new Events()..add(7)).events, actualEvents.events); |
99 })); | 100 })); |
100 sentEvents.replay(c); | 101 sentEvents.replay(c); |
101 }); | 102 }); |
102 | 103 |
103 test("forEachError2", () { | 104 test("forEachError2", () { |
104 Events sentEvents = new Events()..add(7)..add(9)..add(87)..close(); | 105 Events sentEvents = new Events()..add(7)..add(9)..add(87)..close(); |
105 StreamController c = new StreamController(); | 106 StreamController c = new StreamController(); |
106 Events actualEvents = new Events(); | 107 Events actualEvents = new Events(); |
107 Future f = c.stream.forEach((x) { | 108 Future f = c.stream.forEach((x) { |
108 if (x == 9) throw "bad"; | 109 if (x == 9) throw "bad"; |
109 actualEvents.add(x); | 110 actualEvents.add(x); |
110 }); | 111 }); |
111 f.catchError(expectAsync((error) { | 112 f.catchError(expectAsync((error) { |
112 expect("bad", equals(error)); | 113 Expect.equals("bad", error); |
113 expect((new Events()..add(7)).events, equals(actualEvents.events)); | 114 Expect.listEquals((new Events()..add(7)).events, actualEvents.events); |
114 })); | 115 })); |
115 sentEvents.replay(c); | 116 sentEvents.replay(c); |
116 }); | 117 }); |
117 | 118 |
118 test("firstWhere", () { | 119 test("firstWhere", () { |
119 StreamController c = new StreamController(); | 120 StreamController c = new StreamController(); |
120 Future f = c.stream.firstWhere((x) => (x % 3) == 0); | 121 Future f = c.stream.firstWhere((x) => (x % 3) == 0); |
121 f.then(expectAsync((v) { expect(9, equals(v)); })); | 122 f.then(expectAsync((v) { Expect.equals(9, v); })); |
122 sentEvents.replay(c); | 123 sentEvents.replay(c); |
123 }); | 124 }); |
124 | 125 |
125 test("firstWhere 2", () { | 126 test("firstWhere 2", () { |
126 StreamController c = new StreamController(); | 127 StreamController c = new StreamController(); |
127 Future f = c.stream.firstWhere((x) => (x % 4) == 0); | 128 Future f = c.stream.firstWhere((x) => (x % 4) == 0); |
128 f.catchError(expectAsync((e) {})); | 129 f.catchError(expectAsync((e) {})); |
129 sentEvents.replay(c); | 130 sentEvents.replay(c); |
130 }); | 131 }); |
131 | 132 |
132 test("firstWhere 3", () { | 133 test("firstWhere 3", () { |
133 StreamController c = new StreamController(); | 134 StreamController c = new StreamController(); |
134 Future f = c.stream.firstWhere((x) => (x % 4) == 0, defaultValue: () => 999)
; | 135 Future f = c.stream.firstWhere((x) => (x % 4) == 0, defaultValue: () => 999)
; |
135 f.then(expectAsync((v) { expect(999, equals(v)); })); | 136 f.then(expectAsync((v) { Expect.equals(999, v); })); |
136 sentEvents.replay(c); | 137 sentEvents.replay(c); |
137 }); | 138 }); |
138 | 139 |
139 | 140 |
140 test("lastWhere", () { | 141 test("lastWhere", () { |
141 StreamController c = new StreamController(); | 142 StreamController c = new StreamController(); |
142 Future f = c.stream.lastWhere((x) => (x % 3) == 0); | 143 Future f = c.stream.lastWhere((x) => (x % 3) == 0); |
143 f.then(expectAsync((v) { expect(87, equals(v)); })); | 144 f.then(expectAsync((v) { Expect.equals(87, v); })); |
144 sentEvents.replay(c); | 145 sentEvents.replay(c); |
145 }); | 146 }); |
146 | 147 |
147 test("lastWhere 2", () { | 148 test("lastWhere 2", () { |
148 StreamController c = new StreamController(); | 149 StreamController c = new StreamController(); |
149 Future f = c.stream.lastWhere((x) => (x % 4) == 0); | 150 Future f = c.stream.lastWhere((x) => (x % 4) == 0); |
150 f.catchError(expectAsync((e) {})); | 151 f.catchError(expectAsync((e) {})); |
151 sentEvents.replay(c); | 152 sentEvents.replay(c); |
152 }); | 153 }); |
153 | 154 |
154 test("lastWhere 3", () { | 155 test("lastWhere 3", () { |
155 StreamController c = new StreamController(); | 156 StreamController c = new StreamController(); |
156 Future f = c.stream.lastWhere((x) => (x % 4) == 0, defaultValue: () => 999); | 157 Future f = c.stream.lastWhere((x) => (x % 4) == 0, defaultValue: () => 999); |
157 f.then(expectAsync((v) { expect(999, equals(v)); })); | 158 f.then(expectAsync((v) { Expect.equals(999, v); })); |
158 sentEvents.replay(c); | 159 sentEvents.replay(c); |
159 }); | 160 }); |
160 | 161 |
161 test("singleWhere", () { | 162 test("singleWhere", () { |
162 StreamController c = new StreamController(); | 163 StreamController c = new StreamController(); |
163 Future f = c.stream.singleWhere((x) => (x % 9) == 0); | 164 Future f = c.stream.singleWhere((x) => (x % 9) == 0); |
164 f.then(expectAsync((v) { expect(9, equals(v)); })); | 165 f.then(expectAsync((v) { Expect.equals(9, v); })); |
165 sentEvents.replay(c); | 166 sentEvents.replay(c); |
166 }); | 167 }); |
167 | 168 |
168 test("singleWhere 2", () { | 169 test("singleWhere 2", () { |
169 StreamController c = new StreamController(); | 170 StreamController c = new StreamController(); |
170 Future f = c.stream.singleWhere((x) => (x % 3) == 0); // Matches 9 and 87.. | 171 Future f = c.stream.singleWhere((x) => (x % 3) == 0); // Matches 9 and 87.. |
171 f.catchError(expectAsync((error) { expect(error is StateError, isTrue); })); | 172 f.catchError(expectAsync((error) { Expect.isTrue(error is StateError); })); |
172 sentEvents.replay(c); | 173 sentEvents.replay(c); |
173 }); | 174 }); |
174 | 175 |
175 test("first", () { | 176 test("first", () { |
176 StreamController c = new StreamController(); | 177 StreamController c = new StreamController(); |
177 Future f = c.stream.first; | 178 Future f = c.stream.first; |
178 f.then(expectAsync((v) { expect(7, equals(v)); })); | 179 f.then(expectAsync((v) { Expect.equals(7, v);})); |
179 sentEvents.replay(c); | 180 sentEvents.replay(c); |
180 }); | 181 }); |
181 | 182 |
182 test("first empty", () { | 183 test("first empty", () { |
183 StreamController c = new StreamController(); | 184 StreamController c = new StreamController(); |
184 Future f = c.stream.first; | 185 Future f = c.stream.first; |
185 f.catchError(expectAsync((error) { expect(error is StateError, isTrue); })); | 186 f.catchError(expectAsync((error) { Expect.isTrue(error is StateError); })); |
186 Events emptyEvents = new Events()..close(); | 187 Events emptyEvents = new Events()..close(); |
187 emptyEvents.replay(c); | 188 emptyEvents.replay(c); |
188 }); | 189 }); |
189 | 190 |
190 test("first error", () { | 191 test("first error", () { |
191 StreamController c = new StreamController(); | 192 StreamController c = new StreamController(); |
192 Future f = c.stream.first; | 193 Future f = c.stream.first; |
193 f.catchError(expectAsync((error) { expect("error", equals(error)); })); | 194 f.catchError(expectAsync((error) { Expect.equals("error", error); })); |
194 Events errorEvents = new Events()..error("error")..close(); | 195 Events errorEvents = new Events()..error("error")..close(); |
195 errorEvents.replay(c); | 196 errorEvents.replay(c); |
196 }); | 197 }); |
197 | 198 |
198 test("first error 2", () { | 199 test("first error 2", () { |
199 StreamController c = new StreamController(); | 200 StreamController c = new StreamController(); |
200 Future f = c.stream.first; | 201 Future f = c.stream.first; |
201 f.catchError(expectAsync((error) { expect("error", equals(error)); })); | 202 f.catchError(expectAsync((error) { Expect.equals("error", error); })); |
202 Events errorEvents = new Events()..error("error")..error("error2")..close(); | 203 Events errorEvents = new Events()..error("error")..error("error2")..close(); |
203 errorEvents.replay(c); | 204 errorEvents.replay(c); |
204 }); | 205 }); |
205 | 206 |
206 test("last", () { | 207 test("last", () { |
207 StreamController c = new StreamController(); | 208 StreamController c = new StreamController(); |
208 Future f = c.stream.last; | 209 Future f = c.stream.last; |
209 f.then(expectAsync((v) { expect(87, equals(v)); })); | 210 f.then(expectAsync((v) { Expect.equals(87, v);})); |
210 sentEvents.replay(c); | 211 sentEvents.replay(c); |
211 }); | 212 }); |
212 | 213 |
213 test("last empty", () { | 214 test("last empty", () { |
214 StreamController c = new StreamController(); | 215 StreamController c = new StreamController(); |
215 Future f = c.stream.last; | 216 Future f = c.stream.last; |
216 f.catchError(expectAsync((error) { expect(error is StateError, isTrue); })); | 217 f.catchError(expectAsync((error) { Expect.isTrue(error is StateError); })); |
217 Events emptyEvents = new Events()..close(); | 218 Events emptyEvents = new Events()..close(); |
218 emptyEvents.replay(c); | 219 emptyEvents.replay(c); |
219 }); | 220 }); |
220 | 221 |
221 test("last error", () { | 222 test("last error", () { |
222 StreamController c = new StreamController(); | 223 StreamController c = new StreamController(); |
223 Future f = c.stream.last; | 224 Future f = c.stream.last; |
224 f.catchError(expectAsync((error) { expect("error", equals(error)); })); | 225 f.catchError(expectAsync((error) { Expect.equals("error", error); })); |
225 Events errorEvents = new Events()..error("error")..close(); | 226 Events errorEvents = new Events()..error("error")..close(); |
226 errorEvents.replay(c); | 227 errorEvents.replay(c); |
227 }); | 228 }); |
228 | 229 |
229 test("last error 2", () { | 230 test("last error 2", () { |
230 StreamController c = new StreamController(); | 231 StreamController c = new StreamController(); |
231 Future f = c.stream.last; | 232 Future f = c.stream.last; |
232 f.catchError(expectAsync((error) { expect("error", equals(error)); })); | 233 f.catchError(expectAsync((error) { Expect.equals("error", error); })); |
233 Events errorEvents = new Events()..error("error")..error("error2")..close(); | 234 Events errorEvents = new Events()..error("error")..error("error2")..close(); |
234 errorEvents.replay(c); | 235 errorEvents.replay(c); |
235 }); | 236 }); |
236 | 237 |
237 test("elementAt", () { | 238 test("elementAt", () { |
238 StreamController c = new StreamController(); | 239 StreamController c = new StreamController(); |
239 Future f = c.stream.elementAt(2); | 240 Future f = c.stream.elementAt(2); |
240 f.then(expectAsync((v) { expect(13, equals(v)); })); | 241 f.then(expectAsync((v) { Expect.equals(13, v);})); |
241 sentEvents.replay(c); | 242 sentEvents.replay(c); |
242 }); | 243 }); |
243 | 244 |
244 test("elementAt 2", () { | 245 test("elementAt 2", () { |
245 StreamController c = new StreamController(); | 246 StreamController c = new StreamController(); |
246 Future f = c.stream.elementAt(20); | 247 Future f = c.stream.elementAt(20); |
247 f.catchError(expectAsync((error) { expect(error is RangeError, isTrue); })); | 248 f.catchError(expectAsync((error) { Expect.isTrue(error is RangeError); })); |
248 sentEvents.replay(c); | 249 sentEvents.replay(c); |
249 }); | 250 }); |
250 | 251 |
251 test("drain", () { | 252 test("drain", () { |
252 StreamController c = new StreamController(); | 253 StreamController c = new StreamController(); |
253 Future f = c.stream.drain(); | 254 Future f = c.stream.drain(); |
254 f.then(expectAsync((v) { expect(null, equals(v)); })); | 255 f.then(expectAsync((v) { Expect.equals(null, v);})); |
255 sentEvents.replay(c); | 256 sentEvents.replay(c); |
256 }); | 257 }); |
257 | 258 |
258 test("drain error", () { | 259 test("drain error", () { |
259 StreamController c = new StreamController(); | 260 StreamController c = new StreamController(); |
260 Future f = c.stream.drain(); | 261 Future f = c.stream.drain(); |
261 f.catchError(expectAsync((error) { expect("error", equals(error)); })); | 262 f.catchError(expectAsync((error) { Expect.equals("error", error); })); |
262 Events errorEvents = new Events()..error("error")..error("error2")..close(); | 263 Events errorEvents = new Events()..error("error")..error("error2")..close(); |
263 errorEvents.replay(c); | 264 errorEvents.replay(c); |
264 }); | 265 }); |
265 | 266 |
266 } | 267 } |
267 | 268 |
268 testPause() { | 269 testPause() { |
269 test("pause event-unpause", () { | 270 test("pause event-unpause", () { |
270 | 271 |
271 StreamProtocolTest test = new StreamProtocolTest(); | 272 StreamProtocolTest test = new StreamProtocolTest(); |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
358 | 359 |
359 class TestError { const TestError(); } | 360 class TestError { const TestError(); } |
360 | 361 |
361 testRethrow() { | 362 testRethrow() { |
362 TestError error = const TestError(); | 363 TestError error = const TestError(); |
363 | 364 |
364 testStream(name, streamValueTransform) { | 365 testStream(name, streamValueTransform) { |
365 test("rethrow-$name-value", () { | 366 test("rethrow-$name-value", () { |
366 StreamController c = new StreamController(); | 367 StreamController c = new StreamController(); |
367 Stream s = streamValueTransform(c.stream, (v) { throw error; }); | 368 Stream s = streamValueTransform(c.stream, (v) { throw error; }); |
368 s.listen((_) { fail("unexpected value"); }, onError: expectAsync( | 369 s.listen((_) { Expect.fail("unexpected value"); }, onError: expectAsync( |
369 (e) { expect(error, same(e)); })); | 370 (e) { Expect.identical(error, e); })); |
370 c.add(null); | 371 c.add(null); |
371 c.close(); | 372 c.close(); |
372 }); | 373 }); |
373 } | 374 } |
374 | 375 |
375 testStreamError(name, streamErrorTransform) { | 376 testStreamError(name, streamErrorTransform) { |
376 test("rethrow-$name-error", () { | 377 test("rethrow-$name-error", () { |
377 StreamController c = new StreamController(); | 378 StreamController c = new StreamController(); |
378 Stream s = streamErrorTransform(c.stream, (e) { throw error; }); | 379 Stream s = streamErrorTransform(c.stream, (e) { throw error; }); |
379 s.listen((_) { fail("unexpected value"); }, onError: expectAsync( | 380 s.listen((_) { Expect.fail("unexpected value"); }, onError: expectAsync( |
380 (e) { expect(error, same(e)); })); | 381 (e) { Expect.identical(error, e); })); |
381 c.addError("SOME ERROR"); | 382 c.addError("SOME ERROR"); |
382 c.close(); | 383 c.close(); |
383 }); | 384 }); |
384 } | 385 } |
385 | 386 |
386 testFuture(name, streamValueTransform) { | 387 testFuture(name, streamValueTransform) { |
387 test("rethrow-$name-value", () { | 388 test("rethrow-$name-value", () { |
388 StreamController c = new StreamController(); | 389 StreamController c = new StreamController(); |
389 Future f = streamValueTransform(c.stream, (v) { throw error; }); | 390 Future f = streamValueTransform(c.stream, (v) { throw error; }); |
390 f.then((v) { fail("unreachable"); }, | 391 f.then((v) { Expect.fail("unreachable"); }, |
391 onError: expectAsync((e) { expect(error, same(e)); })); | 392 onError: expectAsync((e) { Expect.identical(error, e); })); |
392 // Need two values to trigger compare for reduce. | 393 // Need two values to trigger compare for reduce. |
393 c.add(0); | 394 c.add(0); |
394 c.add(1); | 395 c.add(1); |
395 c.close(); | 396 c.close(); |
396 }); | 397 }); |
397 } | 398 } |
398 | 399 |
399 testStream("where", (s, act) => s.where(act)); | 400 testStream("where", (s, act) => s.where(act)); |
400 testStream("map", (s, act) => s.map(act)); | 401 testStream("map", (s, act) => s.map(act)); |
401 testStream("expand", (s, act) => s.expand(act)); | 402 testStream("expand", (s, act) => s.expand(act)); |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
579 : c.stream); | 580 : c.stream); |
580 var sink = c.sink; | 581 var sink = c.sink; |
581 sink.add(42); | 582 sink.add(42); |
582 sink.addError("error"); | 583 sink.addError("error"); |
583 sink.addStream(new Stream.fromIterable([1, 2, 3, 4, 5])) | 584 sink.addStream(new Stream.fromIterable([1, 2, 3, 4, 5])) |
584 .then((_) { | 585 .then((_) { |
585 sink.add(43); | 586 sink.add(43); |
586 return sink.close(); | 587 return sink.close(); |
587 }) | 588 }) |
588 .then((_) { | 589 .then((_) { |
589 expect(expected.events, equals(actual.events)); | 590 Expect.listEquals(expected.events, actual.events); |
590 done(); | 591 done(); |
591 }); | 592 }); |
592 }); | 593 }); |
593 | 594 |
594 test("$type-controller-sink-canceled", () { | 595 test("$type-controller-sink-canceled", () { |
595 var done = expectAsync((){}); | 596 var done = expectAsync((){}); |
596 var c = broadcast ? new StreamController.broadcast(sync: sync) | 597 var c = broadcast ? new StreamController.broadcast(sync: sync) |
597 : new StreamController(sync: sync); | 598 : new StreamController(sync: sync); |
598 var expected = new Events() | 599 var expected = new Events() |
599 ..add(42)..error("error") | 600 ..add(42)..error("error") |
600 ..add(1)..add(2)..add(3); | 601 ..add(1)..add(2)..add(3); |
601 var stream = asBroadcast ? c.stream.asBroadcastStream() : c.stream; | 602 var stream = asBroadcast ? c.stream.asBroadcastStream() : c.stream; |
602 var actual = new Events(); | 603 var actual = new Events(); |
603 var sub; | 604 var sub; |
604 // Cancel subscription after receiving "3" event. | 605 // Cancel subscription after receiving "3" event. |
605 sub = stream.listen((v) { | 606 sub = stream.listen((v) { |
606 if (v == 3) sub.cancel(); | 607 if (v == 3) sub.cancel(); |
607 actual.add(v); | 608 actual.add(v); |
608 }, onError: actual.error); | 609 }, onError: actual.error); |
609 var sink = c.sink; | 610 var sink = c.sink; |
610 sink.add(42); | 611 sink.add(42); |
611 sink.addError("error"); | 612 sink.addError("error"); |
612 sink.addStream(new Stream.fromIterable([1, 2, 3, 4, 5])) | 613 sink.addStream(new Stream.fromIterable([1, 2, 3, 4, 5])) |
613 .then((_) { | 614 .then((_) { |
614 expect(expected.events, equals(actual.events)); | 615 Expect.listEquals(expected.events, actual.events); |
615 // Close controller as well. It has no listener. If it is a broadcast | 616 // Close controller as well. It has no listener. If it is a broadcast |
616 // stream, it will still be open, and we read the "done" future before | 617 // stream, it will still be open, and we read the "done" future before |
617 // closing. A normal stream is already done when its listener cancels. | 618 // closing. A normal stream is already done when its listener cancels. |
618 Future doneFuture = sink.done; | 619 Future doneFuture = sink.done; |
619 sink.close(); | 620 sink.close(); |
620 return doneFuture; | 621 return doneFuture; |
621 }) | 622 }) |
622 .then((_) { | 623 .then((_) { |
623 // No change in events. | 624 // No change in events. |
624 expect(expected.events, equals(actual.events)); | 625 Expect.listEquals(expected.events, actual.events); |
625 done(); | 626 done(); |
626 }); | 627 }); |
627 }); | 628 }); |
628 | 629 |
629 test("$type-controller-sink-paused", () { | 630 test("$type-controller-sink-paused", () { |
630 var done = expectAsync((){}); | 631 var done = expectAsync((){}); |
631 var c = broadcast ? new StreamController.broadcast(sync: sync) | 632 var c = broadcast ? new StreamController.broadcast(sync: sync) |
632 : new StreamController(sync: sync); | 633 : new StreamController(sync: sync); |
633 var expected = new Events() | 634 var expected = new Events() |
634 ..add(42)..error("error") | 635 ..add(42)..error("error") |
(...skipping 22 matching lines...) Expand all Loading... |
657 return sink.close(); | 658 return sink.close(); |
658 }) | 659 }) |
659 .then((_) { | 660 .then((_) { |
660 if (asBroadcast || broadcast) { | 661 if (asBroadcast || broadcast) { |
661 // The done-future of the sink completes when it passes | 662 // The done-future of the sink completes when it passes |
662 // the done event to the asBroadcastStream controller, which is | 663 // the done event to the asBroadcastStream controller, which is |
663 // before the final listener gets the event. | 664 // before the final listener gets the event. |
664 // Wait for the done event to be *delivered* before testing the | 665 // Wait for the done event to be *delivered* before testing the |
665 // events. | 666 // events. |
666 actual.onDone(() { | 667 actual.onDone(() { |
667 expect(expected.events, equals(actual.events)); | 668 Expect.listEquals(expected.events, actual.events); |
668 done(); | 669 done(); |
669 }); | 670 }); |
670 } else { | 671 } else { |
671 expect(expected.events, equals(actual.events)); | 672 Expect.listEquals(expected.events, actual.events); |
672 done(); | 673 done(); |
673 } | 674 } |
674 }); | 675 }); |
675 }); | 676 }); |
676 | 677 |
677 test("$type-controller-addstream-error-stop", () { | 678 test("$type-controller-addstream-error-stop", () { |
678 // Check that addStream defaults to ending after the first error. | 679 // Check that addStream defaults to ending after the first error. |
679 var done = expectAsync((){}); | 680 var done = expectAsync((){}); |
680 StreamController c = broadcast | 681 StreamController c = broadcast |
681 ? new StreamController.broadcast(sync: sync) | 682 ? new StreamController.broadcast(sync: sync) |
682 : new StreamController(sync: sync); | 683 : new StreamController(sync: sync); |
683 Stream stream = asBroadcast ? c.stream.asBroadcastStream() : c.stream; | 684 Stream stream = asBroadcast ? c.stream.asBroadcastStream() : c.stream; |
684 var actual = new Events.capture(stream); | 685 var actual = new Events.capture(stream); |
685 | 686 |
686 var source = new Events(); | 687 var source = new Events(); |
687 source..add(1)..add(2)..error("BAD")..add(3)..error("FAIL")..close(); | 688 source..add(1)..add(2)..error("BAD")..add(3)..error("FAIL")..close(); |
688 | 689 |
689 var expected = new Events()..add(1)..add(2)..error("BAD")..close(); | 690 var expected = new Events()..add(1)..add(2)..error("BAD")..close(); |
690 StreamController sourceController = new StreamController(); | 691 StreamController sourceController = new StreamController(); |
691 c.addStream(sourceController.stream).then((_) { | 692 c.addStream(sourceController.stream).then((_) { |
692 c.close().then((_) { | 693 c.close().then((_) { |
693 expect(expected.events, equals(actual.events)); | 694 Expect.listEquals(expected.events, actual.events); |
694 done(); | 695 done(); |
695 }); | 696 }); |
696 }); | 697 }); |
697 | 698 |
698 source.replay(sourceController); | 699 source.replay(sourceController); |
699 }); | 700 }); |
700 | 701 |
701 test("$type-controller-addstream-error-forward", () { | 702 test("$type-controller-addstream-error-forward", () { |
702 // Check that addStream with cancelOnError:false passes all data and errors | 703 // Check that addStream with cancelOnError:false passes all data and errors |
703 // to the controller. | 704 // to the controller. |
704 var done = expectAsync((){}); | 705 var done = expectAsync((){}); |
705 StreamController c = broadcast | 706 StreamController c = broadcast |
706 ? new StreamController.broadcast(sync: sync) | 707 ? new StreamController.broadcast(sync: sync) |
707 : new StreamController(sync: sync); | 708 : new StreamController(sync: sync); |
708 Stream stream = asBroadcast ? c.stream.asBroadcastStream() : c.stream; | 709 Stream stream = asBroadcast ? c.stream.asBroadcastStream() : c.stream; |
709 var actual = new Events.capture(stream); | 710 var actual = new Events.capture(stream); |
710 | 711 |
711 var source = new Events(); | 712 var source = new Events(); |
712 source..add(1)..add(2)..addError("BAD")..add(3)..addError("FAIL")..close(); | 713 source..add(1)..add(2)..addError("BAD")..add(3)..addError("FAIL")..close(); |
713 | 714 |
714 StreamController sourceController = new StreamController(); | 715 StreamController sourceController = new StreamController(); |
715 c.addStream(sourceController.stream, cancelOnError: false).then((_) { | 716 c.addStream(sourceController.stream, cancelOnError: false).then((_) { |
716 c.close().then((_) { | 717 c.close().then((_) { |
717 expect(source.events, equals(actual.events)); | 718 Expect.listEquals(source.events, actual.events); |
718 done(); | 719 done(); |
719 }); | 720 }); |
720 }); | 721 }); |
721 | 722 |
722 source.replay(sourceController); | 723 source.replay(sourceController); |
723 }); | 724 }); |
724 | 725 |
725 test("$type-controller-addstream-twice", () { | 726 test("$type-controller-addstream-twice", () { |
726 // Using addStream twice on the same stream | 727 // Using addStream twice on the same stream |
727 var done = expectAsync((){}); | 728 var done = expectAsync((){}); |
(...skipping 10 matching lines...) Expand all Loading... |
738 .map((x) => (x == 3 ? throw x : x)); | 739 .map((x) => (x == 3 ? throw x : x)); |
739 | 740 |
740 Events expected = new Events(); | 741 Events expected = new Events(); |
741 expected..add(1)..add(2)..error(3); | 742 expected..add(1)..add(2)..error(3); |
742 expected..add(1)..add(2)..error(3)..add(4)..add(5); | 743 expected..add(1)..add(2)..error(3)..add(4)..add(5); |
743 expected..close(); | 744 expected..close(); |
744 | 745 |
745 c.addStream(s1).then((_) { | 746 c.addStream(s1).then((_) { |
746 c.addStream(s2, cancelOnError: false).then((_) { | 747 c.addStream(s2, cancelOnError: false).then((_) { |
747 c.close().then((_) { | 748 c.close().then((_) { |
748 expect(expected.events, equals(actual.events)); | 749 Expect.listEquals(expected.events, actual.events); |
749 done(); | 750 done(); |
750 }); | 751 }); |
751 }); | 752 }); |
752 }); | 753 }); |
753 }); | 754 }); |
754 } | 755 } |
755 | 756 |
756 main() { | 757 main() { |
757 testController(); | 758 testController(); |
758 testSingleController(); | 759 testSingleController(); |
759 testExtraMethods(); | 760 testExtraMethods(); |
760 testPause(); | 761 testPause(); |
761 testRethrow(); | 762 testRethrow(); |
762 testBroadcastController(); | 763 testBroadcastController(); |
763 testAsBroadcast(); | 764 testAsBroadcast(); |
764 testSink(sync: true, broadcast: false, asBroadcast: false); | 765 testSink(sync: true, broadcast: false, asBroadcast: false); |
765 testSink(sync: true, broadcast: false, asBroadcast: true); | 766 testSink(sync: true, broadcast: false, asBroadcast: true); |
766 testSink(sync: true, broadcast: true, asBroadcast: false); | 767 testSink(sync: true, broadcast: true, asBroadcast: false); |
767 testSink(sync: false, broadcast: false, asBroadcast: false); | 768 testSink(sync: false, broadcast: false, asBroadcast: false); |
768 testSink(sync: false, broadcast: false, asBroadcast: true); | 769 testSink(sync: false, broadcast: false, asBroadcast: true); |
769 testSink(sync: false, broadcast: true, asBroadcast: false); | 770 testSink(sync: false, broadcast: true, asBroadcast: false); |
770 } | 771 } |
OLD | NEW |