mirror of
https://github.com/dart-lang/sdk
synced 2024-09-05 08:24:39 +00:00
[vm] Fix unmatched asyncLevel with retry() helper in socket tests.
Change-Id: Ia2cba787ff02e1b3d26b8fbab9cc4057842bd4cc Reviewed-on: https://dart-review.googlesource.com/c/87067 Commit-Queue: Samir Jindel <sjindel@google.com> Reviewed-by: Martin Kustermann <kustermann@google.com> Auto-Submit: Samir Jindel <sjindel@google.com>
This commit is contained in:
parent
5c916afad9
commit
77889c10e3
|
@ -5,75 +5,53 @@
|
|||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:async_helper/async_helper.dart';
|
||||
import 'package:expect/expect.dart';
|
||||
|
||||
import 'test_utils.dart' show freeIPv4AndIPv6Port, retry;
|
||||
import 'test_utils.dart' show retry, throws;
|
||||
|
||||
testBindShared(String host, bool v6Only) {
|
||||
asyncStart();
|
||||
ServerSocket.bind(host, 0, v6Only: v6Only, shared: true).then((socket) {
|
||||
Expect.isTrue(socket.port > 0);
|
||||
Future testBindShared(String host, bool v6Only) async {
|
||||
final socket = await ServerSocket.bind(host, 0, v6Only: v6Only, shared: true);
|
||||
Expect.isTrue(socket.port > 0);
|
||||
|
||||
asyncStart();
|
||||
return ServerSocket
|
||||
.bind(host, socket.port, v6Only: v6Only, shared: true)
|
||||
.then((socket2) {
|
||||
Expect.equals(socket.address.address, socket2.address.address);
|
||||
Expect.equals(socket.port, socket2.port);
|
||||
socket.close().whenComplete(asyncEnd);
|
||||
socket2.close().whenComplete(asyncEnd);
|
||||
});
|
||||
});
|
||||
final socket2 =
|
||||
await ServerSocket.bind(host, socket.port, v6Only: v6Only, shared: true);
|
||||
|
||||
Expect.equals(socket.address.address, socket2.address.address);
|
||||
Expect.equals(socket.port, socket2.port);
|
||||
|
||||
await socket.close();
|
||||
await socket2.close();
|
||||
}
|
||||
|
||||
negTestBindSharedMismatch(String host, bool v6Only) {
|
||||
asyncStart();
|
||||
ServerSocket.bind(host, 0, v6Only: v6Only).then((ServerSocket socket) {
|
||||
Expect.isTrue(socket.port > 0);
|
||||
Future negTestBindSharedMismatch(String host, bool v6Only) async {
|
||||
final socket = await ServerSocket.bind(host, 0, v6Only: v6Only);
|
||||
Expect.isTrue(socket.port > 0);
|
||||
|
||||
asyncStart();
|
||||
return ServerSocket
|
||||
.bind(host, socket.port, v6Only: v6Only)
|
||||
.catchError((error) {
|
||||
Expect.isTrue(error is SocketException);
|
||||
Expect.isTrue('$error'.contains('shared flag'));
|
||||
socket.close().whenComplete(asyncEnd);
|
||||
asyncEnd();
|
||||
});
|
||||
});
|
||||
await throws(() => ServerSocket.bind(host, socket.port, v6Only: v6Only),
|
||||
(error) => error is SocketException && '$error'.contains('shared flag'));
|
||||
await socket.close();
|
||||
}
|
||||
|
||||
negTestBindV6OnlyMismatch(String host, bool v6Only) {
|
||||
asyncStart();
|
||||
ServerSocket
|
||||
.bind(host, 0, v6Only: v6Only, shared: true)
|
||||
.then((ServerSocket socket) {
|
||||
Expect.isTrue(socket.port > 0);
|
||||
Future negTestBindV6OnlyMismatch(String host, bool v6Only) async {
|
||||
final socket = await ServerSocket.bind(host, 0, v6Only: v6Only, shared: true);
|
||||
Expect.isTrue(socket.port > 0);
|
||||
|
||||
asyncStart();
|
||||
return ServerSocket
|
||||
.bind(host, socket.port, v6Only: !v6Only, shared: true)
|
||||
.catchError((error) {
|
||||
Expect.isTrue(error is SocketException);
|
||||
Expect.isTrue('$error'.contains('v6Only flag'));
|
||||
socket.close().whenComplete(asyncEnd);
|
||||
asyncEnd();
|
||||
});
|
||||
});
|
||||
await throws(
|
||||
() => ServerSocket.bind(host, socket.port, v6Only: !v6Only, shared: true),
|
||||
(error) => error is SocketException && '$error'.contains('v6Only flag'));
|
||||
|
||||
await socket.close();
|
||||
}
|
||||
|
||||
Future testBindDifferentAddresses(InternetAddress addr1, InternetAddress addr2,
|
||||
bool addr1V6Only, bool addr2V6Only) async {
|
||||
int freePort = await freeIPv4AndIPv6Port();
|
||||
|
||||
var socket = await ServerSocket.bind(addr1, freePort,
|
||||
v6Only: addr1V6Only, shared: false);
|
||||
var socket =
|
||||
await ServerSocket.bind(addr1, 0, v6Only: addr1V6Only, shared: false);
|
||||
|
||||
try {
|
||||
Expect.isTrue(socket.port > 0);
|
||||
|
||||
var socket2 = await ServerSocket.bind(addr2, freePort,
|
||||
var socket2 = await ServerSocket.bind(addr2, socket.port,
|
||||
v6Only: addr2V6Only, shared: false);
|
||||
try {
|
||||
Expect.equals(socket.port, socket2.port);
|
||||
|
@ -85,9 +63,7 @@ Future testBindDifferentAddresses(InternetAddress addr1, InternetAddress addr2,
|
|||
}
|
||||
}
|
||||
|
||||
testListenCloseListenClose(String host) async {
|
||||
asyncStart();
|
||||
|
||||
Future testListenCloseListenClose(String host) async {
|
||||
ServerSocket socket = await ServerSocket.bind(host, 0, shared: true);
|
||||
ServerSocket socket2 =
|
||||
await ServerSocket.bind(host, socket.port, shared: true);
|
||||
|
@ -117,12 +93,9 @@ testListenCloseListenClose(String host) async {
|
|||
|
||||
// Close the second server socket.
|
||||
await socket2.close();
|
||||
asyncEnd();
|
||||
}
|
||||
|
||||
main() async {
|
||||
asyncStart();
|
||||
|
||||
await retry(() async {
|
||||
await testBindDifferentAddresses(
|
||||
InternetAddress.anyIPv6, InternetAddress.anyIPv4, true, false);
|
||||
|
@ -133,16 +106,15 @@ main() async {
|
|||
});
|
||||
|
||||
for (var host in ['127.0.0.1', '::1']) {
|
||||
testBindShared(host, false);
|
||||
testBindShared(host, true);
|
||||
await testBindShared(host, false);
|
||||
await testBindShared(host, true);
|
||||
|
||||
negTestBindSharedMismatch(host, false);
|
||||
negTestBindSharedMismatch(host, true);
|
||||
await negTestBindSharedMismatch(host, false);
|
||||
await negTestBindSharedMismatch(host, true);
|
||||
|
||||
negTestBindV6OnlyMismatch(host, true);
|
||||
negTestBindV6OnlyMismatch(host, false);
|
||||
await negTestBindV6OnlyMismatch(host, true);
|
||||
await negTestBindV6OnlyMismatch(host, false);
|
||||
|
||||
testListenCloseListenClose(host);
|
||||
await testListenCloseListenClose(host);
|
||||
}
|
||||
asyncEnd();
|
||||
}
|
||||
|
|
|
@ -6,44 +6,19 @@
|
|||
import "dart:async";
|
||||
import "dart:io";
|
||||
|
||||
import "package:async_helper/async_helper.dart";
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
import 'test_utils.dart' show freeIPv4AndIPv6Port, retry;
|
||||
|
||||
Future throws(Function f, Function check) async {
|
||||
try {
|
||||
await f();
|
||||
Expect.fail('Did not throw');
|
||||
} catch (e) {
|
||||
if (check != null) {
|
||||
if (!check(e)) {
|
||||
Expect.fail('Unexpected: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
import 'test_utils.dart' show retry, throws;
|
||||
|
||||
Future testArguments(connectFunction) async {
|
||||
int freePort = await freeIPv4AndIPv6Port();
|
||||
|
||||
var sourceAddress;
|
||||
asyncStart();
|
||||
var server;
|
||||
try {
|
||||
server = await ServerSocket.bind(InternetAddress.loopbackIPv4, freePort);
|
||||
} catch (e) {
|
||||
asyncEnd();
|
||||
rethrow;
|
||||
}
|
||||
final server = await ServerSocket.bind(InternetAddress.loopbackIPv4, 0);
|
||||
server.listen((_) {
|
||||
throw 'Unexpected connection from address $sourceAddress';
|
||||
}, onDone: () => asyncEnd());
|
||||
});
|
||||
|
||||
asyncStart();
|
||||
// Illegal type for sourceAddress.
|
||||
for (sourceAddress in ['www.google.com', 'abc']) {
|
||||
await throws(() => connectFunction('127.0.0.1', server.port,
|
||||
await throws(
|
||||
() => connectFunction('127.0.0.1', server.port,
|
||||
sourceAddress: sourceAddress),
|
||||
(e) => e is ArgumentError);
|
||||
}
|
||||
|
@ -63,8 +38,7 @@ Future testArguments(connectFunction) async {
|
|||
sourceAddress: sourceAddress),
|
||||
(e) => e is SocketException);
|
||||
}
|
||||
asyncEnd();
|
||||
server.close();
|
||||
await server.close();
|
||||
}
|
||||
|
||||
// IPv4 addresses to use as source address when connecting locally.
|
||||
|
@ -85,8 +59,6 @@ var ipV6SourceAddresses = [
|
|||
|
||||
Future testConnect(InternetAddress bindAddress, bool v6Only,
|
||||
Function connectFunction, Function closeDestroyFunction) async {
|
||||
int freePort = await freeIPv4AndIPv6Port();
|
||||
|
||||
var successCount = 0;
|
||||
if (!v6Only) successCount += ipV4SourceAddresses.length;
|
||||
if (bindAddress.type == InternetAddressType.IPv6) {
|
||||
|
@ -96,17 +68,14 @@ Future testConnect(InternetAddress bindAddress, bool v6Only,
|
|||
var allConnected = new Completer();
|
||||
if (successCount == 0) allConnected.complete();
|
||||
|
||||
asyncStart();
|
||||
var server = await ServerSocket.bind(bindAddress, freePort, v6Only: v6Only);
|
||||
var server = await ServerSocket.bind(bindAddress, 0, v6Only: v6Only);
|
||||
server.listen((s) {
|
||||
s.destroy();
|
||||
count++;
|
||||
if (count == successCount) allConnected.complete();
|
||||
}, onDone: () => asyncEnd());
|
||||
});
|
||||
|
||||
asyncStart();
|
||||
|
||||
// Connect with IPv4 source addesses.
|
||||
// Connect with IPv4 source addresses.
|
||||
for (var sourceAddress in ipV4SourceAddresses) {
|
||||
if (!v6Only) {
|
||||
var s = await connectFunction(InternetAddress.loopbackIPv4, server.port,
|
||||
|
@ -122,7 +91,7 @@ Future testConnect(InternetAddress bindAddress, bool v6Only,
|
|||
}
|
||||
}
|
||||
|
||||
// Connect with IPv6 source addesses.
|
||||
// Connect with IPv6 source addresses.
|
||||
for (var sourceAddress in ipV6SourceAddresses) {
|
||||
if (bindAddress.type == InternetAddressType.IPv6) {
|
||||
var s = await connectFunction(InternetAddress.loopbackIPv6, server.port,
|
||||
|
@ -139,12 +108,9 @@ Future testConnect(InternetAddress bindAddress, bool v6Only,
|
|||
|
||||
await allConnected.future;
|
||||
await server.close();
|
||||
asyncEnd();
|
||||
}
|
||||
|
||||
main() async {
|
||||
asyncStart();
|
||||
|
||||
await retry(() async {
|
||||
await testArguments(RawSocket.connect);
|
||||
});
|
||||
|
@ -176,6 +142,4 @@ main() async {
|
|||
await testConnect(
|
||||
InternetAddress.anyIPv6, true, Socket.connect, (s) => s.destroy());
|
||||
});
|
||||
|
||||
asyncEnd();
|
||||
}
|
||||
|
|
|
@ -5,13 +5,7 @@
|
|||
import 'dart:async';
|
||||
import 'dart:io';
|
||||
|
||||
Future<int> freeIPv4AndIPv6Port() async {
|
||||
var socket =
|
||||
await ServerSocket.bind(InternetAddress.anyIPv6, 0, v6Only: false);
|
||||
int port = socket.port;
|
||||
await socket.close();
|
||||
return port;
|
||||
}
|
||||
import "package:expect/expect.dart";
|
||||
|
||||
int lastRetryId = 0;
|
||||
|
||||
|
@ -31,3 +25,15 @@ Future retry(Future fun(), {int maxCount: 10}) async {
|
|||
}
|
||||
return await fun();
|
||||
}
|
||||
|
||||
Future throws(Function f, bool check(Object exception)) async {
|
||||
try {
|
||||
await f();
|
||||
} catch (e) {
|
||||
if (!check(e)) {
|
||||
Expect.fail('Unexpected: $e');
|
||||
}
|
||||
return;
|
||||
}
|
||||
Expect.fail('Did not throw');
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue