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