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

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: Fix namespace comment. 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((message, replyTo) {
210 server.dispatch(message, replyTo);
kasperl 2012/08/03 05:20:27 !
211 });
212 }
213
214 class SocketCloseServer {
210 215
211 static final HOST = "127.0.0.1"; 216 static final HOST = "127.0.0.1";
212 217
213 SocketCloseServer() : super() {} 218 SocketCloseServer() : super() {}
214 219
215 void main() { 220 void connectionHandler(ConnectionData data) {
221 var connection = data.connection;
216 222
217 void connectionHandler(ConnectionData data) { 223 void readBytes(whenFiveBytes) {
218 var connection = data.connection; 224 List<int> b = new List<int>(5);
219 225 data.readBytes += connection.readList(b, 0, 5);
220 void readBytes(whenFiveBytes) { 226 if (data.readBytes == 5) {
221 List<int> b = new List<int>(5); 227 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 } 228 }
335 } 229 }
336 230
337 this.port.receive((message, SendPort replyTo) { 231 void writeHello() {
338 _donePort = replyTo; 232 int bytesWritten = 0;
339 if (message != SERVERSHUTDOWN) { 233 while (bytesWritten != 5) {
340 _readBytes = 0; 234 bytesWritten += connection.writeList("Hello".charCodes(),
341 _errorEvents = 0; 235 bytesWritten,
342 _dataEvents = 0; 236 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 } 237 }
357 }); 238 }
239
240 void dataHandler() {
241 switch (_mode) {
242 case 0:
243 Expect.fail("No data expected");
244 break;
245 case 1:
246 readBytes(() { _dataEvents++; });
247 break;
248 case 2:
249 readBytes(() {
250 _dataEvents++;
251 connection.close();
252 });
253 break;
254 case 3:
255 readBytes(() {
256 _dataEvents++;
257 writeHello();
258 connection.close();
259 });
260 break;
261 case 4:
262 readBytes(() {
263 _dataEvents++;
264 writeHello();
265 });
266 break;
267 case 5:
268 case 6:
269 readBytes(() {
270 _dataEvents++;
271 writeHello();
272 connection.close(true);
273 });
274 break;
275 default:
276 Expect.fail("Unknown test mode");
277 }
278 }
279
280 void closeHandler() {
281 _closeEvents++;
282 connection.close();
283 }
284
285 void errorHandler(Exception e) {
286 Expect.fail("Socket error $e");
287 }
288
289 _iterations++;
290
291 connection.onData = dataHandler;
292 connection.onClosed = closeHandler;
293 connection.onError = errorHandler;
294 }
295
296 void errorHandlerServer(Exception e) {
297 Expect.fail("Server socket error");
298 }
299
300 waitForResult(Timer timer) {
301 // Make sure all iterations have been run. In multiple of these
302 // scenarios it is possible to get the SERVERSHUTDOWN message
303 // before we have received the last close event on the
304 // server. In these cases we wait for the correct number of
305 // close events.
306 if (_iterations == ITERATIONS &&
307 (_closeEvents == ITERATIONS || (_mode == 2 || _mode == 3))) {
308 switch (_mode) {
309 case 0:
310 Expect.equals(0, _dataEvents);
311 Expect.equals(ITERATIONS, _closeEvents);
312 break;
313 case 1:
314 Expect.equals(ITERATIONS, _dataEvents);
315 Expect.equals(ITERATIONS, _closeEvents);
316 break;
317 case 2:
318 case 3:
319 Expect.equals(ITERATIONS, _dataEvents);
320 Expect.equals(0, _closeEvents);
321 break;
322 case 4:
323 case 5:
324 case 6:
325 Expect.equals(ITERATIONS, _dataEvents);
326 Expect.equals(ITERATIONS, _closeEvents);
327 break;
328 default:
329 Expect.fail("Unknown test mode");
330 }
331 Expect.equals(0, _errorEvents);
332 _server.close();
333 port.close();
334 _donePort.send(null);
335 } else {
336 new Timer(100, waitForResult);
337 }
338 }
339
340 void dispatch(message, SendPort replyTo) {
341 _donePort = replyTo;
342 if (message != SERVERSHUTDOWN) {
343 _readBytes = 0;
344 _errorEvents = 0;
345 _dataEvents = 0;
346 _closeEvents = 0;
347 _iterations = 0;
348 _mode = message;
349 _server = new ServerSocket(HOST, 0, 10);
350 Expect.equals(true, _server !== null);
351 _server.onConnection = (connection) {
352 var data = new ConnectionData(connection);
353 connectionHandler(data);
354 };
355 _server.onError = errorHandlerServer;
356 replyTo.send(_server.port, null);
357 } else {
358 new Timer(0, waitForResult);
359 }
358 } 360 }
359 361
360 ServerSocket _server; 362 ServerSocket _server;
361 SendPort _donePort; 363 SendPort _donePort;
362 int _readBytes; 364 int _readBytes;
363 int _errorEvents; 365 int _errorEvents;
364 int _dataEvents; 366 int _dataEvents;
365 int _closeEvents; 367 int _closeEvents;
366 int _iterations; 368 int _iterations;
367 int _mode; 369 int _mode;
(...skipping 12 matching lines...) Expand all
380 var tests = 7; 382 var tests = 7;
381 var port = new ReceivePort(); 383 var port = new ReceivePort();
382 var completed = 0; 384 var completed = 0;
383 port.receive((message, ignore) { 385 port.receive((message, ignore) {
384 if (++completed == tests) port.close(); 386 if (++completed == tests) port.close();
385 }); 387 });
386 for (var i = 0; i < tests; i++) { 388 for (var i = 0; i < tests; i++) {
387 new SocketClose.start(i, port.toSendPort()); 389 new SocketClose.start(i, port.toSendPort());
388 } 390 }
389 } 391 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698