2017-09-29 00:37:34 +00:00
|
|
|
// Copyright 2017 The Chromium Authors. 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';
|
2019-06-12 17:22:50 +00:00
|
|
|
import 'dart:typed_data';
|
2017-09-29 00:37:34 +00:00
|
|
|
|
|
|
|
import 'package:flutter_tools/src/base/io.dart' as io;
|
|
|
|
import 'package:flutter_tools/src/base/net.dart';
|
|
|
|
import 'package:quiver/testing/async.dart';
|
|
|
|
|
2018-08-15 03:33:58 +00:00
|
|
|
import '../src/common.dart';
|
2017-09-29 00:37:34 +00:00
|
|
|
import '../src/context.dart';
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
testUsingContext('retry from 500', () async {
|
|
|
|
String error;
|
2018-09-12 06:29:29 +00:00
|
|
|
FakeAsync().run((FakeAsync time) {
|
2017-09-29 00:37:34 +00:00
|
|
|
fetchUrl(Uri.parse('http://example.invalid/')).then((List<int> value) {
|
|
|
|
error = 'test completed unexpectedly';
|
2018-04-16 21:02:07 +00:00
|
|
|
}, onError: (dynamic exception) {
|
|
|
|
error = 'test failed unexpectedly: $exception';
|
2017-09-29 00:37:34 +00:00
|
|
|
});
|
|
|
|
expect(testLogger.statusText, '');
|
|
|
|
time.elapse(const Duration(milliseconds: 10000));
|
|
|
|
expect(testLogger.statusText,
|
|
|
|
'Download failed -- attempting retry 1 in 1 second...\n'
|
|
|
|
'Download failed -- attempting retry 2 in 2 seconds...\n'
|
|
|
|
'Download failed -- attempting retry 3 in 4 seconds...\n'
|
2019-03-01 07:17:55 +00:00
|
|
|
'Download failed -- attempting retry 4 in 8 seconds...\n',
|
2017-09-29 00:37:34 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
expect(testLogger.errorText, isEmpty);
|
|
|
|
expect(error, isNull);
|
|
|
|
}, overrides: <Type, Generator>{
|
2018-09-12 06:29:29 +00:00
|
|
|
HttpClientFactory: () => () => MockHttpClient(500),
|
2017-09-29 00:37:34 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
testUsingContext('retry from network error', () async {
|
|
|
|
String error;
|
2018-09-12 06:29:29 +00:00
|
|
|
FakeAsync().run((FakeAsync time) {
|
2017-09-29 00:37:34 +00:00
|
|
|
fetchUrl(Uri.parse('http://example.invalid/')).then((List<int> value) {
|
|
|
|
error = 'test completed unexpectedly';
|
2018-04-16 21:02:07 +00:00
|
|
|
}, onError: (dynamic exception) {
|
|
|
|
error = 'test failed unexpectedly: $exception';
|
2017-09-29 00:37:34 +00:00
|
|
|
});
|
|
|
|
expect(testLogger.statusText, '');
|
|
|
|
time.elapse(const Duration(milliseconds: 10000));
|
|
|
|
expect(testLogger.statusText,
|
|
|
|
'Download failed -- attempting retry 1 in 1 second...\n'
|
|
|
|
'Download failed -- attempting retry 2 in 2 seconds...\n'
|
|
|
|
'Download failed -- attempting retry 3 in 4 seconds...\n'
|
2019-03-01 07:17:55 +00:00
|
|
|
'Download failed -- attempting retry 4 in 8 seconds...\n',
|
2017-09-29 00:37:34 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
expect(testLogger.errorText, isEmpty);
|
|
|
|
expect(error, isNull);
|
|
|
|
}, overrides: <Type, Generator>{
|
2018-09-12 06:29:29 +00:00
|
|
|
HttpClientFactory: () => () => MockHttpClient(200),
|
2017-09-29 00:37:34 +00:00
|
|
|
});
|
2018-04-16 21:02:07 +00:00
|
|
|
|
|
|
|
testUsingContext('retry from SocketException', () async {
|
|
|
|
String error;
|
2018-09-12 06:29:29 +00:00
|
|
|
FakeAsync().run((FakeAsync time) {
|
2018-04-16 21:02:07 +00:00
|
|
|
fetchUrl(Uri.parse('http://example.invalid/')).then((List<int> value) {
|
|
|
|
error = 'test completed unexpectedly';
|
|
|
|
}, onError: (dynamic exception) {
|
|
|
|
error = 'test failed unexpectedly: $exception';
|
|
|
|
});
|
|
|
|
expect(testLogger.statusText, '');
|
|
|
|
time.elapse(const Duration(milliseconds: 10000));
|
|
|
|
expect(testLogger.statusText,
|
|
|
|
'Download failed -- attempting retry 1 in 1 second...\n'
|
|
|
|
'Download failed -- attempting retry 2 in 2 seconds...\n'
|
|
|
|
'Download failed -- attempting retry 3 in 4 seconds...\n'
|
2019-03-01 07:17:55 +00:00
|
|
|
'Download failed -- attempting retry 4 in 8 seconds...\n',
|
2018-04-16 21:02:07 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
expect(testLogger.errorText, isEmpty);
|
|
|
|
expect(error, isNull);
|
|
|
|
expect(testLogger.traceText, contains('Download error: SocketException'));
|
|
|
|
}, overrides: <Type, Generator>{
|
2018-09-12 06:29:29 +00:00
|
|
|
HttpClientFactory: () => () => MockHttpClientThrowing(
|
2018-04-16 21:02:07 +00:00
|
|
|
const io.SocketException('test exception handling'),
|
|
|
|
),
|
|
|
|
});
|
|
|
|
|
|
|
|
testUsingContext('no retry from HandshakeException', () async {
|
|
|
|
String error;
|
2018-09-12 06:29:29 +00:00
|
|
|
FakeAsync().run((FakeAsync time) {
|
2018-04-16 21:02:07 +00:00
|
|
|
fetchUrl(Uri.parse('http://example.invalid/')).then((List<int> value) {
|
|
|
|
error = 'test completed unexpectedly';
|
|
|
|
}, onError: (dynamic exception) {
|
|
|
|
error = 'test failed: $exception';
|
|
|
|
});
|
|
|
|
expect(testLogger.statusText, '');
|
|
|
|
time.elapse(const Duration(milliseconds: 10000));
|
|
|
|
expect(testLogger.statusText, '');
|
|
|
|
});
|
|
|
|
expect(error, startsWith('test failed'));
|
|
|
|
expect(testLogger.traceText, contains('HandshakeException'));
|
|
|
|
}, overrides: <Type, Generator>{
|
2018-09-12 06:29:29 +00:00
|
|
|
HttpClientFactory: () => () => MockHttpClientThrowing(
|
2018-04-16 21:02:07 +00:00
|
|
|
const io.HandshakeException('test exception handling'),
|
|
|
|
),
|
|
|
|
});
|
2018-10-30 00:03:22 +00:00
|
|
|
|
2019-06-17 17:48:01 +00:00
|
|
|
testUsingContext('retry from HttpException', () async {
|
|
|
|
String error;
|
|
|
|
FakeAsync().run((FakeAsync time) {
|
|
|
|
fetchUrl(Uri.parse('http://example.invalid/')).then((List<int> value) {
|
|
|
|
error = 'test completed unexpectedly';
|
|
|
|
}, onError: (dynamic exception) {
|
|
|
|
error = 'test failed unexpectedly: $exception';
|
|
|
|
});
|
|
|
|
expect(testLogger.statusText, '');
|
|
|
|
time.elapse(const Duration(milliseconds: 10000));
|
|
|
|
expect(testLogger.statusText,
|
|
|
|
'Download failed -- attempting retry 1 in 1 second...\n'
|
|
|
|
'Download failed -- attempting retry 2 in 2 seconds...\n'
|
|
|
|
'Download failed -- attempting retry 3 in 4 seconds...\n'
|
|
|
|
'Download failed -- attempting retry 4 in 8 seconds...\n',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
expect(testLogger.errorText, isEmpty);
|
|
|
|
expect(error, isNull);
|
|
|
|
expect(testLogger.traceText, contains('Download error: HttpException'));
|
|
|
|
}, overrides: <Type, Generator>{
|
|
|
|
HttpClientFactory: () => () => MockHttpClientThrowing(
|
|
|
|
const io.HttpException('test exception handling'),
|
|
|
|
),
|
|
|
|
});
|
|
|
|
|
2019-05-17 01:20:30 +00:00
|
|
|
testUsingContext('max attempts', () async {
|
|
|
|
String error;
|
|
|
|
List<int> actualResult;
|
|
|
|
FakeAsync().run((FakeAsync time) {
|
|
|
|
fetchUrl(Uri.parse('http://example.invalid/'), maxAttempts: 3).then((List<int> value) {
|
|
|
|
actualResult = value;
|
|
|
|
}, onError: (dynamic exception) {
|
|
|
|
error = 'test failed unexpectedly: $exception';
|
|
|
|
});
|
|
|
|
expect(testLogger.statusText, '');
|
|
|
|
time.elapse(const Duration(milliseconds: 10000));
|
|
|
|
expect(testLogger.statusText,
|
|
|
|
'Download failed -- attempting retry 1 in 1 second...\n'
|
|
|
|
'Download failed -- attempting retry 2 in 2 seconds...\n'
|
|
|
|
'Download failed -- retry 3\n',
|
|
|
|
);
|
|
|
|
});
|
|
|
|
expect(testLogger.errorText, isEmpty);
|
|
|
|
expect(error, isNull);
|
|
|
|
expect(actualResult, isNull);
|
|
|
|
}, overrides: <Type, Generator>{
|
|
|
|
HttpClientFactory: () => () => MockHttpClient(500),
|
|
|
|
});
|
|
|
|
|
2018-10-30 00:03:22 +00:00
|
|
|
testUsingContext('remote file non-existant', () async {
|
|
|
|
final Uri invalid = Uri.parse('http://example.invalid/');
|
|
|
|
final bool result = await doesRemoteFileExist(invalid);
|
|
|
|
expect(result, false);
|
|
|
|
}, overrides: <Type, Generator>{
|
|
|
|
HttpClientFactory: () => () => MockHttpClient(404),
|
|
|
|
});
|
|
|
|
|
|
|
|
testUsingContext('remote file server error', () async {
|
|
|
|
final Uri valid = Uri.parse('http://example.valid/');
|
|
|
|
final bool result = await doesRemoteFileExist(valid);
|
|
|
|
expect(result, false);
|
|
|
|
}, overrides: <Type, Generator>{
|
|
|
|
HttpClientFactory: () => () => MockHttpClient(500),
|
|
|
|
});
|
|
|
|
|
|
|
|
testUsingContext('remote file exists', () async {
|
|
|
|
final Uri valid = Uri.parse('http://example.valid/');
|
|
|
|
final bool result = await doesRemoteFileExist(valid);
|
|
|
|
expect(result, true);
|
|
|
|
}, overrides: <Type, Generator>{
|
|
|
|
HttpClientFactory: () => () => MockHttpClient(200),
|
|
|
|
});
|
2018-04-16 21:02:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class MockHttpClientThrowing implements io.HttpClient {
|
|
|
|
MockHttpClientThrowing(this.exception);
|
|
|
|
|
|
|
|
final Exception exception;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<io.HttpClientRequest> getUrl(Uri url) async {
|
|
|
|
throw exception;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
dynamic noSuchMethod(Invocation invocation) {
|
|
|
|
throw 'io.HttpClient - $invocation';
|
|
|
|
}
|
2017-09-29 00:37:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class MockHttpClient implements io.HttpClient {
|
|
|
|
MockHttpClient(this.statusCode);
|
|
|
|
|
|
|
|
final int statusCode;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<io.HttpClientRequest> getUrl(Uri url) async {
|
2018-09-12 06:29:29 +00:00
|
|
|
return MockHttpClientRequest(statusCode);
|
2017-09-29 00:37:34 +00:00
|
|
|
}
|
|
|
|
|
2018-10-30 00:03:22 +00:00
|
|
|
@override
|
|
|
|
Future<io.HttpClientRequest> headUrl(Uri url) async {
|
|
|
|
return MockHttpClientRequest(statusCode);
|
|
|
|
}
|
|
|
|
|
2017-09-29 00:37:34 +00:00
|
|
|
@override
|
|
|
|
dynamic noSuchMethod(Invocation invocation) {
|
|
|
|
throw 'io.HttpClient - $invocation';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class MockHttpClientRequest implements io.HttpClientRequest {
|
|
|
|
MockHttpClientRequest(this.statusCode);
|
|
|
|
|
|
|
|
final int statusCode;
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<io.HttpClientResponse> close() async {
|
2018-09-12 06:29:29 +00:00
|
|
|
return MockHttpClientResponse(statusCode);
|
2017-09-29 00:37:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
dynamic noSuchMethod(Invocation invocation) {
|
|
|
|
throw 'io.HttpClientRequest - $invocation';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-25 06:25:44 +00:00
|
|
|
class MockHttpClientResponse extends Stream<List<int>> implements io.HttpClientResponse {
|
2017-09-29 00:37:34 +00:00
|
|
|
MockHttpClientResponse(this.statusCode);
|
|
|
|
|
|
|
|
@override
|
|
|
|
final int statusCode;
|
|
|
|
|
|
|
|
@override
|
|
|
|
String get reasonPhrase => '<reason phrase>';
|
|
|
|
|
|
|
|
@override
|
2019-06-12 17:22:50 +00:00
|
|
|
StreamSubscription<Uint8List> listen(
|
|
|
|
void onData(Uint8List event), {
|
2019-02-21 08:27:07 +00:00
|
|
|
Function onError,
|
|
|
|
void onDone(),
|
|
|
|
bool cancelOnError,
|
2017-09-29 00:37:34 +00:00
|
|
|
}) {
|
2019-06-12 17:22:50 +00:00
|
|
|
return Stream<Uint8List>.fromFuture(Future<Uint8List>.error(const io.SocketException('test')))
|
2017-09-29 00:37:34 +00:00
|
|
|
.listen(onData, onError: onError, onDone: onDone, cancelOnError: cancelOnError);
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
dynamic noSuchMethod(Invocation invocation) {
|
|
|
|
throw 'io.HttpClientResponse - $invocation';
|
|
|
|
}
|
|
|
|
}
|