Chromium Code Reviews| Index: tests/language/asyncstar_yieldstar_test.dart |
| diff --git a/tests/language/asyncstar_yieldstar_test.dart b/tests/language/asyncstar_yieldstar_test.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a528fc968d3e9e7372192ca86396645923cc9d1f |
| --- /dev/null |
| +++ b/tests/language/asyncstar_yieldstar_test.dart |
| @@ -0,0 +1,82 @@ |
| +// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +import "dart:async"; |
| +import "package:expect/expect.dart"; |
| +import "package:async_helper/async_helper.dart"; |
| + |
| +Stream<int> subStream(p) async* { |
| + yield p; |
| + yield p+1; |
| +} |
| + |
| +Stream foo(Completer<bool> finalized) async* { |
| + int i = 0; |
| + try { |
| + while (true) { |
| + yield "outer"; |
| + yield* subStream(i); |
| + i++; |
| + } |
| + } finally { |
| + // Canceling the stream-subscription should run the finalizer. |
| + finalized.complete(true); |
| + } |
| +} |
| + |
| +foo2(Stream subStream) async* { |
| + yield* subStream; |
| +} |
| + |
| +main () async { |
| + asyncStart(); |
| + Completer<bool> finalized = new Completer<bool>(); |
| + Expect.listEquals(["outer", 0, 1, "outer", 1, 2, "outer", 2], |
| + await (foo(finalized).take(8).toList())); |
| + Expect.isTrue(await (finalized.future)); |
| + finalized = new Completer<bool>(); |
| + // Cancelling the stream while it is yield*-ing from the sub-stream. |
|
floitsch
2015/02/04 15:31:52
Canceling
sigurdm
2015/02/05 14:06:06
Done.
|
| + Expect.listEquals(["outer", 0, 1, "outer", 1, 2, "outer"], |
| + await (foo(finalized).take(7).toList())); |
| + Expect.isTrue(await (finalized.future)); |
|
floitsch
2015/02/04 15:31:52
Also ensure that the loop didn't continue needless
sigurdm
2015/02/05 14:06:06
Done.
|
| + finalized = new Completer<bool>(); |
| + |
| + Completer<bool> pausedCompleter = new Completer<bool>(); |
| + Completer<bool> resumedCompleter = new Completer<bool>(); |
| + |
| + StreamController controller; |
| + // A controller, giving a stream |
| + int i = 0; |
| + addNext() { |
| + if (i >= 4) return; |
| + controller.add(i); |
| + i++; |
| + if (!controller.isPaused) { |
| + scheduleMicrotask(addNext); |
| + } |
| + } |
| + |
| + controller = new StreamController(onListen: () { |
| + scheduleMicrotask(addNext); |
| + }, onPause: () { |
| + pausedCompleter.complete(true); |
| + }, onResume: () { |
| + resumedCompleter.complete(true); |
| + scheduleMicrotask(addNext); |
| + }); |
| + |
| + StreamSubscription subscription; |
| + // Test that the yield*'ed stream is paused and resumed. |
| + subscription = foo2(controller.stream).listen((event) { |
| + if (event == 2) { |
| + subscription.pause(); |
| + scheduleMicrotask(() { |
| + subscription.resume(); |
| + }); |
| + } |
| + }); |
| + Expect.isTrue(await (pausedCompleter.future)); |
| + Expect.isTrue(await (resumedCompleter.future)); |
| + asyncEnd(); |
| +} |