Chromium Code Reviews| 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"; |
| 11 import "dart:io"; | 11 import "dart:io"; |
| 12 import "dart:isolate"; | 12 import "dart:isolate"; |
| 13 | 13 |
| 14 void testCancelResubscribeServerSocket() { | 14 void testCancelResubscribeServerSocket(int socketCount, int backlog) { |
| 15 const int socketCount = 10; | |
| 16 var acceptCount = 0; | 15 var acceptCount = 0; |
| 17 var doneCount = 0; | 16 var doneCount = 0; |
| 18 var closeCount = 0; | 17 var closeCount = 0; |
| 19 var errorCount = 0; | 18 var errorCount = 0; |
| 20 | 19 |
| 21 ReceivePort port = new ReceivePort(); | 20 ReceivePort port = new ReceivePort(); |
| 22 | 21 |
| 23 RawServerSocket.bind().then((server) { | 22 RawServerSocket.bind("127.0.0.1", 0, backlog).then((server) { |
| 24 Expect.isTrue(server.port > 0); | 23 Expect.isTrue(server.port > 0); |
| 25 | 24 |
| 26 void checkDone() { | 25 void checkDone() { |
| 27 if (doneCount == socketCount && | 26 if (doneCount == socketCount && |
| 28 closeCount + errorCount == socketCount) { | 27 closeCount + errorCount == socketCount) { |
| 29 port.close(); | 28 port.close(); |
| 30 } | 29 } |
| 31 } | 30 } |
| 32 | 31 |
| 33 // Subscribe the server socket. Then cancel subscription and | |
| 34 // subscribe again. | |
| 35 var subscription; | 32 var subscription; |
| 36 subscription = server.listen((client) { | 33 subscription = server.listen((client) { |
| 34 client.writeEventsEnabled = false; | |
| 35 client.listen((event) { | |
| 36 switch(event) { | |
| 37 case RawSocketEvent.READ: | |
| 38 client.read(); | |
| 39 break; | |
| 40 case RawSocketEvent.READ_CLOSED: | |
| 41 client.shutdown(SocketDirection.SEND); | |
| 42 break; | |
| 43 case RawSocketEvent.WRITE: | |
| 44 Expect.fail("No write event expected"); | |
| 45 break; | |
| 46 } | |
| 47 }); | |
| 48 | |
| 37 if (++acceptCount == socketCount / 2) { | 49 if (++acceptCount == socketCount / 2) { |
| 50 // Cancel subscription and then attempt to resubscribe. | |
| 38 subscription.cancel(); | 51 subscription.cancel(); |
| 39 Timer.run(() { | 52 Timer.run(() { |
| 40 subscription = server.listen((_) { | 53 subscription = server.listen((_) { |
| 41 // Close on cancel, so no more events. | 54 // Server socket is closed on cancel, so no more events. |
| 42 Expect.fail("Event after closed through cancel"); | 55 Expect.fail("Event after closed through cancel"); |
| 43 }); | 56 }); |
| 44 }); | 57 }); |
| 45 } | 58 } |
| 46 // Close the client socket. | |
| 47 client.close(); | |
| 48 }); | 59 }); |
| 49 | 60 |
| 50 // Connect a number of sockets. | 61 // Connect a number of sockets. |
| 51 for (int i = 0; i < socketCount; i++) { | 62 for (int i = 0; i < socketCount; i++) { |
| 52 RawSocket.connect("127.0.0.1", server.port).then((socket) { | 63 RawSocket.connect("127.0.0.1", server.port).then((socket) { |
| 53 socket.writeEventsEnabled = false; | |
| 54 var subscription; | 64 var subscription; |
| 55 subscription = socket.listen((event) { | 65 subscription = socket.listen((event) { |
| 56 Expect.equals(RawSocketEvent.READ_CLOSED, event); | 66 switch(event) { |
| 57 socket.close(); | 67 case RawSocketEvent.READ: |
| 58 closeCount++; | 68 Expect.fail("No read event expected"); |
| 69 break; | |
| 70 case RawSocketEvent.READ_CLOSED: | |
| 71 closeCount++; | |
| 72 checkDone(); | |
| 73 break; | |
| 74 case RawSocketEvent.WRITE: | |
| 75 socket.write([1,2,3]); | |
|
Bill Hesse
2013/03/05 11:52:44
We don't care if this write succeeds. I'll put in
| |
| 76 socket.shutdown(SocketDirection.SEND); | |
| 77 break; | |
| 78 } | |
| 79 }, | |
| 80 onDone: () { | |
| 81 doneCount++; | |
| 59 checkDone(); | 82 checkDone(); |
| 60 }, | 83 }, |
| 61 onDone: () { doneCount++; checkDone(); }, | 84 onError: (e) { |
| 62 onError: (e) { errorCount++; checkDone(); }); | 85 // "Connection reset by peer" errors are handled here. |
| 86 errorCount++; | |
| 87 checkDone(); | |
| 88 }); | |
| 63 }).catchError((e) { | 89 }).catchError((e) { |
| 64 errorCount++; checkDone(); | 90 // "Connection actively refused by host" errors are handled here. |
| 91 errorCount++; | |
| 92 checkDone(); | |
| 65 }); | 93 }); |
| 66 } | 94 } |
| 67 }); | 95 }); |
| 68 } | 96 } |
| 69 | 97 |
| 70 void main() { | 98 void main() { |
| 71 testCancelResubscribeServerSocket(); | 99 testCancelResubscribeServerSocket(10, 20); |
| 100 testCancelResubscribeServerSocket(20, 5); | |
| 72 } | 101 } |
| OLD | NEW |