Unverified Commit 52e4011a authored by Zachary Anderson's avatar Zachary Anderson Committed by GitHub

[flutter_tool] Timeout the Azure bot detector http request (#53217)

parent 13fa5734
......@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'package:meta/meta.dart';
import 'package:platform/platform.dart';
......@@ -95,12 +97,14 @@ class AzureDetector {
if (_isRunningOnAzure != null) {
return _isRunningOnAzure;
}
const Duration connectionTimeout = Duration(milliseconds: 250);
const Duration requestTimeout = Duration(seconds: 1);
final HttpClient client = _httpClientFactory()
..connectionTimeout = const Duration(milliseconds: 250);
..connectionTimeout = connectionTimeout;
try {
final HttpClientRequest request = await client.getUrl(
Uri.parse(_serviceUrl),
);
).timeout(requestTimeout);
request.headers.add('Metadata', true);
await request.close();
} on SocketException {
......@@ -111,6 +115,10 @@ class AzureDetector {
// If the connection gets set up, but encounters an error condition, it
// still means we're on Azure.
return _isRunningOnAzure = true;
} on TimeoutException {
// The HttpClient connected to a host, but it did not respond in a timely
// fashion. Assume we are not on a bot.
return _isRunningOnAzure = false;
}
// We got a response. We're running on Azure.
return _isRunningOnAzure = true;
......
......@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:async';
import 'package:file/memory.dart';
import 'package:flutter_tools/src/base/bot_detector.dart';
import 'package:flutter_tools/src/base/io.dart';
......@@ -9,6 +11,7 @@ import 'package:flutter_tools/src/base/logger.dart';
import 'package:flutter_tools/src/persistent_tool_state.dart';
import 'package:mockito/mockito.dart';
import 'package:platform/platform.dart';
import 'package:quiver/testing/async.dart';
import '../../src/common.dart';
import '../../src/mocks.dart';
......@@ -132,6 +135,19 @@ void main() {
expect(await azureDetector.isRunningOnAzure, isFalse);
});
testWithoutContext('isRunningOnAzure returns false when the http request times out', () {
FakeAsync().run((FakeAsync time) async {
when(mockHttpClient.getUrl(any)).thenAnswer((_) {
final Completer<HttpClientRequest> completer = Completer<HttpClientRequest>();
return completer.future; // Never completed to test timeout behavior.
});
final Future<bool> onBot = azureDetector.isRunningOnAzure;
time.elapse(const Duration(seconds: 2));
expect(await onBot, isFalse);
});
});
testWithoutContext('isRunningOnAzure returns true when azure metadata is reachable', () async {
when(mockHttpClient.getUrl(any)).thenAnswer((_) {
return Future<HttpClientRequest>.value(mockHttpClientRequest);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment