Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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"; | |
| 7 import "package:async_helper/async_helper.dart"; | 6 import "package:async_helper/async_helper.dart"; |
| 8 | 7 |
| 8 /// This test verifies that an await for loop sends the correct | |
| 9 /// signals to the stream it iterates over: | |
| 10 /// 1) A listen event. | |
| 11 /// 2) A pause event when the loop body is awaiting something, | |
| 12 /// and more elements arrive on the stream. See issue | |
| 13 /// https://github.com/dart-lang/sdk/issues/23996 . | |
| 14 /// 3) A resume event, when the loop is again ready to iterate. | |
| 9 main() { | 15 main() { |
| 10 var sc; | 16 Completer listenEventReceived = new Completer(); |
| 11 var i = 0; | 17 Completer pauseEventReceived = new Completer(); |
| 12 void send() { | 18 Completer resumeEventReceived = new Completer(); |
| 13 if (i == 5) { | 19 StreamController controller = new StreamController( |
| 14 sc.close(); | 20 onListen: () => listenEventReceived.complete(), |
| 15 } else { | 21 onPause: () => pauseEventReceived.complete(), |
| 16 sc.add(i++); | 22 onResume: () => resumeEventReceived.complete()); |
| 23 | |
| 24 Completer forLoopEntered = new Completer(); | |
| 25 | |
| 26 /// The send function puts items on the stream. It waits for a | |
| 27 /// listener, puts "first" on the stream, waits for the for loop | |
| 28 /// to start (and eventually block), puts "second" on the stream | |
| 29 /// multiple times, letting the event loop run, until the for loop | |
| 30 /// pauses the stream because it it blocked. | |
| 31 /// The for loop unblocks after the pause message is received, and | |
| 32 /// reads the stream items, sending a stream resume message when it | |
| 33 /// is ready for more. | |
| 34 /// Then the send function puts a final "third" on the stream, and | |
| 35 /// closes the stream. | |
| 36 send() async { | |
| 37 await listenEventReceived.future; | |
| 38 controller.add("first"); | |
| 39 await forLoopEntered.future; | |
| 40 var timer = new Timer.periodic(new Duration(milliseconds: 10), (timer) { | |
| 41 controller.add("second"); | |
| 42 }); | |
| 43 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.
| |
| 44 timer.cancel(); | |
| 45 await resumeEventReceived.future; | |
| 46 controller.add("third"); | |
| 47 controller.close(); | |
| 48 } | |
| 49 | |
| 50 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
| |
| 51 await for (var entry in controller.stream) { | |
| 52 if (entry == 'first') { | |
| 53 forLoopEntered.complete(); | |
| 54 await pauseEventReceived.future; | |
| 55 } | |
| 17 } | 56 } |
| 18 } | 57 } |
| 19 sc = new StreamController(onListen: send, onResume: send); | |
| 20 | 58 |
| 21 f(s) async { | 59 asyncTest(() async { |
| 22 var r = 0; | 60 // We need to start both functions in parallel, and wait on them both. |
| 23 await for (var i in s) { | 61 var f = send(); |
| 24 r += await new Future.delayed(new Duration(milliseconds: 10), () => i); | 62 await receive(); |
| 25 } | 63 await f; |
| 26 return r; | |
| 27 } | |
| 28 | |
| 29 asyncStart(); | |
| 30 f(sc.stream).then((v) { | |
| 31 Expect.equals(10, v); | |
| 32 asyncEnd(); | |
| 33 }); | 64 }); |
| 34 } | 65 } |
| OLD | NEW |