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

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

Powered by Google App Engine
This is Rietveld 408576698