Files
sdk/tests/standalone_2/io/http_close_test.dart
T
Clement Skau a2aec5eb06 [test] Deflakes, un-excepts http_close_test.
http_close_test was given a test exception in issue 28380.

This CL adds Expects to check for the expected number of error
on both server and client in testClientCloseWhileSendingRequest.
It also moves the server.close() into the server error handler
to make sure we don't flakily close the server before all requests
are fully sent and received in both ends.
This should get rid of:
HttpException: Connection closed while receiving data, uri = /
  #0      _HttpIncoming.listen [...]

The modified test has been unflaky across a few hundred test runs
locally.

A few lints were also fixed in the process such as 'unnecessary new'.

Bug: https://github.com/dart-lang/sdk/issues/28380
Change-Id: I3470f70f7965c34f73da71489511e1cb2d1b7e42
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/109964
Commit-Queue: Clement Skau <cskau@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
2019-07-25 08:27:34 +00:00

152 lines
4.4 KiB
Dart

// Copyright (c) 2013, 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.
//
// VMOptions=
// VMOptions=--short_socket_read
// VMOptions=--short_socket_write
// VMOptions=--short_socket_read --short_socket_write
import "package:expect/expect.dart";
import "dart:async";
import "dart:io";
import "dart:typed_data";
import "dart:math";
testClientAndServerCloseNoListen(int connections) {
HttpServer.bind("127.0.0.1", 0).then((server) {
int closed = 0;
server.listen((request) {
request.response.close();
request.response.done.then((_) {
closed++;
if (closed == connections) {
Expect.equals(0, server.connectionsInfo().active);
Expect.equals(
server.connectionsInfo().total, server.connectionsInfo().idle);
server.close();
}
});
});
var client = HttpClient();
for (int i = 0; i < connections; i++) {
client
.get("127.0.0.1", server.port, "/")
.then((request) => request.close())
.then((response) {});
}
});
}
testClientCloseServerListen(int connections) {
HttpServer.bind("127.0.0.1", 0).then((server) {
int closed = 0;
check() {
closed++;
if (closed == connections * 2) {
Expect.equals(0, server.connectionsInfo().active);
Expect.equals(
server.connectionsInfo().total, server.connectionsInfo().idle);
server.close();
}
}
server.listen((request) {
request.listen((_) {}, onDone: () {
request.response.close();
request.response.done.then((_) => check());
});
});
var client = HttpClient();
for (int i = 0; i < connections; i++) {
client
.get("127.0.0.1", server.port, "/")
.then((request) => request.close())
.then((response) => check());
}
});
}
testClientCloseSendingResponse(int connections) {
var buffer = Uint8List(64 * 1024);
var rand = Random();
for (int i = 0; i < buffer.length; i++) {
buffer[i] = rand.nextInt(256);
}
HttpServer.bind("127.0.0.1", 0).then((server) {
int closed = 0;
check() {
closed++;
// Wait for both server and client to see the connections as closed.
if (closed == connections * 2) {
Expect.equals(0, server.connectionsInfo().active);
Expect.equals(
server.connectionsInfo().total, server.connectionsInfo().idle);
server.close();
}
}
server.listen((request) {
var timer = Timer.periodic(const Duration(milliseconds: 50), (_) {
request.response.add(buffer);
});
request.response.done.catchError((_) {}).whenComplete(() {
check();
timer.cancel();
});
});
var client = HttpClient();
for (int i = 0; i < connections; i++) {
client
.get("127.0.0.1", server.port, "/")
.then((request) => request.close())
.then((response) {
// Ensure we don't accept the response until we have send the entire
// request.
var subscription = response.listen((_) {});
Timer(const Duration(milliseconds: 20), () {
subscription.cancel();
check();
});
});
}
});
}
// Closing the request early, before sending the full request payload should
// result in an error on both server and client.
testClientCloseWhileSendingRequest(int connections) {
HttpServer.bind("127.0.0.1", 0).then((server) {
int serverErrors = 0;
int clientErrors = 0;
server.listen((request) {
request.listen((_) {}, onError: (_) {
serverErrors++;
if (serverErrors == connections) {
server.close();
}
});
}, onDone: () {
Expect.equals(connections, clientErrors);
Expect.equals(connections, serverErrors);
});
var client = HttpClient();
for (int i = 0; i < connections; i++) {
client.post("127.0.0.1", server.port, "/").then((request) {
request.contentLength = 110;
request.write("0123456789");
return request.close();
}).catchError((_) {
clientErrors++;
});
}
});
}
main() {
testClientAndServerCloseNoListen(10);
testClientCloseServerListen(10);
testClientCloseSendingResponse(10);
testClientCloseWhileSendingRequest(10);
}