OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 import "dart:async"; |
| 6 import "package:expect/expect.dart"; |
| 7 import "package:async_helper/async_helper.dart"; |
| 8 |
| 9 Stream<int> subStream(p) async* { |
| 10 yield p; |
| 11 yield p+1; |
| 12 } |
| 13 |
| 14 Stream foo(Completer<bool> finalized) async* { |
| 15 int i = 0; |
| 16 try { |
| 17 while (true) { |
| 18 yield "outer"; |
| 19 yield* subStream(i); |
| 20 i++; |
| 21 } |
| 22 } finally { |
| 23 // See that we did not run too many iterations. |
| 24 Expect.isTrue(i < 10); |
| 25 // Canceling the stream-subscription should run the finalizer. |
| 26 finalized.complete(true); |
| 27 } |
| 28 } |
| 29 |
| 30 foo2(Stream subStream) async* { |
| 31 yield* subStream; |
| 32 } |
| 33 |
| 34 test() async { |
| 35 Expect.listEquals([0, 1], |
| 36 await (subStream(0).toList())); |
| 37 Completer<bool> finalized = new Completer<bool>(); |
| 38 Expect.listEquals(["outer", 0, 1, "outer", 1, 2, "outer", 2], |
| 39 await (foo(finalized).take(8).toList())); |
| 40 Expect.isTrue(await (finalized.future)); |
| 41 |
| 42 finalized = new Completer<bool>(); |
| 43 // Canceling the stream while it is yield*-ing from the sub-stream. |
| 44 Expect.listEquals(["outer", 0, 1, "outer", 1, 2, "outer"], |
| 45 await (foo(finalized).take(7).toList())); |
| 46 Expect.isTrue(await (finalized.future)); |
| 47 finalized = new Completer<bool>(); |
| 48 |
| 49 Completer<bool> pausedCompleter = new Completer<bool>(); |
| 50 Completer<bool> resumedCompleter = new Completer<bool>(); |
| 51 Completer<bool> canceledCompleter = new Completer<bool>(); |
| 52 |
| 53 StreamController controller; |
| 54 int i = 0; |
| 55 addNext() { |
| 56 if (i >= 10) return; |
| 57 controller.add(i); |
| 58 i++; |
| 59 if (!controller.isPaused) { |
| 60 scheduleMicrotask(addNext); |
| 61 } |
| 62 } |
| 63 |
| 64 controller = new StreamController(onListen: () { |
| 65 scheduleMicrotask(addNext); |
| 66 }, onPause: () { |
| 67 pausedCompleter.complete(true); |
| 68 }, onResume: () { |
| 69 resumedCompleter.complete(true); |
| 70 scheduleMicrotask(addNext); |
| 71 }, onCancel: () { |
| 72 canceledCompleter.complete(true); |
| 73 }); |
| 74 |
| 75 StreamSubscription subscription; |
| 76 // Test that the yield*'ed stream is paused and resumed. |
| 77 subscription = foo2(controller.stream).listen((event) { |
| 78 if (event == 2) { |
| 79 subscription.pause(); |
| 80 scheduleMicrotask(() { |
| 81 subscription.resume(); |
| 82 }); |
| 83 } |
| 84 if (event == 5) { |
| 85 subscription.cancel(); |
| 86 } |
| 87 }); |
| 88 // Test that the yield*'ed streamSubscription is paused, resumed and canceled |
| 89 // by the async* stream. |
| 90 Expect.isTrue(await pausedCompleter.future); |
| 91 Expect.isTrue(await resumedCompleter.future); |
| 92 Expect.isTrue(await canceledCompleter.future); |
| 93 } |
| 94 |
| 95 main() { |
| 96 asyncStart(); |
| 97 test().then((_) { |
| 98 asyncEnd(); |
| 99 }); |
| 100 } |
OLD | NEW |