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

Side by Side Diff: mojo/dart/apptests/dart_apptests/lib/src/echo_apptests.dart

Issue 2006093002: Dart: Futures -> Callbacks. (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: Move code from mojo_patch to proxy.dart Created 4 years, 6 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
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 library echo_apptests; 5 library echo_apptests;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 8
9 import 'package:mojo_apptest/apptest.dart'; 9 import 'package:mojo_apptest/apptest.dart';
10 import 'package:mojo/application.dart'; 10 import 'package:mojo/application.dart';
11 import 'package:mojo/bindings.dart'; 11 import 'package:mojo/bindings.dart';
12 import 'package:mojo/core.dart'; 12 import 'package:mojo/core.dart';
13 import 'package:_mojo_for_test_only/test/echo_service.mojom.dart'; 13 import 'package:_mojo_for_test_only/test/echo_service.mojom.dart';
14 14
15 echoApptests(Application application, String url) { 15 echoApptests(Application application, String url) {
16 group('Echo Service Apptests', () { 16 group('Echo Service Apptests', () {
17 test('String', () async { 17 test('String', () async {
18 var echo = EchoService.connectToService(application, "mojo:dart_echo"); 18 var echo = EchoService.connectToService(application, "mojo:dart_echo");
19 19
20 var v = await echo.echoString("foo"); 20 var c = new Completer();
21 expect(v.value, equals("foo")); 21 echo.echoString("foo", (String value) {
22 c.complete(value);
23 });
24 expect(await echo.responseOrError(c.future), equals("foo"));
22 25
23 var q = await echo.echoString("quit"); 26 c = new Completer();
24 expect(q.value, equals("quit")); 27 echo.echoString("quit", (String value) {
28 fail("unreachable");
29 });
30 try {
31 await echo.responseOrError(c.future);
32 fail("unreachable");
33 } catch (e) {
34 expect(e is ProxyError, isTrue);
35 }
25 36
26 await echo.close(); 37 await echo.close();
27 }); 38 });
28 39
29 test('Empty String', () async { 40 test('Empty String', () async {
30 var echo = EchoService.connectToService(application, "mojo:dart_echo"); 41 var echo = EchoService.connectToService(application, "mojo:dart_echo");
31 42
32 var v = await echo.echoString(""); 43 var c = new Completer();
33 expect(v.value, equals("")); 44 echo.echoString("", (String value) {
45 c.complete(value);
46 });
47 expect(await echo.responseOrError(c.future), equals(""));
34 48
35 var q = await echo.echoString("quit"); 49 c = new Completer();
36 expect(q.value, equals("quit")); 50 echo.echoString("quit", (String value) {
51 fail("unreachable");
52 });
53 try {
54 await echo.responseOrError(c.future);
55 fail("unreachable");
56 } catch (e) {
57 expect(e is ProxyError, isTrue);
58 }
37 59
38 await echo.close(); 60 await echo.close();
39 }); 61 });
40 62
41 test('Null String', () async { 63 test('Null String', () async {
42 var echo = EchoService.connectToService(application, "mojo:dart_echo"); 64 var echo = EchoService.connectToService(application, "mojo:dart_echo");
43 65
44 var v = await echo.echoString(null); 66 var c = new Completer();
45 expect(v.value, equals(null)); 67 echo.echoString(null, (String value) {
68 c.complete(value);
69 });
70 expect(await echo.responseOrError(c.future), equals(null));
46 71
47 var q = await echo.echoString("quit"); 72 c = new Completer();
48 expect(q.value, equals("quit")); 73 echo.echoString("quit", (String value) {
74 fail("unreachable");
75 });
76 try {
77 await echo.responseOrError(c.future);
78 fail("unreachable");
79 } catch (e) {
80 expect(e is ProxyError, isTrue);
81 }
49 82
50 await echo.close(); 83 await echo.close();
51 }); 84 });
52 85
53 test('Delayed Success', () async { 86 test('Delayed Success', () async {
54 var echo = EchoService.connectToService(application, "mojo:dart_echo"); 87 var echo = EchoService.connectToService(application, "mojo:dart_echo");
55 88
56 var milliseconds = 100; 89 var milliseconds = 100;
57 var watch = new Stopwatch()..start(); 90 var watch = new Stopwatch()..start();
58 var v = await echo.delayedEchoString("foo", milliseconds); 91 var c = new Completer();
59 var elapsed = watch.elapsedMilliseconds; 92 echo.delayedEchoString("foo", milliseconds, (String value) {
60 expect(v.value, equals("foo")); 93 var elapsed = watch.elapsedMilliseconds;
61 expect(elapsed, greaterThanOrEqualTo(milliseconds)); 94 c.complete([value, elapsed]);
95 });
96 var result = await echo.responseOrError(c.future);
97 expect(result[0], equals("foo"));
98 expect(result[1], greaterThanOrEqualTo(milliseconds));
62 99
63 var q = await echo.echoString("quit"); 100 c = new Completer();
64 expect(q.value, equals("quit")); 101 echo.echoString("quit", (String value) {
102 fail("unreachable");
103 });
104 try {
105 await echo.responseOrError(c.future);
106 fail("unreachable");
107 } catch (e) {
108 expect(e is ProxyError, isTrue);
109 }
65 110
66 await echo.close(); 111 await echo.close();
67 }); 112 });
68 113
69 test('Delayed Close', () { 114 test('Delayed Close', () async {
70 var echo = EchoService.connectToService(application, "mojo:dart_echo"); 115 var echo = EchoService.connectToService(application, "mojo:dart_echo");
71 116
72 var milliseconds = 100; 117 var milliseconds = 100;
73 echo.responseOrError(echo.delayedEchoString( 118 var c = new Completer();
74 "quit", milliseconds)).then((result) { 119 echo.delayedEchoString("quit", milliseconds, (String value) {
75 fail('This future should not complete.'); 120 fail("unreachable");
76 }, onError: (e) { 121 });
122 try {
123 await echo.responseOrError(c.future);
124 fail("unreachable");
125 } catch (e) {
77 expect(e is ProxyError, isTrue); 126 expect(e is ProxyError, isTrue);
78 }); 127 };
79 128
80 return new Future.delayed( 129 return new Future.delayed(
81 new Duration(milliseconds: 10), () => echo.close()); 130 new Duration(milliseconds: 10), () => echo.close());
82 }); 131 });
83 132
84 test('Swap', () async { 133 test('Swap', () async {
85 var echo = EchoService.connectToService(application, "mojo:dart_echo"); 134 var echo = EchoService.connectToService(application, "mojo:dart_echo");
86 135
87 for (int i = 0; i < 10; i++) { 136 for (int i = 0; i < 10; i++) {
88 var v = await echo.responseOrError(echo.echoString("foo")); 137 var c = new Completer();
89 expect(v.value, equals("foo")); 138 echo.echoString("foo", (String value) {
139 c.complete(value);
140 });
141 expect(await echo.responseOrError(c.future), equals("foo"));
90 } 142 }
91 143
92 echo.ctrl.errorFuture.then((e) { 144 echo.ctrl.errorFuture.then((e) {
93 fail("echo: $e"); 145 fail("echo: $e");
94 }); 146 });
95 147
96 // Trigger an implementation swap in the echo server. 148 // Trigger an implementation swap in the echo server.
97 echo.swap(); 149 echo.swap();
98 150
99 expect(echo.ctrl.isBound, isTrue); 151 expect(echo.ctrl.isBound, isTrue);
100 152
101 for (int i = 0; i < 10; i++) { 153 for (int i = 0; i < 10; i++) {
102 var v = await echo.responseOrError(echo.echoString("foo")); 154 var c = new Completer();
103 expect(v.value, equals("foo")); 155 echo.echoString("foo", (String value) {
156 c.complete(value);
157 });
158 expect(await echo.responseOrError(c.future), equals("foo"));
104 } 159 }
105 160
106 var q = await echo.responseOrError(echo.echoString("quit"));
107 expect(q.value, equals("quit"));
108
109 await echo.close(); 161 await echo.close();
110 }); 162 });
111 163
112 test('Multiple Error Checks Success', () { 164 test('Multiple Error Checks Success', () {
113 var echo = EchoService.connectToService(application, "mojo:dart_echo"); 165 var echo = EchoService.connectToService(application, "mojo:dart_echo");
114 166
115 List<Future> futures = []; 167 List<Future> futures = [];
116 for (int i = 0; i < 100; i++) { 168 for (int i = 0; i < 100; i++) {
117 var f = echo.responseOrError(echo.echoString("foo")).then((r) { 169 var c = new Completer();
118 expect(r.value, equals("foo")); 170 echo.echoString("foo", (String value) {
119 }, onError: (e) { 171 c.complete(value);
120 fail('There should be no errors');
121 }); 172 });
122 futures.add(f); 173 futures.add(echo.responseOrError(c.future));
123 } 174 }
124 return Future.wait(futures).whenComplete(() => echo.close()); 175 return Future.wait(futures).whenComplete(() => echo.close());
125 }); 176 });
126 177
127 test('Multiple Error Checks Fail', () { 178 test('Multiple Error Checks Fail', () {
128 var echo = EchoService.connectToService(application, "mojo:dart_echo"); 179 var echo = EchoService.connectToService(application, "mojo:dart_echo");
129 180
130 List<Future> futures = []; 181 List<Future> futures = [];
131 var milliseconds = 100; 182 var milliseconds = 100;
132 for (int i = 0; i < 100; i++) { 183 for (int i = 0; i < 100; i++) {
133 var f = echo.responseOrError( 184 var c = new Completer();
134 echo.delayedEchoString("foo", milliseconds)).then((_) { 185 echo.delayedEchoString("foo", milliseconds, (String value) {
135 fail('This call should fail'); 186 fail("unreachable");
187 });
188 var f = echo.responseOrError(c.future).then((_) {
189 fail("unreachable");
136 }, onError: (e) { 190 }, onError: (e) {
137 expect(e is ProxyError, isTrue); 191 expect(e is ProxyError, isTrue);
138 }); 192 });
139 futures.add(f); 193 futures.add(f);
140 } 194 }
141 return echo.close().then((_) => Future.wait(futures)); 195 return echo.close().then((_) => Future.wait(futures));
142 }); 196 });
143 197
144 test('Uncaught Call Closed', () async { 198 test('Uncaught Call Closed', () async {
145 var echo = EchoService.connectToService(application, "mojo:dart_echo"); 199 var echo = EchoService.connectToService(application, "mojo:dart_echo");
146 200
147 // Do a normal call. 201 // Do a normal call.
148 var v = await echo.echoString("foo"); 202 var c = new Completer();
149 expect(v.value, equals("foo")); 203 echo.echoString("foo", (String value) {
204 c.complete(value);
205 });
206 expect(await echo.responseOrError(c.future), equals("foo"));
150 207
151 // Close the proxy. 208 // Close the proxy.
152 await echo.close(); 209 await echo.close();
153 210
154 // Try to do another call, which should not return. 211 // Try to do another call, which should not return.
155 echo.echoString("foo").then((_) { 212 echo.echoString("foo", (_) {
156 fail('This should be unreachable'); 213 fail('This should be unreachable');
157 }); 214 });
158 }); 215 });
159 216
160 test('Catch Call Closed', () async { 217 test('Catch Call Closed', () async {
161 var echo = EchoService.connectToService(application, "mojo:dart_echo"); 218 var echo = EchoService.connectToService(application, "mojo:dart_echo");
162 219
163 // Do a normal call. 220 // Do a normal call.
164 var v = await echo.echoString("foo"); 221 var c = new Completer();
165 expect(v.value, equals("foo")); 222 echo.echoString("foo", (String value) {
223 c.complete(value);
224 });
225 expect(await echo.responseOrError(c.future), equals("foo"));
166 226
167 // Close the proxy. 227 // Close the proxy.
168 await echo.close(); 228 await echo.close();
169 229
170 // Try to do another call, which should fail. 230 // Try to do another call, which should fail.
171 bool caughtException = false; 231 bool caughtException = false;
232 c = new Completer();
233 echo.echoString("foo", (String value) {
234 fail("unreachable");
235 });
172 try { 236 try {
173 v = await echo.responseOrError(echo.echoString("foo")); 237 await echo.responseOrError(c.future);
174 fail('This should be unreachable'); 238 fail('This should be unreachable');
175 } on ProxyError catch (e) { 239 } on ProxyError catch (e) {
176 caughtException = true; 240 caughtException = true;
177 } 241 }
178 expect(caughtException, isTrue); 242 expect(caughtException, isTrue);
179 }); 243 });
180 244
181 test('Catch Call Sequence Closed Twice', () async { 245 test('Catch Call Sequence Closed Twice', () async {
182 var echo = EchoService.connectToService(application, "mojo:dart_echo"); 246 var echo = EchoService.connectToService(application, "mojo:dart_echo");
183 247
184 // Do a normal call. 248 // Do a normal call.
185 var v = await echo.echoString("foo"); 249 var c = new Completer();
186 expect(v.value, equals("foo")); 250 echo.echoString("foo", (String value) {
251 c.complete(value);
252 });
253 expect(await echo.responseOrError(c.future), equals("foo"));
187 254
188 // Close the proxy. 255 // Close the proxy.
189 await echo.close(); 256 await echo.close();
190 257
191 // Try to do another call, which should fail. 258 // Try to do another call, which should fail.
192 bool caughtException = false; 259 bool caughtException = false;
260 c = new Completer();
261 echo.echoString("foo", (String value) {
262 fail("unreachable");
263 });
193 try { 264 try {
194 v = await echo.responseOrError(echo.echoString("foo")); 265 await echo.responseOrError(c.future);
195 fail('This should be unreachable'); 266 fail('This should be unreachable');
196 } on ProxyError catch (e) { 267 } on ProxyError catch (e) {
197 caughtException = true; 268 caughtException = true;
198 } 269 }
199 expect(caughtException, isTrue); 270 expect(caughtException, isTrue);
200 271
201 // Make sure we can catch an error more than once. 272 // Try to do another call, which should fail.
202 caughtException = false; 273 caughtException = false;
274 c = new Completer();
275 echo.echoString("foo", (String value) {
276 fail("unreachable");
277 });
203 try { 278 try {
204 v = await echo.responseOrError(echo.echoString("foo")); 279 await echo.responseOrError(c.future);
205 fail('This should be unreachable'); 280 fail('This should be unreachable');
206 } on ProxyError catch (e) { 281 } on ProxyError catch (e) {
207 caughtException = true; 282 caughtException = true;
208 } 283 }
209 expect(caughtException, isTrue); 284 expect(caughtException, isTrue);
210 }); 285 });
211 286
212 test('Catch Call Parallel Closed Twice', () async { 287 test('Catch Call Parallel Closed Twice', () async {
213 var echo = EchoService.connectToService(application, "mojo:dart_echo"); 288 var echo = EchoService.connectToService(application, "mojo:dart_echo");
214 289
215 // Do a normal call. 290 // Do a normal call.
216 var v = await echo.echoString("foo"); 291 var c = new Completer();
217 expect(v.value, equals("foo")); 292 echo.echoString("foo", (String value) {
293 c.complete(value);
294 });
295 expect(await echo.responseOrError(c.future), equals("foo"));
218 296
219 // Close the proxy. 297 // Close the proxy.
220 await echo.close(); 298 await echo.close();
221 299
222 // Queue up two calls after the close, and make sure they both fail. 300 // Queue up two calls after the close, and make sure they both fail.
223 var f1 = echo.responseOrError(echo.echoString("foo")).then((_) { 301 var c1 = new Completer();
224 fail('This should be unreachable'); 302 echo.echoString("foo", (String value) {
303 fail("unreachable");
304 });
305 var c2 = new Completer();
306 echo.echoString("foo", (String value) {
307 fail("unreachable");
308 });
309 var f1 = echo.responseOrError(c1.future).then((_) {
310 fail("unreachable");
225 }, onError: (e) { 311 }, onError: (e) {
226 expect(e is ProxyError, isTrue); 312 expect(e is ProxyError, isTrue);
227 }); 313 });
228 314 var f2 = echo.responseOrError(c2.future).then((_) {
229 var f2 = echo.responseOrError(echo.echoString("foo")).then((_) { 315 fail("unreachable");
230 fail('This should be unreachable');
231 }, onError: (e) { 316 }, onError: (e) {
232 expect(e is ProxyError, isTrue); 317 expect(e is ProxyError, isTrue);
233 }); 318 });
234 319
235 return Future.wait([f1, f2]); 320 return Future.wait([f1, f2]);
236 }); 321 });
237 322
238 test('Unbind, close', () async { 323 test('Unbind, Rebind, Close', () async {
239 var echo = EchoService.connectToService(application, "mojo:dart_echo"); 324 var echo = EchoService.connectToService(application, "mojo:dart_echo");
240 325
241 var r = await echo.responseOrError(echo.echoString("foo")); 326 // Do a normal call.
242 expect(r.value, equals("foo")); 327 var c = new Completer();
328 echo.echoString("foo", (String value) {
329 c.complete(value);
330 });
331 expect(await echo.responseOrError(c.future), equals("foo"));
243 332
244 var endpoint = echo.ctrl.unbind(); 333 var endpoint = echo.ctrl.unbind();
334 echo.ctrl.bind(endpoint);
335
336 c = new Completer();
337 echo.echoString("quit", (String value) {
338 fail("unreachable");
339 });
340 try {
341 await echo.responseOrError(c.future);
342 fail("unreachable");
343 } catch (e) {
344 expect(e is ProxyError, isTrue);
345 }
346
245 await echo.close(); 347 await echo.close();
246 endpoint.close();
247 }); 348 });
248 349
249 test('Unbind, rebind to same', () async { 350 test('Unbind, rebind to same', () async {
250 var echo = EchoService.connectToService(application, "mojo:dart_echo"); 351 var echo = EchoService.connectToService(application, "mojo:dart_echo");
251 352
252 var r = await echo.responseOrError(echo.echoString("foo")); 353 // Do a normal call.
253 expect(r.value, equals("foo")); 354 var c = new Completer();
355 echo.echoString("foo", (String value) {
356 c.complete(value);
357 });
358 expect(await echo.responseOrError(c.future), equals("foo"));
254 359
255 var endpoint = echo.ctrl.unbind(); 360 var endpoint = echo.ctrl.unbind();
256 echo.ctrl.bind(endpoint); 361 echo.ctrl.bind(endpoint);
257 362
258 r = await echo.responseOrError(echo.echoString("foo")); 363 // Do a normal call.
259 expect(r.value, equals("foo")); 364 c = new Completer();
365 echo.echoString("foo", (String value) {
366 c.complete(value);
367 });
368 expect(await echo.responseOrError(c.future), equals("foo"));
369
370 c = new Completer();
371 echo.echoString("quit", (String value) {
372 fail("unreachable");
373 });
374 try {
375 await echo.responseOrError(c.future);
376 fail("unreachable");
377 } catch (e) {
378 expect(e is ProxyError, isTrue);
379 }
260 380
261 await echo.close(); 381 await echo.close();
262 }); 382 });
263 383
264 test('Unbind, rebind to different', () async { 384 test('Unbind, rebind to different', () async {
265 var echo = EchoService.connectToService(application, "mojo:dart_echo"); 385 var echo = EchoService.connectToService(application, "mojo:dart_echo");
266 386
267 var r = await echo.responseOrError(echo.echoString("foo")); 387 // Do a normal call.
268 expect(r.value, equals("foo")); 388 var c = new Completer();
389 echo.echoString("foo", (String value) {
390 c.complete(value);
391 });
392 expect(await echo.responseOrError(c.future), equals("foo"));
269 393
270 var endpoint = echo.ctrl.unbind(); 394 var endpoint = echo.ctrl.unbind();
271 var differentEchoProxy = new EchoServiceProxy.fromEndpoint(endpoint); 395 var differentEchoProxy = new EchoServiceProxy.fromEndpoint(endpoint);
272 396
273 r = await differentEchoProxy.responseOrError( 397 // Do a normal call.
274 differentEchoProxy.echoString("foo")); 398 c = new Completer();
275 expect(r.value, equals("foo")); 399 differentEchoProxy.echoString("foo", (String value) {
400 c.complete(value);
401 });
402 expect(await differentEchoProxy.responseOrError(c.future), equals("foo"));
276 403
277 await differentEchoProxy.close(); 404 await differentEchoProxy.close();
278 }); 405 });
279 406
280 test('Unbind, rebind to different, close original', () async { 407 test('Unbind, rebind to different, close original', () async {
281 var echo = EchoService.connectToService(application, "mojo:dart_echo"); 408 var echo = EchoService.connectToService(application, "mojo:dart_echo");
282 409
283 var r = await echo.responseOrError(echo.echoString("foo")); 410 // Do a normal call.
284 expect(r.value, equals("foo")); 411 var c = new Completer();
412 echo.echoString("foo", (String value) {
413 c.complete(value);
414 });
415 expect(await echo.responseOrError(c.future), equals("foo"));
285 416
286 var endpoint = echo.ctrl.unbind(); 417 var endpoint = echo.ctrl.unbind();
287 var differentEchoProxy = new EchoServiceProxy.fromEndpoint(endpoint); 418 var differentEchoProxy = new EchoServiceProxy.fromEndpoint(endpoint);
288 await echo.close(); 419 await echo.close();
289 420
290 r = await differentEchoProxy.responseOrError( 421 // Do a normal call.
291 differentEchoProxy.echoString("foo")); 422 c = new Completer();
292 expect(r.value, equals("foo")); 423 differentEchoProxy.echoString("foo", (String value) {
424 c.complete(value);
425 });
426 expect(await differentEchoProxy.responseOrError(c.future), equals("foo"));
293 427
294 await differentEchoProxy.close(); 428 await differentEchoProxy.close();
295 }); 429 });
296 }); 430 });
297 } 431 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698