| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 // |
| 5 // VMOptions= |
| 6 // VMOptions=--short_socket_read |
| 7 // VMOptions=--short_socket_write |
| 8 // VMOptions=--short_socket_read --short_socket_write |
| 9 |
| 10 library ServerTest; |
| 11 |
| 12 import "dart:async"; |
| 13 import "dart:io"; |
| 14 import "dart:isolate"; |
| 15 part "testing_server.dart"; |
| 16 |
| 17 |
| 18 String getDataFilename(String path) => |
| 19 new File(path).existsSync() ? path : '../$path'; |
| 20 |
| 21 |
| 22 bool compareFileContent(String fileName1, String fileName2) { |
| 23 var contents1 = new File(fileName1).readAsStringSync(); |
| 24 var contents2 = new File(fileName2).readAsStringSync(); |
| 25 return contents1 == contents2; |
| 26 } |
| 27 |
| 28 |
| 29 // This test does: |
| 30 // 1. Opens a socket to the testing server. |
| 31 // 2. Pipes the content of a file to that sockets input stream. |
| 32 // 3. Creates a temp file. |
| 33 // 4. Pipes the socket output stream to the temp file. |
| 34 // 5. Expects the original file and the temp file to be equal. |
| 35 class PipeServerGame { |
| 36 |
| 37 int count = 0; |
| 38 |
| 39 PipeServerGame.start() |
| 40 : _receivePort = new ReceivePort(), |
| 41 _sendPort = null, |
| 42 _messages = 0 { |
| 43 _sendPort = spawnFunction(startPipeServer); |
| 44 initialize(); |
| 45 } |
| 46 |
| 47 void runTest() { |
| 48 |
| 49 void connectHandler() { |
| 50 String srcFileName = |
| 51 getDataFilename("tests/standalone/io/readline_test1.dat"); |
| 52 Stream fileInput = new File(srcFileName).openRead(); |
| 53 |
| 54 fileInput.pipe(_socket).then((_) { |
| 55 var tempDir = new Directory('').createTempSync(); |
| 56 var dstFileName = tempDir.path.concat("/readline_test1.dat"); |
| 57 var dstFile = new File(dstFileName); |
| 58 dstFile.createSync(); |
| 59 var fileOutput = dstFile.openWrite(); |
| 60 _socket.pipe(fileOutput).then((_) { |
| 61 // Check that the resulting file is equal to the initial |
| 62 // file. |
| 63 bool result = compareFileContent(srcFileName, dstFileName); |
| 64 new File(dstFileName).deleteSync(); |
| 65 tempDir.deleteSync(); |
| 66 Expect.isTrue(result); |
| 67 |
| 68 // Run this twice. |
| 69 if (count++ < 2) { |
| 70 runTest(); |
| 71 } else { |
| 72 shutdown(); |
| 73 } |
| 74 }); |
| 75 }); |
| 76 } |
| 77 |
| 78 // Connect to the server. |
| 79 Socket.connect(TestingServer.HOST, _port).then((s) { |
| 80 _socket = s; |
| 81 connectHandler(); |
| 82 }); |
| 83 } |
| 84 |
| 85 void initialize() { |
| 86 _receivePort.receive((var message, SendPort replyTo) { |
| 87 _port = message; |
| 88 runTest(); |
| 89 }); |
| 90 _sendPort.send(TestingServer.INIT, _receivePort.toSendPort()); |
| 91 } |
| 92 |
| 93 void shutdown() { |
| 94 _sendPort.send(TestingServer.SHUTDOWN, _receivePort.toSendPort()); |
| 95 _receivePort.close(); |
| 96 } |
| 97 |
| 98 int _port; |
| 99 ReceivePort _receivePort; |
| 100 SendPort _sendPort; |
| 101 Socket _socket; |
| 102 int _messages; |
| 103 } |
| 104 |
| 105 |
| 106 void startPipeServer() { |
| 107 var server = new PipeServer(); |
| 108 port.receive(server.dispatch); |
| 109 } |
| 110 |
| 111 |
| 112 // The testing server will simply pipe each connecting sockets input |
| 113 // stream to its output stream. |
| 114 class PipeServer extends TestingServer { |
| 115 void onConnection(Socket connection) { |
| 116 connection.pipe(connection); |
| 117 } |
| 118 } |
| 119 |
| 120 |
| 121 main() { |
| 122 PipeServerGame echoServerGame = new PipeServerGame.start(); |
| 123 } |
| OLD | NEW |