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:io'; | 8 import 'dart:io'; |
9 import 'dart:json' as json; | 9 import 'dart:json' as json; |
10 import 'dart:uri'; | 10 import 'dart:uri'; |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 if (lines[j].length > 8) { | 131 if (lines[j].length > 8) { |
132 lines[j] = lines[j].substring(8, lines[j].length); | 132 lines[j] = lines[j].substring(8, lines[j].length); |
133 } else { | 133 } else { |
134 lines[j] = ''; | 134 lines[j] = ''; |
135 } | 135 } |
136 } | 136 } |
137 | 137 |
138 return lines.join('\n'); | 138 return lines.join('\n'); |
139 } | 139 } |
140 | 140 |
141 // TODO(nweiz): Remove this when issue 9252 is fixed. | |
142 /// Asynchronously recursively deletes [dir]. Returns a [Future] that completes | |
143 /// when the deletion is done. | |
144 Future deleteDir(String dir) => | |
145 _attemptRetryable(() => new Directory(dir).delete(recursive: true)); | |
146 | |
147 /// On Windows, we sometimes get failures where the directory is still in use | |
148 /// when we try to do something with it. This is usually because the OS hasn't | |
149 /// noticed yet that a process using that directory has closed. To be a bit | |
150 /// more resilient, we wait and retry a few times. | |
151 /// | |
152 /// Takes a [callback] which returns a future for the operation being attempted. | |
153 /// If that future completes with an error, it will slepp and then [callback] | |
154 /// will be invoked again to retry the operation. It will try a few times before | |
155 /// giving up. | |
156 Future _attemptRetryable(Future callback()) { | |
157 // Only do lame retry logic on Windows. | |
158 if (Platform.operatingSystem != 'windows') return callback(); | |
159 | |
160 var attempts = 0; | |
161 makeAttempt(_) { | |
162 attempts++; | |
163 return callback().catchError((e) { | |
164 if (attempts >= 10) { | |
165 throw 'Could not complete operation. Gave up after $attempts attempts.'; | |
166 } | |
167 | |
168 return new Future.delayed(new Duration(milliseconds: 500)) | |
169 .then(makeAttempt); | |
170 }); | |
171 } | |
172 | |
173 return makeAttempt(null); | |
174 } | |
175 | |
176 /// A matcher that matches JSON that parses to a value that matches the inner | 141 /// A matcher that matches JSON that parses to a value that matches the inner |
177 /// matcher. | 142 /// matcher. |
178 Matcher parse(matcher) => new _Parse(matcher); | 143 Matcher parse(matcher) => new _Parse(matcher); |
179 | 144 |
180 class _Parse extends BaseMatcher { | 145 class _Parse extends BaseMatcher { |
181 final Matcher _matcher; | 146 final Matcher _matcher; |
182 | 147 |
183 _Parse(this._matcher); | 148 _Parse(this._matcher); |
184 | 149 |
185 bool matches(item, MatchState matchState) { | 150 bool matches(item, MatchState matchState) { |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 const isSocketIOException = const _SocketIOException(); | 198 const isSocketIOException = const _SocketIOException(); |
234 | 199 |
235 /// A matcher for functions that throw SocketIOException. | 200 /// A matcher for functions that throw SocketIOException. |
236 const Matcher throwsSocketIOException = | 201 const Matcher throwsSocketIOException = |
237 const Throws(isSocketIOException); | 202 const Throws(isSocketIOException); |
238 | 203 |
239 class _SocketIOException extends TypeMatcher { | 204 class _SocketIOException extends TypeMatcher { |
240 const _SocketIOException() : super("SocketIOException"); | 205 const _SocketIOException() : super("SocketIOException"); |
241 bool matches(item, MatchState matchState) => item is SocketIOException; | 206 bool matches(item, MatchState matchState) => item is SocketIOException; |
242 } | 207 } |
OLD | NEW |