mirror of
https://github.com/dart-lang/sdk
synced 2024-09-19 15:01:29 +00:00
5642199dd0
Change-Id: I35bb926e53e92fd02e264fb5b14feadf063fb8db Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/257961 Reviewed-by: Michael Thomsen <mit@google.com> Commit-Queue: Lasse Nielsen <lrn@google.com>
61 lines
1.5 KiB
Dart
61 lines
1.5 KiB
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';
|
|
|
|
import "package:expect/expect.dart";
|
|
|
|
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();
|
|
}
|
|
|
|
Future throws(Function f, bool check(Object exception)) async {
|
|
try {
|
|
await f();
|
|
} catch (e) {
|
|
if (!check(e)) {
|
|
Expect.fail('Unexpected: $e');
|
|
}
|
|
return;
|
|
}
|
|
Expect.fail('Did not throw');
|
|
}
|
|
|
|
// Create a temporary directory and delete it when the test function exits.
|
|
Future withTempDir(String prefix, Future<void> test(Directory dir)) async {
|
|
final tempDir = Directory.systemTemp.createTempSync(prefix);
|
|
try {
|
|
await runZonedGuarded(() => test(tempDir), (e, st) {
|
|
try {
|
|
tempDir.deleteSync(recursive: true);
|
|
} catch (_) {
|
|
// ignore errors
|
|
}
|
|
throw e;
|
|
});
|
|
} finally {
|
|
try {
|
|
tempDir.deleteSync(recursive: true);
|
|
} catch (_) {
|
|
// ignore errors
|
|
}
|
|
}
|
|
}
|