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

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

Issue 27215002: Very simple version of Isolates. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Address comments. Created 7 years, 1 month 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) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, 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:async'; 12 import 'dart:async';
13 import 'dart:io'; 13 import 'dart:io';
14 import 'dart:isolate'; 14 import 'dart:isolate';
15 15
16 import "package:async_helper/async_helper.dart"; 16 import "package:async_helper/async_helper.dart";
17 import "package:expect/expect.dart"; 17 import "package:expect/expect.dart";
18 18
19 const SERVERSHUTDOWN = -1; 19 const SERVERSHUTDOWN = -1;
20 const ITERATIONS = 10; 20 const ITERATIONS = 10;
21 21
22 22
23 Future sendReceive(SendPort port, message) {
24 ReceivePort receivePort = new ReceivePort();
25 port.send([message, receivePort.sendPort]);
26 return receivePort.first;
27 }
28
23 class SocketClose { 29 class SocketClose {
24 30
25 SocketClose.start(this._mode, this._done) 31 SocketClose.start(this._mode, this._done)
26 : _receivePort = new ReceivePort(), 32 : _sendPort = null,
27 _sendPort = null,
28 _readBytes = 0, 33 _readBytes = 0,
29 _dataEvents = 0, 34 _dataEvents = 0,
30 _closeEvents = 0, 35 _closeEvents = 0,
31 _errorEvents = 0, 36 _errorEvents = 0,
32 _iterations = 0 { 37 _iterations = 0 {
33 _sendPort = spawnFunction(startSocketCloseServer);
34 initialize(); 38 initialize();
35 } 39 }
36 40
37 void proceed() { 41 void proceed() {
38 if (_iterations < ITERATIONS) { 42 if (_iterations < ITERATIONS) {
39 Timer.run(sendData); 43 Timer.run(sendData);
40 } else { 44 } else {
41 shutdown(); 45 shutdown();
42 } 46 }
43 } 47 }
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 break; 139 break;
136 default: 140 default:
137 Expect.fail("Unknown test mode"); 141 Expect.fail("Unknown test mode");
138 } 142 }
139 } 143 }
140 144
141 Socket.connect(SocketCloseServer.HOST, _port).then(connectHandler); 145 Socket.connect(SocketCloseServer.HOST, _port).then(connectHandler);
142 } 146 }
143 147
144 void initialize() { 148 void initialize() {
145 _receivePort.receive((var message, SendPort replyTo) { 149 ReceivePort receivePort = new ReceivePort();
146 _port = message; 150 var remote = Isolate.spawn(startSocketCloseServer, receivePort.sendPort);
147 proceed(); 151
152 receivePort.first.then((message) {
153 this._sendPort = message;
154 sendReceive(_sendPort, _mode).then((int port) {
155 this._port = port;
156 proceed();
157 });
148 }); 158 });
149 _sendPort.send(_mode, _receivePort.toSendPort());
150 } 159 }
151 160
152 void shutdown() { 161 void shutdown() {
153 _sendPort.send(SERVERSHUTDOWN, _receivePort.toSendPort()); 162 sendReceive(_sendPort, SERVERSHUTDOWN).then((_) { _done(); });
154 _receivePort.receive((message, ignore) {
155 _done();
156 _receivePort.close();
157 });
158 163
159 switch (_mode) { 164 switch (_mode) {
160 case 0: 165 case 0:
161 case 1: 166 case 1:
162 Expect.equals(0, _dataEvents); 167 Expect.equals(0, _dataEvents);
163 Expect.equals(ITERATIONS, _closeEvents); 168 Expect.equals(ITERATIONS, _closeEvents);
164 break; 169 break;
165 case 2: 170 case 2:
166 Expect.equals(0, _dataEvents); 171 Expect.equals(0, _dataEvents);
167 Expect.equals(ITERATIONS, _closeEvents); 172 Expect.equals(ITERATIONS, _closeEvents);
168 break; 173 break;
169 case 3: 174 case 3:
170 case 4: 175 case 4:
171 Expect.isTrue(_dataEvents <= ITERATIONS); 176 Expect.isTrue(_dataEvents <= ITERATIONS);
172 Expect.isTrue(_dataEvents >= 0); 177 Expect.isTrue(_dataEvents >= 0);
173 Expect.equals(ITERATIONS, _closeEvents); 178 Expect.equals(ITERATIONS, _closeEvents);
174 break; 179 break;
175 case 5: 180 case 5:
176 case 6: 181 case 6:
177 Expect.equals(ITERATIONS, _dataEvents); 182 Expect.equals(ITERATIONS, _dataEvents);
178 Expect.equals(ITERATIONS, _closeEvents); 183 Expect.equals(ITERATIONS, _closeEvents);
179 break; 184 break;
180 default: 185 default:
181 Expect.fail("Unknown test mode"); 186 Expect.fail("Unknown test mode");
182 } 187 }
183 Expect.equals(0, _errorEvents); 188 Expect.equals(0, _errorEvents);
184 } 189 }
185 190
186 int _port; 191 int _port;
187 ReceivePort _receivePort;
188 SendPort _sendPort; 192 SendPort _sendPort;
189 List<int> _buffer; 193 List<int> _buffer;
190 int _readBytes; 194 int _readBytes;
191 int _dataEvents; 195 int _dataEvents;
192 int _closeEvents; 196 int _closeEvents;
193 int _errorEvents; 197 int _errorEvents;
194 int _iterations; 198 int _iterations;
195 int _mode; 199 int _mode;
196 Function _done; 200 Function _done;
197 } 201 }
198 202
199 203
200 class ConnectionData { 204 class ConnectionData {
201 ConnectionData(Socket this.connection) : readBytes = 0; 205 ConnectionData(Socket this.connection) : readBytes = 0;
202 Socket connection; 206 Socket connection;
203 int readBytes; 207 int readBytes;
204 } 208 }
205 209
206 210
207 void startSocketCloseServer() { 211 void startSocketCloseServer(SendPort replyTo) {
208 var server = new SocketCloseServer(); 212 var server = new SocketCloseServer();
209 port.receive(server.dispatch); 213 replyTo.send(server.dispatchSendPort);
210 } 214 }
211 215
212 class SocketCloseServer { 216 class SocketCloseServer {
213 217
214 static const HOST = "127.0.0.1"; 218 static const HOST = "127.0.0.1";
215 219
216 SocketCloseServer() : super() {} 220 SocketCloseServer() : _dispatchPort = new ReceivePort() {
221 _dispatchPort.listen(dispatch);
222 }
217 223
218 void connectionHandler(ConnectionData data) { 224 void connectionHandler(ConnectionData data) {
219 var connection = data.connection; 225 var connection = data.connection;
220 226
221 void readBytes(bytes, whenFiveBytes) { 227 void readBytes(bytes, whenFiveBytes) {
222 data.readBytes += bytes.length; 228 data.readBytes += bytes.length;
223 Expect.isTrue(data.readBytes <= 5); 229 Expect.isTrue(data.readBytes <= 5);
224 if (data.readBytes == 5) { 230 if (data.readBytes == 5) {
225 whenFiveBytes(); 231 whenFiveBytes();
226 } 232 }
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
318 case 5: 324 case 5:
319 case 6: 325 case 6:
320 Expect.equals(ITERATIONS, _dataEvents); 326 Expect.equals(ITERATIONS, _dataEvents);
321 Expect.equals(ITERATIONS, _closeEvents); 327 Expect.equals(ITERATIONS, _closeEvents);
322 break; 328 break;
323 default: 329 default:
324 Expect.fail("Unknown test mode"); 330 Expect.fail("Unknown test mode");
325 } 331 }
326 Expect.equals(0, _errorEvents); 332 Expect.equals(0, _errorEvents);
327 _server.close(); 333 _server.close();
328 port.close(); 334 _dispatchPort.close();
329 _donePort.send(null); 335 _donePort.send(null);
330 } else { 336 } else {
331 new Timer(new Duration(milliseconds: 100), waitForResult); 337 new Timer(new Duration(milliseconds: 100), waitForResult);
332 } 338 }
333 } 339 }
334 340
335 void dispatch(message, SendPort replyTo) { 341 SendPort get dispatchSendPort => _dispatchPort.sendPort;
342
343 void dispatch(message) {
344 var command = message[0];
345 SendPort replyTo = message[1];
336 _donePort = replyTo; 346 _donePort = replyTo;
337 if (message != SERVERSHUTDOWN) { 347 if (command != SERVERSHUTDOWN) {
338 _readBytes = 0; 348 _readBytes = 0;
339 _errorEvents = 0; 349 _errorEvents = 0;
340 _dataEvents = 0; 350 _dataEvents = 0;
341 _closeEvents = 0; 351 _closeEvents = 0;
342 _iterations = 0; 352 _iterations = 0;
343 _mode = message; 353 _mode = command;
344 ServerSocket.bind("127.0.0.1", 0).then((server) { 354 ServerSocket.bind("127.0.0.1", 0).then((server) {
345 _server = server; 355 _server = server;
346 _server.listen( 356 _server.listen(
347 (socket) { 357 (socket) {
348 var data = new ConnectionData(socket); 358 var data = new ConnectionData(socket);
349 connectionHandler(data); 359 connectionHandler(data);
350 }, 360 },
351 onError: errorHandlerServer 361 onError: errorHandlerServer
352 ); 362 );
353 replyTo.send(_server.port, null); 363 replyTo.send(_server.port, null);
354 }); 364 });
355 } else { 365 } else {
356 Timer.run(waitForResult); 366 Timer.run(waitForResult);
357 } 367 }
358 } 368 }
359 369
360 ServerSocket _server; 370 ServerSocket _server;
371 final ReceivePort _dispatchPort;
361 SendPort _donePort; 372 SendPort _donePort;
362 int _readBytes; 373 int _readBytes;
363 int _errorEvents; 374 int _errorEvents;
364 int _dataEvents; 375 int _dataEvents;
365 int _closeEvents; 376 int _closeEvents;
366 int _iterations; 377 int _iterations;
367 int _mode; 378 int _mode;
368 } 379 }
369 380
370 381
371 main() { 382 main() {
372 // Run the close test in these different "modes". 383 // Run the close test in these different "modes".
373 // 0: Client closes without sending at all. 384 // 0: Client closes without sending at all.
374 // 1: Client sends and destroys. 385 // 1: Client sends and destroys.
375 // 2: Client sends. Server destroys. 386 // 2: Client sends. Server destroys.
376 // 3: Client sends. Server responds and destroys. 387 // 3: Client sends. Server responds and destroys.
377 // 4: Client sends and half-closes. Server responds and destroys. 388 // 4: Client sends and half-closes. Server responds and destroys.
378 // 5: Client sends. Server responds and half closes. 389 // 5: Client sends. Server responds and half closes.
379 // 6: Client sends and half-closes. Server responds and half closes. 390 // 6: Client sends and half-closes. Server responds and half closes.
380 var tests = 7; 391 var tests = 7;
381 for (var i = 0; i < tests; i++) { 392 for (var i = 0; i < tests; i++) {
382 asyncStart(); 393 asyncStart();
383 new SocketClose.start(i, asyncEnd); 394 new SocketClose.start(i, asyncEnd);
384 } 395 }
385 } 396 }
OLDNEW
« no previous file with comments | « tests/standalone/io/process_shell_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