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

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: Updated documentation. 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
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, (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 timeout add events, cancel", () {
36 StreamController c = new StreamController(onCancel: expectAsync0((){}));
37 Stream tos = c.stream.timeout(ms5, (sink, cancel) {
38 sink.add(42);
39 sink.addError("ERROR");
40 sink.close();
41 cancel();
42 });
43 expect(tos.isBroadcast, false);
44 tos.listen(expectAsync1((v) { expect(v, 42); }),
45 onError: expectAsync2((e, s) { expect(e, "ERROR"); }),
46 onDone: expectAsync0((){}));
47 });
48
49 test("stream no timeout", () {
50 StreamController c = new StreamController();
51 Stream tos = c.stream.timeout(halfSec);
52 int ctr = 0;
53 tos.listen((v) {
54 expect(v, 42);
55 ctr++;
56 },
57 onError: (e, s) { fail("No error expected"); },
58 onDone: expectAsync0(() {
59 expect(ctr, 2);
60 }));
61 expect(tos.isBroadcast, false);
62 c..add(42)..add(42)..close(); // Faster than a timeout!
63 });
64
65 test("stream timeout after events", () {
66 StreamController c = new StreamController();
67 Stream tos = c.stream.timeout(halfSec);
68 expect(tos.isBroadcast, false);
69 int ctr = 0;
70 tos.listen((v) {
71 expect(v, 42);
72 ctr++;
73 },
74 onError: expectAsync2((e, s) {
75 expect(ctr, 2);
76 expect(e, new isInstanceOf<TimeoutException>());
77 }));
78 c..add(42)..add(42); // No close, timeout after two events.
79 });
80
81 test("broadcast stream timeout", () {
82 StreamController c = new StreamController.broadcast();
83 Stream tos = c.stream.timeout(ms5);
84 expect(tos.isBroadcast, false);
85 tos.handleError(expectAsync2((e, s) {
86 expect(e, new isInstanceOf<TimeoutException>());
87 expect(s, null);
88 })).listen((v){ fail("Unexpected event"); });
89 });
90
91 test("asBroadcast stream timeout", () {
92 StreamController c = new StreamController.broadcast();
93 Stream tos = c.stream.asBroadcastStream().timeout(ms5);
94 expect(tos.isBroadcast, false);
95 tos.handleError(expectAsync2((e, s) {
96 expect(e, new isInstanceOf<TimeoutException>());
97 expect(s, null);
98 })).listen((v){ fail("Unexpected event"); });
99 });
100
101 test("mapped stream timeout", () {
102 StreamController c = new StreamController();
103 Stream tos = c.stream.map((x) => 2 * x).timeout(ms5);
104 expect(tos.isBroadcast, false);
105 tos.handleError(expectAsync2((e, s) {
106 expect(e, new isInstanceOf<TimeoutException>());
107 expect(s, null);
108 })).listen((v){ fail("Unexpected event"); });
109 });
110
111 test("events prevent timeout", () {
112 StreamController c = new StreamController();
113 Stream tos = c.stream.timeout(halfSec, () {
114 fail("Timeout not prevented by events");
115 });
116 tos.listen((v) { expect(v, 42);}, onDone: expectAsync0((){}));
117 int ctr = 200; // send this many events at 5ms intervals. Then close.
118 new Timer.periodic(ms5, (timer) {
floitsch 2013/11/29 13:43:48 This could be flaky. We have to guard against it.
Lasse Reichstein Nielsen 2013/11/29 13:58:03 It's using timers for both things. Will that not g
floitsch 2013/11/29 15:03:49 Only, if you can guarantee that the periodic timer
119 c.add(42);
120 if (--ctr == 0) {
121 timer.cancel();
122 c.close();
123 }
124 });
125 });
126
127 test("errors prevent timeout", () {
128 StreamController c = new StreamController();
129 Stream tos = c.stream.timeout(halfSec, () {
130 fail("Timeout not prevented by errors");
131 });
132 tos.listen((_) {},
133 onError: (e, s) {
134 expect(e, "ERROR");
135 },
136 onDone: expectAsync0((){}));
137 int ctr = 200; // send this many error events at 5ms intervals. Then close.
138 new Timer.periodic(ms5, (timer) {
floitsch 2013/11/29 13:43:48 ditto.
139 c.addError("ERROR");
140 if (--ctr == 0) {
141 timer.cancel();
142 c.close();
143 }
144 });
145 });
146
147 test("closing prevents timeout", () {
148 StreamController c = new StreamController();
149 Stream tos = c.stream.timeout(halfSec, () {
150 fail("Timeout not prevented by close");
151 });
152 tos.listen((_) {}, onDone: expectAsync0((){}));
153 c.close();
154 });
155
156 test("pausing prevents timeout", () {
157 StreamController c = new StreamController();
158 Stream tos = c.stream.timeout(ms5, () {
159 fail("Timeout not prevented by close");
160 });
161 var subscription = tos.listen((_) {}, onDone: expectAsync0((){}));
162 subscription.pause();
163 new Timer(halfSec, () {
164 c.close();
165 subscription.resume();
166 });
167 });
168 }
OLDNEW
« sdk/lib/async/stream.dart ('K') | « 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