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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: tests/language/asyncstar_yieldstar_test.dart
diff --git a/tests/language/asyncstar_yieldstar_test.dart b/tests/language/asyncstar_yieldstar_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..f5d008c07b6e4d352d884d5d8ff3d12d1703f5ef
--- /dev/null
+++ b/tests/language/asyncstar_yieldstar_test.dart
@@ -0,0 +1,87 @@
+// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+import "dart:async";
+import "package:expect/expect.dart";
+import "package:async_helper/async_helper.dart";
+
+Stream<int> subStream(p) async* {
+ yield p;
+ yield p+1;
+}
+
+Stream foo(Completer<bool> finalized) async* {
+ int i = 0;
+ try {
+ while (true) {
+ yield "outer";
+ yield* subStream(i);
+ i++;
+ }
+ } finally {
+ // See that we did not run too many iterations.
+ Expect.isTrue(i < 10);
+ // Canceling the stream-subscription should run the finalizer.
+ finalized.complete(true);
+ }
+}
+
+foo2(Stream subStream) async* {
+ yield* subStream;
+}
+
+main () async {
+ asyncStart();
+ print("starting");
+ Expect.listEquals([0, 1],
+ await (subStream(0).toList()));
+ Completer<bool> finalized = new Completer<bool>();
+ Expect.listEquals(["outer", 0, 1, "outer", 1, 2, "outer", 2],
+ await (foo(finalized).take(8).toList()));
+ Expect.isTrue(await (finalized.future));
+
+ finalized = new Completer<bool>();
+ // Canceling the stream while it is yield*-ing from the sub-stream.
+ Expect.listEquals(["outer", 0, 1, "outer", 1, 2, "outer"],
+ await (foo(finalized).take(7).toList()));
+ Expect.isTrue(await (finalized.future));
+ finalized = new Completer<bool>();
+
+ Completer<bool> pausedCompleter = new Completer<bool>();
+ Completer<bool> resumedCompleter = new Completer<bool>();
+
+ StreamController controller;
+ int i = 0;
+ addNext() {
+ if (i >= 4) return;
+ controller.add(i);
+ i++;
+ if (!controller.isPaused) {
+ scheduleMicrotask(addNext);
+ }
+ }
+
+ controller = new StreamController(onListen: () {
+ scheduleMicrotask(addNext);
+ }, onPause: () {
+ pausedCompleter.complete(true);
+ }, onResume: () {
+ resumedCompleter.complete(true);
+ scheduleMicrotask(addNext);
+ });
+
+ StreamSubscription subscription;
+ // Test that the yield*'ed stream is paused and resumed.
+ subscription = foo2(controller.stream).listen((event) {
+ if (event == 2) {
+ subscription.pause();
+ scheduleMicrotask(() {
+ subscription.resume();
+ });
+ }
+ });
+ Expect.isTrue(await (pausedCompleter.future));
+ Expect.isTrue(await (resumedCompleter.future));
+ asyncEnd();
+}

Powered by Google App Engine
This is Rietveld 408576698