OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library test.utils; | 5 library test.utils; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:convert'; | 8 import 'dart:convert'; |
9 import 'dart:math' as math; | 9 import 'dart:math' as math; |
10 | 10 |
11 import 'package:crypto/crypto.dart'; | 11 import 'package:crypto/crypto.dart'; |
12 import 'package:path/path.dart' as p; | 12 import 'package:path/path.dart' as p; |
13 import 'package:shelf/shelf.dart' as shelf; | 13 import 'package:shelf/shelf.dart' as shelf; |
14 import 'package:stack_trace/stack_trace.dart'; | 14 import 'package:stack_trace/stack_trace.dart'; |
15 | 15 |
16 import 'backend/operating_system.dart'; | 16 import 'backend/operating_system.dart'; |
| 17 import 'util/cancelable_future.dart'; |
17 import 'util/path_handler.dart'; | 18 import 'util/path_handler.dart'; |
| 19 import 'util/stream_queue.dart'; |
18 | 20 |
19 /// The maximum console line length. | 21 /// The maximum console line length. |
20 const _lineLength = 100; | 22 const _lineLength = 100; |
21 | 23 |
22 /// A typedef for a possibly-asynchronous function. | 24 /// A typedef for a possibly-asynchronous function. |
23 /// | 25 /// |
24 /// The return type should only ever by [Future] or void. | 26 /// The return type should only ever by [Future] or void. |
25 typedef AsyncFunction(); | 27 typedef AsyncFunction(); |
26 | 28 |
27 /// A typedef for a zero-argument callback function. | 29 /// A typedef for a zero-argument callback function. |
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
314 }, onError: (error, stackTrace) { | 316 }, onError: (error, stackTrace) { |
315 completer.completeError(error, stackTrace); | 317 completer.completeError(error, stackTrace); |
316 subscription.cancel(); | 318 subscription.cancel(); |
317 }, onDone: () { | 319 }, onDone: () { |
318 completer.complete(); | 320 completer.complete(); |
319 }); | 321 }); |
320 | 322 |
321 return completer.future; | 323 return completer.future; |
322 } | 324 } |
323 | 325 |
| 326 CancelableFuture cancelableNext(StreamQueue queue) { |
| 327 var fork = queue.fork(); |
| 328 var completer = new CancelableCompleter(() => fork.cancel(immediate: true)); |
| 329 completer.complete(fork.next.then((_) { |
| 330 fork.cancel(); |
| 331 return queue.next; |
| 332 })); |
| 333 return completer.future; |
| 334 } |
| 335 |
| 336 Future race(Iterable<CancelableFuture> futures) { |
| 337 var completer = new Completer.sync(); |
| 338 for (var future in futures) { |
| 339 future.then((value) { |
| 340 if (!completer.isCompleted) completer.complete(value); |
| 341 }).catchError((error, stackTrace) { |
| 342 if (!completer.isCompleted) completer.completeError(error, stackTrace); |
| 343 }); |
| 344 } |
| 345 |
| 346 return completer.future.whenComplete(() { |
| 347 for (var future in futures) { |
| 348 future.cancel(); |
| 349 } |
| 350 }); |
| 351 } |
| 352 |
324 /// Returns a stream that emits [error] and [stackTrace], then closes. | 353 /// Returns a stream that emits [error] and [stackTrace], then closes. |
325 /// | 354 /// |
326 /// This is useful for adding errors to streams defined via `async*`. | 355 /// This is useful for adding errors to streams defined via `async*`. |
327 Stream errorStream(error, StackTrace stackTrace) { | 356 Stream errorStream(error, StackTrace stackTrace) { |
328 var controller = new StreamController(); | 357 var controller = new StreamController(); |
329 controller.addError(error, stackTrace); | 358 controller.addError(error, stackTrace); |
330 controller.close(); | 359 controller.close(); |
331 return controller.stream; | 360 return controller.stream; |
332 } | 361 } |
333 | 362 |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
390 } | 419 } |
391 } | 420 } |
392 | 421 |
393 /// Returns middleware that nests all requests beneath the URL prefix [beneath]. | 422 /// Returns middleware that nests all requests beneath the URL prefix [beneath]. |
394 shelf.Middleware nestingMiddleware(String beneath) { | 423 shelf.Middleware nestingMiddleware(String beneath) { |
395 return (handler) { | 424 return (handler) { |
396 var pathHandler = new PathHandler()..add(beneath, handler); | 425 var pathHandler = new PathHandler()..add(beneath, handler); |
397 return pathHandler.handler; | 426 return pathHandler.handler; |
398 }; | 427 }; |
399 } | 428 } |
OLD | NEW |