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

Side by Side Diff: tests/standalone/src/EchoServerStreamTest.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: Addressed review comments from ager@ 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
« no previous file with comments | « no previous file | tests/standalone/src/EchoServerTest.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 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 class EchoServerStreamTest { 12 #source("TestingServer.dart");
13
14 static void testMain() {
15 EchoServerGame echoServerGame = new EchoServerGame.start();
16 }
17 }
18 13
19 class EchoServerGame { 14 class EchoServerGame {
20 15
21 static final MSGSIZE = 10; 16 static final MSGSIZE = 10;
22 static final SERVERINIT = 0;
23 static final SERVERSHUTDOWN = -1;
24 static final MESSAGES = 100; 17 static final MESSAGES = 100;
25 static final FIRSTCHAR = 65; 18 static final FIRSTCHAR = 65;
26 19
27 EchoServerGame.start() 20 EchoServerGame.start()
28 : _receivePort = new ReceivePort(), 21 : _receivePort = new ReceivePort(),
29 _sendPort = null, 22 _sendPort = null,
30 _buffer = new List<int>(MSGSIZE), 23 _buffer = new List<int>(MSGSIZE),
31 _messages = 0 { 24 _messages = 0 {
32 for (int i = 0; i < MSGSIZE; i++) { 25 for (int i = 0; i < MSGSIZE; i++) {
33 _buffer[i] = FIRSTCHAR + i; 26 _buffer[i] = FIRSTCHAR + i;
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
105 break; 98 break;
106 case 3: 99 case 3:
107 stream.writeFrom(_buffer, len: _buffer.length ~/ 2); 100 stream.writeFrom(_buffer, len: _buffer.length ~/ 2);
108 stream.writeFrom(_buffer, _buffer.length ~/ 2); 101 stream.writeFrom(_buffer, _buffer.length ~/ 2);
109 break; 102 break;
110 } 103 }
111 stream.close(); 104 stream.close();
112 dataSent(); 105 dataSent();
113 } 106 }
114 107
115 _socket = new Socket(EchoServer.HOST, _port); 108 _socket = new Socket(TestingServer.HOST, _port);
116 if (_socket !== null) { 109 if (_socket !== null) {
117 _socket.connectHandler = connectHandler; 110 _socket.connectHandler = connectHandler;
118 } else { 111 } else {
119 Expect.fail("socket creation failed"); 112 Expect.fail("socket creation failed");
120 } 113 }
121 } 114 }
122 115
123 void start() { 116 void start() {
124 _receivePort.receive((var message, SendPort replyTo) { 117 _receivePort.receive((var message, SendPort replyTo) {
125 _port = message; 118 _port = message;
126 sendData(); 119 sendData();
127 }); 120 });
128 _sendPort.send(SERVERINIT, _receivePort.toSendPort()); 121 _sendPort.send(TestingServer.INIT, _receivePort.toSendPort());
129 } 122 }
130 123
131 void shutdown() { 124 void shutdown() {
132 _sendPort.send(SERVERSHUTDOWN, _receivePort.toSendPort()); 125 _sendPort.send(TestingServer.SHUTDOWN, _receivePort.toSendPort());
133 _receivePort.close(); 126 _receivePort.close();
134 } 127 }
135 128
136 int _port; 129 int _port;
137 ReceivePort _receivePort; 130 ReceivePort _receivePort;
138 SendPort _sendPort; 131 SendPort _sendPort;
139 Socket _socket; 132 Socket _socket;
140 List<int> _buffer; 133 List<int> _buffer;
141 int _messages; 134 int _messages;
142 } 135 }
143 136
144 class EchoServer extends Isolate {
145 137
146 static final HOST = "127.0.0.1"; 138 class EchoServer extends TestingServer {
139
147 static final int MSGSIZE = EchoServerGame.MSGSIZE; 140 static final int MSGSIZE = EchoServerGame.MSGSIZE;
148 141
149 void main() { 142 void connectionHandler() {
143 Socket _client;
144 InputStream inputStream;
145 List<int> buffer = new List<int>(MSGSIZE);
146 int offset = 0;
150 147
151 void connectionHandler() { 148 void dataReceived() {
152 Socket _client; 149 SocketOutputStream outputStream;
153 InputStream inputStream; 150 int bytesRead;
154 List<int> buffer = new List<int>(MSGSIZE); 151 outputStream = _client.outputStream;
155 int offset = 0; 152 bytesRead = inputStream.readInto(buffer, offset, MSGSIZE - offset);
156 153 if (bytesRead > 0) {
157 void dataReceived() { 154 offset += bytesRead;
158 SocketOutputStream outputStream; 155 for (int i = 0; i < offset; i++) {
159 int bytesRead; 156 Expect.equals(EchoServerGame.FIRSTCHAR + i, buffer[i]);
160 outputStream = _client.outputStream; 157 }
161 bytesRead = inputStream.readInto(buffer, offset, MSGSIZE - offset); 158 if (offset == MSGSIZE) {
162 if (bytesRead > 0) { 159 outputStream.write(buffer);
163 offset += bytesRead; 160 outputStream.close();
164 for (int i = 0; i < offset; i++) {
165 Expect.equals(EchoServerGame.FIRSTCHAR + i, buffer[i]);
166 }
167 if (offset == MSGSIZE) {
168 outputStream.write(buffer);
169 outputStream.close();
170 }
171 } 161 }
172 } 162 }
173
174 void errorHandler() {
175 Expect.fail("Socket error");
176 }
177
178 _client = _server.accept();
179 inputStream = _client.inputStream;
180 inputStream.dataHandler = dataReceived;
181 _client.errorHandler = errorHandler;
182 } 163 }
183 164
184 void errorHandlerServer() { 165 void errorHandler() {
185 Expect.fail("Server socket error"); 166 Expect.fail("Socket error");
186 } 167 }
187 168
188 this.port.receive((message, SendPort replyTo) { 169 _client = _server.accept();
189 if (message == EchoServerGame.SERVERINIT) { 170 inputStream = _client.inputStream;
190 _server = new ServerSocket(HOST, 0, 10); 171 inputStream.dataHandler = dataReceived;
191 Expect.equals(true, _server !== null); 172 _client.errorHandler = errorHandler;
192 _server.connectionHandler = connectionHandler;
193 _server.errorHandler = errorHandlerServer;
194 replyTo.send(_server.port, null);
195 } else if (message == EchoServerGame.SERVERSHUTDOWN) {
196 _server.close();
197 this.port.close();
198 }
199 });
200 } 173 }
201
202 ServerSocket _server;
203 } 174 }
204 175
205 main() { 176 main() {
206 EchoServerStreamTest.testMain(); 177 EchoServerGame echoServerGame = new EchoServerGame.start();
207 } 178 }
OLDNEW
« no previous file with comments | « no previous file | tests/standalone/src/EchoServerTest.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698