| 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 'dart:async'; | 10 import 'dart:async'; |
| 10 import 'event_helper.dart'; | 11 import 'event_helper.dart'; |
| 11 | 12 |
| 13 fail(e) { Expect.fail("Unexepected error: $e"); } |
| 14 |
| 12 void testMultiController() { | 15 void testMultiController() { |
| 13 // Test normal flow. | 16 // Test normal flow. |
| 14 var c = new StreamController(sync: true); | 17 var c = new StreamController(sync: true); |
| 15 Events expectedEvents = new Events() | 18 Events expectedEvents = new Events() |
| 16 ..add(42) | 19 ..add(42) |
| 17 ..add("dibs") | 20 ..add("dibs") |
| 18 ..error("error!") | 21 ..error("error!") |
| 19 ..error("error too!") | 22 ..error("error too!") |
| 20 ..close(); | 23 ..close(); |
| 21 CaptureEvents actualEvents = new Events.capture(c.stream.asBroadcastStream()); | 24 CaptureEvents actualEvents = new Events.capture(c.stream.asBroadcastStream()); |
| (...skipping 407 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 429 c = new StreamController.broadcast(sync: false); | 432 c = new StreamController.broadcast(sync: false); |
| 430 Expect.equals(c.stream, c.stream); | 433 Expect.equals(c.stream, c.stream); |
| 431 c = new StreamController.broadcast(sync: true); | 434 c = new StreamController.broadcast(sync: true); |
| 432 Expect.equals(c.stream, c.stream); | 435 Expect.equals(c.stream, c.stream); |
| 433 c = new StreamController.broadcast(sync: false, onListen:(){}); | 436 c = new StreamController.broadcast(sync: false, onListen:(){}); |
| 434 Expect.equals(c.stream, c.stream); | 437 Expect.equals(c.stream, c.stream); |
| 435 c = new StreamController.broadcast(sync: true, onListen:(){}); | 438 c = new StreamController.broadcast(sync: true, onListen:(){}); |
| 436 Expect.equals(c.stream, c.stream); | 439 Expect.equals(c.stream, c.stream); |
| 437 } | 440 } |
| 438 | 441 |
| 442 void testCancelThrow() { |
| 443 asyncStart(); |
| 444 asyncStart(); |
| 445 StreamController c = new StreamController(onCancel: () { |
| 446 asyncEnd(); |
| 447 throw "ERROR"; |
| 448 }); |
| 449 c.add(1); |
| 450 c.add(2); |
| 451 c.add(3); |
| 452 Future done = c.close(); |
| 453 StreamSubscription sub; |
| 454 sub = c.stream.listen((v) { |
| 455 Expect.equals(1, v); |
| 456 Future f = sub.cancel(); |
| 457 f.catchError((e) { |
| 458 // Must complete with error from onCancel. |
| 459 Expect.equals("ERROR", e); |
| 460 asyncEnd(); |
| 461 }); |
| 462 }); |
| 463 done.catchError(fail).whenComplete(asyncEnd); // Must complete without error. |
| 464 } |
| 465 |
| 466 void testCancelThrow2() { |
| 467 asyncStart(); |
| 468 asyncStart(); |
| 469 asyncStart(); |
| 470 asyncStart(); |
| 471 asyncStart(); |
| 472 StreamController c2 = new StreamController(onCancel: () { |
| 473 asyncEnd(); |
| 474 throw "ERROR"; |
| 475 }); |
| 476 c2.add(1); |
| 477 c2.add(2); |
| 478 Future done2 = c2.close(); |
| 479 done2.catchError(fail).whenComplete(asyncEnd); // Should not get error; |
| 480 |
| 481 StreamController c = new StreamController(); |
| 482 var sub; |
| 483 sub = c.stream.listen((v) { |
| 484 Expect.equals(1, v); |
| 485 Future f = sub.cancel(); |
| 486 f.catchError((e) { |
| 487 // Error from addStream stream's cancel must go only here. |
| 488 asyncEnd(); |
| 489 Expect.equals("ERROR", e); |
| 490 }); |
| 491 }); |
| 492 var addDone = c.addStream(c2.stream); |
| 493 addDone.catchError(fail).whenComplete(asyncEnd); // Should not get error. |
| 494 var done = c.done; |
| 495 done.catchError(fail).whenComplete(asyncEnd); // Should not get error. |
| 496 } |
| 497 |
| 498 void testCancelThrow3() { |
| 499 asyncStart(); |
| 500 asyncStart(); |
| 501 asyncStart(); |
| 502 asyncStart(); |
| 503 asyncStart(); |
| 504 asyncStart(); |
| 505 StreamController c2 = new StreamController(onCancel: () { |
| 506 asyneEnd(); |
| 507 throw "ERROR2"; |
| 508 }); |
| 509 c2.add(1); |
| 510 c2.add(2); |
| 511 var done2 = c2.close(); |
| 512 done2.catchError(fail).whenComplete(asyncEnd); // Should not get error; |
| 513 |
| 514 StreamController c = new StreamController(onCancel: () { |
| 515 asyncEnd(); |
| 516 throw "ERROR1"; |
| 517 }); |
| 518 var sub; |
| 519 sub = c.stream.listen((v) { |
| 520 Expect.equals(1, v); |
| 521 Future f = sub.cancel(); |
| 522 f.catchError((e) { |
| 523 // Only the last error ends up here. |
| 524 Expect.equals("ERROR1", e); |
| 525 asyncEnd(); |
| 526 }); |
| 527 }); |
| 528 var addDone = c.addStream(c2.stream); |
| 529 addDone.catchError(fail).whenComplete(asyncEnd); // Error must not go here. |
| 530 c.done.catchError(fail).whenComplete(asyncEnd); // Error must not go here. |
| 531 } |
| 532 |
| 439 main() { | 533 main() { |
| 440 testMultiController(); | 534 testMultiController(); |
| 441 testSingleController(); | 535 testSingleController(); |
| 442 testExtraMethods(); | 536 testExtraMethods(); |
| 443 testClosed(); | 537 testClosed(); |
| 444 testStreamEquals(); | 538 testStreamEquals(); |
| 539 testCancelThrow(); |
| 540 testCancelThrow2(); |
| 541 testCancelThrow3(); |
| 445 } | 542 } |
| OLD | NEW |