Drain socket before throwing (#79289)

This commit is contained in:
Dan Field 2021-03-29 21:14:03 -07:00 committed by GitHub
parent 5151ea4801
commit 318ff7a0ac
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 1 deletions

View file

@ -96,6 +96,7 @@ class NetworkImage extends image_provider.ImageProvider<image_provider.NetworkIm
// The network may be only temporarily unavailable, or the file will be
// added on the server later. Avoid having future calls to resolve
// fail to check the network again.
await response.drain<List<int>>();
throw image_provider.NetworkImageLoadException(statusCode: response.statusCode, uri: resolved);
}

View file

@ -35,7 +35,7 @@ void main() {
PaintingBinding.instance!.imageCache!.clearLiveImages();
});
test('Expect thrown exception with statusCode - evicts from cache', () async {
test('Expect thrown exception with statusCode - evicts from cache and drains', () async {
final int errorStatusCode = HttpStatus.notFound;
const String requestUrl = 'foo-url';
@ -68,6 +68,7 @@ void main() {
.having((NetworkImageLoadException e) => e.statusCode, 'statusCode', errorStatusCode)
.having((NetworkImageLoadException e) => e.uri, 'uri', Uri.base.resolve(requestUrl)),
);
expect(httpClient.request.response.drained, true);
}, skip: isBrowser); // Browser implementation does not use HTTP client but an <img> tag.
test('Uses the HttpClient provided by debugNetworkImageHttpClientProvider if set', () async {
@ -234,6 +235,8 @@ class _FakeHttpClientRequest extends Fake implements HttpClientRequest {
}
class _FakeHttpClientResponse extends Fake implements HttpClientResponse {
bool drained = false;
@override
int statusCode = HttpStatus.ok;
@ -254,6 +257,12 @@ class _FakeHttpClientResponse extends Fake implements HttpClientResponse {
cancelOnError: cancelOnError,
);
}
@override
Future<E> drain<E>([E? futureValue]) async {
drained = true;
return futureValue ?? <int>[] as E;
}
}
class FakeCodec implements Codec {