| 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= | 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 library ServerTest; | 9 library ServerTest; |
| 10 | 10 |
| 11 import "dart:io"; | 11 import "dart:io"; |
| 12 import "dart:isolate"; | 12 import "dart:isolate"; |
| 13 part "testing_server.dart"; | |
| 14 | 13 |
| 15 // Helper method to be able to run the test from the runtime | 14 // Helper method to be able to run the test from the runtime |
| 16 // directory, or the top directory. | 15 // directory, or the top directory. |
| 17 String getDataFilename(String path) => | 16 String getDataFilename(String path) => |
| 18 new File(path).existsSync() ? path : '../$path'; | 17 new File(path).existsSync() ? path : '../$path'; |
| 19 | 18 |
| 20 | 19 |
| 21 bool compareFileContent(String fileName1, | 20 bool compareFileContent(String fileName1, |
| 22 String fileName2, | 21 String fileName2, |
| 23 {int file1Offset: 0, | 22 {int file1Offset: 0, |
| (...skipping 25 matching lines...) Expand all Loading... |
| 49 file2.closeSync(); | 48 file2.closeSync(); |
| 50 return false; | 49 return false; |
| 51 } | 50 } |
| 52 } | 51 } |
| 53 file1.closeSync(); | 52 file1.closeSync(); |
| 54 file2.closeSync(); | 53 file2.closeSync(); |
| 55 return true; | 54 return true; |
| 56 } | 55 } |
| 57 | 56 |
| 58 | 57 |
| 59 // This test does: | |
| 60 // 1. Opens a socket to the testing server. | |
| 61 // 2. Pipes the content of a file to that sockets input stream. | |
| 62 // 3. Creates a temp file. | |
| 63 // 4. Pipes the socket output stream to the temp file. | |
| 64 // 5. Expects the original file and the temp file to be equal. | |
| 65 class PipeServerGame { | |
| 66 int count = 0; | |
| 67 | |
| 68 PipeServerGame.start() | |
| 69 : _receivePort = new ReceivePort(), | |
| 70 _sendPort = null, | |
| 71 _messages = 0 { | |
| 72 _sendPort = spawnFunction(startPipeServer); | |
| 73 initialize(); | |
| 74 } | |
| 75 | |
| 76 void runTest() { | |
| 77 | |
| 78 void connectHandler() { | |
| 79 String srcFileName = | |
| 80 getDataFilename("tests/standalone/io/readline_test1.dat"); | |
| 81 | |
| 82 OutputStream socketOutput = _socket.outputStream; | |
| 83 InputStream fileInput = new File(srcFileName).openInputStream(); | |
| 84 | |
| 85 fileInput.onClosed = () { | |
| 86 InputStream socketInput = _socket.inputStream; | |
| 87 var tempDir = new Directory('').createTempSync(); | |
| 88 var dstFileName = tempDir.path.concat("/readline_test1.dat"); | |
| 89 var dstFile = new File(dstFileName); | |
| 90 dstFile.createSync(); | |
| 91 var fileOutput = dstFile.openOutputStream(); | |
| 92 | |
| 93 socketInput.onClosed = () { | |
| 94 // Check that the resulting file is equal to the initial | |
| 95 // file. | |
| 96 fileOutput.onClosed = () { | |
| 97 bool result = compareFileContent(srcFileName, dstFileName); | |
| 98 new File(dstFileName).deleteSync(); | |
| 99 tempDir.deleteSync(); | |
| 100 Expect.isTrue(result); | |
| 101 | |
| 102 _socket.close(); | |
| 103 | |
| 104 // Run this twice. | |
| 105 if (count++ < 2) { | |
| 106 runTest(); | |
| 107 } else { | |
| 108 shutdown(); | |
| 109 } | |
| 110 }; | |
| 111 }; | |
| 112 | |
| 113 socketInput.pipe(fileOutput); | |
| 114 }; | |
| 115 | |
| 116 fileInput.pipe(socketOutput); | |
| 117 } | |
| 118 | |
| 119 // Connect to the server. | |
| 120 _socket = new Socket(TestingServer.HOST, _port); | |
| 121 if (_socket != null) { | |
| 122 _socket.onConnect = connectHandler; | |
| 123 } else { | |
| 124 Expect.fail("socket creation failed"); | |
| 125 } | |
| 126 } | |
| 127 | |
| 128 void initialize() { | |
| 129 _receivePort.receive((var message, SendPort replyTo) { | |
| 130 _port = message; | |
| 131 runTest(); | |
| 132 }); | |
| 133 _sendPort.send(TestingServer.INIT, _receivePort.toSendPort()); | |
| 134 } | |
| 135 | |
| 136 void shutdown() { | |
| 137 _sendPort.send(TestingServer.SHUTDOWN, _receivePort.toSendPort()); | |
| 138 _receivePort.close(); | |
| 139 } | |
| 140 | |
| 141 int _port; | |
| 142 ReceivePort _receivePort; | |
| 143 SendPort _sendPort; | |
| 144 Socket _socket; | |
| 145 int _messages; | |
| 146 } | |
| 147 | |
| 148 | |
| 149 void startPipeServer() { | |
| 150 var server = new PipeServer(); | |
| 151 port.receive(server.dispatch); | |
| 152 } | |
| 153 | |
| 154 | |
| 155 // The testing server will simply pipe each connecting sockets input | |
| 156 // stream to its output stream. | |
| 157 class PipeServer extends TestingServer { | |
| 158 void onConnection(Socket connection) { | |
| 159 connection.onError = (Exception e) { Expect.fail("Socket error $e"); }; | |
| 160 connection.inputStream.pipe(connection.outputStream); | |
| 161 } | |
| 162 } | |
| 163 | |
| 164 | |
| 165 // Test piping from one file to another and closing both streams | 58 // Test piping from one file to another and closing both streams |
| 166 // after wards. | 59 // after wards. |
| 167 testFileToFilePipe1() { | 60 testFileToFilePipe1() { |
| 168 // Force test to timeout if one of the handlers is | 61 // Force test to timeout if one of the handlers is |
| 169 // not called. | 62 // not called. |
| 170 ReceivePort donePort = new ReceivePort(); | 63 ReceivePort donePort = new ReceivePort(); |
| 171 donePort.receive((message, ignore) { donePort.close(); }); | 64 donePort.receive((message, ignore) { donePort.close(); }); |
| 172 | 65 |
| 173 String srcFileName = | 66 String srcFileName = |
| 174 getDataFilename("tests/standalone/io/readline_test1.dat"); | 67 getDataFilename("tests/standalone/io/readline_test1.dat"); |
| 175 var srcStream = new File(srcFileName).openInputStream(); | 68 var srcStream = new File(srcFileName).openRead(); |
| 176 | 69 |
| 177 var tempDir = new Directory('').createTempSync(); | 70 var tempDir = new Directory('').createTempSync(); |
| 178 String dstFileName = tempDir.path.concat("/readline_test1.dat"); | 71 String dstFileName = tempDir.path.concat("/readline_test1.dat"); |
| 179 new File(dstFileName).createSync(); | 72 new File(dstFileName).createSync(); |
| 180 var dstStream = new File(dstFileName).openOutputStream(); | 73 var output = new File(dstFileName).openWrite(); |
| 181 | 74 srcStream.pipe(output).then((_) { |
| 182 dstStream.onClosed = () { | |
| 183 bool result = compareFileContent(srcFileName, dstFileName); | 75 bool result = compareFileContent(srcFileName, dstFileName); |
| 184 new File(dstFileName).deleteSync(); | 76 new File(dstFileName).deleteSync(); |
| 185 tempDir.deleteSync(); | 77 tempDir.deleteSync(); |
| 186 Expect.isTrue(result); | 78 Expect.isTrue(result); |
| 187 donePort.toSendPort().send(null); | 79 donePort.toSendPort().send(null); |
| 188 }; | 80 }); |
| 189 | |
| 190 srcStream.pipe(dstStream); | |
| 191 } | 81 } |
| 192 | 82 |
| 193 | 83 |
| 194 // Test piping from one file to another and write additional data to | 84 // Test piping from one file to another and write additional data to |
| 195 // the output stream after piping finished. | 85 // the output stream after piping finished. |
| 196 testFileToFilePipe2() { | 86 testFileToFilePipe2() { |
| 197 // Force test to timeout if one of the handlers is | 87 // Force test to timeout if one of the handlers is |
| 198 // not called. | 88 // not called. |
| 199 ReceivePort donePort = new ReceivePort(); | 89 ReceivePort donePort = new ReceivePort(); |
| 200 donePort.receive((message, ignore) { donePort.close(); }); | 90 donePort.receive((message, ignore) { donePort.close(); }); |
| 201 | 91 |
| 202 String srcFileName = | 92 String srcFileName = |
| 203 getDataFilename("tests/standalone/io/readline_test1.dat"); | 93 getDataFilename("tests/standalone/io/readline_test1.dat"); |
| 204 var srcFile = new File(srcFileName); | 94 var srcFile = new File(srcFileName); |
| 205 var srcStream = srcFile.openInputStream(); | 95 var srcStream = srcFile.openRead(); |
| 206 | 96 |
| 207 var tempDir = new Directory('').createTempSync(); | 97 var tempDir = new Directory('').createTempSync(); |
| 208 var dstFileName = tempDir.path.concat("/readline_test1.dat"); | 98 var dstFileName = tempDir.path.concat("/readline_test1.dat"); |
| 209 var dstFile = new File(dstFileName); | 99 var dstFile = new File(dstFileName); |
| 210 dstFile.createSync(); | 100 dstFile.createSync(); |
| 211 var dstStream = dstFile.openOutputStream(); | 101 var output = dstFile.openWrite(); |
| 212 | 102 output.addStream(srcStream).then((_) { |
| 213 srcStream.onClosed = () { | 103 output.add([32]); |
| 214 dstStream.write([32]); | 104 output.close(); |
| 215 dstStream.close(); | 105 output.done.then((_) { |
| 216 dstStream.onClosed = () { | |
| 217 var src = srcFile.openSync(); | 106 var src = srcFile.openSync(); |
| 218 var dst = dstFile.openSync(); | 107 var dst = dstFile.openSync(); |
| 219 var srcLength = src.lengthSync(); | 108 var srcLength = src.lengthSync(); |
| 220 var dstLength = dst.lengthSync(); | 109 var dstLength = dst.lengthSync(); |
| 221 Expect.equals(srcLength + 1, dstLength); | 110 Expect.equals(srcLength + 1, dstLength); |
| 222 Expect.isTrue(compareFileContent(srcFileName, | 111 Expect.isTrue(compareFileContent(srcFileName, |
| 223 dstFileName, | 112 dstFileName, |
| 224 count: srcLength)); | 113 count: srcLength)); |
| 225 dst.setPositionSync(srcLength); | 114 dst.setPositionSync(srcLength); |
| 226 var data = new List<int>.fixedLength(1); | 115 var data = new List<int>.fixedLength(1); |
| 227 var read2 = dst.readListSync(data, 0, 1); | 116 var read2 = dst.readListSync(data, 0, 1); |
| 228 Expect.equals(32, data[0]); | 117 Expect.equals(32, data[0]); |
| 229 src.closeSync(); | 118 src.closeSync(); |
| 230 dst.closeSync(); | 119 dst.closeSync(); |
| 231 dstFile.deleteSync(); | 120 dstFile.deleteSync(); |
| 232 tempDir.deleteSync(); | 121 tempDir.deleteSync(); |
| 233 donePort.toSendPort().send(null); | 122 donePort.toSendPort().send(null); |
| 234 }; | 123 }); |
| 235 }; | 124 }); |
| 236 | |
| 237 srcStream.pipe(dstStream, close: false); | |
| 238 } | 125 } |
| 239 | 126 |
| 240 | 127 |
| 241 // Test piping two copies of one file to another. | 128 // Test piping two copies of one file to another. |
| 242 testFileToFilePipe3() { | 129 testFileToFilePipe3() { |
| 243 // Force test to timeout if one of the handlers is | 130 // Force test to timeout if one of the handlers is |
| 244 // not called. | 131 // not called. |
| 245 ReceivePort donePort = new ReceivePort(); | 132 ReceivePort donePort = new ReceivePort(); |
| 246 donePort.receive((message, ignore) { donePort.close(); }); | 133 donePort.receive((message, ignore) { donePort.close(); }); |
| 247 | 134 |
| 248 String srcFileName = | 135 String srcFileName = |
| 249 getDataFilename("tests/standalone/io/readline_test1.dat"); | 136 getDataFilename("tests/standalone/io/readline_test1.dat"); |
| 250 var srcFile = new File(srcFileName); | 137 var srcFile = new File(srcFileName); |
| 251 var srcStream = srcFile.openInputStream(); | 138 var srcStream = srcFile.openRead(); |
| 252 | 139 |
| 253 var tempDir = new Directory('').createTempSync(); | 140 var tempDir = new Directory('').createTempSync(); |
| 254 var dstFileName = tempDir.path.concat("/readline_test1.dat"); | 141 var dstFileName = tempDir.path.concat("/readline_test1.dat"); |
| 255 var dstFile = new File(dstFileName); | 142 var dstFile = new File(dstFileName); |
| 256 dstFile.createSync(); | 143 dstFile.createSync(); |
| 257 var dstStream = dstFile.openOutputStream(); | 144 var output = dstFile.openWrite(); |
| 258 | 145 output.addStream(srcStream).then((_) { |
| 259 srcStream.onClosed = () { | 146 var srcStream2 = srcFile.openRead(); |
| 260 var srcStream2 = srcFile.openInputStream(); | 147 output.addStream(srcStream2).then((_) { |
| 261 | 148 output.close(); |
| 262 dstStream.onClosed = () { | 149 output.done.then((_) { |
| 263 var src = srcFile.openSync(); | 150 var src = srcFile.openSync(); |
| 264 var dst = dstFile.openSync(); | 151 var dst = dstFile.openSync(); |
| 265 var srcLength = src.lengthSync(); | 152 var srcLength = src.lengthSync(); |
| 266 var dstLength = dst.lengthSync(); | 153 var dstLength = dst.lengthSync(); |
| 267 Expect.equals(srcLength * 2, dstLength); | 154 Expect.equals(srcLength * 2, dstLength); |
| 268 Expect.isTrue(compareFileContent(srcFileName, | 155 Expect.isTrue(compareFileContent(srcFileName, |
| 269 dstFileName, | 156 dstFileName, |
| 270 count: srcLength)); | 157 count: srcLength)); |
| 271 Expect.isTrue(compareFileContent(srcFileName, | 158 Expect.isTrue(compareFileContent(srcFileName, |
| 272 dstFileName, | 159 dstFileName, |
| 273 file2Offset: srcLength, | 160 file2Offset: srcLength, |
| 274 count: srcLength)); | 161 count: srcLength)); |
| 275 src.closeSync(); | 162 src.closeSync(); |
| 276 dst.closeSync(); | 163 dst.closeSync(); |
| 277 dstFile.deleteSync(); | 164 dstFile.deleteSync(); |
| 278 tempDir.deleteSync(); | 165 tempDir.deleteSync(); |
| 279 donePort.toSendPort().send(null); | 166 donePort.toSendPort().send(null); |
| 280 }; | 167 }); |
| 281 | 168 }); |
| 282 // Pipe another copy of the source file. | 169 }); |
| 283 srcStream2.pipe(dstStream); | |
| 284 }; | |
| 285 | |
| 286 srcStream.pipe(dstStream, close: false); | |
| 287 } | 170 } |
| 288 | 171 |
| 289 | 172 |
| 290 main() { | 173 main() { |
| 291 testFileToFilePipe1(); | 174 testFileToFilePipe1(); |
| 292 testFileToFilePipe2(); | 175 testFileToFilePipe2(); |
| 293 testFileToFilePipe3(); | 176 testFileToFilePipe3(); |
| 294 PipeServerGame echoServerGame = new PipeServerGame.start(); | |
| 295 } | 177 } |
| OLD | NEW |