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

Side by Side Diff: tests/language/await_for_cancel_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: Implement new ssa-nodes in ssa-tracer. 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) 2014, 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 bool cancelled;
10
11 test1() async {
12 cancelled = false;
13 try {
14 StreamController controller = infiniteStreamController();
15 outer: while(true) {
16 await for (var x in controller.stream) {
17 for (int j = 0; j < 10; j++) {
18 if (j == 5) break outer;
19 }
20 }
21 }
22 } finally {
23 Expect.isTrue(cancelled);
24 }
25 }
26
27 test2() async {
28 cancelled = false;
29 try {
30 StreamController controller = infiniteStreamController();
31 bool first = true;
32 outer: while(true) {
33 if (first) {
34 first = false;
35 } else {
36 break;
37 }
38 await for (var x in controller.stream) {
39 for (int j = 0; j < 10; j++) {
40 if (j == 5) continue outer;
41 }
42 }
43 }
44 } finally {
45 Expect.isTrue(cancelled);
46 }
47 }
48
49 main() async {
50 await test1();
51 await test2();
52
53 }
54
55
56 // Create a stream that produces numbers [1, 2, ... ]
57 StreamController infiniteStreamController() {
58 StreamController controller;
59 Timer timer;
60 int counter = 0;
61
62 void tick(_) {
63 counter++;
64 controller.add(counter); // Ask stream to send counter values as event.
65 }
66
67 void startTimer() {
68 timer = new Timer.periodic(const Duration(milliseconds: 10), tick);
floitsch 2015/02/02 22:00:12 I think there are some engines where a timer != 0
sigurdm 2015/02/03 16:59:33 You mean duration = 0; I changed it.
69 }
70
71 void stopTimer() {
72 if (timer != null) {
73 timer.cancel();
74 timer = null;
75 }
76 }
77
78 controller = new StreamController(
79 onListen: startTimer,
80 onPause: stopTimer,
81 onResume: startTimer,
82 onCancel: () {
83 cancelled = true;
84 stopTimer();
85 });
86
87 return controller;
88 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698