Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(173)

Side by Side Diff: tests/lib/async/stream_controller_test.dart

Issue 11887016: Make StreamController's unnamed constructor create a single-sub stream. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 'dart:async'; 8 import 'dart:async';
9 import 'event_helper.dart'; 9 import 'event_helper.dart';
10 10
11 testController() { 11 testMultiController() {
12 // Test normal flow. 12 // Test normal flow.
13 var c = new StreamController(); 13 var c = new StreamController.multiSubscription();
14 Events expectedEvents = new Events() 14 Events expectedEvents = new Events()
15 ..add(42) 15 ..add(42)
16 ..add("dibs") 16 ..add("dibs")
17 ..error("error!") 17 ..error("error!")
18 ..error("error too!") 18 ..error("error too!")
19 ..close(); 19 ..close();
20 Events actualEvents = new Events.capture(c); 20 Events actualEvents = new Events.capture(c);
21 expectedEvents.replay(c); 21 expectedEvents.replay(c);
22 Expect.listEquals(expectedEvents.events, actualEvents.events); 22 Expect.listEquals(expectedEvents.events, actualEvents.events);
23 23
24 // Test automatic unsubscription on error. 24 // Test automatic unsubscription on error.
25 c = new StreamController(); 25 c = new StreamController.multiSubscription();
26 expectedEvents = new Events()..add(42)..error("error"); 26 expectedEvents = new Events()..add(42)..error("error");
27 actualEvents = new Events.capture(c, unsubscribeOnError: true); 27 actualEvents = new Events.capture(c, unsubscribeOnError: true);
28 Events sentEvents = 28 Events sentEvents =
29 new Events()..add(42)..error("error")..add("Are you there?"); 29 new Events()..add(42)..error("error")..add("Are you there?");
30 sentEvents.replay(c); 30 sentEvents.replay(c);
31 Expect.listEquals(expectedEvents.events, actualEvents.events); 31 Expect.listEquals(expectedEvents.events, actualEvents.events);
32 32
33 // Test manual unsubscription. 33 // Test manual unsubscription.
34 c = new StreamController(); 34 c = new StreamController.multiSubscription();
35 expectedEvents = new Events()..add(42)..error("error")..add(37); 35 expectedEvents = new Events()..add(42)..error("error")..add(37);
36 actualEvents = new Events.capture(c, unsubscribeOnError: false); 36 actualEvents = new Events.capture(c, unsubscribeOnError: false);
37 expectedEvents.replay(c); 37 expectedEvents.replay(c);
38 actualEvents.subscription.cancel(); 38 actualEvents.subscription.cancel();
39 c.add("Are you there"); // Not sent to actualEvents. 39 c.add("Are you there"); // Not sent to actualEvents.
40 Expect.listEquals(expectedEvents.events, actualEvents.events); 40 Expect.listEquals(expectedEvents.events, actualEvents.events);
41 41
42 // Test filter. 42 // Test filter.
43 c = new StreamController(); 43 c = new StreamController.multiSubscription();
44 expectedEvents = new Events() 44 expectedEvents = new Events()
45 ..add("a string")..add("another string")..close(); 45 ..add("a string")..add("another string")..close();
46 sentEvents = new Events() 46 sentEvents = new Events()
47 ..add("a string")..add(42)..add("another string")..close(); 47 ..add("a string")..add(42)..add("another string")..close();
48 actualEvents = new Events.capture(c.where((v) => v is String)); 48 actualEvents = new Events.capture(c.where((v) => v is String));
49 sentEvents.replay(c); 49 sentEvents.replay(c);
50 Expect.listEquals(expectedEvents.events, actualEvents.events); 50 Expect.listEquals(expectedEvents.events, actualEvents.events);
51 51
52 // Test map. 52 // Test map.
53 c = new StreamController(); 53 c = new StreamController.multiSubscription();
54 expectedEvents = new Events()..add("abab")..error("error")..close(); 54 expectedEvents = new Events()..add("abab")..error("error")..close();
55 sentEvents = new Events()..add("ab")..error("error")..close(); 55 sentEvents = new Events()..add("ab")..error("error")..close();
56 actualEvents = new Events.capture(c.mappedBy((v) => "$v$v")); 56 actualEvents = new Events.capture(c.mappedBy((v) => "$v$v"));
57 sentEvents.replay(c); 57 sentEvents.replay(c);
58 Expect.listEquals(expectedEvents.events, actualEvents.events); 58 Expect.listEquals(expectedEvents.events, actualEvents.events);
59 59
60 // Test handleError. 60 // Test handleError.
61 c = new StreamController(); 61 c = new StreamController.multiSubscription();
62 expectedEvents = new Events()..add("ab")..error("[foo]"); 62 expectedEvents = new Events()..add("ab")..error("[foo]");
63 sentEvents = new Events()..add("ab")..error("foo")..add("ab")..close(); 63 sentEvents = new Events()..add("ab")..error("foo")..add("ab")..close();
64 actualEvents = new Events.capture(c.handleError((v) { 64 actualEvents = new Events.capture(c.handleError((v) {
65 if (v.error is String) { 65 if (v.error is String) {
66 throw new AsyncError("[${v.error}]", 66 throw new AsyncError("[${v.error}]",
67 "other stack"); 67 "other stack");
68 } 68 }
69 }), unsubscribeOnError: true); 69 }), unsubscribeOnError: true);
70 sentEvents.replay(c); 70 sentEvents.replay(c);
71 Expect.listEquals(expectedEvents.events, actualEvents.events); 71 Expect.listEquals(expectedEvents.events, actualEvents.events);
72 72
73 // reduce is tested asynchronously and therefore not in this file. 73 // reduce is tested asynchronously and therefore not in this file.
74 74
75 // Test expand 75 // Test expand
76 c = new StreamController(); 76 c = new StreamController.multiSubscription();
77 sentEvents = new Events()..add(3)..add(2)..add(4)..close(); 77 sentEvents = new Events()..add(3)..add(2)..add(4)..close();
78 expectedEvents = new Events()..add(1)..add(2)..add(3) 78 expectedEvents = new Events()..add(1)..add(2)..add(3)
79 ..add(1)..add(2) 79 ..add(1)..add(2)
80 ..add(1)..add(2)..add(3)..add(4) 80 ..add(1)..add(2)..add(3)..add(4)
81 ..close(); 81 ..close();
82 actualEvents = new Events.capture(c.expand((v) { 82 actualEvents = new Events.capture(c.expand((v) {
83 var l = []; 83 var l = [];
84 for (int i = 0; i < v; i++) l.add(i + 1); 84 for (int i = 0; i < v; i++) l.add(i + 1);
85 return l; 85 return l;
86 })); 86 }));
87 sentEvents.replay(c); 87 sentEvents.replay(c);
88 Expect.listEquals(expectedEvents.events, actualEvents.events); 88 Expect.listEquals(expectedEvents.events, actualEvents.events);
89 89
90 // Test transform. 90 // Test transform.
91 c = new StreamController(); 91 c = new StreamController.multiSubscription();
92 sentEvents = new Events()..add("a")..error(42)..add("b")..close(); 92 sentEvents = new Events()..add("a")..error(42)..add("b")..close();
93 expectedEvents = 93 expectedEvents =
94 new Events()..error("a")..add(42)..error("b")..add("foo")..close(); 94 new Events()..error("a")..add(42)..error("b")..add("foo")..close();
95 actualEvents = new Events.capture(c.transform(new StreamTransformer.from( 95 actualEvents = new Events.capture(c.transform(new StreamTransformer.from(
96 onData: (v, s) { s.signalError(new AsyncError(v)); }, 96 onData: (v, s) { s.signalError(new AsyncError(v)); },
97 onError: (e, s) { s.add(e.error); }, 97 onError: (e, s) { s.add(e.error); },
98 onDone: (s) { 98 onDone: (s) {
99 s.add("foo"); 99 s.add("foo");
100 s.close(); 100 s.close();
101 }))); 101 })));
102 sentEvents.replay(c); 102 sentEvents.replay(c);
103 Expect.listEquals(expectedEvents.events, actualEvents.events); 103 Expect.listEquals(expectedEvents.events, actualEvents.events);
104 104
105 // Test multiple filters. 105 // Test multiple filters.
106 c = new StreamController(); 106 c = new StreamController.multiSubscription();
107 sentEvents = new Events()..add(42) 107 sentEvents = new Events()..add(42)
108 ..add("snugglefluffy") 108 ..add("snugglefluffy")
109 ..add(7) 109 ..add(7)
110 ..add("42") 110 ..add("42")
111 ..error("not FormatException") // Unsubscribes. 111 ..error("not FormatException") // Unsubscribes.
112 ..close(); 112 ..close();
113 expectedEvents = new Events()..add(42)..error("not FormatException"); 113 expectedEvents = new Events()..add(42)..error("not FormatException");
114 actualEvents = new Events.capture( 114 actualEvents = new Events.capture(
115 c.where((v) => v is String) 115 c.where((v) => v is String)
116 .mappedBy((v) => int.parse(v)) 116 .mappedBy((v) => int.parse(v))
117 .handleError((v) { 117 .handleError((v) {
118 if (v.error is! FormatException) throw v; 118 if (v.error is! FormatException) throw v;
119 }) 119 })
120 .where((v) => v > 10), 120 .where((v) => v > 10),
121 unsubscribeOnError: true); 121 unsubscribeOnError: true);
122 sentEvents.replay(c); 122 sentEvents.replay(c);
123 Expect.listEquals(expectedEvents.events, actualEvents.events); 123 Expect.listEquals(expectedEvents.events, actualEvents.events);
124 124
125 // Test subscription changes while firing. 125 // Test subscription changes while firing.
126 c = new StreamController(); 126 c = new StreamController.multiSubscription();
127 var sink = c.sink; 127 var sink = c.sink;
128 var stream = c.stream; 128 var stream = c.stream;
129 var counter = 0; 129 var counter = 0;
130 var subscription = stream.listen(null); 130 var subscription = stream.listen(null);
131 subscription.onData((data) { 131 subscription.onData((data) {
132 counter += data; 132 counter += data;
133 subscription.cancel(); 133 subscription.cancel();
134 stream.listen((data) { 134 stream.listen((data) {
135 counter += 10 * data; 135 counter += 10 * data;
136 }); 136 });
137 var subscription2 = stream.listen(null); 137 var subscription2 = stream.listen(null);
138 subscription2.onData((data) { 138 subscription2.onData((data) {
139 counter += 100 * data; 139 counter += 100 * data;
140 if (data == 4) subscription2.cancel(); 140 if (data == 4) subscription2.cancel();
141 }); 141 });
142 }); 142 });
143 sink.add(1); // seen by stream 1 143 sink.add(1); // seen by stream 1
144 sink.add(2); // seen by stream 10 and 100 144 sink.add(2); // seen by stream 10 and 100
145 sink.add(3); // -"- 145 sink.add(3); // -"-
146 sink.add(4); // -"- 146 sink.add(4); // -"-
147 sink.add(5); // seen by stream 10 147 sink.add(5); // seen by stream 10
148 Expect.equals(1 + 20 + 200 + 30 + 300 + 40 + 400 + 50, counter); 148 Expect.equals(1 + 20 + 200 + 30 + 300 + 40 + 400 + 50, counter);
149 } 149 }
150 150
151 testSingleController() { 151 testSingleController() {
152 // Test normal flow. 152 // Test normal flow.
153 var c = new StreamController.singleSubscription(); 153 var c = new StreamController();
154 Events expectedEvents = new Events() 154 Events expectedEvents = new Events()
155 ..add(42) 155 ..add(42)
156 ..add("dibs") 156 ..add("dibs")
157 ..error("error!") 157 ..error("error!")
158 ..error("error too!") 158 ..error("error too!")
159 ..close(); 159 ..close();
160 Events actualEvents = new Events.capture(c); 160 Events actualEvents = new Events.capture(c);
161 expectedEvents.replay(c); 161 expectedEvents.replay(c);
162 Expect.listEquals(expectedEvents.events, actualEvents.events); 162 Expect.listEquals(expectedEvents.events, actualEvents.events);
163 163
164 // Test automatic unsubscription on error. 164 // Test automatic unsubscription on error.
165 c = new StreamController.singleSubscription(); 165 c = new StreamController();
166 expectedEvents = new Events()..add(42)..error("error"); 166 expectedEvents = new Events()..add(42)..error("error");
167 actualEvents = new Events.capture(c, unsubscribeOnError: true); 167 actualEvents = new Events.capture(c, unsubscribeOnError: true);
168 Events sentEvents = 168 Events sentEvents =
169 new Events()..add(42)..error("error")..add("Are you there?"); 169 new Events()..add(42)..error("error")..add("Are you there?");
170 sentEvents.replay(c); 170 sentEvents.replay(c);
171 Expect.listEquals(expectedEvents.events, actualEvents.events); 171 Expect.listEquals(expectedEvents.events, actualEvents.events);
172 172
173 // Test manual unsubscription. 173 // Test manual unsubscription.
174 c = new StreamController.singleSubscription(); 174 c = new StreamController();
175 expectedEvents = new Events()..add(42)..error("error")..add(37); 175 expectedEvents = new Events()..add(42)..error("error")..add(37);
176 actualEvents = new Events.capture(c, unsubscribeOnError: false); 176 actualEvents = new Events.capture(c, unsubscribeOnError: false);
177 expectedEvents.replay(c); 177 expectedEvents.replay(c);
178 actualEvents.subscription.cancel(); 178 actualEvents.subscription.cancel();
179 c.add("Are you there"); // Not sent to actualEvents. 179 c.add("Are you there"); // Not sent to actualEvents.
180 Expect.listEquals(expectedEvents.events, actualEvents.events); 180 Expect.listEquals(expectedEvents.events, actualEvents.events);
181 181
182 // Test filter. 182 // Test filter.
183 c = new StreamController.singleSubscription(); 183 c = new StreamController();
184 expectedEvents = new Events() 184 expectedEvents = new Events()
185 ..add("a string")..add("another string")..close(); 185 ..add("a string")..add("another string")..close();
186 sentEvents = new Events() 186 sentEvents = new Events()
187 ..add("a string")..add(42)..add("another string")..close(); 187 ..add("a string")..add(42)..add("another string")..close();
188 actualEvents = new Events.capture(c.where((v) => v is String)); 188 actualEvents = new Events.capture(c.where((v) => v is String));
189 sentEvents.replay(c); 189 sentEvents.replay(c);
190 Expect.listEquals(expectedEvents.events, actualEvents.events); 190 Expect.listEquals(expectedEvents.events, actualEvents.events);
191 191
192 // Test map. 192 // Test map.
193 c = new StreamController.singleSubscription(); 193 c = new StreamController();
194 expectedEvents = new Events()..add("abab")..error("error")..close(); 194 expectedEvents = new Events()..add("abab")..error("error")..close();
195 sentEvents = new Events()..add("ab")..error("error")..close(); 195 sentEvents = new Events()..add("ab")..error("error")..close();
196 actualEvents = new Events.capture(c.mappedBy((v) => "$v$v")); 196 actualEvents = new Events.capture(c.mappedBy((v) => "$v$v"));
197 sentEvents.replay(c); 197 sentEvents.replay(c);
198 Expect.listEquals(expectedEvents.events, actualEvents.events); 198 Expect.listEquals(expectedEvents.events, actualEvents.events);
199 199
200 // Test handleError. 200 // Test handleError.
201 c = new StreamController.singleSubscription(); 201 c = new StreamController();
202 expectedEvents = new Events()..add("ab")..error("[foo]"); 202 expectedEvents = new Events()..add("ab")..error("[foo]");
203 sentEvents = new Events()..add("ab")..error("foo")..add("ab")..close(); 203 sentEvents = new Events()..add("ab")..error("foo")..add("ab")..close();
204 actualEvents = new Events.capture(c.handleError((v) { 204 actualEvents = new Events.capture(c.handleError((v) {
205 if (v.error is String) { 205 if (v.error is String) {
206 throw new AsyncError("[${v.error}]", 206 throw new AsyncError("[${v.error}]",
207 "other stack"); 207 "other stack");
208 } 208 }
209 }), unsubscribeOnError: true); 209 }), unsubscribeOnError: true);
210 sentEvents.replay(c); 210 sentEvents.replay(c);
211 Expect.listEquals(expectedEvents.events, actualEvents.events); 211 Expect.listEquals(expectedEvents.events, actualEvents.events);
212 212
213 // reduce is tested asynchronously and therefore not in this file. 213 // reduce is tested asynchronously and therefore not in this file.
214 214
215 // Test expand 215 // Test expand
216 c = new StreamController.singleSubscription(); 216 c = new StreamController();
217 sentEvents = new Events()..add(3)..add(2)..add(4)..close(); 217 sentEvents = new Events()..add(3)..add(2)..add(4)..close();
218 expectedEvents = new Events()..add(1)..add(2)..add(3) 218 expectedEvents = new Events()..add(1)..add(2)..add(3)
219 ..add(1)..add(2) 219 ..add(1)..add(2)
220 ..add(1)..add(2)..add(3)..add(4) 220 ..add(1)..add(2)..add(3)..add(4)
221 ..close(); 221 ..close();
222 actualEvents = new Events.capture(c.expand((v) { 222 actualEvents = new Events.capture(c.expand((v) {
223 var l = []; 223 var l = [];
224 for (int i = 0; i < v; i++) l.add(i + 1); 224 for (int i = 0; i < v; i++) l.add(i + 1);
225 return l; 225 return l;
226 })); 226 }));
227 sentEvents.replay(c); 227 sentEvents.replay(c);
228 Expect.listEquals(expectedEvents.events, actualEvents.events); 228 Expect.listEquals(expectedEvents.events, actualEvents.events);
229 229
230 // pipe is tested asynchronously and therefore not in this file. 230 // pipe is tested asynchronously and therefore not in this file.
231 c = new StreamController.singleSubscription(); 231 c = new StreamController();
232 var list = <int>[]; 232 var list = <int>[];
233 c.pipeInto(new CollectionSink<int>(list)) 233 c.pipeInto(new CollectionSink<int>(list))
234 .whenComplete(() { Expect.listEquals(<int>[1,2,9,3,9], list); }); 234 .whenComplete(() { Expect.listEquals(<int>[1,2,9,3,9], list); });
235 c.add(1); 235 c.add(1);
236 c.add(2); 236 c.add(2);
237 c.add(9); 237 c.add(9);
238 c.add(3); 238 c.add(3);
239 c.add(9); 239 c.add(9);
240 c.close(); 240 c.close();
241 241
242 // Test transform. 242 // Test transform.
243 c = new StreamController.singleSubscription(); 243 c = new StreamController();
244 sentEvents = new Events()..add("a")..error(42)..add("b")..close(); 244 sentEvents = new Events()..add("a")..error(42)..add("b")..close();
245 expectedEvents = 245 expectedEvents =
246 new Events()..error("a")..add(42)..error("b")..add("foo")..close(); 246 new Events()..error("a")..add(42)..error("b")..add("foo")..close();
247 actualEvents = new Events.capture(c.transform(new StreamTransformer.from( 247 actualEvents = new Events.capture(c.transform(new StreamTransformer.from(
248 onData: (v, s) { s.signalError(new AsyncError(v)); }, 248 onData: (v, s) { s.signalError(new AsyncError(v)); },
249 onError: (e, s) { s.add(e.error); }, 249 onError: (e, s) { s.add(e.error); },
250 onDone: (s) { 250 onDone: (s) {
251 s.add("foo"); 251 s.add("foo");
252 s.close(); 252 s.close();
253 }))); 253 })));
254 sentEvents.replay(c); 254 sentEvents.replay(c);
255 Expect.listEquals(expectedEvents.events, actualEvents.events); 255 Expect.listEquals(expectedEvents.events, actualEvents.events);
256 256
257 // Test multiple filters. 257 // Test multiple filters.
258 c = new StreamController.singleSubscription(); 258 c = new StreamController();
259 sentEvents = new Events()..add(42) 259 sentEvents = new Events()..add(42)
260 ..add("snugglefluffy") 260 ..add("snugglefluffy")
261 ..add(7) 261 ..add(7)
262 ..add("42") 262 ..add("42")
263 ..error("not FormatException") // Unsubscribes. 263 ..error("not FormatException") // Unsubscribes.
264 ..close(); 264 ..close();
265 expectedEvents = new Events()..add(42)..error("not FormatException"); 265 expectedEvents = new Events()..add(42)..error("not FormatException");
266 actualEvents = new Events.capture( 266 actualEvents = new Events.capture(
267 c.where((v) => v is String) 267 c.where((v) => v is String)
268 .mappedBy((v) => int.parse(v)) 268 .mappedBy((v) => int.parse(v))
269 .handleError((v) { 269 .handleError((v) {
270 if (v.error is! FormatException) throw v; 270 if (v.error is! FormatException) throw v;
271 }) 271 })
272 .where((v) => v > 10), 272 .where((v) => v > 10),
273 unsubscribeOnError: true); 273 unsubscribeOnError: true);
274 sentEvents.replay(c); 274 sentEvents.replay(c);
275 Expect.listEquals(expectedEvents.events, actualEvents.events); 275 Expect.listEquals(expectedEvents.events, actualEvents.events);
276 276
277 // Test that only one subscription is allowed. 277 // Test that only one subscription is allowed.
278 c = new StreamController.singleSubscription(); 278 c = new StreamController();
279 var sink = c.sink; 279 var sink = c.sink;
280 var stream = c.stream; 280 var stream = c.stream;
281 var counter = 0; 281 var counter = 0;
282 var subscription = stream.listen((data) { counter += data; }); 282 var subscription = stream.listen((data) { counter += data; });
283 Expect.throws(() => stream.listen(null), (e) => e is StateError); 283 Expect.throws(() => stream.listen(null), (e) => e is StateError);
284 sink.add(1); 284 sink.add(1);
285 Expect.equals(1, counter); 285 Expect.equals(1, counter);
286 c.close(); 286 c.close();
287 } 287 }
288 288
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
348 ..add(5)..add(6)..add(4)..add(6)..add(8)..add(3)..add(4)..add(1)..close(); 348 ..add(5)..add(6)..add(4)..add(6)..add(8)..add(3)..add(4)..add(1)..close();
349 expectedEvents = new Events() 349 expectedEvents = new Events()
350 ..add(5)..add(4)..add(3)..add(1)..close(); 350 ..add(5)..add(4)..add(3)..add(1)..close();
351 // Use 'distinct' as a filter with access to the previously emitted event. 351 // Use 'distinct' as a filter with access to the previously emitted event.
352 actualEvents = new Events.capture(c.distinct((a, b) => a < b)); 352 actualEvents = new Events.capture(c.distinct((a, b) => a < b));
353 sentEvents.replay(c); 353 sentEvents.replay(c);
354 Expect.listEquals(expectedEvents.events, actualEvents.events); 354 Expect.listEquals(expectedEvents.events, actualEvents.events);
355 } 355 }
356 356
357 main() { 357 main() {
358 testController(); 358 testMultiController();
359 testSingleController(); 359 testSingleController();
360 testExtraMethods(); 360 testExtraMethods();
361 } 361 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698