Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(68)

Unified Diff: tools/gardening/lib/src/util.dart

Issue 2998993002: Use timeout to read most recent build from http (Closed)
Patch Set: Fix test Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « tools/gardening/lib/src/compare_failures_impl.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tools/gardening/lib/src/util.dart
diff --git a/tools/gardening/lib/src/util.dart b/tools/gardening/lib/src/util.dart
index 51b50c1e3dec79d037401a16254d3a4046ee07ee..108fffcb98d4867c62e33d22b2c5b99fb1202bb1 100644
--- a/tools/gardening/lib/src/util.dart
+++ b/tools/gardening/lib/src/util.dart
@@ -64,15 +64,39 @@ class HttpException implements Exception {
String toString() => '$uri: $statusCode';
}
+class TimeoutException implements Exception {
floitsch 2017/08/15 11:26:35 I would have used the builtin TimeoutException cla
Johnni Winther 2017/08/15 12:51:27 Done.
+ final Uri uri;
+
+ TimeoutException(this.uri);
+
+ String toString() => '$uri timeout';
+}
+
/// Reads the content of [uri] as text.
-Future<String> readUriAsText(HttpClient client, Uri uri) async {
+Future<String> readUriAsText(
+ HttpClient client, Uri uri, Duration timeout) async {
HttpClientRequest request = await client.getUrl(uri);
HttpClientResponse response = await request.close();
if (response.statusCode != 200) {
response.drain();
throw new HttpException(uri, response.statusCode);
}
- return response.transform(UTF8.decoder).join();
+ List<int> data = <int>[];
+ Timer timer;
+ Completer<String> completer = new Completer<String>();
+ StreamSubscription subscription = response.listen((List<int> buffer) {
floitsch 2017/08/15 11:26:35 You could consider using .timeout from the Stream
Johnni Winther 2017/08/15 12:51:27 Done.
+ data.addAll(buffer);
+ }, onDone: () {
+ timer?.cancel();
+ completer.complete(UTF8.decode(data));
+ }, cancelOnError: true);
+ if (timeout != null) {
+ timer = new Timer(timeout, () {
+ subscription.cancel();
+ completer.completeError(new TimeoutException(uri));
+ });
+ }
+ return completer.future;
}
ArgParser createArgParser() {
« no previous file with comments | « tools/gardening/lib/src/compare_failures_impl.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698