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

Side by Side Diff: tests/standalone/src/SocketStreamCloseTest.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,
23 _readBytes = 0,
18 _dataEvents = 0, 24 _dataEvents = 0,
19 _closeEvents = 0, 25 _closeEvents = 0,
20 _errorEvents = 0, 26 _errorEvents = 0,
21 _iterations = 0, 27 _iterations = 0 {
22 _mode = mode {
23 new SocketCloseServer().spawn().then((SendPort port) { 28 new SocketCloseServer().spawn().then((SendPort port) {
24 _sendPort = port; 29 _sendPort = port;
25 start(); 30 start();
26 }); 31 });
27 } 32 }
28 33
29 void proceed() { 34 void proceed() {
30 if (_iterations < ITERATIONS) { 35 if (_iterations < ITERATIONS) {
31 new Timer(sendData, 0); 36 new Timer(sendData, 0);
32 } else { 37 } else {
33 shutdown(); 38 shutdown();
34 } 39 }
35 } 40 }
36 41
37 void sendData(Timer timer) { 42 void sendData(Timer timer) {
38 43
39 void dataHandler() { 44 void dataHandler() {
40 switch (_mode) { 45 switch (_mode) {
41 case 0: 46 case 0:
42 case 1: 47 case 1:
43 case 2: 48 case 2:
44 Expect.fail("No data expected"); 49 Expect.fail("No data expected");
45 break; 50 break;
46 case 3: 51 case 3:
47 case 4: 52 case 4:
48 case 5: 53 case 5:
49 case 6: 54 case 6:
50 List<int> b = new List<int>(100); 55 case 7:
51 _socket.readList(b, 0, 100); 56 case 8:
52 _dataEvents++; 57 var read = _socket.inputStream.read();
58 _readBytes += read.length;
59 if ((_readBytes % 5) == 0) {
60 _dataEvents++;
61 }
53 break; 62 break;
54 default: 63 default:
55 Expect.fail("Unknown test mode"); 64 Expect.fail("Unknown test mode");
56 } 65 }
57 } 66 }
58 67
59 void closeHandler() { 68 void closeHandler() {
60 _closeEvents++; 69 _closeEvents++;
61 switch (_mode) { 70 switch (_mode) {
62 case 0: 71 case 0:
63 case 1: 72 case 1:
64 break; 73 break;
65 case 2: 74 case 2:
66 case 3: 75 case 3:
76 case 4:
67 _socket.outputStream.close(); 77 _socket.outputStream.close();
68 proceed(); 78 proceed();
69 break; 79 break;
70 case 4: 80 case 5:
71 proceed(); 81 proceed();
72 break; 82 break;
73 case 5: 83 case 6:
74 _socket.outputStream.close(); 84 _socket.outputStream.close();
75 proceed(); 85 proceed();
76 break; 86 break;
77 case 6: 87 case 7:
88 case 8:
78 proceed(); 89 proceed();
79 break; 90 break;
80 default: 91 default:
81 Expect.fail("Unknown test mode"); 92 Expect.fail("Unknown test mode");
82 } 93 }
83 } 94 }
84 95
85 void errorHandler() { 96 void errorHandler() {
86 _errorEvents++; 97 _errorEvents++;
87 _socket.close(); 98 _socket.close();
88 } 99 }
89 100
90 void connectHandler() { 101 void connectHandler() {
91 _socket.inputStream.dataHandler = dataHandler; 102 _socket.inputStream.dataHandler = dataHandler;
92 _socket.inputStream.closeHandler = closeHandler; 103 _socket.inputStream.closeHandler = closeHandler;
93 _socket.errorHandler = errorHandler; 104 _socket.errorHandler = errorHandler;
94 105
95 _iterations++; 106 _iterations++;
96 switch (_mode) { 107 switch (_mode) {
97 case 0: 108 case 0:
98 _socket.inputStream.close(); 109 _socket.inputStream.close();
99 proceed(); 110 proceed();
100 break; 111 break;
101 case 1: 112 case 1:
102 _socket.outputStream.write("Hello".charCodes()); 113 _socket.outputStream.write("Hello".charCodes());
103 _socket.inputStream.close(); 114 _socket.outputStream.noPendingWriteHandler = () {
104 proceed(); 115 _socket.inputStream.close();
116 proceed();
117 };
105 break; 118 break;
106 case 2: 119 case 2:
107 case 3: 120 case 3:
108 _socket.outputStream.write("Hello".charCodes());
109 break;
110 case 4: 121 case 4:
111 _socket.outputStream.write("Hello".charCodes()); 122 _socket.outputStream.write("Hello".charCodes());
112 _socket.outputStream.close();
113 break; 123 break;
114 case 5: 124 case 5:
115 _socket.outputStream.write("Hello".charCodes()); 125 _socket.outputStream.write("Hello".charCodes());
126 _socket.outputStream.noPendingWriteHandler = () {
127 _socket.outputStream.close();
128 };
116 break; 129 break;
117 case 6: 130 case 6:
118 _socket.outputStream.write("Hello".charCodes()); 131 _socket.outputStream.write("Hello".charCodes());
119 _socket.outputStream.close(); 132 break;
133 case 7:
134 case 8:
135 _socket.outputStream.write("Hello".charCodes());
136 _socket.outputStream.noPendingWriteHandler = () {
137 _socket.outputStream.close();
138 };
120 break; 139 break;
121 default: 140 default:
122 Expect.fail("Unknown test mode"); 141 Expect.fail("Unknown test mode");
123 } 142 }
124 } 143 }
125 144
126 _socket = new Socket(SocketCloseServer.HOST, _port); 145 _socket = new Socket(SocketCloseServer.HOST, _port);
127 Expect.equals(true, _socket !== null); 146 Expect.equals(true, _socket !== null);
128 _socket.connectHandler = connectHandler; 147 _socket.connectHandler = connectHandler;
129 } 148 }
130 149
131 void start() { 150 void start() {
132 _receivePort.receive((var message, SendPort replyTo) { 151 _receivePort.receive((var message, SendPort replyTo) {
133 _port = message; 152 _port = message;
134 proceed(); 153 proceed();
135 }); 154 });
136 _sendPort.send(_mode, _receivePort.toSendPort()); 155 _sendPort.send(_mode, _receivePort.toSendPort());
137 } 156 }
138 157
139 void shutdown() { 158 void shutdown() {
140 _sendPort.send(SERVERSHUTDOWN, _receivePort.toSendPort()); 159 _sendPort.send(SERVERSHUTDOWN, _receivePort.toSendPort());
141 _receivePort.close(); 160 _receivePort.receive((message, ignore) {
161 _donePort.send(null);
162 _receivePort.close();
163 });
142 164
143 switch (_mode) { 165 switch (_mode) {
144 case 0: 166 case 0:
145 case 1: 167 case 1:
146 Expect.equals(0, _dataEvents); 168 Expect.equals(0, _dataEvents);
147 Expect.equals(10, _closeEvents); 169 Expect.equals(0, _closeEvents);
148 break; 170 break;
149 case 2: 171 case 2:
150 Expect.equals(0, _dataEvents); 172 Expect.equals(0, _dataEvents);
151 Expect.equals(ITERATIONS, _closeEvents); 173 Expect.equals(ITERATIONS, _closeEvents);
152 break; 174 break;
153 case 3: 175 case 3:
154 case 4:
155 case 5:
156 case 6:
157 Expect.equals(ITERATIONS, _dataEvents); 176 Expect.equals(ITERATIONS, _dataEvents);
158 Expect.equals(ITERATIONS, _closeEvents); 177 Expect.equals(ITERATIONS, _closeEvents);
159 break; 178 break;
179 case 4:
180 Expect.equals(ITERATIONS, _closeEvents);
181 break;
182 case 5:
183 case 6:
184 case 7:
185 case 8:
186 Expect.equals(ITERATIONS, _dataEvents);
187 Expect.equals(ITERATIONS, _closeEvents);
188 break;
160 default: 189 default:
161 Expect.fail("Unknown test mode"); 190 Expect.fail("Unknown test mode");
162 } 191 }
163 Expect.equals(0, _errorEvents); 192 Expect.equals(0, _errorEvents);
164 } 193 }
165 194
166 int _port; 195 int _port;
167 ReceivePort _receivePort; 196 ReceivePort _receivePort;
168 SendPort _sendPort; 197 SendPort _sendPort;
169 Socket _socket; 198 Socket _socket;
170 List<int> _buffer; 199 List<int> _buffer;
200 int _readBytes;
171 int _dataEvents; 201 int _dataEvents;
172 int _closeEvents; 202 int _closeEvents;
173 int _errorEvents; 203 int _errorEvents;
174 int _iterations; 204 int _iterations;
175 int _mode; 205 int _mode;
206 int _donePort;
176 } 207 }
177 208
209
210 class ConnectionData {
211 ConnectionData(Socket this.connection) : readBytes = 0;
212 Socket connection;
213 int readBytes;
214 }
215
216
178 class SocketCloseServer extends Isolate { 217 class SocketCloseServer extends Isolate {
179 218
180 static final HOST = "127.0.0.1"; 219 static final HOST = "127.0.0.1";
181 220
182 SocketCloseServer() : super() {} 221 SocketCloseServer() : super() {}
183 222
184 void main() { 223 void main() {
185 224
186 void connectionHandler(Socket connection) { 225 void connectionHandler(ConnectionData data) {
226 var connection = data.connection;
227
228 void readBytes(whenFiveBytes) {
229 var read = connection.inputStream.read();
230 data.readBytes += read.length;
231 if (data.readBytes == 5) {
232 whenFiveBytes();
233 }
234 }
187 235
188 void dataHandler() { 236 void dataHandler() {
189 _dataEvents++;
190 switch (_mode) { 237 switch (_mode) {
191 case 0: 238 case 0:
192 Expect.fail("No data expected"); 239 Expect.fail("No data expected");
193 break; 240 break;
194 case 1: 241 case 1:
195 connection.inputStream.read(); 242 readBytes(() {
243 _dataEvents++;
244 });
196 break; 245 break;
197 case 2: 246 case 2:
198 connection.inputStream.read(); 247 readBytes(() {
199 connection.inputStream.close(); 248 _dataEvents++;
249 connection.inputStream.close();
250 });
200 break; 251 break;
201 case 3: 252 case 3:
202 connection.inputStream.read(); 253 readBytes(() {
203 connection.outputStream.write("Hello".charCodes()); 254 _dataEvents++;
204 connection.inputStream.close(); 255 connection.outputStream.write("Hello".charCodes());
205 //connection.outputStream.close(); 256 connection.outputStream.noPendingWriteHandler = () {
257 connection.inputStream.close();
258 };
259 });
206 break; 260 break;
207 case 4: 261 case 4:
208 connection.inputStream.read(); 262 readBytes(() {
209 connection.outputStream.write("Hello".charCodes()); 263 _dataEvents++;
264 connection.outputStream.write("Hello".charCodes());
265 connection.inputStream.close();
266 });
210 break; 267 break;
211 case 5: 268 case 5:
269 readBytes(() {
270 _dataEvents++;
271 connection.outputStream.write("Hello".charCodes());
272 });
273 break;
212 case 6: 274 case 6:
213 connection.inputStream.read(); 275 case 7:
214 connection.outputStream.write("Hello".charCodes()); 276 readBytes(() {
215 connection.outputStream.close(); 277 _dataEvents++;
278 connection.outputStream.write("Hello".charCodes());
279 connection.outputStream.noPendingWriteHandler = () {
280 connection.outputStream.close();
281 };
282 });
283 break;
284 case 8:
285 readBytes(() {
286 _dataEvents++;
287 connection.outputStream.write("Hello".charCodes());
288 connection.outputStream.close();
289 });
216 break; 290 break;
217 default: 291 default:
218 Expect.fail("Unknown test mode"); 292 Expect.fail("Unknown test mode");
219 } 293 }
220 } 294 }
221 295
222 void closeHandler() { 296 void closeHandler() {
223 _closeEvents++; 297 _closeEvents++;
224 connection.close(); 298 connection.outputStream.close();
225 } 299 }
226 300
227 void errorHandler() { 301 void errorHandler() {
228 Expect.fail("Socket error"); 302 Expect.fail("Socket error");
229 } 303 }
230 304
231 _iterations++; 305 _iterations++;
232 306
233 connection.inputStream.dataHandler = dataHandler; 307 connection.inputStream.dataHandler = dataHandler;
234 connection.inputStream.closeHandler = closeHandler; 308 connection.inputStream.closeHandler = closeHandler;
235 connection.errorHandler = errorHandler; 309 connection.errorHandler = errorHandler;
236 } 310 }
237 311
238 void errorHandlerServer() { 312 void errorHandlerServer() {
239 Expect.fail("Server socket error"); 313 Expect.fail("Server socket error");
240 } 314 }
241 315
242 waitForResult(Timer timer) { 316 waitForResult(Timer timer) {
243 // Make sure all iterations have been run. In multiple of these 317 // Make sure all iterations have been run. In multiple of these
244 // scenarios it is possible to get the SERVERSHUTDOWN message 318 // scenarios it is possible to get the SERVERSHUTDOWN message
245 // before we have received the last close event on the 319 // before we have received the last close event on the
246 // server. We therefore always wait for the correct number of 320 // server. In these cases we wait for the correct number of
247 // close events. 321 // close events.
248 if (_iterations == ITERATIONS && _closeEvents == ITERATIONS) { 322 if (_iterations == ITERATIONS &&
323 (_closeEvents == ITERATIONS ||
324 (_mode == 2 || _mode == 3 || _mode == 4))) {
249 switch (_mode) { 325 switch (_mode) {
250 case 0: 326 case 0:
251 Expect.equals(0, _dataEvents); 327 Expect.equals(0, _dataEvents);
252 Expect.equals(ITERATIONS, _closeEvents); 328 Expect.equals(ITERATIONS, _closeEvents);
253 break; 329 break;
254 case 1: 330 case 1:
255 Expect.equals(ITERATIONS, _dataEvents); 331 Expect.equals(ITERATIONS, _dataEvents);
256 Expect.equals(ITERATIONS, _closeEvents); 332 Expect.equals(ITERATIONS, _closeEvents);
257 break; 333 break;
258 case 2: 334 case 2:
259 case 3: 335 case 3:
336 case 4:
337 Expect.equals(ITERATIONS, _dataEvents);
338 Expect.equals(0, _closeEvents);
339 break;
340 case 5:
341 case 6:
342 case 7:
343 case 8:
260 Expect.equals(ITERATIONS, _dataEvents); 344 Expect.equals(ITERATIONS, _dataEvents);
261 Expect.equals(ITERATIONS, _closeEvents); 345 Expect.equals(ITERATIONS, _closeEvents);
262 break; 346 break;
263 case 4:
264 case 5:
265 case 6:
266 Expect.equals(ITERATIONS, _dataEvents);
267 Expect.equals(ITERATIONS, _closeEvents);
268 break;
269 default: 347 default:
270 Expect.fail("Unknown test mode"); 348 Expect.fail("Unknown test mode");
271 } 349 }
272 Expect.equals(0, _errorEvents); 350 Expect.equals(0, _errorEvents);
273 _server.close(); 351 _server.close();
274 this.port.close(); 352 this.port.close();
353 _donePort.send(null);
275 } else { 354 } else {
276 new Timer(waitForResult, 100); 355 new Timer(waitForResult, 100);
277 } 356 }
278 } 357 }
279 358
280 this.port.receive((message, SendPort replyTo) { 359 this.port.receive((message, SendPort replyTo) {
360 _donePort = replyTo;
281 if (message != SERVERSHUTDOWN) { 361 if (message != SERVERSHUTDOWN) {
362 _readBytes = 0;
282 _errorEvents = 0; 363 _errorEvents = 0;
283 _dataEvents = 0; 364 _dataEvents = 0;
284 _closeEvents = 0; 365 _closeEvents = 0;
285 _iterations = 0; 366 _iterations = 0;
286 _mode = message; 367 _mode = message;
287 _server = new ServerSocket(HOST, 0, 10); 368 _server = new ServerSocket(HOST, 0, 10);
288 Expect.equals(true, _server !== null); 369 Expect.equals(true, _server !== null);
289 _server.connectionHandler = connectionHandler; 370 _server.connectionHandler = (connection) {
371 var data = new ConnectionData(connection);
372 connectionHandler(data);
373 };
290 _server.errorHandler = errorHandlerServer; 374 _server.errorHandler = errorHandlerServer;
291 replyTo.send(_server.port, null); 375 replyTo.send(_server.port, null);
292 } else { 376 } else {
293 new Timer(waitForResult, 0); 377 new Timer(waitForResult, 0);
294 } 378 }
295 }); 379 });
296 } 380 }
297 381
298 ServerSocket _server; 382 ServerSocket _server;
383 SendPort _donePort;
384 int _readBytes;
299 int _errorEvents; 385 int _errorEvents;
300 int _dataEvents; 386 int _dataEvents;
301 int _closeEvents; 387 int _closeEvents;
302 int _iterations; 388 int _iterations;
303 int _mode; 389 int _mode;
304 } 390 }
305 391
306 392
307 main() { 393 main() {
308 // Run the close test in these different "modes". 394 // Run the close test in these different "modes".
309 // 0: Client closes without sending at all. 395 // 0: Client closes without sending at all.
310 // 1: Client sends and closes. 396 // 1: Client sends and closes.
311 // 2: Client sends. Server closes. 397 // 2: Client sends. Server closes.
312 // 3: Client sends. Server responds and closes. 398 // 3: Client sends. Server responds and closes.
313 // 4: Client sends and half-closes. Server responds and closes. 399 // 4: Client sends. Server responds and closes without waiting for everything
314 // 5: Client sends. Server responds and half closes. 400 // being sent.
315 // 6: Client sends and half-closes. Server responds and half closes. 401 // 5: Client sends and half-closes. Server responds and closes.
316 new SocketClose.start(0); 402 // 6: Client sends. Server responds and half closes.
317 new SocketClose.start(1); 403 // 7: Client sends and half-closes. Server responds and half closes.
318 new SocketClose.start(2); 404 // 8: Client sends and half-closes. Server responds and half closes without
319 new SocketClose.start(3); 405 // explicitly waiting for everything being sent.
320 new SocketClose.start(4); 406 var tests = 9;
321 new SocketClose.start(5); 407 var port = new ReceivePort();
322 new SocketClose.start(6); 408 var completed = 0;
409 port.receive((message, ignore) {
410 if (++completed == tests) port.close();
411 });
412 for (var i = 0; i < tests; i++) {
413 new SocketClose.start(i, port.toSendPort());
414 }
323 } 415 }
OLDNEW
« runtime/bin/socket_stream_impl.dart ('K') | « tests/standalone/src/SocketCloseTest.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698