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

Side by Side Diff: tests/lib/async/stream_timeout_test.dart

Issue 185543005: Make stream_timeout_test less likely to flake. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | 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) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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:unittest/unittest.dart"; 6 import "package:unittest/unittest.dart";
7 7
8 main() { 8 main() {
9 const ms5 = const Duration(milliseconds: 5); 9 const ms5 = const Duration(milliseconds: 5);
10 const halfSec = const Duration(milliseconds: 500); 10 const twoSecs = const Duration(seconds: 2);
11 11
12 test("stream timeout", () { 12 test("stream timeout", () {
13 StreamController c = new StreamController(); 13 StreamController c = new StreamController();
14 Stream tos = c.stream.timeout(ms5); 14 Stream tos = c.stream.timeout(ms5);
15 expect(tos.isBroadcast, false); 15 expect(tos.isBroadcast, false);
16 tos.handleError(expectAsync2((e, s) { 16 tos.handleError(expectAsync2((e, s) {
17 expect(e, new isInstanceOf<TimeoutException>()); 17 expect(e, new isInstanceOf<TimeoutException>());
18 expect(s, null); 18 expect(s, null);
19 })).listen((v){ fail("Unexpected event"); }); 19 })).listen((v){ fail("Unexpected event"); });
20 }); 20 });
21 21
22 test("stream timeout add events", () { 22 test("stream timeout add events", () {
23 StreamController c = new StreamController(); 23 StreamController c = new StreamController();
24 Stream tos = c.stream.timeout(ms5, onTimeout: (sink) { 24 Stream tos = c.stream.timeout(ms5, onTimeout: (sink) {
25 sink.add(42); 25 sink.add(42);
26 sink.addError("ERROR"); 26 sink.addError("ERROR");
27 sink.close(); 27 sink.close();
28 }); 28 });
29 expect(tos.isBroadcast, false); 29 expect(tos.isBroadcast, false);
30 tos.listen(expectAsync1((v) { expect(v, 42); }), 30 tos.listen(expectAsync1((v) { expect(v, 42); }),
31 onError: expectAsync2((e, s) { expect(e, "ERROR"); }), 31 onError: expectAsync2((e, s) { expect(e, "ERROR"); }),
32 onDone: expectAsync0((){})); 32 onDone: expectAsync0((){}));
33 }); 33 });
34 34
35 test("stream no timeout", () { 35 test("stream no timeout", () {
36 StreamController c = new StreamController(); 36 StreamController c = new StreamController();
37 Stream tos = c.stream.timeout(halfSec); 37 Stream tos = c.stream.timeout(twoSecs);
38 int ctr = 0; 38 int ctr = 0;
39 tos.listen((v) { 39 tos.listen((v) {
40 expect(v, 42); 40 expect(v, 42);
41 ctr++; 41 ctr++;
42 }, 42 },
43 onError: (e, s) { fail("No error expected"); }, 43 onError: (e, s) { fail("No error expected"); },
44 onDone: expectAsync0(() { 44 onDone: expectAsync0(() {
45 expect(ctr, 2); 45 expect(ctr, 2);
46 })); 46 }));
47 expect(tos.isBroadcast, false); 47 expect(tos.isBroadcast, false);
48 c..add(42)..add(42)..close(); // Faster than a timeout! 48 c..add(42)..add(42)..close(); // Faster than a timeout!
49 }); 49 });
50 50
51 test("stream timeout after events", () { 51 test("stream timeout after events", () {
52 StreamController c = new StreamController(); 52 StreamController c = new StreamController();
53 Stream tos = c.stream.timeout(halfSec); 53 Stream tos = c.stream.timeout(twoSecs);
54 expect(tos.isBroadcast, false); 54 expect(tos.isBroadcast, false);
55 int ctr = 0; 55 int ctr = 0;
56 tos.listen((v) { 56 tos.listen((v) {
57 expect(v, 42); 57 expect(v, 42);
58 ctr++; 58 ctr++;
59 }, 59 },
60 onError: expectAsync2((e, s) { 60 onError: expectAsync2((e, s) {
61 expect(ctr, 2); 61 expect(ctr, 2);
62 expect(e, new isInstanceOf<TimeoutException>()); 62 expect(e, new isInstanceOf<TimeoutException>());
63 })); 63 }));
(...skipping 26 matching lines...) Expand all
90 expect(tos.isBroadcast, false); 90 expect(tos.isBroadcast, false);
91 tos.handleError(expectAsync2((e, s) { 91 tos.handleError(expectAsync2((e, s) {
92 expect(e, new isInstanceOf<TimeoutException>()); 92 expect(e, new isInstanceOf<TimeoutException>());
93 expect(s, null); 93 expect(s, null);
94 })).listen((v){ fail("Unexpected event"); }); 94 })).listen((v){ fail("Unexpected event"); });
95 }); 95 });
96 96
97 test("events prevent timeout", () { 97 test("events prevent timeout", () {
98 Stopwatch sw = new Stopwatch(); 98 Stopwatch sw = new Stopwatch();
99 StreamController c = new StreamController(); 99 StreamController c = new StreamController();
100 Stream tos = c.stream.timeout(halfSec, onTimeout: (_) { 100 Stream tos = c.stream.timeout(twoSecs, onTimeout: (_) {
101 int elapsed = sw.elapsedMilliseconds; 101 int elapsed = sw.elapsedMilliseconds;
102 if (elapsed > 250) { 102 if (elapsed > 250) {
103 // This should not happen, but it does occasionally. 103 // This should not happen, but it does occasionally.
104 // Starving the periodic timer has made the test useless. 104 // Starving the periodic timer has made the test useless.
105 print("Periodic timer of 5 ms delayed $elapsed ms."); 105 print("Periodic timer of 5 ms delayed $elapsed ms.");
106 return; 106 return;
107 } 107 }
108 fail("Timeout not prevented by events"); 108 fail("Timeout not prevented by events");
109 throw "ERROR"; 109 throw "ERROR";
110 }); 110 });
111 tos.listen((v) { expect(v, 42);}, onDone: expectAsync0((){})); 111 // Start the periodic timer before we start listening to the stream.
112 // This should reduce the flakiness of the test.
112 int ctr = 200; // send this many events at 5ms intervals. Then close. 113 int ctr = 200; // send this many events at 5ms intervals. Then close.
113 new Timer.periodic(ms5, (timer) { 114 new Timer.periodic(ms5, (timer) {
114 sw.reset(); 115 sw.reset();
115 c.add(42); 116 c.add(42);
116 if (--ctr == 0) { 117 if (--ctr == 0) {
117 timer.cancel(); 118 timer.cancel();
118 c.close(); 119 c.close();
119 } 120 }
120 }); 121 });
121 sw.start(); 122 sw.start();
123
124 tos.listen((v) { expect(v, 42);}, onDone: expectAsync0((){}));
122 }); 125 });
123 126
124 test("errors prevent timeout", () { 127 test("errors prevent timeout", () {
125 Stopwatch sw = new Stopwatch(); 128 Stopwatch sw = new Stopwatch();
126 StreamController c = new StreamController(); 129 StreamController c = new StreamController();
127 Stream tos = c.stream.timeout(halfSec, onTimeout: (_) { 130 Stream tos = c.stream.timeout(twoSecs, onTimeout: (_) {
128 int elapsed = sw.elapsedMilliseconds; 131 int elapsed = sw.elapsedMilliseconds;
129 if (elapsed > 250) { 132 if (elapsed > 250) {
130 // This should not happen, but it does occasionally. 133 // This should not happen, but it does occasionally.
131 // Starving the periodic timer has made the test useless. 134 // Starving the periodic timer has made the test useless.
132 print("Periodic timer of 5 ms delayed $elapsed ms."); 135 print("Periodic timer of 5 ms delayed $elapsed ms.");
133 return; 136 return;
134 } 137 }
135 fail("Timeout not prevented by errors"); 138 fail("Timeout not prevented by errors");
136 }); 139 });
137 tos.listen((_) {}, 140
138 onError: (e, s) { 141 // Start the periodic timer before we start listening to the stream.
139 expect(e, "ERROR"); 142 // This should reduce the flakiness of the test.
140 },
141 onDone: expectAsync0((){}));
142 int ctr = 200; // send this many error events at 5ms intervals. Then close. 143 int ctr = 200; // send this many error events at 5ms intervals. Then close.
143 new Timer.periodic(ms5, (timer) { 144 new Timer.periodic(ms5, (timer) {
144 sw.reset(); 145 sw.reset();
145 c.addError("ERROR"); 146 c.addError("ERROR");
146 if (--ctr == 0) { 147 if (--ctr == 0) {
147 timer.cancel(); 148 timer.cancel();
148 c.close(); 149 c.close();
149 } 150 }
150 }); 151 });
151 sw.start(); 152 sw.start();
153
154 tos.listen((_) {},
155 onError: (e, s) {
156 expect(e, "ERROR");
157 },
158 onDone: expectAsync0((){}));
152 }); 159 });
153 160
154 test("closing prevents timeout", () { 161 test("closing prevents timeout", () {
155 StreamController c = new StreamController(); 162 StreamController c = new StreamController();
156 Stream tos = c.stream.timeout(halfSec, onTimeout: (_) { 163 Stream tos = c.stream.timeout(twoSecs, onTimeout: (_) {
157 fail("Timeout not prevented by close"); 164 fail("Timeout not prevented by close");
158 }); 165 });
159 tos.listen((_) {}, onDone: expectAsync0((){})); 166 tos.listen((_) {}, onDone: expectAsync0((){}));
160 c.close(); 167 c.close();
161 }); 168 });
162 169
163 test("pausing prevents timeout", () { 170 test("pausing prevents timeout", () {
164 StreamController c = new StreamController(); 171 StreamController c = new StreamController();
165 Stream tos = c.stream.timeout(ms5, onTimeout: (_) { 172 Stream tos = c.stream.timeout(ms5, onTimeout: (_) {
166 fail("Timeout not prevented by close"); 173 fail("Timeout not prevented by close");
167 }); 174 });
168 var subscription = tos.listen((_) {}, onDone: expectAsync0((){})); 175 var subscription = tos.listen((_) {}, onDone: expectAsync0((){}));
169 subscription.pause(); 176 subscription.pause();
170 new Timer(halfSec, () { 177 new Timer(twoSecs, () {
171 c.close(); 178 c.close();
172 subscription.resume(); 179 subscription.resume();
173 }); 180 });
174 }); 181 });
175 } 182 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698