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

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

Issue 137143014: Attempt to make stream-timeout test less flaky. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 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
« 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 halfSec = const Duration(milliseconds: 500);
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
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(halfSec, onTimeout: (_) {
101 int elapsed = sw.elapsedMilliseconds; 101 int elapsed = sw.elapsedMilliseconds;
102 if (elapsed > 250) { 102 if (elapsed > 250) {
103 // This should not happen. 103 // This should not happen, but it does occasionally.
104 // Starving the periodic timer has made the test useless.
104 print("Periodic timer of 5 ms delayed $elapsed ms."); 105 print("Periodic timer of 5 ms delayed $elapsed ms.");
106 return;
105 } 107 }
106 fail("Timeout not prevented by events"); 108 fail("Timeout not prevented by events");
107 throw "ERROR"; 109 throw "ERROR";
108 }); 110 });
109 tos.listen((v) { expect(v, 42);}, onDone: expectAsync0((){})); 111 tos.listen((v) { expect(v, 42);}, onDone: expectAsync0((){}));
110 int ctr = 200; // send this many events at 5ms intervals. Then close. 112 int ctr = 200; // send this many events at 5ms intervals. Then close.
111 new Timer.periodic(ms5, (timer) { 113 new Timer.periodic(ms5, (timer) {
112 sw.reset(); 114 sw.reset();
113 c.add(42); 115 c.add(42);
114 if (--ctr == 0) { 116 if (--ctr == 0) {
115 timer.cancel(); 117 timer.cancel();
116 c.close(); 118 c.close();
117 } 119 }
118 }); 120 });
119 sw.start(); 121 sw.start();
120 }); 122 });
121 123
122 test("errors prevent timeout", () { 124 test("errors prevent timeout", () {
123 Stopwatch sw = new Stopwatch(); 125 Stopwatch sw = new Stopwatch();
124 StreamController c = new StreamController(); 126 StreamController c = new StreamController();
125 Stream tos = c.stream.timeout(halfSec, onTimeout: (_) { 127 Stream tos = c.stream.timeout(halfSec, onTimeout: (_) {
126 int elapsed = sw.elapsedMilliseconds; 128 int elapsed = sw.elapsedMilliseconds;
127 if (elapsed > 250) { 129 if (elapsed > 250) {
128 // This should not happen. 130 // This should not happen, but it does occasionally.
131 // Starving the periodic timer has made the test useless.
129 print("Periodic timer of 5 ms delayed $elapsed ms."); 132 print("Periodic timer of 5 ms delayed $elapsed ms.");
133 return;
130 } 134 }
131 fail("Timeout not prevented by errors"); 135 fail("Timeout not prevented by errors");
132 }); 136 });
133 tos.listen((_) {}, 137 tos.listen((_) {},
134 onError: (e, s) { 138 onError: (e, s) {
135 expect(e, "ERROR"); 139 expect(e, "ERROR");
136 }, 140 },
137 onDone: expectAsync0((){})); 141 onDone: expectAsync0((){}));
138 int ctr = 200; // send this many error events at 5ms intervals. Then close. 142 int ctr = 200; // send this many error events at 5ms intervals. Then close.
139 new Timer.periodic(ms5, (timer) { 143 new Timer.periodic(ms5, (timer) {
(...skipping 22 matching lines...) Expand all
162 fail("Timeout not prevented by close"); 166 fail("Timeout not prevented by close");
163 }); 167 });
164 var subscription = tos.listen((_) {}, onDone: expectAsync0((){})); 168 var subscription = tos.listen((_) {}, onDone: expectAsync0((){}));
165 subscription.pause(); 169 subscription.pause();
166 new Timer(halfSec, () { 170 new Timer(halfSec, () {
167 c.close(); 171 c.close();
168 subscription.resume(); 172 subscription.resume();
169 }); 173 });
170 }); 174 });
171 } 175 }
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