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