Files
sdk/tests/standalone_2/io/test_utils.dart
T
Zach Anderson afb490adbc [dart:io] Provide modern Dart-styled constants
This CL updates dart:io but not dart:_http. It updates the sdk sources,
the patch files, and tests.

Change-Id: I64c3da407f09fa2bc6eec582049c4ae3a8afbe6d
Reviewed-on: https://dart-review.googlesource.com/52990
Commit-Queue: Zach Anderson <zra@google.com>
Reviewed-by: Lasse R.H. Nielsen <lrn@google.com>
2018-05-03 17:00:14 +00:00

34 lines
986 B
Dart

// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:async';
import 'dart:io';
Future<int> freeIPv4AndIPv6Port() async {
var socket =
await ServerSocket.bind(InternetAddress.anyIPv6, 0, v6Only: false);
int port = socket.port;
await socket.close();
return port;
}
int lastRetryId = 0;
Future retry(Future fun(), {int maxCount: 10}) async {
final int id = lastRetryId++;
for (int i = 0; i < maxCount; i++) {
try {
// If there is no exception this will simply return, otherwise we keep
// trying.
return await fun();
} catch (e, stack) {
print("Failed to execute test closure (retry id: ${id}) in attempt $i "
"(${maxCount - i} retries left).");
print("Exception: ${e}");
print("Stacktrace: ${stack}");
}
}
return await fun();
}