OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 // Test socket close events. | 5 // Test socket close events. |
6 | 6 |
| 7 |
| 8 final SERVERSHUTDOWN = -1; |
| 9 final ITERATIONS = 10; |
| 10 |
| 11 |
| 12 // Run the close test in these different "modes". |
| 13 // 0: Client closes without sending at all. |
| 14 // 1: Client sends and closes. |
| 15 // 2: Client sends. Server closes. |
| 16 // 3: Client sends. Server responds and closes. |
| 17 // 4: Client sends and half-closes. Server responds and closes. |
| 18 // 5: Client sends. Server responds and half closes. |
| 19 // 6: Client sends and half-closes. Server responds and half closes. |
7 class SocketCloseTest { | 20 class SocketCloseTest { |
8 | |
9 static void testMain() { | 21 static void testMain() { |
10 SocketClose socketClose = new SocketClose.start(); | 22 new SocketClose.start(0); |
| 23 new SocketClose.start(1); |
| 24 new SocketClose.start(2); |
| 25 new SocketClose.start(3); |
| 26 new SocketClose.start(4); |
| 27 new SocketClose.start(5); |
| 28 new SocketClose.start(6); |
11 } | 29 } |
12 } | 30 } |
13 | 31 |
| 32 |
14 class SocketClose { | 33 class SocketClose { |
15 | 34 |
16 static final SERVERINIT = 0; | 35 SocketClose.start(mode) |
17 static final SERVERSHUTDOWN = -1; | |
18 static final ITERATIONS = 100; | |
19 | |
20 SocketClose.start() | |
21 : _receivePort = new ReceivePort(), | 36 : _receivePort = new ReceivePort(), |
22 _sendPort = null, | 37 _sendPort = null, |
23 _dataEvents = 0, | 38 _dataEvents = 0, |
24 _closeEvents = 0, | 39 _closeEvents = 0, |
25 _errorEvents = 0, | 40 _errorEvents = 0, |
26 _iterations = 0 { | 41 _iterations = 0, |
| 42 _mode = mode { |
27 new SocketCloseServer().spawn().then((SendPort port) { | 43 new SocketCloseServer().spawn().then((SendPort port) { |
28 _sendPort = port; | 44 _sendPort = port; |
29 start(); | 45 start(); |
30 }); | 46 }); |
31 } | 47 } |
32 | 48 |
33 void sendData() { | 49 void proceed() { |
| 50 if (_iterations < ITERATIONS) { |
| 51 new Timer(sendData, 0, false); |
| 52 } else { |
| 53 shutdown(); |
| 54 } |
| 55 } |
| 56 |
| 57 void sendData(Timer timer) { |
34 | 58 |
35 void dataHandler() { | 59 void dataHandler() { |
36 _dataEvents++; | 60 switch (_mode) { |
| 61 case 0: |
| 62 case 1: |
| 63 case 2: |
| 64 Expect.fail("No data expected"); |
| 65 break; |
| 66 case 3: |
| 67 case 4: |
| 68 case 5: |
| 69 case 6: |
| 70 List<int> b = new List<int>(100); |
| 71 _socket.readList(b, 0, 100); |
| 72 _dataEvents++; |
| 73 break; |
| 74 default: |
| 75 Expect.fail("Unknown test mode"); |
| 76 } |
37 } | 77 } |
38 | 78 |
39 void closeHandler() { | 79 void closeHandler() { |
40 _closeEvents++; | 80 _closeEvents++; |
41 _iterations++; | 81 switch (_mode) { |
42 _socket.close(); | 82 case 0: |
43 if (_iterations < ITERATIONS) { | 83 case 1: |
44 sendData(); | 84 Expect.fail("No close expected"); |
45 } else { | 85 break; |
46 shutdown(); | 86 case 2: |
| 87 case 3: |
| 88 _socket.close(); |
| 89 proceed(); |
| 90 break; |
| 91 case 4: |
| 92 proceed(); |
| 93 break; |
| 94 case 5: |
| 95 _socket.close(); |
| 96 proceed(); |
| 97 break; |
| 98 case 6: |
| 99 proceed(); |
| 100 break; |
| 101 default: |
| 102 Expect.fail("Unknown test mode"); |
47 } | 103 } |
48 } | 104 } |
49 | 105 |
50 void errorHandler() { | 106 void errorHandler() { |
51 _errorEvents++; | 107 _errorEvents++; |
52 _socket.close(); | 108 _socket.close(); |
53 } | 109 } |
54 | 110 |
55 void connectHandler() { | 111 void connectHandler() { |
56 _socket.dataHandler = dataHandler; | 112 _socket.dataHandler = dataHandler; |
57 _socket.closeHandler = closeHandler; | 113 _socket.closeHandler = closeHandler; |
58 _socket.errorHandler = errorHandler; | 114 _socket.errorHandler = errorHandler; |
59 | 115 |
60 if ((_iterations % 2) == 0) { | 116 _iterations++; |
61 _socket.writeList("Hello".charCodes(), 0, 5); | 117 switch (_mode) { |
| 118 case 0: |
| 119 _socket.close(); |
| 120 proceed(); |
| 121 break; |
| 122 case 1: |
| 123 int bytesWritten = _socket.writeList("Hello".charCodes(), 0, 5); |
| 124 Expect.equals(5, bytesWritten); |
| 125 _socket.close(); |
| 126 proceed(); |
| 127 break; |
| 128 case 2: |
| 129 case 3: |
| 130 int bytesWritten = _socket.writeList("Hello".charCodes(), 0, 5); |
| 131 Expect.equals(5, bytesWritten); |
| 132 break; |
| 133 case 4: |
| 134 int bytesWritten = _socket.writeList("Hello".charCodes(), 0, 5); |
| 135 Expect.equals(5, bytesWritten); |
| 136 _socket.close(true); |
| 137 break; |
| 138 case 5: |
| 139 int bytesWritten = _socket.writeList("Hello".charCodes(), 0, 5); |
| 140 Expect.equals(5, bytesWritten); |
| 141 break; |
| 142 case 6: |
| 143 int bytesWritten = _socket.writeList("Hello".charCodes(), 0, 5); |
| 144 Expect.equals(5, bytesWritten); |
| 145 _socket.close(true); |
| 146 break; |
| 147 default: |
| 148 Expect.fail("Unknown test mode"); |
62 } | 149 } |
63 } | 150 } |
64 | 151 |
65 _socket = new Socket(SocketCloseServer.HOST, _port); | 152 _socket = new Socket(SocketCloseServer.HOST, _port); |
66 Expect.equals(true, _socket !== null); | 153 Expect.equals(true, _socket !== null); |
67 _socket.connectHandler = connectHandler; | 154 _socket.connectHandler = connectHandler; |
68 } | 155 } |
69 | 156 |
70 void start() { | 157 void start() { |
71 _receivePort.receive((var message, SendPort replyTo) { | 158 _receivePort.receive((var message, SendPort replyTo) { |
72 _port = message; | 159 _port = message; |
73 sendData(); | 160 proceed(); |
74 }); | 161 }); |
75 _sendPort.send(SERVERINIT, _receivePort.toSendPort()); | 162 _sendPort.send(_mode, _receivePort.toSendPort()); |
76 } | 163 } |
77 | 164 |
78 void shutdown() { | 165 void shutdown() { |
79 _sendPort.send(SERVERSHUTDOWN, _receivePort.toSendPort()); | 166 _sendPort.send(SERVERSHUTDOWN, _receivePort.toSendPort()); |
80 _receivePort.close(); | 167 _receivePort.close(); |
81 | 168 |
82 /* | 169 switch (_mode) { |
83 * Note that it is not guaranteed that _dataEvents == 0 due to spurious | 170 case 0: |
84 * wakeups. | 171 case 1: |
85 */ | 172 Expect.equals(0, _dataEvents); |
86 Expect.equals(ITERATIONS, _closeEvents); | 173 Expect.equals(0, _closeEvents); |
| 174 break; |
| 175 case 2: |
| 176 Expect.equals(0, _dataEvents); |
| 177 Expect.equals(ITERATIONS, _closeEvents); |
| 178 break; |
| 179 case 3: |
| 180 case 4: |
| 181 case 5: |
| 182 case 6: |
| 183 Expect.equals(ITERATIONS, _dataEvents); |
| 184 Expect.equals(ITERATIONS, _closeEvents); |
| 185 break; |
| 186 default: |
| 187 Expect.fail("Unknown test mode"); |
| 188 } |
87 Expect.equals(0, _errorEvents); | 189 Expect.equals(0, _errorEvents); |
88 } | 190 } |
89 | 191 |
90 int _port; | 192 int _port; |
91 ReceivePort _receivePort; | 193 ReceivePort _receivePort; |
92 SendPort _sendPort; | 194 SendPort _sendPort; |
93 Socket _socket; | 195 Socket _socket; |
94 List<int> _buffer; | 196 List<int> _buffer; |
95 int _dataEvents; | 197 int _dataEvents; |
96 int _closeEvents; | 198 int _closeEvents; |
97 int _errorEvents; | 199 int _errorEvents; |
98 int _iterations; | 200 int _iterations; |
| 201 int _mode; |
99 } | 202 } |
100 | 203 |
101 class SocketCloseServer extends Isolate { | 204 class SocketCloseServer extends Isolate { |
102 | 205 |
103 static final HOST = "127.0.0.1"; | 206 static final HOST = "127.0.0.1"; |
104 | 207 |
105 SocketCloseServer() : super() {} | 208 SocketCloseServer() : super() {} |
106 | 209 |
107 void main() { | 210 void main() { |
108 | 211 |
109 void connectionHandler() { | 212 void connectionHandler() { |
110 Socket _client; | 213 Socket _client; |
111 | 214 |
112 void messageHandler() { | 215 void dataHandler() { |
113 _dataEvents++; | 216 _dataEvents++; |
114 _client.close(); | 217 switch (_mode) { |
| 218 case 0: |
| 219 Expect.fail("No data expected"); |
| 220 break; |
| 221 case 1: |
| 222 List<int> b = new List<int>(100); |
| 223 _client.readList(b, 0, 100); |
| 224 break; |
| 225 case 2: |
| 226 List<int> b = new List<int>(100); |
| 227 _client.readList(b, 0, 100); |
| 228 _client.close(); |
| 229 break; |
| 230 case 3: |
| 231 List<int> b = new List<int>(100); |
| 232 _client.readList(b, 0, 100); |
| 233 _client.writeList("Hello".charCodes(), 0, 5); |
| 234 _client.close(); |
| 235 break; |
| 236 case 4: |
| 237 List<int> b = new List<int>(100); |
| 238 _client.readList(b, 0, 100); |
| 239 _client.writeList("Hello".charCodes(), 0, 5); |
| 240 break; |
| 241 case 5: |
| 242 case 6: |
| 243 List<int> b = new List<int>(100); |
| 244 _client.readList(b, 0, 100); |
| 245 _client.writeList("Hello".charCodes(), 0, 5); |
| 246 _client.close(true); |
| 247 break; |
| 248 default: |
| 249 Expect.fail("Unknown test mode"); |
| 250 } |
115 } | 251 } |
116 | 252 |
117 void closeHandler() { | 253 void closeHandler() { |
118 _closeEvents++; | 254 _closeEvents++; |
119 _client.close(); | 255 _client.close(); |
120 } | 256 } |
121 | 257 |
122 void errorHandler() { | 258 void errorHandler() { |
123 _errorEvents++; | 259 Expect.fail("Socket error"); |
124 _client.close(); | |
125 } | 260 } |
126 | 261 |
127 _client = _server.accept(); | 262 _client = _server.accept(); |
128 if ((_iterations % 2) == 1) { | 263 _iterations++; |
129 _client.close(); | 264 |
130 } | 265 _client.dataHandler = dataHandler; |
131 _client.dataHandler = messageHandler; | |
132 _client.closeHandler = closeHandler; | 266 _client.closeHandler = closeHandler; |
133 _client.errorHandler = errorHandler; | 267 _client.errorHandler = errorHandler; |
134 _iterations++; | |
135 } | 268 } |
136 | 269 |
137 void errorHandlerServer() { | 270 void errorHandlerServer() { |
138 _server.close(); | 271 Expect.fail("Server socket error"); |
| 272 } |
| 273 |
| 274 waitForResult(Timer timer) { |
| 275 // Make sure all iterations have been run. For mode 0 and 1 the |
| 276 // client just closes the socket and after the last iteration |
| 277 // signals the server. The server might now be finished just |
| 278 // because iterations have reached the limit as this number is |
| 279 // incremented just after accept. In that case wait for the last |
| 280 // close event. |
| 281 if (_iterations == ITERATIONS && |
| 282 (_mode > 1 || _closeEvents == ITERATIONS)) { |
| 283 switch (_mode) { |
| 284 case 0: |
| 285 Expect.equals(0, _dataEvents); |
| 286 Expect.equals(ITERATIONS, _closeEvents); |
| 287 break; |
| 288 case 1: |
| 289 Expect.equals(ITERATIONS, _dataEvents); |
| 290 Expect.equals(ITERATIONS, _closeEvents); |
| 291 break; |
| 292 case 2: |
| 293 case 3: |
| 294 Expect.equals(ITERATIONS, _dataEvents); |
| 295 Expect.equals(0, _closeEvents); |
| 296 break; |
| 297 case 4: |
| 298 case 5: |
| 299 case 6: |
| 300 Expect.equals(ITERATIONS, _dataEvents); |
| 301 Expect.equals(ITERATIONS, _closeEvents); |
| 302 break; |
| 303 default: |
| 304 Expect.fail("Unknown test mode"); |
| 305 } |
| 306 Expect.equals(0, _errorEvents); |
| 307 _server.close(); |
| 308 this.port.close(); |
| 309 } else { |
| 310 new Timer(waitForResult, 100, false); |
| 311 } |
139 } | 312 } |
140 | 313 |
141 this.port.receive((message, SendPort replyTo) { | 314 this.port.receive((message, SendPort replyTo) { |
142 if (message == SocketClose.SERVERINIT) { | 315 if (message != SERVERSHUTDOWN) { |
143 _errorEvents = 0; | 316 _errorEvents = 0; |
144 _dataEvents = 0; | 317 _dataEvents = 0; |
145 _closeEvents = 0; | 318 _closeEvents = 0; |
146 _iterations = 0; | 319 _iterations = 0; |
| 320 _mode = message; |
147 _server = new ServerSocket(HOST, 0, 10); | 321 _server = new ServerSocket(HOST, 0, 10); |
148 Expect.equals(true, _server !== null); | 322 Expect.equals(true, _server !== null); |
149 _server.connectionHandler = connectionHandler; | 323 _server.connectionHandler = connectionHandler; |
150 _server.errorHandler = errorHandlerServer; | 324 _server.errorHandler = errorHandlerServer; |
151 replyTo.send(_server.port, null); | 325 replyTo.send(_server.port, null); |
152 } else if (message == SocketClose.SERVERSHUTDOWN) { | 326 } else { |
153 Expect.equals(SocketClose.ITERATIONS/2, _dataEvents); | 327 new Timer(waitForResult, 0, false); |
154 Expect.equals(0, _closeEvents); | |
155 Expect.equals(0, _errorEvents); | |
156 _server.close(); | |
157 this.port.close(); | |
158 } | 328 } |
159 }); | 329 }); |
160 } | 330 } |
161 | 331 |
162 ServerSocket _server; | 332 ServerSocket _server; |
163 int _errorEvents; | 333 int _errorEvents; |
164 int _dataEvents; | 334 int _dataEvents; |
165 int _closeEvents; | 335 int _closeEvents; |
166 int _iterations; | 336 int _iterations; |
| 337 int _mode; |
167 } | 338 } |
168 | 339 |
169 | 340 |
170 main() { | 341 main() { |
171 SocketCloseTest.testMain(); | 342 SocketCloseTest.testMain(); |
172 } | 343 } |
OLD | NEW |