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

Side by Side Diff: tests/standalone/io/socket_close_test.dart

Issue 10837070: Remove old isolate API and update all code in the repository to use (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address review comments. Created 8 years, 4 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= 5 // VMOptions=
6 // VMOptions=--short_socket_read 6 // VMOptions=--short_socket_read
7 // VMOptions=--short_socket_write 7 // VMOptions=--short_socket_write
8 // VMOptions=--short_socket_read --short_socket_write 8 // VMOptions=--short_socket_read --short_socket_write
9 // 9 //
10 // Test socket close events. 10 // Test socket close events.
11 11
12 #import("dart:io"); 12 #import("dart:io");
13 #import("dart:isolate"); 13 #import("dart:isolate");
14 14
15 final SERVERSHUTDOWN = -1; 15 final SERVERSHUTDOWN = -1;
16 final ITERATIONS = 10; 16 final ITERATIONS = 10;
17 17
18 18
19 class SocketClose { 19 class SocketClose {
20 20
21 SocketClose.start(this._mode, this._donePort) 21 SocketClose.start(this._mode, this._donePort)
22 : _receivePort = new ReceivePort(), 22 : _receivePort = new ReceivePort(),
23 _sendPort = null, 23 _sendPort = null,
24 _readBytes = 0, 24 _readBytes = 0,
25 _dataEvents = 0, 25 _dataEvents = 0,
26 _closeEvents = 0, 26 _closeEvents = 0,
27 _errorEvents = 0, 27 _errorEvents = 0,
28 _iterations = 0 { 28 _iterations = 0 {
29 new SocketCloseServer().spawn().then((SendPort port) { 29 _sendPort = spawnFunction(startSocketCloseServer);
30 _sendPort = port; 30 start();
31 start();
32 });
33 } 31 }
34 32
35 void proceed() { 33 void proceed() {
36 if (_iterations < ITERATIONS) { 34 if (_iterations < ITERATIONS) {
37 new Timer(0, sendData); 35 new Timer(0, sendData);
38 } else { 36 } else {
39 shutdown(); 37 shutdown();
40 } 38 }
41 } 39 }
42 40
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
199 } 197 }
200 198
201 199
202 class ConnectionData { 200 class ConnectionData {
203 ConnectionData(Socket this.connection) : readBytes = 0; 201 ConnectionData(Socket this.connection) : readBytes = 0;
204 Socket connection; 202 Socket connection;
205 int readBytes; 203 int readBytes;
206 } 204 }
207 205
208 206
209 class SocketCloseServer extends Isolate { 207 void startSocketCloseServer() {
208 var server = new SocketCloseServer();
209 port.receive(server.dispatch);
210 }
211
212 class SocketCloseServer {
210 213
211 static final HOST = "127.0.0.1"; 214 static final HOST = "127.0.0.1";
212 215
213 SocketCloseServer() : super() {} 216 SocketCloseServer() : super() {}
214 217
215 void main() { 218 void connectionHandler(ConnectionData data) {
219 var connection = data.connection;
216 220
217 void connectionHandler(ConnectionData data) { 221 void readBytes(whenFiveBytes) {
218 var connection = data.connection; 222 List<int> b = new List<int>(5);
219 223 data.readBytes += connection.readList(b, 0, 5);
220 void readBytes(whenFiveBytes) { 224 if (data.readBytes == 5) {
221 List<int> b = new List<int>(5); 225 whenFiveBytes();
222 data.readBytes += connection.readList(b, 0, 5);
223 if (data.readBytes == 5) {
224 whenFiveBytes();
225 }
226 }
227
228 void writeHello() {
229 int bytesWritten = 0;
230 while (bytesWritten != 5) {
231 bytesWritten += connection.writeList("Hello".charCodes(),
232 bytesWritten,
233 5 - bytesWritten);
234 }
235 }
236
237 void dataHandler() {
238 switch (_mode) {
239 case 0:
240 Expect.fail("No data expected");
241 break;
242 case 1:
243 readBytes(() { _dataEvents++; });
244 break;
245 case 2:
246 readBytes(() {
247 _dataEvents++;
248 connection.close();
249 });
250 break;
251 case 3:
252 readBytes(() {
253 _dataEvents++;
254 writeHello();
255 connection.close();
256 });
257 break;
258 case 4:
259 readBytes(() {
260 _dataEvents++;
261 writeHello();
262 });
263 break;
264 case 5:
265 case 6:
266 readBytes(() {
267 _dataEvents++;
268 writeHello();
269 connection.close(true);
270 });
271 break;
272 default:
273 Expect.fail("Unknown test mode");
274 }
275 }
276
277 void closeHandler() {
278 _closeEvents++;
279 connection.close();
280 }
281
282 void errorHandler(Exception e) {
283 Expect.fail("Socket error $e");
284 }
285
286 _iterations++;
287
288 connection.onData = dataHandler;
289 connection.onClosed = closeHandler;
290 connection.onError = errorHandler;
291 }
292
293 void errorHandlerServer(Exception e) {
294 Expect.fail("Server socket error");
295 }
296
297 waitForResult(Timer timer) {
298 // Make sure all iterations have been run. In multiple of these
299 // scenarios it is possible to get the SERVERSHUTDOWN message
300 // before we have received the last close event on the
301 // server. In these cases we wait for the correct number of
302 // close events.
303 if (_iterations == ITERATIONS &&
304 (_closeEvents == ITERATIONS || (_mode == 2 || _mode == 3))) {
305 switch (_mode) {
306 case 0:
307 Expect.equals(0, _dataEvents);
308 Expect.equals(ITERATIONS, _closeEvents);
309 break;
310 case 1:
311 Expect.equals(ITERATIONS, _dataEvents);
312 Expect.equals(ITERATIONS, _closeEvents);
313 break;
314 case 2:
315 case 3:
316 Expect.equals(ITERATIONS, _dataEvents);
317 Expect.equals(0, _closeEvents);
318 break;
319 case 4:
320 case 5:
321 case 6:
322 Expect.equals(ITERATIONS, _dataEvents);
323 Expect.equals(ITERATIONS, _closeEvents);
324 break;
325 default:
326 Expect.fail("Unknown test mode");
327 }
328 Expect.equals(0, _errorEvents);
329 _server.close();
330 this.port.close();
331 _donePort.send(null);
332 } else {
333 new Timer(100, waitForResult);
334 } 226 }
335 } 227 }
336 228
337 this.port.receive((message, SendPort replyTo) { 229 void writeHello() {
338 _donePort = replyTo; 230 int bytesWritten = 0;
339 if (message != SERVERSHUTDOWN) { 231 while (bytesWritten != 5) {
340 _readBytes = 0; 232 bytesWritten += connection.writeList("Hello".charCodes(),
341 _errorEvents = 0; 233 bytesWritten,
342 _dataEvents = 0; 234 5 - bytesWritten);
343 _closeEvents = 0;
344 _iterations = 0;
345 _mode = message;
346 _server = new ServerSocket(HOST, 0, 10);
347 Expect.equals(true, _server !== null);
348 _server.onConnection = (connection) {
349 var data = new ConnectionData(connection);
350 connectionHandler(data);
351 };
352 _server.onError = errorHandlerServer;
353 replyTo.send(_server.port, null);
354 } else {
355 new Timer(0, waitForResult);
356 } 235 }
357 }); 236 }
237
238 void dataHandler() {
239 switch (_mode) {
240 case 0:
241 Expect.fail("No data expected");
242 break;
243 case 1:
244 readBytes(() { _dataEvents++; });
245 break;
246 case 2:
247 readBytes(() {
248 _dataEvents++;
249 connection.close();
250 });
251 break;
252 case 3:
253 readBytes(() {
254 _dataEvents++;
255 writeHello();
256 connection.close();
257 });
258 break;
259 case 4:
260 readBytes(() {
261 _dataEvents++;
262 writeHello();
263 });
264 break;
265 case 5:
266 case 6:
267 readBytes(() {
268 _dataEvents++;
269 writeHello();
270 connection.close(true);
271 });
272 break;
273 default:
274 Expect.fail("Unknown test mode");
275 }
276 }
277
278 void closeHandler() {
279 _closeEvents++;
280 connection.close();
281 }
282
283 void errorHandler(Exception e) {
284 Expect.fail("Socket error $e");
285 }
286
287 _iterations++;
288
289 connection.onData = dataHandler;
290 connection.onClosed = closeHandler;
291 connection.onError = errorHandler;
292 }
293
294 void errorHandlerServer(Exception e) {
295 Expect.fail("Server socket error");
296 }
297
298 waitForResult(Timer timer) {
299 // Make sure all iterations have been run. In multiple of these
300 // scenarios it is possible to get the SERVERSHUTDOWN message
301 // before we have received the last close event on the
302 // server. In these cases we wait for the correct number of
303 // close events.
304 if (_iterations == ITERATIONS &&
305 (_closeEvents == ITERATIONS || (_mode == 2 || _mode == 3))) {
306 switch (_mode) {
307 case 0:
308 Expect.equals(0, _dataEvents);
309 Expect.equals(ITERATIONS, _closeEvents);
310 break;
311 case 1:
312 Expect.equals(ITERATIONS, _dataEvents);
313 Expect.equals(ITERATIONS, _closeEvents);
314 break;
315 case 2:
316 case 3:
317 Expect.equals(ITERATIONS, _dataEvents);
318 Expect.equals(0, _closeEvents);
319 break;
320 case 4:
321 case 5:
322 case 6:
323 Expect.equals(ITERATIONS, _dataEvents);
324 Expect.equals(ITERATIONS, _closeEvents);
325 break;
326 default:
327 Expect.fail("Unknown test mode");
328 }
329 Expect.equals(0, _errorEvents);
330 _server.close();
331 port.close();
332 _donePort.send(null);
333 } else {
334 new Timer(100, waitForResult);
335 }
336 }
337
338 void dispatch(message, SendPort replyTo) {
339 _donePort = replyTo;
340 if (message != SERVERSHUTDOWN) {
341 _readBytes = 0;
342 _errorEvents = 0;
343 _dataEvents = 0;
344 _closeEvents = 0;
345 _iterations = 0;
346 _mode = message;
347 _server = new ServerSocket(HOST, 0, 10);
348 Expect.equals(true, _server !== null);
349 _server.onConnection = (connection) {
350 var data = new ConnectionData(connection);
351 connectionHandler(data);
352 };
353 _server.onError = errorHandlerServer;
354 replyTo.send(_server.port, null);
355 } else {
356 new Timer(0, waitForResult);
357 }
358 } 358 }
359 359
360 ServerSocket _server; 360 ServerSocket _server;
361 SendPort _donePort; 361 SendPort _donePort;
362 int _readBytes; 362 int _readBytes;
363 int _errorEvents; 363 int _errorEvents;
364 int _dataEvents; 364 int _dataEvents;
365 int _closeEvents; 365 int _closeEvents;
366 int _iterations; 366 int _iterations;
367 int _mode; 367 int _mode;
(...skipping 12 matching lines...) Expand all
380 var tests = 7; 380 var tests = 7;
381 var port = new ReceivePort(); 381 var port = new ReceivePort();
382 var completed = 0; 382 var completed = 0;
383 port.receive((message, ignore) { 383 port.receive((message, ignore) {
384 if (++completed == tests) port.close(); 384 if (++completed == tests) port.close();
385 }); 385 });
386 for (var i = 0; i < tests; i++) { 386 for (var i = 0; i < tests; i++) {
387 new SocketClose.start(i, port.toSendPort()); 387 new SocketClose.start(i, port.toSendPort());
388 } 388 }
389 } 389 }
OLDNEW
« no previous file with comments | « tests/standalone/io/http_read_test.dart ('k') | tests/standalone/io/socket_many_connections_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698