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

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

Issue 2802973005: Migrate async tests to strong (Closed)
Patch Set: Created 3 years, 8 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
OLDNEW
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4
5 import "dart:async";
6 import "package:unittest/unittest.dart";
7
8 main() {
9 const ms5 = const Duration(milliseconds: 5);
10 const twoSecs = const Duration(seconds: 2);
11
12 test("stream timeout", () {
13 StreamController c = new StreamController();
14 Stream tos = c.stream.timeout(ms5);
15 expect(tos.isBroadcast, false);
16 tos.handleError(expectAsync((e, s) {
17 expect(e, new isInstanceOf<TimeoutException>());
18 expect(s, null);
19 })).listen((v){ fail("Unexpected event"); });
20 });
21
22 test("stream timeout add events", () {
23 StreamController c = new StreamController();
24 Stream tos = c.stream.timeout(ms5, onTimeout: (sink) {
25 sink.add(42);
26 sink.addError("ERROR");
27 sink.close();
28 });
29 expect(tos.isBroadcast, false);
30 tos.listen(expectAsync((v) { expect(v, 42); }),
31 onError: expectAsync((e, s) { expect(e, "ERROR"); }),
32 onDone: expectAsync((){}));
33 });
34
35 test("stream no timeout", () {
36 StreamController c = new StreamController();
37 Stream tos = c.stream.timeout(twoSecs);
38 int ctr = 0;
39 tos.listen((v) {
40 expect(v, 42);
41 ctr++;
42 },
43 onError: (e, s) { fail("No error expected"); },
44 onDone: expectAsync(() {
45 expect(ctr, 2);
46 }));
47 expect(tos.isBroadcast, false);
48 c..add(42)..add(42)..close(); // Faster than a timeout!
49 });
50
51 test("stream timeout after events", () {
52 StreamController c = new StreamController();
53 Stream tos = c.stream.timeout(twoSecs);
54 expect(tos.isBroadcast, false);
55 int ctr = 0;
56 tos.listen((v) {
57 expect(v, 42);
58 ctr++;
59 },
60 onError: expectAsync((e, s) {
61 expect(ctr, 2);
62 expect(e, new isInstanceOf<TimeoutException>());
63 }));
64 c..add(42)..add(42); // No close, timeout after two events.
65 });
66
67 test("broadcast stream timeout", () {
68 StreamController c = new StreamController.broadcast();
69 Stream tos = c.stream.timeout(ms5);
70 expect(tos.isBroadcast, true);
71 tos.handleError(expectAsync((e, s) {
72 expect(e, new isInstanceOf<TimeoutException>());
73 expect(s, null);
74 })).listen((v){ fail("Unexpected event"); });
75 });
76
77 test("asBroadcast stream timeout", () {
78 StreamController c = new StreamController.broadcast();
79 Stream tos = c.stream.asBroadcastStream().timeout(ms5);
80 expect(tos.isBroadcast, true);
81 tos.handleError(expectAsync((e, s) {
82 expect(e, new isInstanceOf<TimeoutException>());
83 expect(s, null);
84 })).listen((v){ fail("Unexpected event"); });
85 });
86
87 test("mapped stream timeout", () {
88 StreamController c = new StreamController();
89 Stream tos = c.stream.map((x) => 2 * x).timeout(ms5);
90 expect(tos.isBroadcast, false);
91 tos.handleError(expectAsync((e, s) {
92 expect(e, new isInstanceOf<TimeoutException>());
93 expect(s, null);
94 })).listen((v){ fail("Unexpected event"); });
95 });
96
97 test("events prevent timeout", () {
98 Stopwatch sw = new Stopwatch();
99 StreamController c = new StreamController();
100 Stream tos = c.stream.timeout(twoSecs, onTimeout: (_) {
101 int elapsed = sw.elapsedMilliseconds;
102 if (elapsed > 250) {
103 // This should not happen, but it does occasionally.
104 // Starving the periodic timer has made the test useless.
105 print("Periodic timer of 5 ms delayed $elapsed ms.");
106 return;
107 }
108 fail("Timeout not prevented by events");
109 throw "ERROR";
110 });
111 // Start the periodic timer before we start listening to the stream.
112 // This should reduce the flakiness of the test.
113 int ctr = 200; // send this many events at 5ms intervals. Then close.
114 new Timer.periodic(ms5, (timer) {
115 sw.reset();
116 c.add(42);
117 if (--ctr == 0) {
118 timer.cancel();
119 c.close();
120 }
121 });
122 sw.start();
123
124 tos.listen((v) { expect(v, 42);}, onDone: expectAsync((){}));
125 });
126
127 test("errors prevent timeout", () {
128 Stopwatch sw = new Stopwatch();
129 StreamController c = new StreamController();
130 Stream tos = c.stream.timeout(twoSecs, onTimeout: (_) {
131 int elapsed = sw.elapsedMilliseconds;
132 if (elapsed > 250) {
133 // This should not happen, but it does occasionally.
134 // Starving the periodic timer has made the test useless.
135 print("Periodic timer of 5 ms delayed $elapsed ms.");
136 return;
137 }
138 fail("Timeout not prevented by errors");
139 });
140
141 // Start the periodic timer before we start listening to the stream.
142 // This should reduce the flakiness of the test.
143 int ctr = 200; // send this many error events at 5ms intervals. Then close.
144 new Timer.periodic(ms5, (timer) {
145 sw.reset();
146 c.addError("ERROR");
147 if (--ctr == 0) {
148 timer.cancel();
149 c.close();
150 }
151 });
152 sw.start();
153
154 tos.listen((_) {},
155 onError: (e, s) {
156 expect(e, "ERROR");
157 },
158 onDone: expectAsync((){}));
159 });
160
161 test("closing prevents timeout", () {
162 StreamController c = new StreamController();
163 Stream tos = c.stream.timeout(twoSecs, onTimeout: (_) {
164 fail("Timeout not prevented by close");
165 });
166 tos.listen((_) {}, onDone: expectAsync((){}));
167 c.close();
168 });
169
170 test("pausing prevents timeout", () {
171 StreamController c = new StreamController();
172 Stream tos = c.stream.timeout(ms5, onTimeout: (_) {
173 fail("Timeout not prevented by close");
174 });
175 var subscription = tos.listen((_) {}, onDone: expectAsync((){}));
176 subscription.pause();
177 new Timer(twoSecs, () {
178 c.close();
179 subscription.resume();
180 });
181 });
182 }
OLDNEW
« no previous file with comments | « tests/lib_strong/async/stream_take_test.dart ('k') | tests/lib_strong/async/stream_transform_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698