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 96473003: Add Stream.timeout method. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Document zone usage. Created 7 years 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 | « sdk/lib/async/stream.dart ('k') | tests/lib/lib.status » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 halfSec = const Duration(milliseconds: 500);
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(expectAsync2((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(expectAsync1((v) { expect(v, 42); }),
31 onError: expectAsync2((e, s) { expect(e, "ERROR"); }),
32 onDone: expectAsync0((){}));
33 });
34
35 test("stream no timeout", () {
36 StreamController c = new StreamController();
37 Stream tos = c.stream.timeout(halfSec);
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: expectAsync0(() {
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(halfSec);
54 expect(tos.isBroadcast, false);
55 int ctr = 0;
56 tos.listen((v) {
57 expect(v, 42);
58 ctr++;
59 },
60 onError: expectAsync2((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, false);
71 tos.handleError(expectAsync2((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, false);
81 tos.handleError(expectAsync2((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(expectAsync2((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(halfSec, onTimeout: (_) {
101 int elapsed = sw.elapsedMilliseconds;
102 if (elapsed > 250) {
103 // This should not happen.
104 print("Periodic timer of 5 ms delayed $elapsed ms.");
105 }
106 fail("Timeout not prevented by events");
107 throw "ERROR";
108 });
109 tos.listen((v) { expect(v, 42);}, onDone: expectAsync0((){}));
110 int ctr = 200; // send this many events at 5ms intervals. Then close.
111 new Timer.periodic(ms5, (timer) {
112 sw.reset();
113 c.add(42);
114 if (--ctr == 0) {
115 timer.cancel();
116 c.close();
117 }
118 });
119 sw.start();
120 });
121
122 test("errors prevent timeout", () {
123 Stopwatch sw = new Stopwatch();
124 StreamController c = new StreamController();
125 Stream tos = c.stream.timeout(halfSec, onTimeout: (_) {
126 int elapsed = sw.elapsedMilliseconds;
127 if (elapsed > 250) {
128 // This should not happen.
129 print("Periodic timer of 5 ms delayed $elapsed ms.");
130 }
131 fail("Timeout not prevented by errors");
132 });
133 tos.listen((_) {},
134 onError: (e, s) {
135 expect(e, "ERROR");
136 },
137 onDone: expectAsync0((){}));
138 int ctr = 200; // send this many error events at 5ms intervals. Then close.
139 new Timer.periodic(ms5, (timer) {
140 sw.reset();
141 c.addError("ERROR");
142 if (--ctr == 0) {
143 timer.cancel();
144 c.close();
145 }
146 });
147 sw.start();
148 });
149
150 test("closing prevents timeout", () {
151 StreamController c = new StreamController();
152 Stream tos = c.stream.timeout(halfSec, onTimeout: (_) {
153 fail("Timeout not prevented by close");
154 });
155 tos.listen((_) {}, onDone: expectAsync0((){}));
156 c.close();
157 });
158
159 test("pausing prevents timeout", () {
160 StreamController c = new StreamController();
161 Stream tos = c.stream.timeout(ms5, onTimeout: (_) {
162 fail("Timeout not prevented by close");
163 });
164 var subscription = tos.listen((_) {}, onDone: expectAsync0((){}));
165 subscription.pause();
166 new Timer(halfSec, () {
167 c.close();
168 subscription.resume();
169 });
170 });
171 }
OLDNEW
« no previous file with comments | « sdk/lib/async/stream.dart ('k') | tests/lib/lib.status » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698