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

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

Issue 9382001: Another attempt at fixing the Socket tests. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 10 months 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) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, 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 // VMOptions=
6 // VMOptions=--short_socket_read
7 // VMOptions=--short_socket_write
8 // VMOptions=--short_socket_read --short_socket_write
9 //
5 // Test socket close events. 10 // Test socket close events.
6 11
7 #import("dart:io"); 12 #import("dart:io");
8 13
9 final SERVERSHUTDOWN = -1; 14 final SERVERSHUTDOWN = -1;
10 final ITERATIONS = 10; 15 final ITERATIONS = 10;
11 16
12 17
13 class SocketClose { 18 class SocketClose {
14 19
15 SocketClose.start(mode) 20 SocketClose.start(this._mode, this._donePort)
16 : _receivePort = new ReceivePort(), 21 : _receivePort = new ReceivePort(),
17 _sendPort = null, 22 _sendPort = null,
18 _readBytes = 0, 23 _readBytes = 0,
19 _dataEvents = 0, 24 _dataEvents = 0,
20 _closeEvents = 0, 25 _closeEvents = 0,
21 _errorEvents = 0, 26 _errorEvents = 0,
22 _iterations = 0, 27 _iterations = 0 {
23 _mode = mode {
24 new SocketCloseServer().spawn().then((SendPort port) { 28 new SocketCloseServer().spawn().then((SendPort port) {
25 _sendPort = port; 29 _sendPort = port;
26 start(); 30 start();
27 }); 31 });
28 } 32 }
29 33
30 void proceed() { 34 void proceed() {
31 if (_iterations < ITERATIONS) { 35 if (_iterations < ITERATIONS) {
32 new Timer(sendData, 0); 36 new Timer(sendData, 0);
33 } else { 37 } else {
34 shutdown(); 38 shutdown();
35 } 39 }
36 } 40 }
37 41
38 void sendData(Timer timer) { 42 void sendData(Timer timer) {
39 43
40 void dataHandler() { 44 void dataHandler() {
41 switch (_mode) { 45 switch (_mode) {
42 case 0: 46 case 0:
43 case 1: 47 case 1:
44 case 2: 48 case 2:
45 Expect.fail("No data expected"); 49 Expect.fail("No data expected");
46 break; 50 break;
47 case 3: 51 case 3:
48 case 4: 52 case 4:
49 case 5: 53 case 5:
50 case 6: 54 case 6:
51 List<int> b = new List<int>(100); 55 List<int> b = new List<int>(5);
52 _readBytes += _socket.readList(b, 0, 100); 56 _readBytes += _socket.readList(b, 0, 5);
53 if ((_readBytes % 5) == 0) { 57 if ((_readBytes % 5) == 0) {
54 _dataEvents++; 58 _dataEvents++;
55 } 59 }
56 break; 60 break;
57 default: 61 default:
58 Expect.fail("Unknown test mode"); 62 Expect.fail("Unknown test mode");
59 } 63 }
60 } 64 }
61 65
62 void closeHandler() { 66 void closeHandler() {
(...skipping 26 matching lines...) Expand all
89 void errorHandler() { 93 void errorHandler() {
90 _errorEvents++; 94 _errorEvents++;
91 _socket.close(); 95 _socket.close();
92 } 96 }
93 97
94 void connectHandler() { 98 void connectHandler() {
95 _socket.dataHandler = dataHandler; 99 _socket.dataHandler = dataHandler;
96 _socket.closeHandler = closeHandler; 100 _socket.closeHandler = closeHandler;
97 _socket.errorHandler = errorHandler; 101 _socket.errorHandler = errorHandler;
98 102
103 void writeHello() {
104 int bytesWritten = 0;
105 while (bytesWritten != 5) {
106 bytesWritten += _socket.writeList("Hello".charCodes(),
107 bytesWritten,
108 5 - bytesWritten);
109 }
110 }
111
99 _iterations++; 112 _iterations++;
100 switch (_mode) { 113 switch (_mode) {
101 case 0: 114 case 0:
102 _socket.close(); 115 _socket.close();
103 proceed(); 116 proceed();
104 break; 117 break;
105 case 1: 118 case 1:
106 int bytesWritten = _socket.writeList("Hello".charCodes(), 0, 5); 119 writeHello();
107 Expect.equals(5, bytesWritten);
108 _socket.close(); 120 _socket.close();
109 proceed(); 121 proceed();
110 break; 122 break;
111 case 2: 123 case 2:
112 case 3: 124 case 3:
113 int bytesWritten = _socket.writeList("Hello".charCodes(), 0, 5); 125 writeHello();
114 Expect.equals(5, bytesWritten);
115 break; 126 break;
116 case 4: 127 case 4:
117 int bytesWritten = _socket.writeList("Hello".charCodes(), 0, 5); 128 writeHello();
118 Expect.equals(5, bytesWritten);
119 _socket.close(true); 129 _socket.close(true);
120 break; 130 break;
121 case 5: 131 case 5:
122 int bytesWritten = _socket.writeList("Hello".charCodes(), 0, 5); 132 writeHello();
123 Expect.equals(5, bytesWritten);
124 break; 133 break;
125 case 6: 134 case 6:
126 int bytesWritten = _socket.writeList("Hello".charCodes(), 0, 5); 135 writeHello();
127 Expect.equals(5, bytesWritten);
128 _socket.close(true); 136 _socket.close(true);
129 break; 137 break;
130 default: 138 default:
131 Expect.fail("Unknown test mode"); 139 Expect.fail("Unknown test mode");
132 } 140 }
133 } 141 }
134 142
135 _socket = new Socket(SocketCloseServer.HOST, _port); 143 _socket = new Socket(SocketCloseServer.HOST, _port);
136 Expect.equals(true, _socket !== null); 144 Expect.equals(true, _socket !== null);
137 _socket.connectHandler = connectHandler; 145 _socket.connectHandler = connectHandler;
138 } 146 }
139 147
140 void start() { 148 void start() {
141 _receivePort.receive((var message, SendPort replyTo) { 149 _receivePort.receive((var message, SendPort replyTo) {
142 _port = message; 150 _port = message;
143 proceed(); 151 proceed();
144 }); 152 });
145 _sendPort.send(_mode, _receivePort.toSendPort()); 153 _sendPort.send(_mode, _receivePort.toSendPort());
146 } 154 }
147 155
148 void shutdown() { 156 void shutdown() {
149 _sendPort.send(SERVERSHUTDOWN, _receivePort.toSendPort()); 157 _sendPort.send(SERVERSHUTDOWN, _receivePort.toSendPort());
150 _receivePort.close(); 158 _receivePort.receive((message, ignore) {
159 _donePort.send(null);
160 _receivePort.close();
161 });
151 162
152 switch (_mode) { 163 switch (_mode) {
153 case 0: 164 case 0:
154 case 1: 165 case 1:
155 Expect.equals(0, _dataEvents); 166 Expect.equals(0, _dataEvents);
156 Expect.equals(0, _closeEvents); 167 Expect.equals(0, _closeEvents);
157 break; 168 break;
158 case 2: 169 case 2:
159 Expect.equals(0, _dataEvents); 170 Expect.equals(0, _dataEvents);
160 Expect.equals(ITERATIONS, _closeEvents); 171 Expect.equals(ITERATIONS, _closeEvents);
(...skipping 15 matching lines...) Expand all
176 ReceivePort _receivePort; 187 ReceivePort _receivePort;
177 SendPort _sendPort; 188 SendPort _sendPort;
178 Socket _socket; 189 Socket _socket;
179 List<int> _buffer; 190 List<int> _buffer;
180 int _readBytes; 191 int _readBytes;
181 int _dataEvents; 192 int _dataEvents;
182 int _closeEvents; 193 int _closeEvents;
183 int _errorEvents; 194 int _errorEvents;
184 int _iterations; 195 int _iterations;
185 int _mode; 196 int _mode;
197 int _donePort;
186 } 198 }
187 199
200
201 class ConnectionData {
202 ConnectionData(Socket this.connection) : readBytes = 0;
203 Socket connection;
204 int readBytes;
205 }
206
207
188 class SocketCloseServer extends Isolate { 208 class SocketCloseServer extends Isolate {
189 209
190 static final HOST = "127.0.0.1"; 210 static final HOST = "127.0.0.1";
191 211
192 SocketCloseServer() : super() {} 212 SocketCloseServer() : super() {}
193 213
194 void main() { 214 void main() {
195 215
196 void connectionHandler(Socket connection) { 216 void connectionHandler(ConnectionData data) {
217 var connection = data.connection;
197 218
198 void readBytes(whenFiveBytes) { 219 void readBytes(whenFiveBytes) {
199 List<int> b = new List<int>(100); 220 List<int> b = new List<int>(5);
200 _readBytes += connection.readList(b, 0, 100); 221 data.readBytes += connection.readList(b, 0, 5);
201 if ((_readBytes % 5) == 0) { 222 if (data.readBytes == 5) {
202 whenFiveBytes(); 223 whenFiveBytes();
203 } 224 }
204 } 225 }
205 226
227 void writeHello() {
228 int bytesWritten = 0;
229 while (bytesWritten != 5) {
230 bytesWritten += connection.writeList("Hello".charCodes(),
231 bytesWritten,
232 5 - bytesWritten);
233 }
234 }
235
206 void dataHandler() { 236 void dataHandler() {
207 _dataEvents++;
208 switch (_mode) { 237 switch (_mode) {
209 case 0: 238 case 0:
210 Expect.fail("No data expected"); 239 Expect.fail("No data expected");
211 break; 240 break;
212 case 1: 241 case 1:
213 readBytes(() { }); 242 readBytes(() { _dataEvents++; });
214 break; 243 break;
215 case 2: 244 case 2:
216 readBytes(() { 245 readBytes(() {
246 _dataEvents++;
217 connection.close(); 247 connection.close();
218 }); 248 });
219 break; 249 break;
220 case 3: 250 case 3:
221 readBytes(() { 251 readBytes(() {
222 connection.writeList("Hello".charCodes(), 0, 5); 252 _dataEvents++;
253 writeHello();
223 connection.close(); 254 connection.close();
224 }); 255 });
225 break; 256 break;
226 case 4: 257 case 4:
227 readBytes(() { 258 readBytes(() {
228 connection.writeList("Hello".charCodes(), 0, 5); 259 _dataEvents++;
260 writeHello();
229 }); 261 });
230 break; 262 break;
231 case 5: 263 case 5:
232 case 6: 264 case 6:
233 readBytes(() { 265 readBytes(() {
234 connection.writeList("Hello".charCodes(), 0, 5); 266 _dataEvents++;
267 writeHello();
235 connection.close(true); 268 connection.close(true);
236 }); 269 });
237 break; 270 break;
238 default: 271 default:
239 Expect.fail("Unknown test mode"); 272 Expect.fail("Unknown test mode");
240 } 273 }
241 } 274 }
242 275
243 void closeHandler() { 276 void closeHandler() {
244 _closeEvents++; 277 _closeEvents++;
(...skipping 12 matching lines...) Expand all
257 } 290 }
258 291
259 void errorHandlerServer() { 292 void errorHandlerServer() {
260 Expect.fail("Server socket error"); 293 Expect.fail("Server socket error");
261 } 294 }
262 295
263 waitForResult(Timer timer) { 296 waitForResult(Timer timer) {
264 // Make sure all iterations have been run. In multiple of these 297 // Make sure all iterations have been run. In multiple of these
265 // scenarios it is possible to get the SERVERSHUTDOWN message 298 // scenarios it is possible to get the SERVERSHUTDOWN message
266 // before we have received the last close event on the 299 // before we have received the last close event on the
267 // server. We therefore always wait for the correct number of 300 // server. In these cases we wait for the correct number of
268 // close events. 301 // close events.
269 if (_iterations == ITERATIONS && _closeEvents == ITERATIONS) { 302 if (_iterations == ITERATIONS &&
303 (_closeEvents == ITERATIONS || (_mode == 2 || _mode == 3))) {
270 switch (_mode) { 304 switch (_mode) {
271 case 0: 305 case 0:
272 Expect.equals(0, _dataEvents); 306 Expect.equals(0, _dataEvents);
273 Expect.equals(ITERATIONS, _closeEvents); 307 Expect.equals(ITERATIONS, _closeEvents);
274 break; 308 break;
275 case 1: 309 case 1:
276 Expect.equals(ITERATIONS, _dataEvents); 310 Expect.equals(ITERATIONS, _dataEvents);
277 Expect.equals(ITERATIONS, _closeEvents); 311 Expect.equals(ITERATIONS, _closeEvents);
278 break; 312 break;
279 case 2: 313 case 2:
280 case 3: 314 case 3:
281 Expect.equals(ITERATIONS, _dataEvents); 315 Expect.equals(ITERATIONS, _dataEvents);
282 Expect.equals(0, _closeEvents); 316 Expect.equals(0, _closeEvents);
283 break; 317 break;
284 case 4: 318 case 4:
285 case 5: 319 case 5:
286 case 6: 320 case 6:
287 Expect.equals(ITERATIONS, _dataEvents); 321 Expect.equals(ITERATIONS, _dataEvents);
288 Expect.equals(ITERATIONS, _closeEvents); 322 Expect.equals(ITERATIONS, _closeEvents);
289 break; 323 break;
290 default: 324 default:
291 Expect.fail("Unknown test mode"); 325 Expect.fail("Unknown test mode");
292 } 326 }
293 Expect.equals(0, _errorEvents); 327 Expect.equals(0, _errorEvents);
294 _server.close(); 328 _server.close();
295 this.port.close(); 329 this.port.close();
330 _donePort.send(null);
296 } else { 331 } else {
297 new Timer(waitForResult, 100); 332 new Timer(waitForResult, 100);
298 } 333 }
299 } 334 }
300 335
301 this.port.receive((message, SendPort replyTo) { 336 this.port.receive((message, SendPort replyTo) {
337 _donePort = replyTo;
302 if (message != SERVERSHUTDOWN) { 338 if (message != SERVERSHUTDOWN) {
303 _readBytes = 0; 339 _readBytes = 0;
304 _errorEvents = 0; 340 _errorEvents = 0;
305 _dataEvents = 0; 341 _dataEvents = 0;
306 _closeEvents = 0; 342 _closeEvents = 0;
307 _iterations = 0; 343 _iterations = 0;
308 _mode = message; 344 _mode = message;
309 _server = new ServerSocket(HOST, 0, 10); 345 _server = new ServerSocket(HOST, 0, 10);
310 Expect.equals(true, _server !== null); 346 Expect.equals(true, _server !== null);
311 _server.connectionHandler = connectionHandler; 347 _server.connectionHandler = (connection) {
348 var data = new ConnectionData(connection);
349 connectionHandler(data);
350 };
312 _server.errorHandler = errorHandlerServer; 351 _server.errorHandler = errorHandlerServer;
313 replyTo.send(_server.port, null); 352 replyTo.send(_server.port, null);
314 } else { 353 } else {
315 new Timer(waitForResult, 0); 354 new Timer(waitForResult, 0);
316 } 355 }
317 }); 356 });
318 } 357 }
319 358
320 ServerSocket _server; 359 ServerSocket _server;
360 SendPort _donePort;
321 int _readBytes; 361 int _readBytes;
322 int _errorEvents; 362 int _errorEvents;
323 int _dataEvents; 363 int _dataEvents;
324 int _closeEvents; 364 int _closeEvents;
325 int _iterations; 365 int _iterations;
326 int _mode; 366 int _mode;
327 } 367 }
328 368
329 369
330 main() { 370 main() {
331 // Run the close test in these different "modes". 371 // Run the close test in these different "modes".
332 // 0: Client closes without sending at all. 372 // 0: Client closes without sending at all.
333 // 1: Client sends and closes. 373 // 1: Client sends and closes.
334 // 2: Client sends. Server closes. 374 // 2: Client sends. Server closes.
335 // 3: Client sends. Server responds and closes. 375 // 3: Client sends. Server responds and closes.
336 // 4: Client sends and half-closes. Server responds and closes. 376 // 4: Client sends and half-closes. Server responds and closes.
337 // 5: Client sends. Server responds and half closes. 377 // 5: Client sends. Server responds and half closes.
338 // 6: Client sends and half-closes. Server responds and half closes. 378 // 6: Client sends and half-closes. Server responds and half closes.
339 new SocketClose.start(0); 379 var tests = 7;
340 new SocketClose.start(1); 380 var port = new ReceivePort();
341 new SocketClose.start(2); 381 var completed = 0;
342 new SocketClose.start(3); 382 port.receive((message, ignore) {
343 new SocketClose.start(4); 383 if (++completed == tests) port.close();
344 new SocketClose.start(5); 384 });
345 new SocketClose.start(6); 385 for (var i = 0; i < tests; i++) {
386 new SocketClose.start(i, port.toSendPort());
387 }
346 } 388 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698