OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import 'dart:async'; | 5 import 'dart:async'; |
6 import 'dart:io'; | 6 import 'dart:io'; |
7 import 'dart:convert'; | 7 import 'dart:convert'; |
8 | 8 |
9 import 'package:async_helper/async_helper.dart'; | 9 import 'package:async_helper/async_helper.dart'; |
10 import 'package:expect/expect.dart'; | 10 import 'package:expect/expect.dart'; |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 Expect.equals(socket.port, socket2.port); | 75 Expect.equals(socket.port, socket2.port); |
76 | 76 |
77 return Future.wait([ | 77 return Future.wait([ |
78 socket.close().whenComplete(asyncEnd), | 78 socket.close().whenComplete(asyncEnd), |
79 socket2.close().whenComplete(asyncEnd), | 79 socket2.close().whenComplete(asyncEnd), |
80 ]); | 80 ]); |
81 }); | 81 }); |
82 }); | 82 }); |
83 } | 83 } |
84 | 84 |
85 testSocketReferenceInteroperability(String host) { | |
86 asyncStart(); | |
87 ServerSocket.bind(host, 0).then((ServerSocket socket) { | |
88 Expect.isTrue(socket.port > 0); | |
89 | |
90 asyncStart(); | |
91 socket.reference.create().then((socket2) { | |
92 bool gotResponseFrom1; | |
93 bool gotResponseFrom2; | |
94 | |
95 Expect.isTrue(socket.port > 0); | |
96 Expect.equals(socket.port, socket2.port); | |
97 | |
98 asyncStart(); | |
99 asyncStart(); | |
100 asyncStart(); | |
101 socket.listen((client) { | |
102 client.drain().whenComplete(asyncEnd); | |
103 client.write('1: hello world'); | |
104 client.close().whenComplete(asyncEnd); | |
105 // NOTE: Closing the socket un-subscribes as well, which means the | |
106 // other client connection must go to the other socket. | |
107 socket.close().whenComplete(asyncEnd); | |
108 }, onDone: asyncEnd); | |
109 | |
110 asyncStart(); | |
111 asyncStart(); | |
112 asyncStart(); | |
113 socket2.listen((client) { | |
114 client.drain().whenComplete(asyncEnd); | |
115 client.write('2: hello world'); | |
116 client.close().whenComplete(asyncEnd); | |
117 // NOTE: Closing the socket un-subscribes as well, which means the | |
118 // other client connection must go to the other socket. | |
119 socket2.close().whenComplete(asyncEnd); | |
120 }, onDone: asyncEnd); | |
121 | |
122 var futures = []; | |
123 for (int i = 0; i < 2; i++) { | |
124 asyncStart(); | |
125 futures.add( | |
126 Socket.connect(socket.address, socket.port).then((Socket socket) { | |
127 socket.close().whenComplete(asyncEnd); | |
128 asyncStart(); | |
129 return socket | |
130 .transform(ASCII.decoder).join('').then((String result) { | |
131 if (result == '1: hello world') gotResponseFrom1 = true; | |
132 else if (result == '2: hello world') gotResponseFrom2 = true; | |
133 else throw 'Unexpected result from server: $result'; | |
134 asyncEnd(); | |
135 }); | |
136 })); | |
137 } | |
138 asyncStart(); | |
139 Future.wait(futures).then((_) { | |
140 Expect.isTrue(gotResponseFrom1); | |
141 Expect.isTrue(gotResponseFrom2); | |
142 asyncEnd(); | |
143 }); | |
144 }); | |
145 }); | |
146 } | |
147 | |
148 testListenCloseListenClose(String host) async { | 85 testListenCloseListenClose(String host) async { |
149 asyncStart(); | 86 asyncStart(); |
150 | 87 |
151 ServerSocket socket = | 88 ServerSocket socket = |
152 await ServerSocket.bind(host, 0, shared: true); | 89 await ServerSocket.bind(host, 0, shared: true); |
153 ServerSocket socket2 = | 90 ServerSocket socket2 = |
154 await ServerSocket.bind(host, socket.port, shared: true); | 91 await ServerSocket.bind(host, socket.port, shared: true); |
155 | 92 |
156 var subscription = socket.listen((_) { throw 'error'; }); | 93 var subscription = socket.listen((_) { throw 'error'; }); |
157 subscription.cancel(); | 94 subscription.cancel(); |
(...skipping 19 matching lines...) Expand all Loading... |
177 for (var host in ['127.0.0.1', '::1']) { | 114 for (var host in ['127.0.0.1', '::1']) { |
178 testBindShared(host, false); | 115 testBindShared(host, false); |
179 testBindShared(host, true); | 116 testBindShared(host, true); |
180 | 117 |
181 negTestBindSharedMismatch(host, false); | 118 negTestBindSharedMismatch(host, false); |
182 negTestBindSharedMismatch(host, true); | 119 negTestBindSharedMismatch(host, true); |
183 | 120 |
184 negTestBindV6OnlyMismatch(host, true); | 121 negTestBindV6OnlyMismatch(host, true); |
185 negTestBindV6OnlyMismatch(host, false); | 122 negTestBindV6OnlyMismatch(host, false); |
186 | 123 |
187 testSocketReferenceInteroperability(host); | |
188 | |
189 testListenCloseListenClose(host); | 124 testListenCloseListenClose(host); |
190 } | 125 } |
191 | 126 |
192 asyncStart(); | 127 asyncStart(); |
193 testBindDifferentAddresses(InternetAddress.ANY_IP_V6, | 128 testBindDifferentAddresses(InternetAddress.ANY_IP_V6, |
194 InternetAddress.ANY_IP_V4, | 129 InternetAddress.ANY_IP_V4, |
195 true, | 130 true, |
196 false).then((_) { | 131 false).then((_) { |
197 testBindDifferentAddresses(InternetAddress.ANY_IP_V4, | 132 testBindDifferentAddresses(InternetAddress.ANY_IP_V4, |
198 InternetAddress.ANY_IP_V6, | 133 InternetAddress.ANY_IP_V6, |
199 false, | 134 false, |
200 true); | 135 true); |
201 asyncEnd(); | 136 asyncEnd(); |
202 }); | 137 }); |
203 } | 138 } |
OLD | NEW |