Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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 // Echo server test program to test socket streams. | 5 // Echo server test program to test socket streams. |
| 6 // | 6 // |
| 7 // VMOptions= | 7 // VMOptions= |
| 8 // VMOptions=--short_socket_read | 8 // VMOptions=--short_socket_read |
| 9 // VMOptions=--short_socket_write | 9 // VMOptions=--short_socket_write |
| 10 // VMOptions=--short_socket_read --short_socket_write | 10 // VMOptions=--short_socket_read --short_socket_write |
| 11 | 11 |
| 12 library ServerTest; | 12 library ServerTest; |
| 13 | 13 |
| 14 import "package:expect/expect.dart"; | 14 import "package:expect/expect.dart"; |
| 15 import "dart:async"; | 15 import "dart:async"; |
| 16 import "dart:io"; | 16 import "dart:io"; |
| 17 import "dart:isolate"; | 17 import "dart:isolate"; |
| 18 part "testing_server.dart"; | 18 part "testing_server.dart"; |
| 19 | 19 |
| 20 int count = 0; | |
| 20 class EchoServerGame { | 21 class EchoServerGame { |
| 21 | 22 |
| 22 static const MSGSIZE = 10; | 23 static const MSGSIZE = 10; |
| 23 static const MESSAGES = 100; | 24 static const MESSAGES = 100; |
| 24 static const FIRSTCHAR = 65; | 25 static const FIRSTCHAR = 65; |
| 25 | 26 |
| 26 EchoServerGame.start() | 27 EchoServerGame.start() |
| 27 : _receivePort = new ReceivePort(), | 28 : _receivePort = new ReceivePort(), |
| 28 _sendPort = null, | 29 _sendPort = null, |
| 29 _buffer = new List<int>(MSGSIZE), | 30 _buffer = new List<int>(MSGSIZE), |
| 30 _messages = 0 { | 31 _messages = 0 { |
| 31 for (int i = 0; i < MSGSIZE; i++) { | 32 for (int i = 0; i < MSGSIZE; i++) { |
| 32 _buffer[i] = FIRSTCHAR + i; | 33 _buffer[i] = FIRSTCHAR + i; |
| 33 } | 34 } |
| 34 _sendPort = spawnFunction(startEchoServer); | 35 _sendPort = spawnFunction(startEchoServer); |
| 35 initialize(); | 36 initialize(); |
| 36 } | 37 } |
| 37 | 38 |
| 38 void sendData() { | 39 void sendData() { |
| 39 int offset = 0; | 40 int offset = 0; |
| 40 List<int> data; | 41 List<int> data; |
| 41 | 42 |
| 42 void onData(List<int> data) { | 43 void onData(List<int> data) { |
| 44 print("onData"); | |
|
Anders Johnsen
2013/05/08 13:11:47
Debug stuff in this file.
| |
| 43 int bytesRead = data.length; | 45 int bytesRead = data.length; |
| 44 for (int i = 0; i < data.length; i++) { | 46 for (int i = 0; i < data.length; i++) { |
| 45 Expect.equals(FIRSTCHAR + i + offset, data[i]); | 47 Expect.equals(FIRSTCHAR + i + offset, data[i]); |
| 46 } | 48 } |
| 47 offset += bytesRead; | 49 offset += bytesRead; |
| 48 } | 50 } |
| 49 | 51 |
| 50 void onClosed() { | 52 void onClosed() { |
| 53 print("onClosed ${++count}"); | |
| 51 Expect.equals(MSGSIZE, offset); | 54 Expect.equals(MSGSIZE, offset); |
| 52 _messages++; | 55 _messages++; |
| 53 if (_messages < MESSAGES) { | 56 if (_messages < MESSAGES) { |
| 54 sendData(); | 57 sendData(); |
| 55 } else { | 58 } else { |
| 56 shutdown(); | 59 shutdown(); |
| 57 } | 60 } |
| 58 } | 61 } |
| 59 | 62 |
| 60 void errorHandler(e) { | 63 void errorHandler(e) { |
| 61 String msg = "Socket error $e"; | 64 String msg = "Socket error $e"; |
| 62 var trace = getAttachedStackTrace(e); | 65 var trace = getAttachedStackTrace(e); |
| 63 if (trace != null) msg += "\nStackTrace: $trace"; | 66 if (trace != null) msg += "\nStackTrace: $trace"; |
| 64 Expect.fail(msg); | 67 Expect.fail(msg); |
| 65 } | 68 } |
| 66 | 69 |
| 67 void connectHandler() { | 70 void connectHandler() { |
| 68 _socket.listen(onData, | 71 _socket.listen(onData, |
| 69 onError: errorHandler, | 72 onError: errorHandler, |
| 70 onDone: onClosed); | 73 onDone: onClosed); |
| 71 _socket.add(_buffer); | 74 _socket.add(_buffer); |
| 72 _socket.close(); | 75 _socket.close(); |
| 76 print("After close"); | |
| 73 data = new List<int>(MSGSIZE); | 77 data = new List<int>(MSGSIZE); |
| 74 } | 78 } |
| 75 | 79 |
| 76 Socket.connect(TestingServer.HOST, _port).then((s) { | 80 Socket.connect(TestingServer.HOST, _port).then((s) { |
| 81 print("After connect"); | |
| 77 _socket = s; | 82 _socket = s; |
| 78 connectHandler(); | 83 connectHandler(); |
| 79 }); | 84 }); |
| 80 } | 85 } |
| 81 | 86 |
| 82 void initialize() { | 87 void initialize() { |
| 83 _receivePort.receive((var message, SendPort replyTo) { | 88 _receivePort.receive((var message, SendPort replyTo) { |
| 84 _port = message; | 89 _port = message; |
| 85 sendData(); | 90 sendData(); |
| 86 }); | 91 }); |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 109 | 114 |
| 110 class EchoServer extends TestingServer { | 115 class EchoServer extends TestingServer { |
| 111 | 116 |
| 112 static const int MSGSIZE = EchoServerGame.MSGSIZE; | 117 static const int MSGSIZE = EchoServerGame.MSGSIZE; |
| 113 | 118 |
| 114 void onConnection(Socket connection) { | 119 void onConnection(Socket connection) { |
| 115 List<int> buffer = new List<int>(MSGSIZE); | 120 List<int> buffer = new List<int>(MSGSIZE); |
| 116 int offset = 0; | 121 int offset = 0; |
| 117 | 122 |
| 118 void dataReceived(List<int> data) { | 123 void dataReceived(List<int> data) { |
| 124 print("dataReceived"); | |
| 119 int bytesRead; | 125 int bytesRead; |
| 120 bytesRead = data.length; | 126 bytesRead = data.length; |
| 121 if (bytesRead > 0) { | 127 if (bytesRead > 0) { |
| 122 buffer.setRange(offset, offset + data.length, data); | 128 buffer.setRange(offset, offset + data.length, data); |
| 123 offset += bytesRead; | 129 offset += bytesRead; |
| 124 for (int i = 0; i < offset; i++) { | 130 for (int i = 0; i < offset; i++) { |
| 125 Expect.equals(EchoServerGame.FIRSTCHAR + i, buffer[i]); | 131 Expect.equals(EchoServerGame.FIRSTCHAR + i, buffer[i]); |
| 126 } | 132 } |
| 127 if (offset == MSGSIZE) { | 133 if (offset == MSGSIZE) { |
| 128 connection.add(buffer); | 134 connection.add(buffer); |
| 129 connection.close(); | 135 connection.close(); |
| 130 } | 136 } |
| 131 } | 137 } |
| 132 } | 138 } |
| 133 | 139 |
| 134 void errorHandler(e) { | 140 void errorHandler(e) { |
| 135 String msg = "Socket error $e"; | 141 String msg = "Socket error $e"; |
| 136 var trace = getAttachedStackTrace(e); | 142 var trace = getAttachedStackTrace(e); |
| 137 if (trace != null) msg += "\nStackTrace: $trace"; | 143 if (trace != null) msg += "\nStackTrace: $trace"; |
| 138 Expect.fail(msg); | 144 Expect.fail(msg); |
| 139 } | 145 } |
| 140 | 146 |
| 141 connection.listen(dataReceived, onError: errorHandler); | 147 connection.listen(dataReceived, onError: errorHandler); |
| 142 } | 148 } |
| 143 } | 149 } |
| 144 | 150 |
| 145 main() { | 151 main() { |
| 146 EchoServerGame echoServerGame = new EchoServerGame.start(); | 152 EchoServerGame echoServerGame = new EchoServerGame.start(); |
| 147 } | 153 } |
| OLD | NEW |