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:io'; |
8 import 'dart:async'; | 8 import 'dart:async'; |
9 | 9 |
10 import '../lib/src/utils.dart'; | 10 import '../lib/src/utils.dart'; |
(...skipping 27 matching lines...) Expand all Loading... |
38 } | 38 } |
39 | 39 |
40 /// Returns a [Future] that will complete in [milliseconds]. | 40 /// Returns a [Future] that will complete in [milliseconds]. |
41 Future sleep(int milliseconds) { | 41 Future sleep(int milliseconds) { |
42 var completer = new Completer(); | 42 var completer = new Completer(); |
43 mock_clock.newTimer(new Duration(milliseconds: milliseconds), () { | 43 mock_clock.newTimer(new Duration(milliseconds: milliseconds), () { |
44 completer.complete(); | 44 completer.complete(); |
45 }); | 45 }); |
46 return completer.future; | 46 return completer.future; |
47 } | 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 |