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

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

Issue 2734143002: Add test for cancel-future being awaited after an `await for`. (Closed)
Patch Set: Move some status to language_kernel.status Created 3 years, 9 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
« no previous file with comments | « no previous file | tests/language/language.status » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 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 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"; 6 import "package:expect/expect.dart";
7 import "package:async_helper/async_helper.dart"; 7 import "package:async_helper/async_helper.dart";
8 8
9 class Trace { 9 class Trace {
10 String trace = ""; 10 String trace;
11 record(x) { 11 Trace(this.trace);
12 void record(x) {
12 trace += x.toString(); 13 trace += x.toString();
13 } 14 }
14 toString() => trace; 15 String toString() => trace;
15 } 16 }
16 17
17 18
18 Stream makeMeAStream() { 19 Stream makeMeAStream() {
19 return timedCounter(5); 20 return timedCounter(5);
20 } 21 }
21 22
22 Trace t1 = new Trace();
23 23
24 consumeOne() async { 24 consumeOne(trace) async {
25 // Equivalent to await for (x in makeMeAStream()) { ... } 25 // Equivalent to await for (x in makeMeAStream()) { ... }
26 var s = makeMeAStream(); 26 var s = makeMeAStream();
27 var it = new StreamIterator(s); 27 var it = new StreamIterator(s);
28 while (await it.moveNext()) { 28 while (await it.moveNext()) {
29 var x = it.current; 29 var x = it.current;
30 t1.record(x); 30 trace.record(x);
31 } 31 }
32 t1.record("X"); 32 trace.record("X");
33 } 33 }
34 34
35 Trace t2 = new Trace();
36 35
37 consumeTwo() async { 36 consumeTwo(trace) async {
38 await for (var x in makeMeAStream()) { 37 await for (var x in makeMeAStream()) {
39 t2.record(x); 38 trace.record(x);
40 } 39 }
41 t2.record("Y"); 40 trace.record("Y");
42 } 41 }
43 42
44 Trace t3 = new Trace();
45 43
46 consumeNested() async { 44 consumeNested(trace) async {
47 await for (var x in makeMeAStream()) { 45 await for (var x in makeMeAStream()) {
48 t3.record(x); 46 trace.record(x);
49 await for (var y in makeMeAStream()) { 47 await for (var y in makeMeAStream()) {
50 t3.record(y); 48 trace.record(y);
51 } 49 }
52 t3.record("|"); 50 trace.record("|");
53 } 51 }
54 t3.record("Z"); 52 trace.record("Z");
55 } 53 }
56 54
57 Trace t4 = new Trace(); 55 consumeSomeOfInfinite(trace) async {
58
59 consumeSomeOfInfinite() async {
60 int i = 0; 56 int i = 0;
61 await for (var x in infiniteStream()) { 57 await for (var x in infiniteStream()) {
62 i++; 58 i++;
63 if (i > 10) break; 59 if (i > 10) break;
64 t4.record(x); 60 trace.record(x);
65 } 61 }
66 t4.record("U"); 62 trace.record("U");
63 }
64
65 const String cancelError =
66 "ERROR: Error in future returned by .cancel() must be caught";
67
68 /// Creates a stream that yields integers forever, but throws when cancelled.
floitsch 2017/03/07 12:31:18 canceled
69 ///
70 /// The thrown error should end up in the future returned by `cancel`.
71 Stream<int> errorOnCancelStream(int n) async* {
72 try {
73 while (true) yield n++;
74 } finally {
75 throw cancelError;
76 }
77 }
78
79
80 // Sanity-check that the errorOnCancelStream behaves as expected.
81 testErrorOnCancel() {
82 var stream = errorOnCancelStream(0);
83 var subscription = stream.listen(null);
84 return subscription.cancel().then((_) {
85 Expect.fail("Cancel future did not contain error");
86 }, onError: (e) {
87 Expect.equals(cancelError, e);
88 });
89 }
90
91 testCancelAwaited() async {
92 return runZoned(() async {
93 var stream = errorOnCancelStream(0);
94 try {
95 var n = 0;
96 await for (var x in stream) {
97 Expect.equals(n++, x);
98 if (x == 5) break;
99 }
100 Expect.fail("Didn't await the cancel future.");
101 } on String catch (e) {
102 Expect.equals(cancelError, e);
103 }
104 }, onError: (e) {
105 // Catch the error if it's uncaught.
106 if (cancelError == e) {
107 Expect.fail("Error in cancel is considered uncaught");
108 }
109 throw e;
110 });
67 } 111 }
68 112
69 main() { 113 main() {
70 var f1 = consumeOne(); 114 Trace t1 = new Trace("T1:");
71 t1.record("T1:"); 115 var f1 = consumeOne(t1);
72 116
73 var f2 = consumeTwo(); 117 Trace t2 = new Trace("T2:");
74 t2.record("T2:"); 118 var f2 = consumeTwo(t2);
75 119
76 var f3 = consumeNested(); 120 Trace t3 = new Trace("T3:");
77 t3.record("T3:"); 121 var f3 = consumeNested(t3);
78 122
79 var f4 = consumeSomeOfInfinite(); 123 Trace t4 = new Trace("T4:");
80 t4.record("T4:"); 124 var f4 = consumeSomeOfInfinite(t4);
125
126 var f5 = testErrorOnCancel();
127
128 var f6 = testCancelAwaited();
81 129
82 asyncStart(); 130 asyncStart();
83 Future.wait([f1, f2, f3, f4]).then((_) { 131 Future.wait([f1, f2, f3, f4, f5, f6]).then((_) {
84 Expect.equals("T1:12345X", t1.toString()); 132 Expect.equals("T1:12345X", t1.toString());
85 Expect.equals("T2:12345Y", t2.toString()); 133 Expect.equals("T2:12345Y", t2.toString());
86 Expect.equals("T3:112345|212345|312345|412345|512345|Z", t3.toString()); 134 Expect.equals("T3:112345|212345|312345|412345|512345|Z", t3.toString());
87 Expect.equals("T4:12345678910U", t4.toString()); 135 Expect.equals("T4:12345678910U", t4.toString());
88 asyncEnd(); 136 asyncEnd();
89 }); 137 });
90 } 138 }
91 139
92 // Create a stream that produces numbers [1, 2, ... maxCount] 140 // Create a stream that produces numbers [1, 2, ... maxCount]
93 Stream timedCounter(int maxCount) { 141 Stream timedCounter(int maxCount) {
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
147 } 195 }
148 196
149 controller = new StreamController( 197 controller = new StreamController(
150 onListen: startTimer, 198 onListen: startTimer,
151 onPause: stopTimer, 199 onPause: stopTimer,
152 onResume: startTimer, 200 onResume: startTimer,
153 onCancel: stopTimer); 201 onCancel: stopTimer);
154 202
155 return controller.stream; 203 return controller.stream;
156 } 204 }
OLDNEW
« no previous file with comments | « no previous file | tests/language/language.status » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698