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.singleSubscription. | 5 // Test the basic StreamController and StreamController.singleSubscription. |
6 library stream_controller_test; | 6 library stream_controller_test; |
7 | 7 |
8 import "package:expect/expect.dart"; | 8 import "package:expect/expect.dart"; |
9 import "package:async_helper/async_helper.dart"; | 9 import "package:async_helper/async_helper.dart"; |
10 import 'dart:async'; | 10 import 'dart:async'; |
11 import 'event_helper.dart'; | 11 import 'event_helper.dart'; |
12 | 12 |
13 const MS = const Duration(milliseconds: 1); | 13 const MS = const Duration(milliseconds: 1); |
14 | 14 |
15 fail(e) { Expect.fail("Unexepected error: $e"); } | 15 fail(e) { Expect.fail("Unexepected error: $e"); } |
16 | 16 |
17 void testMultiController() { | 17 void testMultiController() { |
18 // Test normal flow. | 18 // Test normal flow. |
19 var c = new StreamController(sync: true); | 19 { |
20 Events expectedEvents = new Events() | 20 var c = new StreamController(sync: true); |
21 ..add(42) | 21 Events expectedEvents = new Events() |
22 ..add("dibs") | 22 ..add(42) |
23 ..error("error!") | 23 ..add("dibs") |
24 ..error("error too!") | 24 ..error("error!") |
25 ..close(); | 25 ..error("error too!") |
26 CaptureEvents actualEvents = new Events.capture(c.stream.asBroadcastStream()); | 26 ..close(); |
27 expectedEvents.replay(c); | 27 CaptureEvents actualEvents = |
28 Expect.listEquals(expectedEvents.events, actualEvents.events); | 28 new Events.capture(c.stream.asBroadcastStream()); |
| 29 expectedEvents.replay(c); |
| 30 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 31 } |
29 | 32 |
30 // Test automatic unsubscription on error. | 33 // Test automatic unsubscription on error. |
31 c = new StreamController(sync: true); | 34 { |
32 expectedEvents = new Events()..add(42)..error("error"); | 35 var c = new StreamController(sync: true); |
33 actualEvents = new Events.capture(c.stream.asBroadcastStream(), | 36 var expectedEvents = new Events()..add(42)..error("error"); |
34 cancelOnError: true); | 37 var actualEvents = new Events.capture(c.stream.asBroadcastStream(), |
35 Events sentEvents = | 38 cancelOnError: true); |
36 new Events()..add(42)..error("error")..add("Are you there?"); | 39 Events sentEvents = |
37 sentEvents.replay(c); | 40 new Events()..add(42)..error("error")..add("Are you there?"); |
38 Expect.listEquals(expectedEvents.events, actualEvents.events); | 41 sentEvents.replay(c); |
| 42 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 43 } |
39 | 44 |
40 // Test manual unsubscription. | 45 // Test manual unsubscription. |
41 c = new StreamController(sync: true); | 46 { |
42 expectedEvents = new Events()..add(42)..error("error")..add(37); | 47 var c = new StreamController(sync: true); |
43 actualEvents = new Events.capture(c.stream.asBroadcastStream(), | 48 var expectedEvents = new Events()..add(42)..error("error")..add(37); |
44 cancelOnError: false); | 49 var actualEvents = new Events.capture(c.stream.asBroadcastStream(), |
45 expectedEvents.replay(c); | 50 cancelOnError: false); |
46 actualEvents.subscription.cancel(); | 51 expectedEvents.replay(c); |
47 c.add("Are you there"); // Not sent to actualEvents. | 52 actualEvents.subscription.cancel(); |
48 Expect.listEquals(expectedEvents.events, actualEvents.events); | 53 c.add("Are you there"); // Not sent to actualEvents. |
| 54 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 55 } |
49 | 56 |
50 // Test filter. | 57 // Test filter. |
51 c = new StreamController(sync: true); | 58 { |
52 expectedEvents = new Events() | 59 var c = new StreamController(sync: true); |
53 ..add("a string")..add("another string")..close(); | 60 var expectedEvents = new Events() |
54 sentEvents = new Events() | 61 ..add("a string")..add("another string")..close(); |
55 ..add("a string")..add(42)..add("another string")..close(); | 62 var sentEvents = new Events() |
56 actualEvents = new Events.capture(c.stream | 63 ..add("a string")..add(42)..add("another string")..close(); |
57 .asBroadcastStream() | 64 var actualEvents = new Events.capture(c.stream |
58 .where((v) => v is String)); | 65 .asBroadcastStream() |
59 sentEvents.replay(c); | 66 .where((v) => v is String)); |
60 Expect.listEquals(expectedEvents.events, actualEvents.events); | 67 sentEvents.replay(c); |
| 68 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 69 } |
61 | 70 |
62 // Test map. | 71 // Test map. |
63 c = new StreamController(sync: true); | 72 { |
64 expectedEvents = new Events()..add("abab")..error("error")..close(); | 73 var c = new StreamController(sync: true); |
65 sentEvents = new Events()..add("ab")..error("error")..close(); | 74 var expectedEvents = new Events()..add("abab")..error("error")..close(); |
66 actualEvents = new Events.capture(c.stream | 75 var sentEvents = new Events()..add("ab")..error("error")..close(); |
67 .asBroadcastStream() | 76 var actualEvents = new Events.capture(c.stream |
68 .map((v) => "$v$v")); | 77 .asBroadcastStream() |
69 sentEvents.replay(c); | 78 .map((v) => "$v$v")); |
70 Expect.listEquals(expectedEvents.events, actualEvents.events); | 79 sentEvents.replay(c); |
| 80 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 81 } |
71 | 82 |
72 // Test handleError. | 83 // Test handleError. |
73 c = new StreamController(sync: true); | 84 { |
74 expectedEvents = new Events()..add("ab")..error("[foo]"); | 85 var c = new StreamController(sync: true); |
75 sentEvents = new Events()..add("ab")..error("foo")..add("ab")..close(); | 86 var expectedEvents = new Events()..add("ab")..error("[foo]"); |
76 actualEvents = new Events.capture(c.stream | 87 var sentEvents = new Events()..add("ab")..error("foo")..add("ab")..close(); |
77 .asBroadcastStream() | 88 var actualEvents = new Events.capture(c.stream |
78 .handleError((error) { | 89 .asBroadcastStream() |
79 if (error is String) { | 90 .handleError((error) { |
80 // TODO(floitsch): this test originally changed the stacktrace. | 91 if (error is String) { |
81 throw "[${error}]"; | 92 // TODO(floitsch): this test originally changed the stacktrace. |
82 } | 93 throw "[${error}]"; |
83 }), cancelOnError: true); | 94 } |
84 sentEvents.replay(c); | 95 }), cancelOnError: true); |
85 Expect.listEquals(expectedEvents.events, actualEvents.events); | 96 sentEvents.replay(c); |
| 97 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 98 } |
86 | 99 |
87 // reduce is tested asynchronously and therefore not in this file. | 100 // reduce is tested asynchronously and therefore not in this file. |
88 | 101 |
89 // Test expand | 102 // Test expand |
90 c = new StreamController(sync: true); | 103 { |
91 sentEvents = new Events()..add(3)..add(2)..add(4)..close(); | 104 var c = new StreamController(sync: true); |
92 expectedEvents = new Events()..add(1)..add(2)..add(3) | 105 var sentEvents = new Events()..add(3)..add(2)..add(4)..close(); |
93 ..add(1)..add(2) | 106 var expectedEvents = new Events()..add(1)..add(2)..add(3) |
94 ..add(1)..add(2)..add(3)..add(4) | 107 ..add(1)..add(2) |
95 ..close(); | 108 ..add(1)..add(2)..add(3)..add(4) |
96 actualEvents = new Events.capture(c.stream.asBroadcastStream().expand((v) { | 109 ..close(); |
97 var l = []; | 110 var actualEvents = |
98 for (int i = 0; i < v; i++) l.add(i + 1); | 111 new Events.capture(c.stream.asBroadcastStream().expand((v) { |
99 return l; | 112 var l = []; |
100 })); | 113 for (int i = 0; i < v; i++) l.add(i + 1); |
101 sentEvents.replay(c); | 114 return l; |
102 Expect.listEquals(expectedEvents.events, actualEvents.events); | 115 })); |
| 116 sentEvents.replay(c); |
| 117 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 118 } |
103 | 119 |
104 // Test transform. | 120 // Test transform. |
105 c = new StreamController(sync: true); | 121 { |
106 sentEvents = new Events()..add("a")..error(42)..add("b")..close(); | 122 var c = new StreamController(sync: true); |
107 expectedEvents = | 123 var sentEvents = new Events()..add("a")..error(42)..add("b")..close(); |
108 new Events()..error("a")..add(42)..error("b")..add("foo")..close(); | 124 var expectedEvents = |
109 actualEvents = new Events.capture(c.stream.asBroadcastStream().transform( | 125 new Events()..error("a")..add(42)..error("b")..add("foo")..close(); |
110 new StreamTransformer.fromHandlers( | 126 var actualEvents = |
111 handleData: (v, s) { s.addError(v); }, | 127 new Events.capture(c.stream.asBroadcastStream().transform( |
112 handleError: (e, st, s) { s.add(e); }, | 128 new StreamTransformer.fromHandlers( |
113 handleDone: (s) { | 129 handleData: (v, s) { s.addError(v); }, |
114 s.add("foo"); | 130 handleError: (e, st, s) { s.add(e); }, |
115 s.close(); | 131 handleDone: (s) { |
116 }))); | 132 s.add("foo"); |
117 sentEvents.replay(c); | 133 s.close(); |
118 Expect.listEquals(expectedEvents.events, actualEvents.events); | 134 }))); |
| 135 sentEvents.replay(c); |
| 136 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 137 } |
119 | 138 |
120 // Test multiple filters. | 139 // Test multiple filters. |
121 c = new StreamController(sync: true); | 140 { |
122 sentEvents = new Events()..add(42) | 141 var c = new StreamController(sync: true); |
123 ..add("snugglefluffy") | 142 var sentEvents = new Events()..add(42) |
124 ..add(7) | 143 ..add("snugglefluffy") |
125 ..add("42") | 144 ..add(7) |
126 ..error("not FormatException") // Unsubscribes. | 145 ..add("42") |
127 ..close(); | 146 ..error("not FormatException") // Unsubscribes. |
128 expectedEvents = new Events()..add(42)..error("not FormatException"); | 147 ..close(); |
129 actualEvents = new Events.capture( | 148 var expectedEvents = new Events()..add(42)..error("not FormatException"); |
130 c.stream.asBroadcastStream().where((v) => v is String) | 149 var actualEvents = new Events.capture( |
131 .map((v) => int.parse(v)) | 150 c.stream.asBroadcastStream().where((v) => v is String) |
132 .handleError((error) { | 151 .map((v) => int.parse(v)) |
133 if (error is! FormatException) throw error; | 152 .handleError((error) { |
134 }) | 153 if (error is! FormatException) throw error; |
135 .where((v) => v > 10), | 154 }) |
136 cancelOnError: true); | 155 .where((v) => v > 10), |
137 sentEvents.replay(c); | 156 cancelOnError: true); |
138 Expect.listEquals(expectedEvents.events, actualEvents.events); | 157 sentEvents.replay(c); |
| 158 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 159 } |
139 | 160 |
140 // Test subscription changes while firing. | 161 // Test subscription changes while firing. |
141 c = new StreamController(sync: true); | 162 { |
142 var sink = c.sink; | 163 var c = new StreamController(sync: true); |
143 var stream = c.stream.asBroadcastStream(); | 164 var sink = c.sink; |
144 var counter = 0; | 165 var stream = c.stream.asBroadcastStream(); |
145 var subscription = stream.listen(null); | 166 var counter = 0; |
146 subscription.onData((data) { | 167 var subscription = stream.listen(null); |
147 counter += data; | 168 subscription.onData((data) { |
148 subscription.cancel(); | 169 counter += data; |
149 stream.listen((data) { | 170 subscription.cancel(); |
150 counter += 10 * data; | 171 stream.listen((data) { |
| 172 counter += 10 * data; |
| 173 }); |
| 174 var subscription2 = stream.listen(null); |
| 175 subscription2.onData((data) { |
| 176 counter += 100 * data; |
| 177 if (data == 4) subscription2.cancel(); |
| 178 }); |
151 }); | 179 }); |
152 var subscription2 = stream.listen(null); | 180 sink.add(1); // seen by stream 1 |
153 subscription2.onData((data) { | 181 sink.add(2); // seen by stream 10 and 100 |
154 counter += 100 * data; | 182 sink.add(3); // -"- |
155 if (data == 4) subscription2.cancel(); | 183 sink.add(4); // -"- |
156 }); | 184 sink.add(5); // seen by stream 10 |
157 }); | 185 Expect.equals(1 + 20 + 200 + 30 + 300 + 40 + 400 + 50, counter); |
158 sink.add(1); // seen by stream 1 | 186 } |
159 sink.add(2); // seen by stream 10 and 100 | |
160 sink.add(3); // -"- | |
161 sink.add(4); // -"- | |
162 sink.add(5); // seen by stream 10 | |
163 Expect.equals(1 + 20 + 200 + 30 + 300 + 40 + 400 + 50, counter); | |
164 } | 187 } |
165 | 188 |
166 testSingleController() { | 189 testSingleController() { |
167 // Test normal flow. | 190 // Test normal flow. |
168 var c = new StreamController(sync: true); | 191 { |
169 Events expectedEvents = new Events() | 192 var c = new StreamController(sync: true); |
170 ..add(42) | 193 Events expectedEvents = new Events() |
171 ..add("dibs") | 194 ..add(42) |
172 ..error("error!") | 195 ..add("dibs") |
173 ..error("error too!") | 196 ..error("error!") |
174 ..close(); | 197 ..error("error too!") |
175 CaptureEvents actualEvents = new Events.capture(c.stream); | 198 ..close(); |
176 expectedEvents.replay(c); | 199 CaptureEvents actualEvents = new Events.capture(c.stream); |
177 Expect.listEquals(expectedEvents.events, actualEvents.events); | 200 expectedEvents.replay(c); |
| 201 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 202 } |
178 | 203 |
179 // Test automatic unsubscription on error. | 204 // Test automatic unsubscription on error. |
180 c = new StreamController(sync: true); | 205 { |
181 expectedEvents = new Events()..add(42)..error("error"); | 206 var c = new StreamController(sync: true); |
182 actualEvents = new Events.capture(c.stream, cancelOnError: true); | 207 var expectedEvents = new Events()..add(42)..error("error"); |
183 Events sentEvents = | 208 var actualEvents = new Events.capture(c.stream, cancelOnError: true); |
184 new Events()..add(42)..error("error")..add("Are you there?"); | 209 Events sentEvents = |
185 sentEvents.replay(c); | 210 new Events()..add(42)..error("error")..add("Are you there?"); |
186 Expect.listEquals(expectedEvents.events, actualEvents.events); | 211 sentEvents.replay(c); |
| 212 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 213 } |
187 | 214 |
188 // Test manual unsubscription. | 215 // Test manual unsubscription. |
189 c = new StreamController(sync: true); | 216 { |
190 expectedEvents = new Events()..add(42)..error("error")..add(37); | 217 var c = new StreamController(sync: true); |
191 actualEvents = new Events.capture(c.stream, cancelOnError: false); | 218 var expectedEvents = new Events()..add(42)..error("error")..add(37); |
192 expectedEvents.replay(c); | 219 var actualEvents = new Events.capture(c.stream, cancelOnError: false); |
193 actualEvents.subscription.cancel(); | 220 expectedEvents.replay(c); |
194 c.add("Are you there"); // Not sent to actualEvents. | 221 actualEvents.subscription.cancel(); |
195 Expect.listEquals(expectedEvents.events, actualEvents.events); | 222 c.add("Are you there"); // Not sent to actualEvents. |
| 223 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 224 } |
196 | 225 |
197 // Test filter. | 226 // Test filter. |
198 c = new StreamController(sync: true); | 227 { |
199 expectedEvents = new Events() | 228 var c = new StreamController(sync: true); |
200 ..add("a string")..add("another string")..close(); | 229 var expectedEvents = new Events() |
201 sentEvents = new Events() | 230 ..add("a string")..add("another string")..close(); |
202 ..add("a string")..add(42)..add("another string")..close(); | 231 var sentEvents = new Events() |
203 actualEvents = new Events.capture(c.stream.where((v) => v is String)); | 232 ..add("a string")..add(42)..add("another string")..close(); |
204 sentEvents.replay(c); | 233 var actualEvents = new Events.capture(c.stream.where((v) => v is String)); |
205 Expect.listEquals(expectedEvents.events, actualEvents.events); | 234 sentEvents.replay(c); |
| 235 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 236 } |
206 | 237 |
207 // Test map. | 238 // Test map. |
208 c = new StreamController(sync: true); | 239 { |
209 expectedEvents = new Events()..add("abab")..error("error")..close(); | 240 var c = new StreamController(sync: true); |
210 sentEvents = new Events()..add("ab")..error("error")..close(); | 241 var expectedEvents = new Events()..add("abab")..error("error")..close(); |
211 actualEvents = new Events.capture(c.stream.map((v) => "$v$v")); | 242 var sentEvents = new Events()..add("ab")..error("error")..close(); |
212 sentEvents.replay(c); | 243 var actualEvents = new Events.capture(c.stream.map((v) => "$v$v")); |
213 Expect.listEquals(expectedEvents.events, actualEvents.events); | 244 sentEvents.replay(c); |
| 245 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 246 } |
214 | 247 |
215 // Test handleError. | 248 // Test handleError. |
216 c = new StreamController(sync: true); | 249 { |
217 expectedEvents = new Events()..add("ab")..error("[foo]"); | 250 var c = new StreamController(sync: true); |
218 sentEvents = new Events()..add("ab")..error("foo")..add("ab")..close(); | 251 var expectedEvents = new Events()..add("ab")..error("[foo]"); |
219 actualEvents = new Events.capture(c.stream.handleError((error) { | 252 var sentEvents = new Events()..add("ab")..error("foo")..add("ab")..close(); |
220 if (error is String) { | 253 var actualEvents = new Events.capture(c.stream.handleError((error) { |
221 // TODO(floitsch): this error originally changed the stack trace. | 254 if (error is String) { |
222 throw "[${error}]"; | 255 // TODO(floitsch): this error originally changed the stack trace. |
223 } | 256 throw "[${error}]"; |
224 }), cancelOnError: true); | 257 } |
225 sentEvents.replay(c); | 258 }), cancelOnError: true); |
226 Expect.listEquals(expectedEvents.events, actualEvents.events); | 259 sentEvents.replay(c); |
| 260 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 261 } |
227 | 262 |
228 // reduce is tested asynchronously and therefore not in this file. | 263 // reduce is tested asynchronously and therefore not in this file. |
229 | 264 |
230 // Test expand | 265 // Test expand |
231 c = new StreamController(sync: true); | 266 { |
232 sentEvents = new Events()..add(3)..add(2)..add(4)..close(); | 267 var c = new StreamController(sync: true); |
233 expectedEvents = new Events()..add(1)..add(2)..add(3) | 268 var sentEvents = new Events()..add(3)..add(2)..add(4)..close(); |
234 ..add(1)..add(2) | 269 var expectedEvents = new Events()..add(1)..add(2)..add(3) |
235 ..add(1)..add(2)..add(3)..add(4) | 270 ..add(1)..add(2) |
236 ..close(); | 271 ..add(1)..add(2)..add(3)..add(4) |
237 actualEvents = new Events.capture(c.stream.expand((v) { | 272 ..close(); |
238 var l = []; | 273 var actualEvents = new Events.capture(c.stream.expand((v) { |
239 for (int i = 0; i < v; i++) l.add(i + 1); | 274 var l = []; |
240 return l; | 275 for (int i = 0; i < v; i++) l.add(i + 1); |
241 })); | 276 return l; |
242 sentEvents.replay(c); | 277 })); |
243 Expect.listEquals(expectedEvents.events, actualEvents.events); | 278 sentEvents.replay(c); |
| 279 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 280 } |
244 | 281 |
245 // test contains. | 282 // test contains. |
246 { | 283 { |
247 c = new StreamController(sync: true); | 284 var c = new StreamController(sync: true); |
248 // Error after match is not important. | 285 // Error after match is not important. |
249 sentEvents = new Events()..add("a")..add("x")..error("FAIL")..close(); | 286 var sentEvents = new Events()..add("a")..add("x")..error("FAIL")..close(); |
250 Future<bool> contains = c.stream.contains("x"); | 287 Future<bool> contains = c.stream.contains("x"); |
251 contains.then((var c) { | 288 contains.then((var c) { |
252 Expect.isTrue(c); | 289 Expect.isTrue(c); |
253 }); | 290 }); |
254 sentEvents.replay(c); | 291 sentEvents.replay(c); |
255 } | 292 } |
256 | 293 |
257 { | 294 { |
258 c = new StreamController(sync: true); | 295 var c = new StreamController(sync: true); |
259 // Not matching is ok. | 296 // Not matching is ok. |
260 sentEvents = new Events()..add("a")..add("x")..add("b")..close(); | 297 var sentEvents = new Events()..add("a")..add("x")..add("b")..close(); |
261 Future<bool> contains = c.stream.contains("y"); | 298 Future<bool> contains = c.stream.contains("y"); |
262 contains.then((var c) { | 299 contains.then((var c) { |
263 Expect.isFalse(c); | 300 Expect.isFalse(c); |
264 }); | 301 }); |
265 sentEvents.replay(c); | 302 sentEvents.replay(c); |
266 } | 303 } |
267 | 304 |
268 { | 305 { |
269 c = new StreamController(sync: true); | 306 var c = new StreamController(sync: true); |
270 // Error before match makes future err. | 307 // Error before match makes future err. |
271 sentEvents = new Events()..add("a")..error("FAIL")..add("b")..close(); | 308 var sentEvents = new Events()..add("a")..error("FAIL")..add("b")..close(); |
272 Future<bool> contains = c.stream.contains("b"); | 309 Future<bool> contains = c.stream.contains("b"); |
273 contains.then((var c) { | 310 contains.then((var c) { |
274 Expect.fail("no value expected"); | 311 Expect.fail("no value expected"); |
275 }).catchError((error) { | 312 }).catchError((error) { |
276 Expect.equals("FAIL", error); | 313 Expect.equals("FAIL", error); |
277 }); | 314 }); |
278 sentEvents.replay(c); | 315 sentEvents.replay(c); |
279 } | 316 } |
280 | 317 |
281 // Test transform. | 318 // Test transform. |
282 c = new StreamController(sync: true); | 319 { |
283 sentEvents = new Events()..add("a")..error(42)..add("b")..close(); | 320 var c = new StreamController(sync: true); |
284 expectedEvents = | 321 var sentEvents = new Events()..add("a")..error(42)..add("b")..close(); |
285 new Events()..error("a")..add(42)..error("b")..add("foo")..close(); | 322 var expectedEvents = |
286 actualEvents = new Events.capture(c.stream.transform( | 323 new Events()..error("a")..add(42)..error("b")..add("foo")..close(); |
287 new StreamTransformer.fromHandlers( | 324 var actualEvents = new Events.capture(c.stream.transform( |
288 handleData: (v, s) { s.addError(v); }, | 325 new StreamTransformer.fromHandlers( |
289 handleError: (e, st, s) { s.add(e); }, | 326 handleData: (v, s) { s.addError(v); }, |
290 handleDone: (s) { | 327 handleError: (e, st, s) { s.add(e); }, |
291 s.add("foo"); | 328 handleDone: (s) { |
292 s.close(); | 329 s.add("foo"); |
293 }))); | 330 s.close(); |
294 sentEvents.replay(c); | 331 }))); |
295 Expect.listEquals(expectedEvents.events, actualEvents.events); | 332 sentEvents.replay(c); |
| 333 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 334 } |
296 | 335 |
297 // Test multiple filters. | 336 // Test multiple filters. |
298 c = new StreamController(sync: true); | 337 { |
299 sentEvents = new Events()..add(42) | 338 var c = new StreamController(sync: true); |
300 ..add("snugglefluffy") | 339 var sentEvents = new Events()..add(42) |
301 ..add(7) | 340 ..add("snugglefluffy") |
302 ..add("42") | 341 ..add(7) |
303 ..error("not FormatException") // Unsubscribes. | 342 ..add("42") |
304 ..close(); | 343 ..error("not FormatException") // Unsubscribes. |
305 expectedEvents = new Events()..add(42)..error("not FormatException"); | 344 ..close(); |
306 actualEvents = new Events.capture( | 345 var expectedEvents = new Events()..add(42)..error("not FormatException"); |
307 c.stream.where((v) => v is String) | 346 var actualEvents = new Events.capture( |
308 .map((v) => int.parse(v)) | 347 c.stream.where((v) => v is String) |
309 .handleError((error) { | 348 .map((v) => int.parse(v)) |
310 if (error is! FormatException) throw error; | 349 .handleError((error) { |
311 }) | 350 if (error is! FormatException) throw error; |
312 .where((v) => v > 10), | 351 }) |
313 cancelOnError: true); | 352 .where((v) => v > 10), |
314 sentEvents.replay(c); | 353 cancelOnError: true); |
315 Expect.listEquals(expectedEvents.events, actualEvents.events); | 354 sentEvents.replay(c); |
| 355 Expect.listEquals(expectedEvents.events, actualEvents.events); |
| 356 } |
316 | 357 |
317 // Test that only one subscription is allowed. | 358 // Test that only one subscription is allowed. |
318 c = new StreamController(sync: true); | 359 { |
319 var sink = c.sink; | 360 var c = new StreamController(sync: true); |
320 var stream = c.stream; | 361 var sink = c.sink; |
321 var counter = 0; | 362 var stream = c.stream; |
322 var subscription = stream.listen((data) { counter += data; }); | 363 var counter = 0; |
323 Expect.throws(() => stream.listen(null), (e) => e is StateError); | 364 var subscription = stream.listen((data) { counter += data; }); |
324 sink.add(1); | 365 Expect.throws(() => stream.listen(null), (e) => e is StateError); |
325 Expect.equals(1, counter); | 366 sink.add(1); |
326 c.close(); | 367 Expect.equals(1, counter); |
| 368 c.close(); |
| 369 } |
327 } | 370 } |
328 | 371 |
329 testExtraMethods() { | 372 testExtraMethods() { |
330 Events sentEvents = new Events()..add(1)..add(2)..add(3)..close(); | 373 Events sentEvents = new Events()..add(1)..add(2)..add(3)..close(); |
331 | 374 |
332 var c = new StreamController(sync: true); | 375 var c = new StreamController(sync: true); |
333 Events expectedEvents = new Events()..add(3)..close(); | 376 Events expectedEvents = new Events()..add(3)..close(); |
334 Events actualEvents = new Events.capture(c.stream.skip(2)); | 377 Events actualEvents = new Events.capture(c.stream.skip(2)); |
335 sentEvents.replay(c); | 378 sentEvents.replay(c); |
336 Expect.listEquals(expectedEvents.events, actualEvents.events); | 379 Expect.listEquals(expectedEvents.events, actualEvents.events); |
(...skipping 570 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
907 testAsBroadcastListenAfterClose(); | 950 testAsBroadcastListenAfterClose(); |
908 testAsBroadcastListenAfterClosePaused(); | 951 testAsBroadcastListenAfterClosePaused(); |
909 testEventInListen(); | 952 testEventInListen(); |
910 testSyncControllerNotReentrant(); | 953 testSyncControllerNotReentrant(); |
911 testSettingCallbacks(); | 954 testSettingCallbacks(); |
912 testSettingNullCallbacks(); | 955 testSettingNullCallbacks(); |
913 testBroadcastSettingCallbacks(); | 956 testBroadcastSettingCallbacks(); |
914 testBroadcastSettingNullCallbacks(); | 957 testBroadcastSettingNullCallbacks(); |
915 asyncEnd(); | 958 asyncEnd(); |
916 } | 959 } |
OLD | NEW |