Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 // | |
| 5 // Echo server test program to test socket streams. | |
|
Mads Ager (google)
2011/12/14 13:49:11
Update comment.
Søren Gjesse
2011/12/19 07:46:02
Removed, other comments should cover purpose.
| |
| 6 // | |
| 7 // VMOptions= | |
| 8 // VMOptions=--short_socket_read | |
| 9 // VMOptions=--short_socket_write | |
| 10 // VMOptions=--short_socket_read --short_socket_write | |
| 11 | |
| 12 #library("StreamPipeTest"); | |
| 13 #source("TestingServer.dart"); | |
| 14 | |
| 15 // Helper method to be able to run the test from the runtime | |
| 16 // directory, or the top directory. | |
| 17 String getDataFilename(String path) => | |
| 18 new File(path).existsSync() ? path : '../' + path; | |
| 19 | |
| 20 | |
| 21 bool compareFileContent(String fileName1, | |
| 22 String fileName2, | |
| 23 [int file1Offset = 0, | |
| 24 int file2Offset = 0, | |
| 25 int count]) { | |
| 26 var file1 = new File(fileName1).openSync(); | |
| 27 var file2 = new File(fileName2).openSync(); | |
| 28 var length1 = file1.lengthSync(); | |
| 29 var length2 = file2.lengthSync(); | |
| 30 if (file1Offset == 0 && file2Offset == 0 && count == null) { | |
| 31 if (length1 != length2) { | |
| 32 file1.closeSync(); | |
| 33 file2.closeSync(); | |
| 34 return false; | |
| 35 } | |
| 36 } | |
| 37 if (count == null) count = length1; | |
| 38 var data1 = new List<int>(count); | |
| 39 var data2 = new List<int>(count); | |
| 40 if (file1Offset != 0) file1.setPositionSync(file1Offset); | |
| 41 if (file2Offset != 0) file2.setPositionSync(file2Offset); | |
| 42 var read1 = file1.readListSync(data1, 0, count); | |
| 43 Expect.equals(count, read1); | |
| 44 var read2 = file2.readListSync(data2, 0, count); | |
| 45 Expect.equals(count, read2); | |
| 46 for (var i = 0; i < count; i++) { | |
| 47 if (data1[i] != data2[i]) { | |
| 48 file1.closeSync(); | |
| 49 file2.closeSync(); | |
| 50 return false; | |
| 51 } | |
| 52 } | |
| 53 file1.closeSync(); | |
| 54 file2.closeSync(); | |
| 55 return true; | |
| 56 } | |
| 57 | |
| 58 | |
| 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 new PipeServer().spawn().then((SendPort port) { | |
| 73 _sendPort = port; | |
| 74 start(); | |
| 75 }); | |
| 76 } | |
| 77 | |
| 78 void runTest() { | |
| 79 | |
| 80 void connectHandler() { | |
| 81 String srcFileName = | |
| 82 getDataFilename("tests/standalone/src/data/dart-logo.png"); | |
|
Mads Ager (google)
2011/12/14 13:49:11
Would it be simpler to just use one of the other d
Søren Gjesse
2011/12/19 07:46:02
The only data files we have are quite small test f
Søren Gjesse
2011/12/19 14:22:32
Used the existing readline_test1.dat for now.
| |
| 83 | |
| 84 SocketOutputStream socketOutput = _socket.outputStream; | |
| 85 InputStream fileInput = new File(srcFileName).openInputStream(); | |
| 86 | |
| 87 fileInput.closeHandler = () { | |
| 88 SocketInputStream socketInput = _socket.inputStream; | |
| 89 var tempDir = new Directory(''); | |
| 90 tempDir.createTempSync(); | |
| 91 var dstFileName = tempDir.path + "/dart-logo.png"; | |
| 92 var dstFile = new File(dstFileName); | |
| 93 dstFile.createSync(); | |
| 94 var fileOutput = dstFile.openOutputStream(); | |
| 95 | |
| 96 socketInput.closeHandler = () { | |
| 97 // Check that the resulting file is equal to the initial | |
| 98 // file. | |
| 99 bool result = compareFileContent(srcFileName, dstFileName); | |
| 100 new File(dstFileName).deleteSync(); | |
| 101 Expect.isTrue(result); | |
| 102 | |
| 103 _socket.close(); | |
| 104 if (count++ < 10) { | |
|
Mads Ager (google)
2011/12/14 13:49:11
Do we really need to run this 10 times?
Søren Gjesse
2011/12/19 07:46:02
Reduced to 2.
| |
| 105 runTest(); | |
| 106 } else { | |
| 107 shutdown(); | |
| 108 } | |
| 109 }; | |
| 110 | |
| 111 socketInput.pipe(fileOutput); | |
| 112 }; | |
| 113 | |
| 114 fileInput.pipe(socketOutput); | |
| 115 } | |
| 116 | |
| 117 // Connect to the server. | |
| 118 _socket = new Socket(TestingServer.HOST, _port); | |
| 119 if (_socket !== null) { | |
| 120 _socket.connectHandler = connectHandler; | |
| 121 } else { | |
| 122 Expect.fail("socket creation failed"); | |
| 123 } | |
| 124 } | |
| 125 | |
| 126 void start() { | |
| 127 _receivePort.receive((var message, SendPort replyTo) { | |
| 128 _port = message; | |
| 129 runTest(); | |
| 130 }); | |
| 131 _sendPort.send(TestingServer.INIT, _receivePort.toSendPort()); | |
| 132 } | |
| 133 | |
| 134 void shutdown() { | |
| 135 _sendPort.send(TestingServer.SHUTDOWN, _receivePort.toSendPort()); | |
| 136 _receivePort.close(); | |
| 137 } | |
| 138 | |
| 139 int _port; | |
| 140 ReceivePort _receivePort; | |
| 141 SendPort _sendPort; | |
| 142 Socket _socket; | |
| 143 int _messages; | |
| 144 } | |
| 145 | |
| 146 | |
| 147 // The testing server will simply pipe each connecting sockets input | |
| 148 // stream to its output stream. | |
| 149 class PipeServer extends TestingServer { | |
| 150 void connectionHandler(Socket connection) { | |
| 151 connection.errorHandler = () { Expect.fail("Socket error"); }; | |
| 152 connection.inputStream.pipe(connection.outputStream); | |
| 153 } | |
| 154 } | |
| 155 | |
| 156 | |
| 157 // Test piping from one file to another and closing both streams | |
| 158 // after wards. | |
| 159 testFileToFilePipe1() { | |
| 160 String srcFileName = | |
| 161 getDataFilename("tests/standalone/src/data/dart-logo.png"); | |
| 162 var srcStream = new File(srcFileName).openInputStream(); | |
| 163 | |
| 164 var tempDir = new Directory(''); | |
| 165 tempDir.createTempSync(); | |
| 166 String dstFileName = tempDir.path + "/dart-logo.png"; | |
| 167 new File(dstFileName).createSync(); | |
| 168 var dstStream = new File(dstFileName).openOutputStream(); | |
| 169 | |
| 170 srcStream.closeHandler = () { | |
| 171 bool result = compareFileContent(srcFileName, dstFileName); | |
| 172 new File(dstFileName).deleteSync(); | |
| 173 Expect.isTrue(result); | |
| 174 }; | |
| 175 | |
| 176 srcStream.pipe(dstStream); | |
| 177 } | |
| 178 | |
| 179 | |
| 180 // Test piping from one file to another and write additional data to | |
| 181 // the output stream after piping finished. | |
| 182 testFileToFilePipe2() { | |
| 183 String srcFileName = | |
| 184 getDataFilename("tests/standalone/src/data/dart-logo.png"); | |
| 185 var srcFile = new File(srcFileName); | |
| 186 var srcStream = srcFile.openInputStream(); | |
| 187 | |
| 188 var tempDir = new Directory(''); | |
| 189 tempDir.createTempSync(); | |
| 190 var dstFileName = tempDir.path + "/dart-logo.png"; | |
| 191 var dstFile = new File(dstFileName); | |
| 192 dstFile.createSync(); | |
| 193 var dstStream = dstFile.openOutputStream(); | |
| 194 | |
| 195 srcStream.closeHandler = () { | |
| 196 dstStream.write([32]); | |
| 197 dstStream.close(); | |
| 198 var src = srcFile.openSync(); | |
| 199 var dst = dstFile.openSync(); | |
| 200 var srcLength = src.lengthSync(); | |
| 201 var dstLength = dst.lengthSync(); | |
| 202 Expect.equals(srcLength + 1, dstLength); | |
| 203 Expect.isTrue(compareFileContent(srcFileName, | |
| 204 dstFileName, | |
| 205 count: srcLength)); | |
| 206 dst.setPositionSync(srcLength); | |
| 207 var data = new List<int>(1); | |
| 208 var read2 = dst.readListSync(data, 0, 1); | |
| 209 Expect.equals(32, data[0]); | |
| 210 src.closeSync(); | |
| 211 dst.closeSync(); | |
| 212 dstFile.deleteSync(); | |
| 213 }; | |
| 214 | |
| 215 srcStream.pipe(dstStream, close: false); | |
| 216 } | |
| 217 | |
| 218 | |
| 219 // Test piping two copies of one file to another. | |
| 220 testFileToFilePipe3() { | |
| 221 String srcFileName = | |
| 222 getDataFilename("tests/standalone/src/data/dart-logo.png"); | |
| 223 var srcFile = new File(srcFileName); | |
| 224 var srcStream = srcFile.openInputStream(); | |
| 225 | |
| 226 var tempDir = new Directory(''); | |
| 227 tempDir.createTempSync(); | |
| 228 var dstFileName = tempDir.path + "/dart-logo.png"; | |
| 229 var dstFile = new File(dstFileName); | |
| 230 dstFile.createSync(); | |
| 231 var dstStream = dstFile.openOutputStream(); | |
| 232 | |
| 233 srcStream.closeHandler = () { | |
| 234 var srcStream2 = srcFile.openInputStream(); | |
| 235 | |
| 236 srcStream2.closeHandler = () { | |
| 237 var src = srcFile.openSync(); | |
| 238 var dst = dstFile.openSync(); | |
| 239 var srcLength = src.lengthSync(); | |
| 240 var dstLength = dst.lengthSync(); | |
| 241 Expect.equals(srcLength * 2, dstLength); | |
| 242 Expect.isTrue(compareFileContent(srcFileName, | |
| 243 dstFileName, | |
| 244 count: srcLength)); | |
| 245 Expect.isTrue(compareFileContent(srcFileName, | |
| 246 dstFileName, | |
| 247 file2Offset: srcLength, | |
| 248 count: srcLength)); | |
| 249 src.closeSync(); | |
| 250 dst.closeSync(); | |
| 251 dstFile.deleteSync(); | |
| 252 }; | |
| 253 | |
| 254 // Pipe another copy of the source file. | |
| 255 srcStream2.pipe(dstStream); | |
| 256 }; | |
| 257 | |
| 258 srcStream.pipe(dstStream, close: false); | |
| 259 } | |
| 260 | |
| 261 | |
| 262 main() { | |
| 263 //testFileToFilePipe1(); | |
|
Mads Ager (google)
2011/12/14 13:49:11
Commented out code.
Søren Gjesse
2011/12/19 07:46:02
Thanks. Re-activated.
| |
| 264 //testFileToFilePipe2(); | |
| 265 //testFileToFilePipe3(); | |
| 266 PipeServerGame echoServerGame = new PipeServerGame.start(); | |
| 267 } | |
| OLD | NEW |