[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:
Samir Jindel 2018-12-17 13:38:28 +00:00 committed by commit-bot@chromium.org
parent 5c916afad9
commit 77889c10e3
3 changed files with 60 additions and 118 deletions

View file

@ -5,75 +5,53 @@
import 'dart:async'; import 'dart:async';
import 'dart:io'; import 'dart:io';
import 'package:async_helper/async_helper.dart';
import 'package:expect/expect.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) { Future testBindShared(String host, bool v6Only) async {
asyncStart(); final socket = await ServerSocket.bind(host, 0, v6Only: v6Only, shared: true);
ServerSocket.bind(host, 0, v6Only: v6Only, shared: true).then((socket) { Expect.isTrue(socket.port > 0);
Expect.isTrue(socket.port > 0);
asyncStart(); final socket2 =
return ServerSocket await ServerSocket.bind(host, socket.port, v6Only: v6Only, shared: true);
.bind(host, socket.port, v6Only: v6Only, shared: true)
.then((socket2) { Expect.equals(socket.address.address, socket2.address.address);
Expect.equals(socket.address.address, socket2.address.address); Expect.equals(socket.port, socket2.port);
Expect.equals(socket.port, socket2.port);
socket.close().whenComplete(asyncEnd); await socket.close();
socket2.close().whenComplete(asyncEnd); await socket2.close();
});
});
} }
negTestBindSharedMismatch(String host, bool v6Only) { Future negTestBindSharedMismatch(String host, bool v6Only) async {
asyncStart(); final socket = await ServerSocket.bind(host, 0, v6Only: v6Only);
ServerSocket.bind(host, 0, v6Only: v6Only).then((ServerSocket socket) { Expect.isTrue(socket.port > 0);
Expect.isTrue(socket.port > 0);
asyncStart(); await throws(() => ServerSocket.bind(host, socket.port, v6Only: v6Only),
return ServerSocket (error) => error is SocketException && '$error'.contains('shared flag'));
.bind(host, socket.port, v6Only: v6Only) await socket.close();
.catchError((error) {
Expect.isTrue(error is SocketException);
Expect.isTrue('$error'.contains('shared flag'));
socket.close().whenComplete(asyncEnd);
asyncEnd();
});
});
} }
negTestBindV6OnlyMismatch(String host, bool v6Only) { Future negTestBindV6OnlyMismatch(String host, bool v6Only) async {
asyncStart(); final socket = await ServerSocket.bind(host, 0, v6Only: v6Only, shared: true);
ServerSocket Expect.isTrue(socket.port > 0);
.bind(host, 0, v6Only: v6Only, shared: true)
.then((ServerSocket socket) {
Expect.isTrue(socket.port > 0);
asyncStart(); await throws(
return ServerSocket () => ServerSocket.bind(host, socket.port, v6Only: !v6Only, shared: true),
.bind(host, socket.port, v6Only: !v6Only, shared: true) (error) => error is SocketException && '$error'.contains('v6Only flag'));
.catchError((error) {
Expect.isTrue(error is SocketException); await socket.close();
Expect.isTrue('$error'.contains('v6Only flag'));
socket.close().whenComplete(asyncEnd);
asyncEnd();
});
});
} }
Future testBindDifferentAddresses(InternetAddress addr1, InternetAddress addr2, Future testBindDifferentAddresses(InternetAddress addr1, InternetAddress addr2,
bool addr1V6Only, bool addr2V6Only) async { bool addr1V6Only, bool addr2V6Only) async {
int freePort = await freeIPv4AndIPv6Port(); var socket =
await ServerSocket.bind(addr1, 0, v6Only: addr1V6Only, shared: false);
var socket = await ServerSocket.bind(addr1, freePort,
v6Only: addr1V6Only, shared: false);
try { try {
Expect.isTrue(socket.port > 0); Expect.isTrue(socket.port > 0);
var socket2 = await ServerSocket.bind(addr2, freePort, var socket2 = await ServerSocket.bind(addr2, socket.port,
v6Only: addr2V6Only, shared: false); v6Only: addr2V6Only, shared: false);
try { try {
Expect.equals(socket.port, socket2.port); Expect.equals(socket.port, socket2.port);
@ -85,9 +63,7 @@ Future testBindDifferentAddresses(InternetAddress addr1, InternetAddress addr2,
} }
} }
testListenCloseListenClose(String host) async { Future testListenCloseListenClose(String host) async {
asyncStart();
ServerSocket socket = await ServerSocket.bind(host, 0, shared: true); ServerSocket socket = await ServerSocket.bind(host, 0, shared: true);
ServerSocket socket2 = ServerSocket socket2 =
await ServerSocket.bind(host, socket.port, shared: true); await ServerSocket.bind(host, socket.port, shared: true);
@ -117,12 +93,9 @@ testListenCloseListenClose(String host) async {
// Close the second server socket. // Close the second server socket.
await socket2.close(); await socket2.close();
asyncEnd();
} }
main() async { main() async {
asyncStart();
await retry(() async { await retry(() async {
await testBindDifferentAddresses( await testBindDifferentAddresses(
InternetAddress.anyIPv6, InternetAddress.anyIPv4, true, false); InternetAddress.anyIPv6, InternetAddress.anyIPv4, true, false);
@ -133,16 +106,15 @@ main() async {
}); });
for (var host in ['127.0.0.1', '::1']) { for (var host in ['127.0.0.1', '::1']) {
testBindShared(host, false); await testBindShared(host, false);
testBindShared(host, true); await testBindShared(host, true);
negTestBindSharedMismatch(host, false); await negTestBindSharedMismatch(host, false);
negTestBindSharedMismatch(host, true); await negTestBindSharedMismatch(host, true);
negTestBindV6OnlyMismatch(host, true); await negTestBindV6OnlyMismatch(host, true);
negTestBindV6OnlyMismatch(host, false); await negTestBindV6OnlyMismatch(host, false);
testListenCloseListenClose(host); await testListenCloseListenClose(host);
} }
asyncEnd();
} }

View file

@ -6,44 +6,19 @@
import "dart:async"; import "dart:async";
import "dart:io"; import "dart:io";
import "package:async_helper/async_helper.dart"; import 'test_utils.dart' show retry, throws;
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');
}
}
}
}
Future testArguments(connectFunction) async { Future testArguments(connectFunction) async {
int freePort = await freeIPv4AndIPv6Port();
var sourceAddress; var sourceAddress;
asyncStart(); final server = await ServerSocket.bind(InternetAddress.loopbackIPv4, 0);
var server;
try {
server = await ServerSocket.bind(InternetAddress.loopbackIPv4, freePort);
} catch (e) {
asyncEnd();
rethrow;
}
server.listen((_) { server.listen((_) {
throw 'Unexpected connection from address $sourceAddress'; throw 'Unexpected connection from address $sourceAddress';
}, onDone: () => asyncEnd()); });
asyncStart();
// Illegal type for sourceAddress. // Illegal type for sourceAddress.
for (sourceAddress in ['www.google.com', 'abc']) { 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), sourceAddress: sourceAddress),
(e) => e is ArgumentError); (e) => e is ArgumentError);
} }
@ -63,8 +38,7 @@ Future testArguments(connectFunction) async {
sourceAddress: sourceAddress), sourceAddress: sourceAddress),
(e) => e is SocketException); (e) => e is SocketException);
} }
asyncEnd(); await server.close();
server.close();
} }
// IPv4 addresses to use as source address when connecting locally. // IPv4 addresses to use as source address when connecting locally.
@ -85,8 +59,6 @@ var ipV6SourceAddresses = [
Future testConnect(InternetAddress bindAddress, bool v6Only, Future testConnect(InternetAddress bindAddress, bool v6Only,
Function connectFunction, Function closeDestroyFunction) async { Function connectFunction, Function closeDestroyFunction) async {
int freePort = await freeIPv4AndIPv6Port();
var successCount = 0; var successCount = 0;
if (!v6Only) successCount += ipV4SourceAddresses.length; if (!v6Only) successCount += ipV4SourceAddresses.length;
if (bindAddress.type == InternetAddressType.IPv6) { if (bindAddress.type == InternetAddressType.IPv6) {
@ -96,17 +68,14 @@ Future testConnect(InternetAddress bindAddress, bool v6Only,
var allConnected = new Completer(); var allConnected = new Completer();
if (successCount == 0) allConnected.complete(); if (successCount == 0) allConnected.complete();
asyncStart(); var server = await ServerSocket.bind(bindAddress, 0, v6Only: v6Only);
var server = await ServerSocket.bind(bindAddress, freePort, v6Only: v6Only);
server.listen((s) { server.listen((s) {
s.destroy(); s.destroy();
count++; count++;
if (count == successCount) allConnected.complete(); if (count == successCount) allConnected.complete();
}, onDone: () => asyncEnd()); });
asyncStart(); // Connect with IPv4 source addresses.
// Connect with IPv4 source addesses.
for (var sourceAddress in ipV4SourceAddresses) { for (var sourceAddress in ipV4SourceAddresses) {
if (!v6Only) { if (!v6Only) {
var s = await connectFunction(InternetAddress.loopbackIPv4, server.port, 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) { for (var sourceAddress in ipV6SourceAddresses) {
if (bindAddress.type == InternetAddressType.IPv6) { if (bindAddress.type == InternetAddressType.IPv6) {
var s = await connectFunction(InternetAddress.loopbackIPv6, server.port, var s = await connectFunction(InternetAddress.loopbackIPv6, server.port,
@ -139,12 +108,9 @@ Future testConnect(InternetAddress bindAddress, bool v6Only,
await allConnected.future; await allConnected.future;
await server.close(); await server.close();
asyncEnd();
} }
main() async { main() async {
asyncStart();
await retry(() async { await retry(() async {
await testArguments(RawSocket.connect); await testArguments(RawSocket.connect);
}); });
@ -176,6 +142,4 @@ main() async {
await testConnect( await testConnect(
InternetAddress.anyIPv6, true, Socket.connect, (s) => s.destroy()); InternetAddress.anyIPv6, true, Socket.connect, (s) => s.destroy());
}); });
asyncEnd();
} }

View file

@ -5,13 +5,7 @@
import 'dart:async'; import 'dart:async';
import 'dart:io'; import 'dart:io';
Future<int> freeIPv4AndIPv6Port() async { import "package:expect/expect.dart";
var socket =
await ServerSocket.bind(InternetAddress.anyIPv6, 0, v6Only: false);
int port = socket.port;
await socket.close();
return port;
}
int lastRetryId = 0; int lastRetryId = 0;
@ -31,3 +25,15 @@ Future retry(Future fun(), {int maxCount: 10}) async {
} }
return await fun(); 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');
}