Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(242)

Unified Diff: dart/tests/standalone/io/socket_bind_test.dart

Issue 879353003: Introduce optional 'bool shared' parameter to ServerSocket.bind() ... (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: Addressed comments Created 5 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « dart/sdk/lib/io/socket.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: dart/tests/standalone/io/socket_bind_test.dart
diff --git a/dart/tests/standalone/io/socket_bind_test.dart b/dart/tests/standalone/io/socket_bind_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..18fbaacbb2f4495f6608b39664fb178374a0d0cd
--- /dev/null
+++ b/dart/tests/standalone/io/socket_bind_test.dart
@@ -0,0 +1,165 @@
+// 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.
+
+import 'dart:async';
+import 'dart:io';
+import 'dart:convert';
+
+import 'package:async_helper/async_helper.dart';
+import 'package:expect/expect.dart';
+
+testBindShared(bool v6Only) {
+ asyncStart();
+ ServerSocket.bind(
+ 'localhost', 0, v6Only: v6Only, shared: true).then((socket) {
+ Expect.isTrue(socket.port > 0);
+
+ asyncStart();
+ return ServerSocket.bind(
+ 'localhost', 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);
+ });
+ });
+}
+
+negTestBindSharedMismatch(bool v6Only) {
+ asyncStart();
+ ServerSocket.bind('localhost', 0, v6Only: v6Only).then((ServerSocket socket) {
+ Expect.isTrue(socket.port > 0);
+
+ asyncStart();
+ return ServerSocket.bind(
+ 'localhost', socket.port, v6Only: v6Only).catchError((error) {
+ Expect.isTrue(error is SocketException);
+ Expect.isTrue('$error'.contains('shared flag'));
+ socket.close().whenComplete(asyncEnd);
+ asyncEnd();
+ });
+ });
+}
+
+negTestBindV6OnlyMismatch(bool v6Only) {
+ asyncStart();
+ ServerSocket.bind(
+ 'localhost', 0, v6Only: v6Only, shared: true).then((ServerSocket socket) {
+ Expect.isTrue(socket.port > 0);
+
+ asyncStart();
+ return ServerSocket.bind(
+ 'localhost', socket.port, v6Only: !v6Only, shared: true)
+ .catchError((error) {
+ Expect.isTrue(error is SocketException);
+ Expect.isTrue('$error'.contains('v6Only flag'));
+ socket.close().whenComplete(asyncEnd);
+ asyncEnd();
+ });
+ });
+}
+
+Future testBindDifferentAddresses(InternetAddress addr1,
+ InternetAddress addr2,
+ bool addr1V6Only,
+ bool addr2V6Only) {
+ asyncStart();
+ return ServerSocket.bind(
+ addr1, 0, v6Only: addr1V6Only, shared: false).then((socket) {
+ Expect.isTrue(socket.port > 0);
+
+ asyncStart();
+ return ServerSocket.bind(
+ addr2, socket.port, v6Only: addr2V6Only, shared: false).then((socket2) {
+ Expect.equals(socket.port, socket2.port);
+
+ return Future.wait([
+ socket.close().whenComplete(asyncEnd),
+ socket2.close().whenComplete(asyncEnd),
+ ]);
+ });
+ });
+}
+
+testSocketReferenceInteroperability() {
+ asyncStart();
+ ServerSocket.bind('localhost', 0).then((ServerSocket socket) {
+ Expect.isTrue(socket.port > 0);
+
+ asyncStart();
+ socket.reference.create().then((socket2) {
+ bool gotResponseFrom1;
+ bool gotResponseFrom2;
+
+ Expect.isTrue(socket.port > 0);
+ Expect.equals(socket.port, socket2.port);
+
+ asyncStart();
+ asyncStart();
+ socket.listen((client) {
+ client.drain().whenComplete(asyncEnd);
+ client.write('1: hello world');
+ client.close();
+ socket.close().whenComplete(asyncEnd);
+ }, onDone: asyncEnd);
+
+ asyncStart();
+ asyncStart();
+ socket2.listen((client) {
+ client.drain().whenComplete(asyncEnd);
+ client.write('2: hello world');
+ client.close();
+ socket2.close().whenComplete(asyncEnd);
+ }, onDone: asyncEnd);
+
+ var futures = [];
+ for (int i = 0; i < 2; i++) {
+ asyncStart();
+ futures.add(
+ Socket.connect(socket.address, socket.port).then((Socket socket) {
+ socket.close().whenComplete(asyncEnd);
+ asyncStart();
+ return socket
+ .transform(ASCII.decoder).join('').then((String result) {
+ if (result == '1: hello world') gotResponseFrom1 = true;
+ else if (result == '2: hello world') gotResponseFrom2 = true;
+ else throw 'Unexpected result from server: $result';
+ asyncEnd();
+ });
+ }));
+ }
+ asyncStart();
+ Future.wait(futures).then((_) {
+ Expect.isTrue(gotResponseFrom1);
+ Expect.isTrue(gotResponseFrom2);
+ asyncEnd();
+ });
+ });
+ });
+}
+
+void main() {
+ testBindShared(false);
+ testBindShared(true);
+
+ negTestBindSharedMismatch(false);
+ negTestBindSharedMismatch(true);
+
+ negTestBindV6OnlyMismatch(true);
+ negTestBindV6OnlyMismatch(false);
+
+ asyncStart();
+ testBindDifferentAddresses(InternetAddress.ANY_IP_V6,
+ InternetAddress.ANY_IP_V4,
+ true,
+ false).then((_) {
+ testBindDifferentAddresses(InternetAddress.ANY_IP_V4,
+ InternetAddress.ANY_IP_V6,
+ false,
+ true);
+ asyncEnd();
+ });
+
+ testSocketReferenceInteroperability();
+}
« no previous file with comments | « dart/sdk/lib/io/socket.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698