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

Side by Side Diff: sdk/lib/_internal/pub/test/error_group_test.dart

Issue 14753009: Make StreamSubscription be the active part of a stream. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Made tests run (mostly) Created 7 years, 7 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) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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 library error_group_test; 5 library error_group_test;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 8
9 import 'package:unittest/unittest.dart'; 9 import 'package:unittest/unittest.dart';
10 10
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
203 StreamController controller; 203 StreamController controller;
204 Stream stream; 204 Stream stream;
205 205
206 setUp(() { 206 setUp(() {
207 errorGroup = new ErrorGroup(); 207 errorGroup = new ErrorGroup();
208 controller = new StreamController(); 208 controller = new StreamController();
209 stream = errorGroup.registerStream(controller.stream.asBroadcastStream()); 209 stream = errorGroup.registerStream(controller.stream.asBroadcastStream());
210 }); 210 });
211 211
212 test('should pass through values from the stream', () { 212 test('should pass through values from the stream', () {
213 expect(stream.elementAt(0), completion(equals(1))); 213 StreamIterator iter = new StreamIterator(stream);
214 expect(stream.elementAt(1), completion(equals(2))); 214 iter.moveNext().then((hasNext) {
215 expect(hasNext, isTrue);
216 expect(iter.current, equals(1));
217 iter.moveNext().then((hasNext) {
218 expect(hasNext, isTrue);
219 expect(iter.current, equals(2));
220 expect(iter.moveNext(), completion(isFalse));
floitsch 2013/05/22 16:26:29 I think the `completion` is called too late. The t
Lasse Reichstein Nielsen 2013/05/24 06:02:49 Then how do you write a test that expects an async
floitsch 2013/05/24 13:53:41 expectAsync the first asynchronous part. then the
221 });
222 });
215 expect(errorGroup.done, completes); 223 expect(errorGroup.done, completes);
216 224
217 controller..add(1)..add(2)..close(); 225 controller..add(1)..add(2)..close();
218 }); 226 });
219 227
220 test('should pass through an error from the stream if it has a ' 228 test('should pass through an error from the stream if it has a '
221 'listener', () { 229 'listener', () {
222 expect(stream.first, throwsFormatException); 230 expect(stream.first, throwsFormatException);
223 // errorGroup shouldn't top-level the exception 231 // errorGroup shouldn't top-level the exception
224 controller.addError(new FormatException()); 232 controller.addError(new FormatException());
225 }); 233 });
226 234
227 test('should notify the error group of an exception from the stream even ' 235 test('should notify the error group of an exception from the stream even '
228 'if it has a listener', () { 236 'if it has a listener', () {
229 expect(stream.first, throwsFormatException); 237 expect(stream.first, throwsFormatException);
230 expect(errorGroup.done, throwsFormatException); 238 expect(errorGroup.done, throwsFormatException);
231 controller.addError(new FormatException()); 239 controller.addError(new FormatException());
232 }); 240 });
233 241
234 test('should pass a signaled exception to the stream if it has a listener ' 242 test('should pass a signaled exception to the stream if it has a listener '
235 'and should unsubscribe that stream', () { 243 'and should unsubscribe that stream', () {
236 expect(stream.first, throwsFormatException);
237 // errorGroup shouldn't top-level the exception 244 // errorGroup shouldn't top-level the exception
238 errorGroup.signalError(new FormatException()); 245 errorGroup.signalError(new FormatException());
239 246
240 expect(stream.first.catchError((_) { 247 expect(stream.first.catchError((_) {
241 controller.add('value'); 248 controller.add('value');
242 return stream.isEmpty; 249 }), completes);
243 }), completion(isTrue));
244 }); 250 });
245 251
246 test('should notify the error group of a signaled exception even if the ' 252 test('should notify the error group of a signaled exception even if the '
247 'stream has a listener', () { 253 'stream has a listener', () {
248 expect(stream.first, throwsFormatException); 254 expect(stream.first, throwsFormatException);
249 expect(errorGroup.done, throwsFormatException); 255 expect(errorGroup.done, throwsFormatException);
250 errorGroup.signalError(new FormatException()); 256 errorGroup.signalError(new FormatException());
251 }); 257 });
252 258
253 test("should see one value and complete .done when the stream is done even " 259 test("should see one value and complete .done when the stream is done even "
254 "if the stream doesn't have a listener", () { 260 "if the stream doesn't have a listener", () {
255 expect(errorGroup.done, completes); 261 expect(errorGroup.done, completes);
256 controller.add('value'); 262 controller.add('value');
257 controller.close(); 263 controller.close();
258
floitsch 2013/05/22 16:26:29 keep line.
Lasse Reichstein Nielsen 2013/05/24 06:02:49 ok.
259 // Now that broadcast controllers have been removed a listener should 264 // Now that broadcast controllers have been removed a listener should
260 // see the value that has been put into the controller. 265 // see the value that has been put into the controller.
261 expect(errorGroup.done.then((_) => stream.toList()), 266 expect(errorGroup.done.then((_) => stream.toList()),
262 completion(equals(['value']))); 267 completion(equals(['value'])));
263 }); 268 });
264 269
265 }); 270 });
266 271
267 group('with a single single-subscription stream', () { 272 group('with a single single-subscription stream', () {
268 StreamController controller; 273 StreamController controller;
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
338 expect(stream1.toList(), completion(equals(['value1.1', 'value1.2']))); 343 expect(stream1.toList(), completion(equals(['value1.1', 'value1.2'])));
339 expect(stream2.toList(), completion(equals(['value2.1', 'value2.2']))); 344 expect(stream2.toList(), completion(equals(['value2.1', 'value2.2'])));
340 expect(errorGroup.done, completes); 345 expect(errorGroup.done, completes);
341 346
342 controller1..add('value1.1')..add('value1.2')..close(); 347 controller1..add('value1.1')..add('value1.2')..close();
343 controller2..add('value2.1')..add('value2.2')..close(); 348 controller2..add('value2.1')..add('value2.2')..close();
344 }); 349 });
345 350
346 test("shouldn't throw a top-level exception if a stream receives an error " 351 test("shouldn't throw a top-level exception if a stream receives an error "
347 "after the other listened stream completes", () { 352 "after the other listened stream completes", () {
348 expect(stream1.toList(), completion(equals(['value1', 'value2']))); 353 var signal = new Completer();
354 expect(stream1.toList().whenComplete(signal.complete),
355 completion(equals(['value1', 'value2'])));
349 controller1..add('value1')..add('value2')..close(); 356 controller1..add('value1')..add('value2')..close();
350 357
351 expect(stream1.toList().then((_) { 358 expect(signal.future.then((_) {
352 // shouldn't cause a top-level exception 359 // shouldn't cause a top-level exception
353 controller2.addError(new FormatException()); 360 controller2.addError(new FormatException());
354 }), completes); 361 }), completes);
355 }); 362 });
356 363
357 test("shouldn't throw a top-level exception if an error is signaled after " 364 test("shouldn't throw a top-level exception if an error is signaled after "
358 "one listened stream completes", () { 365 "one listened stream completes", () {
359 expect(stream1.toList(), completion(equals(['value1', 'value2']))); 366 var signal = new Completer();
367 expect(stream1.toList().whenComplete(signal.complete),
368 completion(equals(['value1', 'value2'])));
360 controller1..add('value1')..add('value2')..close(); 369 controller1..add('value1')..add('value2')..close();
361 370
362 expect(stream1.toList().then((_) { 371 expect(signal.future.then((_) {
363 // shouldn't cause a top-level exception 372 // shouldn't cause a top-level exception
364 errorGroup.signalError(new FormatException()); 373 errorGroup.signalError(new FormatException());
365 }), completes); 374 }), completes);
366 }); 375 });
367 }); 376 });
368 377
369 group('with a stream and a future', () { 378 group('with a stream and a future', () {
370 StreamController controller; 379 StreamController controller;
371 Stream stream; 380 Stream stream;
372 Completer completer; 381 Completer completer;
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
412 completer.complete('value'); 421 completer.complete('value');
413 422
414 expect(future.then((_) { 423 expect(future.then((_) {
415 // shouldn't cause a top-level exception 424 // shouldn't cause a top-level exception
416 controller.addError(new FormatException()); 425 controller.addError(new FormatException());
417 }), completes); 426 }), completes);
418 }); 427 });
419 428
420 test("shouldn't throw a top-level exception if the future receives an " 429 test("shouldn't throw a top-level exception if the future receives an "
421 "error after the listened stream completes", () { 430 "error after the listened stream completes", () {
422 expect(stream.toList(), completion(equals(['value1', 'value2']))); 431 var signal = new Completer();
432 expect(stream.toList().whenComplete(signal.complete),
433 completion(equals(['value1', 'value2'])));
423 controller..add('value1')..add('value2')..close(); 434 controller..add('value1')..add('value2')..close();
424 435
425 expect(stream.toList().then((_) { 436 expect(signal.future.then((_) {
426 // shouldn't cause a top-level exception 437 // shouldn't cause a top-level exception
427 completer.completeError(new FormatException()); 438 completer.completeError(new FormatException());
428 }), completes); 439 }), completes);
429 }); 440 });
430 }); 441 });
431 } 442 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698