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

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

Issue 91213002: Add Future.timeout. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Update indentation. 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 | « tests/lib/async/future_test.dart ('k') | tests/lib/async/future_value_chain4_test.dart » ('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) 2011, 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 library future_timeout_test;
6
7 import 'dart:async';
8 import '../../../pkg/unittest/lib/unittest.dart';
9
10 main() {
11 test("timeoutNoComplete", () {
12 Completer completer = new Completer();
13 Future timedOut = completer.future.timeout(
14 const Duration(milliseconds: 5), () => 42);
15 timedOut.then(expectAsync1((v) {
16 expect(v, 42);
17 }));
18 });
19
20 test("timeoutCompleteAfterTimeout", () {
21 Completer completer = new Completer();
22 Future timedOut = completer.future.timeout(
23 const Duration(milliseconds: 5), () => 42);
24 Timer timer = new Timer(const Duration(seconds: 1), () {
25 completer.complete(-1);
26 });
27 timedOut.then(expectAsync1((v) {
28 expect(v, 42);
29 }));
30 });
31
32 test("timeoutCompleteBeforeTimeout", () {
33 Completer completer = new Completer();
34 Timer timer = new Timer(const Duration(milliseconds: 5), () {
35 completer.complete(42);
36 });
37 Future timedOut = completer.future.timeout(
38 const Duration(seconds: 1), () => -1);
39 timedOut.then(expectAsync1((v) {
40 expect(v, 42);
41 }));
42 });
43
44 test("timeoutCompleteBeforeCreate", () {
45 Completer completer = new Completer.sync();
46 completer.complete(42);
47 Future timedOut = completer.future.timeout(
48 const Duration(milliseconds: 5), () => -1);
49 timedOut.then(expectAsync1((v) {
50 expect(v, 42);
51 }));
52 });
53
54 test("timeoutThrows", () {
55 Completer completer = new Completer();
56 Future timedOut = completer.future.timeout(
57 const Duration(milliseconds: 5), () { throw "EXN1"; });
58 timedOut.catchError(expectAsync2((e, s) {
59 expect(e, "EXN1");
60 }));
61 });
62
63 test("timeoutThrowAfterTimeout", () {
64 Completer completer = new Completer();
65 Future timedOut = completer.future.timeout(
66 const Duration(milliseconds: 5), () => 42);
67 Timer timer = new Timer(const Duration(seconds: 1), () {
68 completer.completeError("EXN2");
69 });
70 timedOut.then(expectAsync1((v) {
71 expect(v, 42);
72 }));
73 });
74
75 test("timeoutThrowBeforeTimeout", () {
76 Completer completer = new Completer();
77 Timer timer = new Timer(const Duration(milliseconds: 5), () {
78 completer.completeError("EXN3");
79 });
80 Future timedOut = completer.future.timeout(
81 const Duration(seconds: 1), () => -1);
82 timedOut.catchError(expectAsync2((e, s) {
83 expect(e, "EXN3");
84 }));
85 });
86
87 test("timeoutThrowBeforeCreate", () {
88 // Prevent uncaught error when we create the error.
89 Completer completer = new Completer.sync()..future.catchError((e){});
90 completer.completeError("EXN4");
91 Future timedOut = completer.future.timeout(
92 const Duration(milliseconds: 5), () => -1);
93 timedOut.catchError(expectAsync2((e, s) {
94 expect(e, "EXN4");
95 }));
96 });
97
98 test("timeoutReturnFutureValue", () {
99 Future result = new Future.value(42);
100 Completer completer = new Completer();
101 Future timedOut = completer.future.timeout(
102 const Duration(milliseconds: 5), () => result);
103 timedOut.then(expectAsync1((v) {
104 expect(v, 42);
105 }));
106 });
107
108 test("timeoutReturnFutureError", () {
109 Future result = new Future.error("EXN5")..catchError((e){});
110 Completer completer = new Completer();
111 Future timedOut = completer.future.timeout(
112 const Duration(milliseconds: 5), () => result);
113 timedOut.catchError(expectAsync2((e, s) {
114 expect(e, "EXN5");
115 }));
116 });
117
118 test("timeoutReturnFutureValueLater", () {
119 Completer result = new Completer();
120 Completer completer = new Completer();
121 Future timedOut = completer.future.timeout(
122 const Duration(milliseconds: 5), () {
123 result.complete(42);
124 return result.future;
125 });
126 timedOut.then(expectAsync1((v) {
127 expect(v, 42);
128 }));
129 });
130
131 test("timeoutReturnFutureErrorLater", () {
132 Completer result = new Completer();
133 Completer completer = new Completer();
134 Future timedOut = completer.future.timeout(
135 const Duration(milliseconds: 5), () {
136 result.completeError("EXN6");
137 return result.future;
138 });
139 timedOut.catchError(expectAsync2((e, s) {
140 expect(e, "EXN6");
141 }));
142 });
143
144 test("timeoutZone", () {
145 Zone forked;
146 int registerCallDelta = 0;
147 bool callbackCalled = false;
148 Function callback = () {
149 expect(callbackCalled, false);
150 callbackCalled = true;
151 expect(Zone.current, forked);
152 return 42;
153 };
154 forked = Zone.current.fork(specification: new ZoneSpecification(
155 registerCallback: (Zone self, ZoneDelegate parent, Zone origin, f()) {
156 if (!identical(f, callback)) return f;
157 registerCallDelta++; // Increment calls to register.
158 expect(origin, forked);
159 expect(self, forked);
160 return expectAsync0(() { registerCallDelta--; return f(); });
161 }
162 ));
163 Completer completer = new Completer();
164 Future timedOut;
165 forked.run(() {
166 timedOut = completer.future.timeout(const Duration(milliseconds: 5),
167 callback);
168 });
169 timedOut.then(expectAsync1((v) {
170 expect(callbackCalled, true);
171 expect(registerCallDelta, 0);
172 expect(Zone.current, Zone.ROOT);
173 expect(v, 42);
174 }));
175 });
176
177 test("timeoutNoFunction", () {
178 Completer completer = new Completer();
179 Future timedOut = completer.future.timeout(
180 const Duration(milliseconds: 5));
181 timedOut.catchError(expectAsync2((e, s) {
182 expect(e, new isInstanceOf<TimeoutException>());
183 expect(e.duration, const Duration(milliseconds: 5));
184 expect(s, null);
185 }));
186 });
187 }
OLDNEW
« no previous file with comments | « tests/lib/async/future_test.dart ('k') | tests/lib/async/future_value_chain4_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698