OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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.util.io; | 5 library test.util.io; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:convert'; | 8 import 'dart:convert'; |
9 import 'dart:io'; | 9 import 'dart:io'; |
10 import 'dart:mirrors'; | 10 import 'dart:mirrors'; |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 /// returned. | 188 /// returned. |
189 String libraryPath(Symbol libraryName, {String packageRoot}) { | 189 String libraryPath(Symbol libraryName, {String packageRoot}) { |
190 var lib = currentMirrorSystem().findLibrary(libraryName); | 190 var lib = currentMirrorSystem().findLibrary(libraryName); |
191 if (lib.uri.scheme != 'package') return p.fromUri(lib.uri); | 191 if (lib.uri.scheme != 'package') return p.fromUri(lib.uri); |
192 | 192 |
193 // TODO(nweiz): is there a way to avoid assuming this is being run next to a | 193 // TODO(nweiz): is there a way to avoid assuming this is being run next to a |
194 // packages directory?. | 194 // packages directory?. |
195 if (packageRoot == null) packageRoot = p.absolute('packages'); | 195 if (packageRoot == null) packageRoot = p.absolute('packages'); |
196 return p.join(packageRoot, p.fromUri(lib.uri.path)); | 196 return p.join(packageRoot, p.fromUri(lib.uri.path)); |
197 } | 197 } |
| 198 |
| 199 /// Repeatedly finds a probably-unused port on localhost and passes it to |
| 200 /// [tryPort] until it binds successfully. |
| 201 /// |
| 202 /// [tryPort] should return a non-`null` value or a Future completing to a |
| 203 /// non-`null` value once it binds successfully. This value will be returned |
| 204 /// by [getUnusedPort] in turn. |
| 205 /// |
| 206 /// This is necessary for ensuring that our port binding isn't flaky for |
| 207 /// applications that don't print out the bound port. |
| 208 Future getUnusedPort(tryPort(int port)) { |
| 209 var value; |
| 210 return Future.doWhile(() async { |
| 211 var socket = await RawServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0); |
| 212 var port = socket.port; |
| 213 await socket.close(); |
| 214 |
| 215 value = await tryPort(port); |
| 216 return value == null; |
| 217 }).then((_) => value); |
| 218 } |
OLD | NEW |