Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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= | |
| 6 // VMOptions=--short_socket_read | |
| 7 // VMOptions=--short_socket_write | |
| 8 // VMOptions=--short_socket_read --short_socket_write | |
| 9 // | |
| 5 // Test socket close events. | 10 // Test socket close events. |
| 6 | 11 |
| 7 #import("dart:io"); | 12 #import("dart:io"); |
| 8 | 13 |
| 9 final SERVERSHUTDOWN = -1; | 14 final SERVERSHUTDOWN = -1; |
| 10 final ITERATIONS = 10; | 15 final ITERATIONS = 10; |
| 11 | 16 |
| 12 | 17 |
| 13 class SocketClose { | 18 class SocketClose { |
| 14 | 19 |
| 15 SocketClose.start(mode) | 20 SocketClose.start(mode) |
| 16 : _receivePort = new ReceivePort(), | 21 : _receivePort = new ReceivePort(), |
| 17 _sendPort = null, | 22 _sendPort = null, |
| 23 _readBytes = 0, | |
| 18 _dataEvents = 0, | 24 _dataEvents = 0, |
| 19 _closeEvents = 0, | 25 _closeEvents = 0, |
| 20 _errorEvents = 0, | 26 _errorEvents = 0, |
| 21 _iterations = 0, | 27 _iterations = 0, |
| 22 _mode = mode { | 28 _mode = mode { |
| 23 new SocketCloseServer().spawn().then((SendPort port) { | 29 new SocketCloseServer().spawn().then((SendPort port) { |
| 24 _sendPort = port; | 30 _sendPort = port; |
| 25 start(); | 31 start(); |
| 26 }); | 32 }); |
| 27 } | 33 } |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 40 switch (_mode) { | 46 switch (_mode) { |
| 41 case 0: | 47 case 0: |
| 42 case 1: | 48 case 1: |
| 43 case 2: | 49 case 2: |
| 44 Expect.fail("No data expected"); | 50 Expect.fail("No data expected"); |
| 45 break; | 51 break; |
| 46 case 3: | 52 case 3: |
| 47 case 4: | 53 case 4: |
| 48 case 5: | 54 case 5: |
| 49 case 6: | 55 case 6: |
| 50 List<int> b = new List<int>(100); | 56 var read = _socket.inputStream.read(); |
| 51 _socket.readList(b, 0, 100); | 57 _readBytes += read.length; |
| 52 _dataEvents++; | 58 if ((_readBytes % 5) == 0) { |
| 59 _dataEvents++; | |
| 60 } | |
| 53 break; | 61 break; |
| 54 default: | 62 default: |
| 55 Expect.fail("Unknown test mode"); | 63 Expect.fail("Unknown test mode"); |
| 56 } | 64 } |
| 57 } | 65 } |
| 58 | 66 |
| 59 void closeHandler() { | 67 void closeHandler() { |
| 60 _closeEvents++; | 68 _closeEvents++; |
| 61 switch (_mode) { | 69 switch (_mode) { |
| 62 case 0: | 70 case 0: |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 93 _socket.errorHandler = errorHandler; | 101 _socket.errorHandler = errorHandler; |
| 94 | 102 |
| 95 _iterations++; | 103 _iterations++; |
| 96 switch (_mode) { | 104 switch (_mode) { |
| 97 case 0: | 105 case 0: |
| 98 _socket.inputStream.close(); | 106 _socket.inputStream.close(); |
| 99 proceed(); | 107 proceed(); |
| 100 break; | 108 break; |
| 101 case 1: | 109 case 1: |
| 102 _socket.outputStream.write("Hello".charCodes()); | 110 _socket.outputStream.write("Hello".charCodes()); |
| 103 _socket.inputStream.close(); | 111 _socket.outputStream.noPendingWriteHandler = () { |
| 104 proceed(); | 112 _socket.inputStream.close(); |
| 113 proceed(); | |
| 114 }; | |
| 105 break; | 115 break; |
| 106 case 2: | 116 case 2: |
| 107 case 3: | 117 case 3: |
| 108 _socket.outputStream.write("Hello".charCodes()); | 118 _socket.outputStream.write("Hello".charCodes()); |
| 109 break; | 119 break; |
| 110 case 4: | 120 case 4: |
| 111 _socket.outputStream.write("Hello".charCodes()); | 121 _socket.outputStream.write("Hello".charCodes()); |
| 112 _socket.outputStream.close(); | 122 _socket.outputStream.noPendingWriteHandler = () { |
| 123 _socket.outputStream.close(); | |
| 124 }; | |
| 113 break; | 125 break; |
| 114 case 5: | 126 case 5: |
| 115 _socket.outputStream.write("Hello".charCodes()); | 127 _socket.outputStream.write("Hello".charCodes()); |
| 116 break; | 128 break; |
| 117 case 6: | 129 case 6: |
| 118 _socket.outputStream.write("Hello".charCodes()); | 130 _socket.outputStream.write("Hello".charCodes()); |
| 119 _socket.outputStream.close(); | 131 _socket.outputStream.noPendingWriteHandler = () { |
| 132 _socket.outputStream.close(); | |
| 133 }; | |
| 120 break; | 134 break; |
| 121 default: | 135 default: |
| 122 Expect.fail("Unknown test mode"); | 136 Expect.fail("Unknown test mode"); |
| 123 } | 137 } |
| 124 } | 138 } |
| 125 | 139 |
| 126 _socket = new Socket(SocketCloseServer.HOST, _port); | 140 _socket = new Socket(SocketCloseServer.HOST, _port); |
| 127 Expect.equals(true, _socket !== null); | 141 Expect.equals(true, _socket !== null); |
| 128 _socket.connectHandler = connectHandler; | 142 _socket.connectHandler = connectHandler; |
| 129 } | 143 } |
| 130 | 144 |
| 131 void start() { | 145 void start() { |
| 132 _receivePort.receive((var message, SendPort replyTo) { | 146 _receivePort.receive((var message, SendPort replyTo) { |
| 133 _port = message; | 147 _port = message; |
| 134 proceed(); | 148 proceed(); |
| 135 }); | 149 }); |
| 136 _sendPort.send(_mode, _receivePort.toSendPort()); | 150 _sendPort.send(_mode, _receivePort.toSendPort()); |
| 137 } | 151 } |
| 138 | 152 |
| 139 void shutdown() { | 153 void shutdown() { |
| 140 _sendPort.send(SERVERSHUTDOWN, _receivePort.toSendPort()); | 154 _sendPort.send(SERVERSHUTDOWN, _receivePort.toSendPort()); |
| 141 _receivePort.close(); | 155 _receivePort.close(); |
| 142 | 156 |
| 143 switch (_mode) { | 157 switch (_mode) { |
| 144 case 0: | 158 case 0: |
| 145 case 1: | 159 case 1: |
| 146 Expect.equals(0, _dataEvents); | 160 Expect.equals(0, _dataEvents); |
| 147 Expect.equals(10, _closeEvents); | 161 Expect.equals(ITERATIONS, _closeEvents); |
| 148 break; | 162 break; |
| 149 case 2: | 163 case 2: |
| 150 Expect.equals(0, _dataEvents); | 164 Expect.equals(0, _dataEvents); |
| 151 Expect.equals(ITERATIONS, _closeEvents); | 165 Expect.equals(ITERATIONS, _closeEvents); |
| 152 break; | 166 break; |
| 153 case 3: | 167 case 3: |
| 154 case 4: | 168 case 4: |
| 155 case 5: | 169 case 5: |
| 156 case 6: | 170 case 6: |
| 157 Expect.equals(ITERATIONS, _dataEvents); | 171 Expect.equals(ITERATIONS, _dataEvents); |
| 158 Expect.equals(ITERATIONS, _closeEvents); | 172 Expect.equals(ITERATIONS, _closeEvents); |
| 159 break; | 173 break; |
| 160 default: | 174 default: |
| 161 Expect.fail("Unknown test mode"); | 175 Expect.fail("Unknown test mode"); |
| 162 } | 176 } |
| 163 Expect.equals(0, _errorEvents); | 177 Expect.equals(0, _errorEvents); |
| 164 } | 178 } |
| 165 | 179 |
| 166 int _port; | 180 int _port; |
| 167 ReceivePort _receivePort; | 181 ReceivePort _receivePort; |
| 168 SendPort _sendPort; | 182 SendPort _sendPort; |
| 169 Socket _socket; | 183 Socket _socket; |
| 170 List<int> _buffer; | 184 List<int> _buffer; |
| 185 int _readBytes; | |
| 171 int _dataEvents; | 186 int _dataEvents; |
| 172 int _closeEvents; | 187 int _closeEvents; |
| 173 int _errorEvents; | 188 int _errorEvents; |
| 174 int _iterations; | 189 int _iterations; |
| 175 int _mode; | 190 int _mode; |
| 176 } | 191 } |
| 177 | 192 |
| 178 class SocketCloseServer extends Isolate { | 193 class SocketCloseServer extends Isolate { |
| 179 | 194 |
| 180 static final HOST = "127.0.0.1"; | 195 static final HOST = "127.0.0.1"; |
| 181 | 196 |
| 182 SocketCloseServer() : super() {} | 197 SocketCloseServer() : super() {} |
| 183 | 198 |
| 184 void main() { | 199 void main() { |
| 185 | 200 |
| 186 void connectionHandler(Socket connection) { | 201 void connectionHandler(Socket connection) { |
| 187 | 202 |
| 203 void readBytes(whenFiveBytes) { | |
| 204 var read = connection.inputStream.read(); | |
| 205 _readBytes += read.length; | |
| 206 if ((_readBytes % 5) == 0) { | |
| 207 whenFiveBytes(); | |
| 208 } | |
| 209 } | |
| 210 | |
| 188 void dataHandler() { | 211 void dataHandler() { |
| 189 _dataEvents++; | |
| 190 switch (_mode) { | 212 switch (_mode) { |
| 191 case 0: | 213 case 0: |
| 192 Expect.fail("No data expected"); | 214 Expect.fail("No data expected"); |
| 193 break; | 215 break; |
| 194 case 1: | 216 case 1: |
| 195 connection.inputStream.read(); | 217 readBytes(() => _dataEvents++); |
| 196 break; | 218 break; |
| 197 case 2: | 219 case 2: |
| 198 connection.inputStream.read(); | 220 readBytes(() { |
| 199 connection.inputStream.close(); | 221 _dataEvents++; |
| 222 connection.inputStream.close(); | |
| 223 }); | |
| 200 break; | 224 break; |
| 201 case 3: | 225 case 3: |
| 202 connection.inputStream.read(); | 226 readBytes(() { |
| 203 connection.outputStream.write("Hello".charCodes()); | 227 _dataEvents++; |
| 204 connection.inputStream.close(); | 228 connection.outputStream.write("Hello".charCodes()); |
|
Søren Gjesse
2012/02/09 20:19:42
Maybe we should add two more cases (variants of 3
Mads Ager (google)
2012/02/10 06:56:48
Done.
| |
| 205 //connection.outputStream.close(); | 229 connection.outputStream.noPendingWriteHandler = () { |
| 230 print("connection.inputStream.close();"); | |
|
Søren Gjesse
2012/02/09 20:19:42
Debug print.
Mads Ager (google)
2012/02/10 06:56:48
Done.
| |
| 231 connection.inputStream.close(); | |
| 232 }; | |
| 233 }); | |
| 206 break; | 234 break; |
| 207 case 4: | 235 case 4: |
| 208 connection.inputStream.read(); | 236 readBytes(() { |
| 209 connection.outputStream.write("Hello".charCodes()); | 237 _dataEvents++; |
| 238 connection.outputStream.write("Hello".charCodes()); | |
| 239 }); | |
| 210 break; | 240 break; |
| 211 case 5: | 241 case 5: |
| 212 case 6: | 242 case 6: |
| 213 connection.inputStream.read(); | 243 readBytes(() { |
| 214 connection.outputStream.write("Hello".charCodes()); | 244 _dataEvents++; |
| 215 connection.outputStream.close(); | 245 connection.outputStream.write("Hello".charCodes()); |
| 246 connection.outputStream.noPendingWriteHandler = () { | |
| 247 connection.outputStream.close(); | |
| 248 }; | |
| 249 }); | |
| 216 break; | 250 break; |
| 217 default: | 251 default: |
| 218 Expect.fail("Unknown test mode"); | 252 Expect.fail("Unknown test mode"); |
| 219 } | 253 } |
| 220 } | 254 } |
| 221 | 255 |
| 222 void closeHandler() { | 256 void closeHandler() { |
| 223 _closeEvents++; | 257 _closeEvents++; |
| 224 connection.close(); | 258 connection.outputStream.close(); |
| 225 } | 259 } |
| 226 | 260 |
| 227 void errorHandler() { | 261 void errorHandler() { |
| 228 Expect.fail("Socket error"); | 262 Expect.fail("Socket error"); |
| 229 } | 263 } |
| 230 | 264 |
| 231 _iterations++; | 265 _iterations++; |
| 232 | 266 |
| 233 connection.inputStream.dataHandler = dataHandler; | 267 connection.inputStream.dataHandler = dataHandler; |
| 234 connection.inputStream.closeHandler = closeHandler; | 268 connection.inputStream.closeHandler = closeHandler; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 272 Expect.equals(0, _errorEvents); | 306 Expect.equals(0, _errorEvents); |
| 273 _server.close(); | 307 _server.close(); |
| 274 this.port.close(); | 308 this.port.close(); |
| 275 } else { | 309 } else { |
| 276 new Timer(waitForResult, 100); | 310 new Timer(waitForResult, 100); |
| 277 } | 311 } |
| 278 } | 312 } |
| 279 | 313 |
| 280 this.port.receive((message, SendPort replyTo) { | 314 this.port.receive((message, SendPort replyTo) { |
| 281 if (message != SERVERSHUTDOWN) { | 315 if (message != SERVERSHUTDOWN) { |
| 316 _readBytes = 0; | |
| 282 _errorEvents = 0; | 317 _errorEvents = 0; |
| 283 _dataEvents = 0; | 318 _dataEvents = 0; |
| 284 _closeEvents = 0; | 319 _closeEvents = 0; |
| 285 _iterations = 0; | 320 _iterations = 0; |
| 286 _mode = message; | 321 _mode = message; |
| 287 _server = new ServerSocket(HOST, 0, 10); | 322 _server = new ServerSocket(HOST, 0, 10); |
| 288 Expect.equals(true, _server !== null); | 323 Expect.equals(true, _server !== null); |
| 289 _server.connectionHandler = connectionHandler; | 324 _server.connectionHandler = connectionHandler; |
| 290 _server.errorHandler = errorHandlerServer; | 325 _server.errorHandler = errorHandlerServer; |
| 291 replyTo.send(_server.port, null); | 326 replyTo.send(_server.port, null); |
| 292 } else { | 327 } else { |
| 293 new Timer(waitForResult, 0); | 328 new Timer(waitForResult, 0); |
| 294 } | 329 } |
| 295 }); | 330 }); |
| 296 } | 331 } |
| 297 | 332 |
| 298 ServerSocket _server; | 333 ServerSocket _server; |
| 334 int _readBytes; | |
| 299 int _errorEvents; | 335 int _errorEvents; |
| 300 int _dataEvents; | 336 int _dataEvents; |
| 301 int _closeEvents; | 337 int _closeEvents; |
| 302 int _iterations; | 338 int _iterations; |
| 303 int _mode; | 339 int _mode; |
| 304 } | 340 } |
| 305 | 341 |
| 306 | 342 |
| 307 main() { | 343 main() { |
| 308 // Run the close test in these different "modes". | 344 // Run the close test in these different "modes". |
| 309 // 0: Client closes without sending at all. | 345 // 0: Client closes without sending at all. |
| 310 // 1: Client sends and closes. | 346 // 1: Client sends and closes. |
| 311 // 2: Client sends. Server closes. | 347 // 2: Client sends. Server closes. |
| 312 // 3: Client sends. Server responds and closes. | 348 // 3: Client sends. Server responds and closes. |
| 313 // 4: Client sends and half-closes. Server responds and closes. | 349 // 4: Client sends and half-closes. Server responds and closes. |
| 314 // 5: Client sends. Server responds and half closes. | 350 // 5: Client sends. Server responds and half closes. |
| 315 // 6: Client sends and half-closes. Server responds and half closes. | 351 // 6: Client sends and half-closes. Server responds and half closes. |
| 316 new SocketClose.start(0); | 352 new SocketClose.start(0); |
| 317 new SocketClose.start(1); | 353 new SocketClose.start(1); |
| 318 new SocketClose.start(2); | 354 new SocketClose.start(2); |
| 319 new SocketClose.start(3); | 355 new SocketClose.start(3); |
| 320 new SocketClose.start(4); | 356 new SocketClose.start(4); |
| 321 new SocketClose.start(5); | 357 new SocketClose.start(5); |
| 322 new SocketClose.start(6); | 358 new SocketClose.start(6); |
| 323 } | 359 } |
| OLD | NEW |