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 // VMOptions= | 5 // VMOptions= |
6 // VMOptions=--short_socket_read | 6 // VMOptions=--short_socket_read |
7 // VMOptions=--short_socket_write | 7 // VMOptions=--short_socket_write |
8 // VMOptions=--short_socket_read --short_socket_write | 8 // VMOptions=--short_socket_read --short_socket_write |
9 | 9 |
10 import "package:expect/expect.dart"; | 10 import "package:expect/expect.dart"; |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 List<int> expected = createTestData(); | 150 List<int> expected = createTestData(); |
151 for (int i = 0; i < messageSize; i++) { | 151 for (int i = 0; i < messageSize; i++) { |
152 Expect.equals(expected[i], data[i]); | 152 Expect.equals(expected[i], data[i]); |
153 } | 153 } |
154 } | 154 } |
155 | 155 |
156 RawServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { | 156 RawServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { |
157 server.listen((client) { | 157 server.listen((client) { |
158 int bytesRead = 0; | 158 int bytesRead = 0; |
159 int bytesWritten = 0; | 159 int bytesWritten = 0; |
| 160 bool closedEventReceived = false; |
160 List<int> data = new List<int>(messageSize); | 161 List<int> data = new List<int>(messageSize); |
161 | 162 |
162 client.writeEventsEnabled = false; | 163 client.writeEventsEnabled = false; |
163 client.listen((event) { | 164 client.listen((event) { |
164 switch (event) { | 165 switch (event) { |
165 case RawSocketEvent.READ: | 166 case RawSocketEvent.READ: |
166 Expect.isTrue(bytesWritten == 0); | 167 Expect.isTrue(bytesWritten == 0); |
167 Expect.isTrue(client.available() > 0); | 168 Expect.isTrue(client.available() > 0); |
168 var buffer = client.read(); | 169 var buffer = client.read(); |
169 data.setRange(bytesRead, bytesRead + buffer.length, buffer); | 170 data.setRange(bytesRead, bytesRead + buffer.length, buffer); |
(...skipping 10 matching lines...) Expand all Loading... |
180 if (bytesWritten < data.length) { | 181 if (bytesWritten < data.length) { |
181 client.writeEventsEnabled = true; | 182 client.writeEventsEnabled = true; |
182 } | 183 } |
183 if (bytesWritten == data.length) { | 184 if (bytesWritten == data.length) { |
184 client.shutdown(SocketDirection.SEND); | 185 client.shutdown(SocketDirection.SEND); |
185 } | 186 } |
186 break; | 187 break; |
187 case RawSocketEvent.READ_CLOSED: | 188 case RawSocketEvent.READ_CLOSED: |
188 server.close(); | 189 server.close(); |
189 break; | 190 break; |
| 191 case RawSocketEvent.CLOSED: |
| 192 Expect.isFalse(closedEventReceived); |
| 193 closedEventReceived = true; |
| 194 break; |
190 default: throw "Unexpected event $event"; | 195 default: throw "Unexpected event $event"; |
191 } | 196 } |
192 }); | 197 }, |
| 198 onDone: () => Expect.isTrue(closedEventReceived)); |
193 }); | 199 }); |
194 | 200 |
195 RawSocket.connect("127.0.0.1", server.port).then((socket) { | 201 RawSocket.connect("127.0.0.1", server.port).then((socket) { |
196 int bytesRead = 0; | 202 int bytesRead = 0; |
197 int bytesWritten = 0; | 203 int bytesWritten = 0; |
| 204 bool closedEventReceived = false; |
198 List<int> data = createTestData(); | 205 List<int> data = createTestData(); |
199 | 206 |
200 socket.listen((event) { | 207 socket.listen((event) { |
201 switch (event) { | 208 switch (event) { |
202 case RawSocketEvent.READ: | 209 case RawSocketEvent.READ: |
203 Expect.isTrue(socket.available() > 0); | 210 Expect.isTrue(socket.available() > 0); |
204 var buffer = socket.read(); | 211 var buffer = socket.read(); |
205 data.setRange(bytesRead, bytesRead + buffer.length, buffer); | 212 data.setRange(bytesRead, bytesRead + buffer.length, buffer); |
206 bytesRead += buffer.length; | 213 bytesRead += buffer.length; |
207 break; | 214 break; |
208 case RawSocketEvent.WRITE: | 215 case RawSocketEvent.WRITE: |
209 Expect.isTrue(bytesRead == 0); | 216 Expect.isTrue(bytesRead == 0); |
210 Expect.isFalse(socket.writeEventsEnabled); | 217 Expect.isFalse(socket.writeEventsEnabled); |
211 bytesWritten += socket.write( | 218 bytesWritten += socket.write( |
212 data, bytesWritten, data.length - bytesWritten); | 219 data, bytesWritten, data.length - bytesWritten); |
213 if (bytesWritten < data.length) { | 220 if (bytesWritten < data.length) { |
214 socket.writeEventsEnabled = true; | 221 socket.writeEventsEnabled = true; |
215 } else { | 222 } else { |
216 data = new List<int>(messageSize); | 223 data = new List<int>(messageSize); |
217 } | 224 } |
218 break; | 225 break; |
219 case RawSocketEvent.READ_CLOSED: | 226 case RawSocketEvent.READ_CLOSED: |
220 verifyTestData(data); | 227 verifyTestData(data); |
221 socket.close(); | 228 socket.close(); |
222 break; | 229 break; |
| 230 case RawSocketEvent.CLOSED: |
| 231 Expect.isFalse(closedEventReceived); |
| 232 closedEventReceived = true; |
| 233 break; |
223 default: throw "Unexpected event $event"; | 234 default: throw "Unexpected event $event"; |
224 } | 235 } |
225 }, | 236 }, |
226 onDone: () => port.close()); | 237 onDone: () { |
| 238 Expect.isTrue(closedEventReceived); |
| 239 port.close(); |
| 240 }); |
227 }); | 241 }); |
228 }); | 242 }); |
229 } | 243 } |
230 | 244 |
231 testPauseServerSocket() { | 245 testPauseServerSocket() { |
232 const int socketCount = 10; | 246 const int socketCount = 10; |
233 var acceptCount = 0; | 247 var acceptCount = 0; |
234 var resumed = false; | 248 var resumed = false; |
235 | 249 |
236 ReceivePort port = new ReceivePort(); | 250 ReceivePort port = new ReceivePort(); |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 int bytesWritten = 0; | 286 int bytesWritten = 0; |
273 int bytesRead = 0; | 287 int bytesRead = 0; |
274 var writeSubscription; | 288 var writeSubscription; |
275 var readSubscription; | 289 var readSubscription; |
276 | 290 |
277 ReceivePort port = new ReceivePort(); | 291 ReceivePort port = new ReceivePort(); |
278 | 292 |
279 RawServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { | 293 RawServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { |
280 Expect.isTrue(server.port > 0); | 294 Expect.isTrue(server.port > 0); |
281 server.listen((client) { | 295 server.listen((client) { |
| 296 bool closedEventReceived = false; |
282 List<int> data = new List<int>.filled(messageSize, 0); | 297 List<int> data = new List<int>.filled(messageSize, 0); |
283 writeSubscription = client.listen((event) { | 298 writeSubscription = client.listen((event) { |
284 switch (event) { | 299 switch (event) { |
285 case RawSocketEvent.READ: | 300 case RawSocketEvent.READ: |
286 throw "Unexpected read event"; | 301 throw "Unexpected read event"; |
287 case RawSocketEvent.WRITE: | 302 case RawSocketEvent.WRITE: |
288 if (pauseResumeCount == loopCount) return; | 303 if (pauseResumeCount == loopCount) return; |
289 Expect.isFalse(client.writeEventsEnabled); | 304 Expect.isFalse(client.writeEventsEnabled); |
290 Expect.equals(0, bytesRead); // Checks that reader is paused. | 305 Expect.equals(0, bytesRead); // Checks that reader is paused. |
291 bytesWritten += client.write( | 306 bytesWritten += client.write( |
292 data, bytesWritten, data.length - bytesWritten); | 307 data, bytesWritten, data.length - bytesWritten); |
293 // Ensure all data is written. When done disable the write | 308 // Ensure all data is written. When done disable the write |
294 // event and resume the receiver. | 309 // event and resume the receiver. |
295 if (bytesWritten == data.length) { | 310 if (bytesWritten == data.length) { |
296 writeSubscription.pause(); | 311 writeSubscription.pause(); |
297 bytesWritten = 0; | 312 bytesWritten = 0; |
298 connected.future.then((_) { readSubscription.resume(); }); | 313 connected.future.then((_) { readSubscription.resume(); }); |
299 } | 314 } |
300 client.writeEventsEnabled = true; | 315 client.writeEventsEnabled = true; |
301 break; | 316 break; |
302 case RawSocketEvent.READ_CLOSED: | 317 case RawSocketEvent.READ_CLOSED: |
303 client.close(); | 318 client.close(); |
304 server.close(); | 319 server.close(); |
305 break; | 320 break; |
| 321 case RawSocketEvent.CLOSED: |
| 322 Expect.isFalse(closedEventReceived); |
| 323 closedEventReceived = true; |
| 324 break; |
306 default: throw "Unexpected event $event"; | 325 default: throw "Unexpected event $event"; |
307 } | 326 } |
308 }); | 327 }, |
| 328 onDone: () => Expect.isTrue(closedEventReceived)); |
309 }); | 329 }); |
310 | 330 |
311 RawSocket.connect("127.0.0.1", server.port).then((socket) { | 331 RawSocket.connect("127.0.0.1", server.port).then((socket) { |
| 332 bool closedEventReceived = false; |
312 socket.writeEventsEnabled = false; | 333 socket.writeEventsEnabled = false; |
313 readSubscription = socket.listen((event) { | 334 readSubscription = socket.listen((event) { |
314 switch (event) { | 335 switch (event) { |
315 case RawSocketEvent.READ: | 336 case RawSocketEvent.READ: |
316 Expect.equals(0, bytesWritten); // Checks that writer is paused. | 337 Expect.equals(0, bytesWritten); // Checks that writer is paused. |
317 Expect.isTrue(socket.available() > 0); | 338 Expect.isTrue(socket.available() > 0); |
318 var buffer = socket.read(); | 339 var buffer = socket.read(); |
319 bytesRead += buffer.length; | 340 bytesRead += buffer.length; |
320 // Ensure all data is read. When done pause and resume the sender | 341 // Ensure all data is read. When done pause and resume the sender |
321 if (bytesRead == messageSize) { | 342 if (bytesRead == messageSize) { |
322 if (++pauseResumeCount == loopCount) { | 343 if (++pauseResumeCount == loopCount) { |
323 socket.close(); | 344 socket.close(); |
324 port.close(); | 345 port.close(); |
325 } else { | 346 } else { |
326 readSubscription.pause(); | 347 readSubscription.pause(); |
327 } | 348 } |
328 // Always resume writer as it needs the read closed | 349 // Always resume writer as it needs the read closed |
329 // event when done. | 350 // event when done. |
330 bytesRead = 0; | 351 bytesRead = 0; |
331 writeSubscription.resume(); | 352 writeSubscription.resume(); |
332 } | 353 } |
333 break; | 354 break; |
334 case RawSocketEvent.WRITE: | 355 case RawSocketEvent.WRITE: |
335 throw "Unexpected write event"; | 356 throw "Unexpected write event"; |
336 case RawSocketEvent.READ_CLOSED: | 357 case RawSocketEvent.READ_CLOSED: |
337 throw "Unexpected close event"; | 358 throw "Unexpected read closed event"; |
| 359 case RawSocketEvent.CLOSED: |
| 360 Expect.isFalse(closedEventReceived); |
| 361 closedEventReceived = true; |
| 362 break; |
338 default: throw "Unexpected event $event"; | 363 default: throw "Unexpected event $event"; |
339 } | 364 } |
340 }); | 365 }, |
| 366 onDone: () => Expect.isTrue(closedEventReceived)); |
341 readSubscription.pause(); | 367 readSubscription.pause(); |
342 connected.complete(true); | 368 connected.complete(true); |
343 }); | 369 }); |
344 }); | 370 }); |
345 } | 371 } |
346 | 372 |
347 main() { | 373 main() { |
348 testArguments(); | 374 testArguments(); |
349 testSimpleBind(); | 375 testSimpleBind(); |
350 testCloseOneEnd("client"); | 376 testCloseOneEnd("client"); |
351 testCloseOneEnd("server"); | 377 testCloseOneEnd("server"); |
352 testInvalidBind(); | 378 testInvalidBind(); |
353 testSimpleConnect(); | 379 testSimpleConnect(); |
354 testServerListenAfterConnect(); | 380 testServerListenAfterConnect(); |
355 testSimpleReadWrite(); | 381 testSimpleReadWrite(); |
356 testPauseServerSocket(); | 382 testPauseServerSocket(); |
357 testPauseSocket(); | 383 testPauseSocket(); |
358 } | 384 } |
OLD | NEW |