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 main () async { |
| 35 asyncStart(); |
| 36 print("starting"); |
| 37 Expect.listEquals([0, 1], |
| 38 await (subStream(0).toList())); |
| 39 Completer<bool> finalized = new Completer<bool>(); |
| 40 Expect.listEquals(["outer", 0, 1, "outer", 1, 2, "outer", 2], |
| 41 await (foo(finalized).take(8).toList())); |
| 42 Expect.isTrue(await (finalized.future)); |
| 43 |
| 44 finalized = new Completer<bool>(); |
| 45 // Canceling the stream while it is yield*-ing from the sub-stream. |
| 46 Expect.listEquals(["outer", 0, 1, "outer", 1, 2, "outer"], |
| 47 await (foo(finalized).take(7).toList())); |
| 48 Expect.isTrue(await (finalized.future)); |
| 49 finalized = new Completer<bool>(); |
| 50 |
| 51 Completer<bool> pausedCompleter = new Completer<bool>(); |
| 52 Completer<bool> resumedCompleter = new Completer<bool>(); |
| 53 Completer<bool> canceledCompleter = new Completer<bool>(); |
| 54 |
| 55 StreamController controller; |
| 56 int i = 0; |
| 57 addNext() { |
| 58 if (i >= 10) return; |
| 59 controller.add(i); |
| 60 i++; |
| 61 if (!controller.isPaused) { |
| 62 scheduleMicrotask(addNext); |
| 63 } |
| 64 } |
| 65 |
| 66 controller = new StreamController(onListen: () { |
| 67 scheduleMicrotask(addNext); |
| 68 }, onPause: () { |
| 69 pausedCompleter.complete(true); |
| 70 }, onResume: () { |
| 71 resumedCompleter.complete(true); |
| 72 scheduleMicrotask(addNext); |
| 73 }, onCancel: () { |
| 74 canceledCompleter.complete(true); |
| 75 }); |
| 76 |
| 77 StreamSubscription subscription; |
| 78 // Test that the yield*'ed stream is paused and resumed. |
| 79 subscription = foo2(controller.stream).listen((event) { |
| 80 if (event == 2) { |
| 81 subscription.pause(); |
| 82 scheduleMicrotask(() { |
| 83 subscription.resume(); |
| 84 }); |
| 85 } |
| 86 if (event == 5) { |
| 87 subscription.cancel(); |
| 88 } |
| 89 }); |
| 90 // Test that the yield*'ed streamSubscription is paused, resumed and canceled |
| 91 // by the async* stream. |
| 92 Expect.isTrue(await pausedCompleter.future); |
| 93 Expect.isTrue(await resumedCompleter.future); |
| 94 Expect.isTrue(await canceledCompleter.future); |
| 95 |
| 96 asyncEnd(); |
| 97 } |
OLD | NEW |