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