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 // Canceling the stream-subscription should run the finalizer. | |
24 finalized.complete(true); | |
25 } | |
26 } | |
27 | |
28 foo2(Stream subStream) async* { | |
29 yield* subStream; | |
30 } | |
31 | |
32 main () async { | |
33 asyncStart(); | |
34 Completer<bool> finalized = new Completer<bool>(); | |
35 Expect.listEquals(["outer", 0, 1, "outer", 1, 2, "outer", 2], | |
36 await (foo(finalized).take(8).toList())); | |
37 Expect.isTrue(await (finalized.future)); | |
38 finalized = new Completer<bool>(); | |
39 // 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.
| |
40 Expect.listEquals(["outer", 0, 1, "outer", 1, 2, "outer"], | |
41 await (foo(finalized).take(7).toList())); | |
42 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.
| |
43 finalized = new Completer<bool>(); | |
44 | |
45 Completer<bool> pausedCompleter = new Completer<bool>(); | |
46 Completer<bool> resumedCompleter = new Completer<bool>(); | |
47 | |
48 StreamController controller; | |
49 // A controller, giving a stream | |
50 int i = 0; | |
51 addNext() { | |
52 if (i >= 4) return; | |
53 controller.add(i); | |
54 i++; | |
55 if (!controller.isPaused) { | |
56 scheduleMicrotask(addNext); | |
57 } | |
58 } | |
59 | |
60 controller = new StreamController(onListen: () { | |
61 scheduleMicrotask(addNext); | |
62 }, onPause: () { | |
63 pausedCompleter.complete(true); | |
64 }, onResume: () { | |
65 resumedCompleter.complete(true); | |
66 scheduleMicrotask(addNext); | |
67 }); | |
68 | |
69 StreamSubscription subscription; | |
70 // Test that the yield*'ed stream is paused and resumed. | |
71 subscription = foo2(controller.stream).listen((event) { | |
72 if (event == 2) { | |
73 subscription.pause(); | |
74 scheduleMicrotask(() { | |
75 subscription.resume(); | |
76 }); | |
77 } | |
78 }); | |
79 Expect.isTrue(await (pausedCompleter.future)); | |
80 Expect.isTrue(await (resumedCompleter.future)); | |
81 asyncEnd(); | |
82 } | |
OLD | NEW |