Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(947)

Side by Side Diff: tests/language/asyncstar_yieldstar_test.dart

Issue 839323003: Implementation of async-await transformation on js ast. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments. Fix setting of the handler in finallies... Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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
54 StreamController controller;
55 int i = 0;
56 addNext() {
57 if (i >= 4) return;
58 controller.add(i);
59 i++;
60 if (!controller.isPaused) {
61 scheduleMicrotask(addNext);
62 }
63 }
64
65 controller = new StreamController(onListen: () {
66 scheduleMicrotask(addNext);
67 }, onPause: () {
68 pausedCompleter.complete(true);
69 }, onResume: () {
70 resumedCompleter.complete(true);
71 scheduleMicrotask(addNext);
72 });
73
74 StreamSubscription subscription;
75 // Test that the yield*'ed stream is paused and resumed.
76 subscription = foo2(controller.stream).listen((event) {
77 if (event == 2) {
78 subscription.pause();
79 scheduleMicrotask(() {
80 subscription.resume();
81 });
82 }
83 });
84 Expect.isTrue(await (pausedCompleter.future));
85 Expect.isTrue(await (resumedCompleter.future));
86 asyncEnd();
87 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698