Chromium Code Reviews| Index: tests/language/async_star_await_pauses_test.dart |
| diff --git a/tests/language/async_star_await_pauses_test.dart b/tests/language/async_star_await_pauses_test.dart |
| index c16fbdd7ff898666c1462fca8a336a020e9679ee..2a896b7f784d3c059dc0ee6a820b4ca25e0a47e5 100644 |
| --- a/tests/language/async_star_await_pauses_test.dart |
| +++ b/tests/language/async_star_await_pauses_test.dart |
| @@ -1,34 +1,65 @@ |
| -// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| +// Copyright (c) 2016, 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"; |
| +/// This test verifies that an await for loop sends the correct |
| +/// signals to the stream it iterates over: |
| +/// 1) A listen event. |
| +/// 2) A pause event when the loop body is awaiting something, |
| +/// and more elements arrive on the stream. See issue |
| +/// https://github.com/dart-lang/sdk/issues/23996 . |
| +/// 3) A resume event, when the loop is again ready to iterate. |
| main() { |
| - var sc; |
| - var i = 0; |
| - void send() { |
| - if (i == 5) { |
| - sc.close(); |
| - } else { |
| - sc.add(i++); |
| - } |
| + Completer listenEventReceived = new Completer(); |
| + Completer pauseEventReceived = new Completer(); |
| + Completer resumeEventReceived = new Completer(); |
| + StreamController controller = new StreamController( |
| + onListen: () => listenEventReceived.complete(), |
| + onPause: () => pauseEventReceived.complete(), |
| + onResume: () => resumeEventReceived.complete()); |
| + |
| + Completer forLoopEntered = new Completer(); |
| + |
| + /// The send function puts items on the stream. It waits for a |
| + /// listener, puts "first" on the stream, waits for the for loop |
| + /// to start (and eventually block), puts "second" on the stream |
| + /// multiple times, letting the event loop run, until the for loop |
| + /// pauses the stream because it it blocked. |
| + /// The for loop unblocks after the pause message is received, and |
| + /// reads the stream items, sending a stream resume message when it |
| + /// is ready for more. |
| + /// Then the send function puts a final "third" on the stream, and |
| + /// closes the stream. |
| + send() async { |
| + await listenEventReceived.future; |
| + controller.add("first"); |
| + await forLoopEntered.future; |
| + var timer = new Timer.periodic(new Duration(milliseconds: 10), (timer) { |
| + controller.add("second"); |
| + }); |
| + await pauseEventReceived.future; |
|
ahe
2016/04/08 12:00:01
Add a comment here, something along:
pauseEventRe
Bill Hesse
2016/04/08 12:16:47
Done.
|
| + timer.cancel(); |
| + await resumeEventReceived.future; |
| + controller.add("third"); |
| + controller.close(); |
| } |
| - sc = new StreamController(onListen: send, onResume: send); |
| - f(s) async { |
| - var r = 0; |
| - await for (var i in s) { |
| - r += await new Future.delayed(new Duration(milliseconds: 10), () => i); |
| + receive() async { |
|
ahe
2016/04/08 12:00:01
I suggest adding something like this:
if (entry =
Bill Hesse
2016/04/08 12:16:47
There is no guarantee that we get a "second", sinc
ahe
2016/04/08 12:20:17
Good point. Hopefully, they'll work on changing th
|
| + await for (var entry in controller.stream) { |
| + if (entry == 'first') { |
| + forLoopEntered.complete(); |
| + await pauseEventReceived.future; |
| + } |
| } |
| - return r; |
| } |
| - asyncStart(); |
| - f(sc.stream).then((v) { |
| - Expect.equals(10, v); |
| - asyncEnd(); |
| + asyncTest(() async { |
| + // We need to start both functions in parallel, and wait on them both. |
| + var f = send(); |
| + await receive(); |
| + await f; |
| }); |
| } |