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 "dart:async"; | 10 import "dart:async"; |
(...skipping 13 matching lines...) Expand all Loading... |
24 RawServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((s) { | 24 RawServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((s) { |
25 Expect.isTrue(s.port > 0); | 25 Expect.isTrue(s.port > 0); |
26 s.close(); | 26 s.close(); |
27 asyncEnd(); | 27 asyncEnd(); |
28 }); | 28 }); |
29 } | 29 } |
30 | 30 |
31 void testInvalidBind() { | 31 void testInvalidBind() { |
32 // Bind to a unknown DNS name. | 32 // Bind to a unknown DNS name. |
33 asyncStart(); | 33 asyncStart(); |
34 RawServerSocket.bind("ko.faar.__hest__", 0) | 34 RawServerSocket.bind("ko.faar.__hest__", 0).then((_) { |
35 .then((_) { Expect.fail("Failure expected"); } ) | 35 Expect.fail("Failure expected"); |
36 .catchError((error) { | 36 }).catchError((error) { |
37 Expect.isTrue(error is SocketException); | 37 Expect.isTrue(error is SocketException); |
38 asyncEnd(); | 38 asyncEnd(); |
39 }); | 39 }); |
40 | 40 |
41 // Bind to an unavaliable IP-address. | 41 // Bind to an unavaliable IP-address. |
42 asyncStart(); | 42 asyncStart(); |
43 RawServerSocket.bind("8.8.8.8", 0) | 43 RawServerSocket.bind("8.8.8.8", 0).then((_) { |
44 .then((_) { Expect.fail("Failure expected"); } ) | 44 Expect.fail("Failure expected"); |
45 .catchError((error) { | 45 }).catchError((error) { |
46 Expect.isTrue(error is SocketException); | 46 Expect.isTrue(error is SocketException); |
47 asyncEnd(); | 47 asyncEnd(); |
48 }); | 48 }); |
49 | 49 |
50 // Bind to a port already in use. | 50 // Bind to a port already in use. |
51 asyncStart(); | 51 asyncStart(); |
52 RawServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0) | 52 RawServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((s) { |
53 .then((s) { | 53 RawServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, s.port).then((t) { |
54 RawServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, s.port) | 54 Expect.fail("Multiple listens on same port"); |
55 .then((t) { | 55 }).catchError((error) { |
56 Expect.fail("Multiple listens on same port"); | 56 Expect.isTrue(error is SocketException); |
57 }) | 57 s.close(); |
58 .catchError((error) { | 58 asyncEnd(); |
59 Expect.isTrue(error is SocketException); | 59 }); |
60 s.close(); | 60 }); |
61 asyncEnd(); | |
62 }); | |
63 }); | |
64 } | 61 } |
65 | 62 |
66 void testSimpleConnect() { | 63 void testSimpleConnect() { |
67 asyncStart(); | 64 asyncStart(); |
68 RawServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { | 65 RawServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { |
69 server.listen((socket) { socket.close(); }); | 66 server.listen((socket) { |
| 67 socket.close(); |
| 68 }); |
70 RawSocket.connect("127.0.0.1", server.port).then((socket) { | 69 RawSocket.connect("127.0.0.1", server.port).then((socket) { |
71 server.close(); | 70 server.close(); |
72 socket.close(); | 71 socket.close(); |
73 asyncEnd(); | 72 asyncEnd(); |
74 }); | 73 }); |
75 }); | 74 }); |
76 } | 75 } |
77 | 76 |
78 void testCloseOneEnd(String toClose) { | 77 void testCloseOneEnd(String toClose) { |
79 asyncStart(); | 78 asyncStart(); |
80 Completer serverDone = new Completer(); | 79 Completer serverDone = new Completer(); |
81 Completer serverEndDone = new Completer(); | 80 Completer serverEndDone = new Completer(); |
82 Completer clientEndDone = new Completer(); | 81 Completer clientEndDone = new Completer(); |
83 Future.wait([serverDone.future, serverEndDone.future, clientEndDone.future]) | 82 Future.wait([ |
84 .then((_) { | 83 serverDone.future, |
85 asyncEnd(); | 84 serverEndDone.future, |
86 }); | 85 clientEndDone.future |
| 86 ]).then((_) { |
| 87 asyncEnd(); |
| 88 }); |
87 RawServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { | 89 RawServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { |
88 server.listen((serverConnection) { | 90 server.listen((serverConnection) { |
89 serverConnection.listen((event) { | 91 serverConnection.listen((event) { |
90 if (toClose == "server" || event == RawSocketEvent.READ_CLOSED) { | 92 if (toClose == "server" || event == RawSocketEvent.READ_CLOSED) { |
91 serverConnection.shutdown(SocketDirection.SEND); | 93 serverConnection.shutdown(SocketDirection.SEND); |
92 } | 94 } |
93 }, | 95 }, onDone: () { |
94 onDone: () { | |
95 serverEndDone.complete(null); | 96 serverEndDone.complete(null); |
96 }); | 97 }); |
97 }, | 98 }, onDone: () { |
98 onDone:() { | |
99 serverDone.complete(null); | 99 serverDone.complete(null); |
100 }); | 100 }); |
101 RawSocket.connect("127.0.0.1", server.port).then((clientConnection) { | 101 RawSocket.connect("127.0.0.1", server.port).then((clientConnection) { |
102 clientConnection.listen((event){ | 102 clientConnection.listen((event) { |
103 if (toClose == "client" || event == RawSocketEvent.READ_CLOSED) { | 103 if (toClose == "client" || event == RawSocketEvent.READ_CLOSED) { |
104 clientConnection.shutdown(SocketDirection.SEND); | 104 clientConnection.shutdown(SocketDirection.SEND); |
105 } | 105 } |
106 }, | 106 }, onDone: () { |
107 onDone: () { | |
108 clientEndDone.complete(null); | 107 clientEndDone.complete(null); |
109 server.close(); | 108 server.close(); |
110 }); | 109 }); |
111 }); | 110 }); |
112 }); | 111 }); |
113 } | 112 } |
114 | 113 |
115 void testServerListenAfterConnect() { | 114 void testServerListenAfterConnect() { |
116 asyncStart(); | 115 asyncStart(); |
117 RawServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { | 116 RawServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 var buffer = client.read(200); | 173 var buffer = client.read(200); |
175 data.setRange(bytesRead, bytesRead + buffer.length, buffer); | 174 data.setRange(bytesRead, bytesRead + buffer.length, buffer); |
176 bytesRead += buffer.length; | 175 bytesRead += buffer.length; |
177 if (bytesRead == data.length) { | 176 if (bytesRead == data.length) { |
178 verifyTestData(data); | 177 verifyTestData(data); |
179 client.writeEventsEnabled = true; | 178 client.writeEventsEnabled = true; |
180 } | 179 } |
181 break; | 180 break; |
182 case RawSocketEvent.WRITE: | 181 case RawSocketEvent.WRITE: |
183 Expect.isFalse(client.writeEventsEnabled); | 182 Expect.isFalse(client.writeEventsEnabled); |
184 bytesWritten += client.write( | 183 bytesWritten += |
185 data, bytesWritten, data.length - bytesWritten); | 184 client.write(data, bytesWritten, data.length - bytesWritten); |
186 if (bytesWritten < data.length) { | 185 if (bytesWritten < data.length) { |
187 client.writeEventsEnabled = true; | 186 client.writeEventsEnabled = true; |
188 } | 187 } |
189 if (bytesWritten == data.length) { | 188 if (bytesWritten == data.length) { |
190 client.shutdown(SocketDirection.SEND); | 189 client.shutdown(SocketDirection.SEND); |
191 } | 190 } |
192 break; | 191 break; |
193 case RawSocketEvent.READ_CLOSED: | 192 case RawSocketEvent.READ_CLOSED: |
194 server.close(); | 193 server.close(); |
195 break; | 194 break; |
196 case RawSocketEvent.CLOSED: | 195 case RawSocketEvent.CLOSED: |
197 Expect.isFalse(closedEventReceived); | 196 Expect.isFalse(closedEventReceived); |
198 closedEventReceived = true; | 197 closedEventReceived = true; |
199 break; | 198 break; |
200 default: throw "Unexpected event $event"; | 199 default: |
| 200 throw "Unexpected event $event"; |
201 } | 201 } |
202 }, | 202 }, onDone: () => Expect.isTrue(closedEventReceived)); |
203 onDone: () => Expect.isTrue(closedEventReceived)); | |
204 }); | 203 }); |
205 | 204 |
206 RawSocket.connect("127.0.0.1", server.port).then((socket) { | 205 RawSocket.connect("127.0.0.1", server.port).then((socket) { |
207 int bytesRead = 0; | 206 int bytesRead = 0; |
208 int bytesWritten = 0; | 207 int bytesWritten = 0; |
209 bool closedEventReceived = false; | 208 bool closedEventReceived = false; |
210 List<int> data = createTestData(); | 209 List<int> data = createTestData(); |
211 | 210 |
212 socket.listen((event) { | 211 socket.listen((event) { |
213 switch (event) { | 212 switch (event) { |
214 case RawSocketEvent.READ: | 213 case RawSocketEvent.READ: |
215 Expect.isTrue(socket.available() > 0); | 214 Expect.isTrue(socket.available() > 0); |
216 if (dropReads) { | 215 if (dropReads) { |
217 if (clientReadCount != 10) { | 216 if (clientReadCount != 10) { |
218 clientReadCount++; | 217 clientReadCount++; |
219 break; | 218 break; |
220 } else { | 219 } else { |
221 clientReadCount = 0; | 220 clientReadCount = 0; |
222 } | 221 } |
223 } | 222 } |
224 var buffer = socket.read(); | 223 var buffer = socket.read(); |
225 data.setRange(bytesRead, bytesRead + buffer.length, buffer); | 224 data.setRange(bytesRead, bytesRead + buffer.length, buffer); |
226 bytesRead += buffer.length; | 225 bytesRead += buffer.length; |
227 break; | 226 break; |
228 case RawSocketEvent.WRITE: | 227 case RawSocketEvent.WRITE: |
229 Expect.isTrue(bytesRead == 0); | 228 Expect.isTrue(bytesRead == 0); |
230 Expect.isFalse(socket.writeEventsEnabled); | 229 Expect.isFalse(socket.writeEventsEnabled); |
231 bytesWritten += socket.write( | 230 bytesWritten += |
232 data, bytesWritten, data.length - bytesWritten); | 231 socket.write(data, bytesWritten, data.length - bytesWritten); |
233 if (bytesWritten < data.length) { | 232 if (bytesWritten < data.length) { |
234 socket.writeEventsEnabled = true; | 233 socket.writeEventsEnabled = true; |
235 } else { | 234 } else { |
236 data = new List<int>(messageSize); | 235 data = new List<int>(messageSize); |
237 } | 236 } |
238 break; | 237 break; |
239 case RawSocketEvent.READ_CLOSED: | 238 case RawSocketEvent.READ_CLOSED: |
240 verifyTestData(data); | 239 verifyTestData(data); |
241 socket.close(); | 240 socket.close(); |
242 break; | 241 break; |
243 case RawSocketEvent.CLOSED: | 242 case RawSocketEvent.CLOSED: |
244 Expect.isFalse(closedEventReceived); | 243 Expect.isFalse(closedEventReceived); |
245 closedEventReceived = true; | 244 closedEventReceived = true; |
246 break; | 245 break; |
247 default: throw "Unexpected event $event"; | 246 default: |
| 247 throw "Unexpected event $event"; |
248 } | 248 } |
249 }, | 249 }, onDone: () { |
250 onDone: () { | |
251 Expect.isTrue(closedEventReceived); | 250 Expect.isTrue(closedEventReceived); |
252 asyncEnd(); | 251 asyncEnd(); |
253 }); | 252 }); |
254 }); | 253 }); |
255 }); | 254 }); |
256 } | 255 } |
257 | 256 |
258 testPauseServerSocket() { | 257 testPauseServerSocket() { |
259 const int socketCount = 10; | 258 const int socketCount = 10; |
260 var acceptCount = 0; | 259 var acceptCount = 0; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
310 server.listen((client) { | 309 server.listen((client) { |
311 bool closedEventReceived = false; | 310 bool closedEventReceived = false; |
312 List<int> data = new List<int>.filled(messageSize, 0); | 311 List<int> data = new List<int>.filled(messageSize, 0); |
313 writeSubscription = client.listen((event) { | 312 writeSubscription = client.listen((event) { |
314 switch (event) { | 313 switch (event) { |
315 case RawSocketEvent.READ: | 314 case RawSocketEvent.READ: |
316 throw "Unexpected read event"; | 315 throw "Unexpected read event"; |
317 case RawSocketEvent.WRITE: | 316 case RawSocketEvent.WRITE: |
318 if (pauseResumeCount == loopCount) return; | 317 if (pauseResumeCount == loopCount) return; |
319 Expect.isFalse(client.writeEventsEnabled); | 318 Expect.isFalse(client.writeEventsEnabled); |
320 Expect.equals(0, bytesRead); // Checks that reader is paused. | 319 Expect.equals(0, bytesRead); // Checks that reader is paused. |
321 bytesWritten += client.write( | 320 bytesWritten += |
322 data, bytesWritten, data.length - bytesWritten); | 321 client.write(data, bytesWritten, data.length - bytesWritten); |
323 // Ensure all data is written. When done disable the write | 322 // Ensure all data is written. When done disable the write |
324 // event and resume the receiver. | 323 // event and resume the receiver. |
325 if (bytesWritten == data.length) { | 324 if (bytesWritten == data.length) { |
326 writeSubscription.pause(); | 325 writeSubscription.pause(); |
327 bytesWritten = 0; | 326 bytesWritten = 0; |
328 connected.future.then((_) { readSubscription.resume(); }); | 327 connected.future.then((_) { |
| 328 readSubscription.resume(); |
| 329 }); |
329 } | 330 } |
330 client.writeEventsEnabled = true; | 331 client.writeEventsEnabled = true; |
331 break; | 332 break; |
332 case RawSocketEvent.READ_CLOSED: | 333 case RawSocketEvent.READ_CLOSED: |
333 client.close(); | 334 client.close(); |
334 server.close(); | 335 server.close(); |
335 break; | 336 break; |
336 case RawSocketEvent.CLOSED: | 337 case RawSocketEvent.CLOSED: |
337 Expect.isFalse(closedEventReceived); | 338 Expect.isFalse(closedEventReceived); |
338 closedEventReceived = true; | 339 closedEventReceived = true; |
339 break; | 340 break; |
340 default: throw "Unexpected event $event"; | 341 default: |
| 342 throw "Unexpected event $event"; |
341 } | 343 } |
342 }, | 344 }, onDone: () => Expect.isTrue(closedEventReceived)); |
343 onDone: () => Expect.isTrue(closedEventReceived)); | |
344 }); | 345 }); |
345 | 346 |
346 RawSocket.connect("127.0.0.1", server.port).then((socket) { | 347 RawSocket.connect("127.0.0.1", server.port).then((socket) { |
347 bool closedEventReceived = false; | 348 bool closedEventReceived = false; |
348 socket.writeEventsEnabled = false; | 349 socket.writeEventsEnabled = false; |
349 readSubscription = socket.listen((event) { | 350 readSubscription = socket.listen((event) { |
350 switch (event) { | 351 switch (event) { |
351 case RawSocketEvent.READ: | 352 case RawSocketEvent.READ: |
352 Expect.equals(0, bytesWritten); // Checks that writer is paused. | 353 Expect.equals(0, bytesWritten); // Checks that writer is paused. |
353 Expect.isTrue(socket.available() > 0); | 354 Expect.isTrue(socket.available() > 0); |
354 var buffer = socket.read(); | 355 var buffer = socket.read(); |
355 bytesRead += buffer.length; | 356 bytesRead += buffer.length; |
356 // Ensure all data is read. When done pause and resume the sender | 357 // Ensure all data is read. When done pause and resume the sender |
357 if (bytesRead == messageSize) { | 358 if (bytesRead == messageSize) { |
358 if (++pauseResumeCount == loopCount) { | 359 if (++pauseResumeCount == loopCount) { |
359 socket.close(); | 360 socket.close(); |
360 asyncEnd(); | 361 asyncEnd(); |
361 } else { | 362 } else { |
362 readSubscription.pause(); | 363 readSubscription.pause(); |
363 } | 364 } |
364 // Always resume writer as it needs the read closed | 365 // Always resume writer as it needs the read closed |
365 // event when done. | 366 // event when done. |
366 bytesRead = 0; | 367 bytesRead = 0; |
367 writeSubscription.resume(); | 368 writeSubscription.resume(); |
368 } | 369 } |
369 break; | 370 break; |
370 case RawSocketEvent.WRITE: | 371 case RawSocketEvent.WRITE: |
371 throw "Unexpected write event"; | 372 throw "Unexpected write event"; |
372 case RawSocketEvent.READ_CLOSED: | 373 case RawSocketEvent.READ_CLOSED: |
373 throw "Unexpected read closed event"; | 374 throw "Unexpected read closed event"; |
374 case RawSocketEvent.CLOSED: | 375 case RawSocketEvent.CLOSED: |
375 Expect.isFalse(closedEventReceived); | 376 Expect.isFalse(closedEventReceived); |
376 closedEventReceived = true; | 377 closedEventReceived = true; |
377 break; | 378 break; |
378 default: throw "Unexpected event $event"; | 379 default: |
| 380 throw "Unexpected event $event"; |
379 } | 381 } |
380 }, | 382 }, onDone: () => Expect.isTrue(closedEventReceived)); |
381 onDone: () => Expect.isTrue(closedEventReceived)); | |
382 readSubscription.pause(); | 383 readSubscription.pause(); |
383 connected.complete(true); | 384 connected.complete(true); |
384 }); | 385 }); |
385 }); | 386 }); |
386 } | 387 } |
387 | 388 |
388 void testSocketZone() { | 389 void testSocketZone() { |
389 asyncStart(); | 390 asyncStart(); |
390 Expect.equals(Zone.ROOT, Zone.current); | 391 Expect.equals(Zone.ROOT, Zone.current); |
391 runZoned(() { | 392 runZoned(() { |
(...skipping 24 matching lines...) Expand all Loading... |
416 Expect.notEquals(Zone.ROOT, Zone.current); | 417 Expect.notEquals(Zone.ROOT, Zone.current); |
417 RawServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { | 418 RawServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { |
418 Expect.notEquals(Zone.ROOT, Zone.current); | 419 Expect.notEquals(Zone.ROOT, Zone.current); |
419 server.listen((socket) { | 420 server.listen((socket) { |
420 Expect.notEquals(Zone.ROOT, Zone.current); | 421 Expect.notEquals(Zone.ROOT, Zone.current); |
421 var timer; | 422 var timer; |
422 void write() { | 423 void write() { |
423 socket.write(const [0]); | 424 socket.write(const [0]); |
424 timer = new Timer(const Duration(milliseconds: 5), write); | 425 timer = new Timer(const Duration(milliseconds: 5), write); |
425 } | 426 } |
| 427 |
426 write(); | 428 write(); |
427 socket.listen((_) { | 429 socket.listen((_) {}, onError: (error) { |
428 }, onError: (error) { | |
429 timer.cancel(); | 430 timer.cancel(); |
430 Expect.notEquals(Zone.ROOT, Zone.current); | 431 Expect.notEquals(Zone.ROOT, Zone.current); |
431 socket.close(); | 432 socket.close(); |
432 server.close(); | 433 server.close(); |
433 throw error; | 434 throw error; |
434 }); | 435 }); |
435 }); | 436 }); |
436 RawSocket.connect("127.0.0.1", server.port).then((socket) { | 437 RawSocket.connect("127.0.0.1", server.port).then((socket) { |
437 socket.close(); | 438 socket.close(); |
438 }); | 439 }); |
439 }); | 440 }); |
440 }, onError: (e) { | 441 }, onError: (e) { |
441 asyncEnd(); | 442 asyncEnd(); |
442 }); | 443 }); |
443 } | 444 } |
444 | 445 |
445 void testClosedError() { | 446 void testClosedError() { |
446 asyncStart(); | 447 asyncStart(); |
447 RawServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { | 448 RawServerSocket.bind(InternetAddress.LOOPBACK_IP_V4, 0).then((server) { |
448 server.listen((socket) { socket.close(); }); | 449 server.listen((socket) { |
| 450 socket.close(); |
| 451 }); |
449 RawSocket.connect("127.0.0.1", server.port).then((socket) { | 452 RawSocket.connect("127.0.0.1", server.port).then((socket) { |
450 server.close(); | 453 server.close(); |
451 socket.close(); | 454 socket.close(); |
452 Expect.throws(() => socket.remotePort, (e) => e is SocketException); | 455 Expect.throws(() => socket.remotePort, (e) => e is SocketException); |
453 Expect.throws(() => socket.remoteAddress, (e) => e is SocketException); | 456 Expect.throws(() => socket.remoteAddress, (e) => e is SocketException); |
454 asyncEnd(); | 457 asyncEnd(); |
455 }); | 458 }); |
456 }); | 459 }); |
457 } | 460 } |
458 | 461 |
459 main() { | 462 main() { |
460 asyncStart(); | 463 asyncStart(); |
461 testArguments(); | 464 testArguments(); |
462 testSimpleBind(); | 465 testSimpleBind(); |
463 testCloseOneEnd("client"); | 466 testCloseOneEnd("client"); |
464 testCloseOneEnd("server"); | 467 testCloseOneEnd("server"); |
465 testInvalidBind(); | 468 testInvalidBind(); |
466 testSimpleConnect(); | 469 testSimpleConnect(); |
467 testServerListenAfterConnect(); | 470 testServerListenAfterConnect(); |
468 testSimpleReadWrite(dropReads: false); | 471 testSimpleReadWrite(dropReads: false); |
469 testSimpleReadWrite(dropReads: true); | 472 testSimpleReadWrite(dropReads: true); |
470 testPauseServerSocket(); | 473 testPauseServerSocket(); |
471 testPauseSocket(); | 474 testPauseSocket(); |
472 testSocketZone(); | 475 testSocketZone(); |
473 testSocketZoneError(); | 476 testSocketZoneError(); |
474 testClosedError(); | 477 testClosedError(); |
475 asyncEnd(); | 478 asyncEnd(); |
476 } | 479 } |
OLD | NEW |