Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(80)

Side by Side Diff: tests/standalone/src/EchoServerTest.dart

Issue 8662038: Refactor some of the duplicated code in the tests using a socket server (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Minor fix Created 9 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 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 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 for testing sockets. 5 // Echo server test program for testing sockets.
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("EchoServerTest.dart"); 12 #source("TestingServer.dart");
13 13
14 class EchoServerTest { 14 class EchoServerTest {
15 15
16 static void testMain() { 16 static void testMain() {
17 EchoServerGame echoServerGame = new EchoServerGame.start(); 17 EchoServerGame echoServerGame = new EchoServerGame.start();
18 } 18 }
19 } 19 }
20 20
21 class EchoServerGame { 21 class EchoServerGame {
22 22
23 static final MSGSIZE = 10; 23 static final MSGSIZE = 10;
24 static final SERVERINIT = 0;
25 static final SERVERSHUTDOWN = -1;
26 static final MESSAGES = 100; 24 static final MESSAGES = 100;
27 static final FIRSTCHAR = 65; 25 static final FIRSTCHAR = 65;
28 26
29 EchoServerGame.start() 27 EchoServerGame.start()
30 : _receivePort = new ReceivePort(), 28 : _receivePort = new ReceivePort(),
31 _sendPort = null, 29 _sendPort = null,
32 _buffer = new List<int>(MSGSIZE), 30 _buffer = new List<int>(MSGSIZE),
33 _messages = 0 { 31 _messages = 0 {
34 for (int i = 0; i < MSGSIZE; i++) { 32 for (int i = 0; i < MSGSIZE; i++) {
35 _buffer[i] = FIRSTCHAR + i; 33 _buffer[i] = FIRSTCHAR + i;
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
93 } 91 }
94 92
95 handleWrite(); 93 handleWrite();
96 } 94 }
97 95
98 _socket.dataHandler = messageHandler; 96 _socket.dataHandler = messageHandler;
99 _socket.errorHandler = errorHandler; 97 _socket.errorHandler = errorHandler;
100 writeMessage(); 98 writeMessage();
101 } 99 }
102 100
103 _socket = new Socket(EchoServer.HOST, _port); 101 _socket = new Socket(TestingServer.HOST, _port);
104 if (_socket !== null) { 102 if (_socket !== null) {
105 _socket.connectHandler = connectHandler; 103 _socket.connectHandler = connectHandler;
106 } else { 104 } else {
107 Expect.fail("Socket creation failed"); 105 Expect.fail("Socket creation failed");
108 } 106 }
109 } 107 }
110 108
111 void start() { 109 void start() {
112 _receivePort.receive((var message, SendPort replyTo) { 110 _receivePort.receive((var message, SendPort replyTo) {
113 _port = message; 111 _port = message;
114 sendData(); 112 sendData();
115 }); 113 });
116 _sendPort.send(SERVERINIT, _receivePort.toSendPort()); 114 _sendPort.send(TestingServer.INIT, _receivePort.toSendPort());
117 } 115 }
118 116
119 void shutdown() { 117 void shutdown() {
120 _sendPort.send(SERVERSHUTDOWN, _receivePort.toSendPort()); 118 _sendPort.send(TestingServer.SHUTDOWN, _receivePort.toSendPort());
121 _receivePort.close(); 119 _receivePort.close();
122 } 120 }
123 121
124 int _port; 122 int _port;
125 ReceivePort _receivePort; 123 ReceivePort _receivePort;
126 SendPort _sendPort; 124 SendPort _sendPort;
127 List<int> _buffer; 125 List<int> _buffer;
128 int _messages; 126 int _messages;
129 } 127 }
130 128
131 class EchoServer extends Isolate { 129 class EchoServer extends TestingServer {
132 130
133 static final HOST = "127.0.0.1";
134 static final msgSize = EchoServerGame.MSGSIZE; 131 static final msgSize = EchoServerGame.MSGSIZE;
135 132
133 void connectionHandler() {
134 Socket _client;
136 135
137 void main() { 136 void messageHandler() {
138 137
139 void connectionHandler() { 138 List<int> buffer = new List<int>(msgSize);
140 Socket _client; 139 int bytesRead = 0;
141 140
142 void messageHandler() { 141 void handleRead() {
142 int read = _client.readList(buffer, bytesRead, msgSize - bytesRead);
143 if (read > 0) {
144 bytesRead += read;
145 if (bytesRead < msgSize) {
146 // We check every time the whole buffer to verify data integrity.
147 for (int i = 0; i < bytesRead; i++) {
148 Expect.equals(EchoServerGame.FIRSTCHAR + i, buffer[i]);
149 }
150 _client.dataHandler = handleRead;
151 } else {
152 // We check every time the whole buffer to verify data integrity.
153 for (int i = 0; i < msgSize; i++) {
154 Expect.equals(EchoServerGame.FIRSTCHAR + i, buffer[i]);
155 }
143 156
144 List<int> buffer = new List<int>(msgSize); 157 void writeMessage() {
145 int bytesRead = 0;
146 158
147 void handleRead() { 159 int bytesWritten = 0;
148 int read = _client.readList(buffer, bytesRead, msgSize - bytesRead); 160
149 if (read > 0) { 161 void handleWrite() {
150 bytesRead += read; 162 int written = _client.writeList(
151 if (bytesRead < msgSize) { 163 buffer, bytesWritten, msgSize - bytesWritten);
152 // We check every time the whole buffer to verify data integrity. 164 bytesWritten += written;
153 for (int i = 0; i < bytesRead; i++) { 165 if (bytesWritten < msgSize) {
154 Expect.equals(EchoServerGame.FIRSTCHAR + i, buffer[i]); 166 _client.writeHandler = handleWrite;
167 } else {
168 _client.close(true);
169 }
155 } 170 }
156 _client.dataHandler = handleRead; 171 handleWrite();
157 } else {
158 // We check every time the whole buffer to verify data integrity.
159 for (int i = 0; i < msgSize; i++) {
160 Expect.equals(EchoServerGame.FIRSTCHAR + i, buffer[i]);
161 }
162
163 void writeMessage() {
164
165 int bytesWritten = 0;
166
167 void handleWrite() {
168 int written = _client.writeList(
169 buffer, bytesWritten, msgSize - bytesWritten);
170 bytesWritten += written;
171 if (bytesWritten < msgSize) {
172 _client.writeHandler = handleWrite;
173 } else {
174 _client.close(true);
175 }
176 }
177 handleWrite();
178 }
179 writeMessage();
180 } 172 }
173 writeMessage();
181 } 174 }
182 } 175 }
183
184 handleRead();
185 } 176 }
186 177
187 void closeHandler() { 178 handleRead();
188 _client.close();
189 }
190
191 void errorHandler() {
192 Expect.fail("Socket error");
193 }
194
195 _client = _server.accept();
196 _client.dataHandler = messageHandler;
197 _client.closeHandler = closeHandler;
198 _client.errorHandler = errorHandler;
199 } 179 }
200 180
201 void errorHandlerServer() { 181 void closeHandler() {
202 Expect.fail("Server socket error"); 182 _client.close();
203 } 183 }
204 184
205 this.port.receive((message, SendPort replyTo) { 185 void errorHandler() {
206 if (message == EchoServerGame.SERVERINIT) { 186 Expect.fail("Socket error");
207 _server = new ServerSocket(HOST, 0, 10); 187 }
208 Expect.equals(true, _server !== null); 188
209 _server.connectionHandler = connectionHandler; 189 _client = _server.accept();
210 _server.errorHandler = errorHandlerServer; 190 _client.dataHandler = messageHandler;
211 replyTo.send(_server.port, null); 191 _client.closeHandler = closeHandler;
212 } else if (message == EchoServerGame.SERVERSHUTDOWN) { 192 _client.errorHandler = errorHandler;
213 _server.close();
214 this.port.close();
215 }
216 });
217 } 193 }
218
219 ServerSocket _server;
220 } 194 }
221 195
222 main() { 196 main() {
223 EchoServerTest.testMain(); 197 EchoServerTest.testMain();
224 } 198 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698