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:io'; |
7 import 'dart:async'; | 8 import 'dart:async'; |
8 | 9 |
9 import '../lib/src/utils.dart'; | 10 import '../lib/src/utils.dart'; |
10 import '../lib/src/mock_clock.dart' as mock_clock; | 11 import '../lib/src/mock_clock.dart' as mock_clock; |
11 | 12 |
12 export '../lib/src/utils.dart'; | 13 export '../lib/src/utils.dart'; |
13 | 14 |
14 /// Wraps [input] to provide a timeout. If [input] completes before | 15 /// Wraps [input] to provide a timeout. If [input] completes before |
15 /// [milliseconds] have passed, then the return value completes in the same way. | 16 /// [milliseconds] have passed, then the return value completes in the same way. |
16 /// However, if [milliseconds] pass before [input] has completed, [onTimeout] is | 17 /// However, if [milliseconds] pass before [input] has completed, [onTimeout] is |
(...skipping 20 matching lines...) Expand all Loading... |
37 } | 38 } |
38 | 39 |
39 /// Returns a [Future] that will complete in [milliseconds]. | 40 /// Returns a [Future] that will complete in [milliseconds]. |
40 Future sleep(int milliseconds) { | 41 Future sleep(int milliseconds) { |
41 var completer = new Completer(); | 42 var completer = new Completer(); |
42 mock_clock.newTimer(new Duration(milliseconds: milliseconds), () { | 43 mock_clock.newTimer(new Duration(milliseconds: milliseconds), () { |
43 completer.complete(); | 44 completer.complete(); |
44 }); | 45 }); |
45 return completer.future; | 46 return completer.future; |
46 } | 47 } |
| 48 |
| 49 // TODO(nweiz): Remove this when issue 9252 is fixed. |
| 50 /// Asynchronously recursively deletes [dir]. Returns a [Future] that completes |
| 51 /// when the deletion is done. |
| 52 Future deleteDir(String dir) => |
| 53 _attemptRetryable(() => new Directory(dir).delete(recursive: true)); |
| 54 |
| 55 /// On Windows, we sometimes get failures where the directory is still in use |
| 56 /// when we try to do something with it. This is usually because the OS hasn't |
| 57 /// noticed yet that a process using that directory has closed. To be a bit |
| 58 /// more resilient, we wait and retry a few times. |
| 59 /// |
| 60 /// Takes a [callback] which returns a future for the operation being attempted. |
| 61 /// If that future completes with an error, it will slepp and then [callback] |
| 62 /// will be invoked again to retry the operation. It will try a few times before |
| 63 /// giving up. |
| 64 Future _attemptRetryable(Future callback()) { |
| 65 // Only do lame retry logic on Windows. |
| 66 if (Platform.operatingSystem != 'windows') return callback(); |
| 67 |
| 68 var attempts = 0; |
| 69 makeAttempt(_) { |
| 70 attempts++; |
| 71 return callback().catchError((e) { |
| 72 if (attempts >= 10) { |
| 73 throw 'Could not complete operation. Gave up after $attempts attempts.'; |
| 74 } |
| 75 |
| 76 return sleep(500).then(makeAttempt); |
| 77 }); |
| 78 } |
| 79 |
| 80 return makeAttempt(null); |
| 81 } |
OLD | NEW |