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 9029001: Add close to input stream and cleanup socket and streams (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Addressed comments by ager@ Created 8 years, 11 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
« no previous file with comments | « tests/standalone/src/SocketCloseTest.dart ('k') | tests/stub-generator/test_config.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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 // Test socket close events. 5 // Test socket close events.
6 6
7 7
8 final SERVERSHUTDOWN = -1; 8 final SERVERSHUTDOWN = -1;
9 final ITERATIONS = 10; 9 final ITERATIONS = 10;
10 10
11 11
12 // Run the close test in these different "modes".
13 // 0: Client closes without sending at all.
14 // 1: Client sends and closes.
15 // 2: Client sends. Server closes.
16 // 3: Client sends. Server responds and closes.
17 // 4: Client sends and half-closes. Server responds and closes.
18 // 5: Client sends. Server responds and half closes.
19 // 6: Client sends and half-closes. Server responds and half closes.
20 class SocketCloseTest {
21 static void testMain() {
22 new SocketClose.start(0);
23 new SocketClose.start(1);
24 new SocketClose.start(2);
25 new SocketClose.start(3);
26 new SocketClose.start(4);
27 new SocketClose.start(5);
28 new SocketClose.start(6);
29 }
30 }
31
32
33 class SocketClose { 12 class SocketClose {
34 13
35 SocketClose.start(mode) 14 SocketClose.start(mode)
36 : _receivePort = new ReceivePort(), 15 : _receivePort = new ReceivePort(),
37 _sendPort = null, 16 _sendPort = null,
38 _dataEvents = 0, 17 _dataEvents = 0,
39 _closeEvents = 0, 18 _closeEvents = 0,
40 _errorEvents = 0, 19 _errorEvents = 0,
41 _iterations = 0, 20 _iterations = 0,
42 _mode = mode { 21 _mode = mode {
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 default: 53 default:
75 Expect.fail("Unknown test mode"); 54 Expect.fail("Unknown test mode");
76 } 55 }
77 } 56 }
78 57
79 void closeHandler() { 58 void closeHandler() {
80 _closeEvents++; 59 _closeEvents++;
81 switch (_mode) { 60 switch (_mode) {
82 case 0: 61 case 0:
83 case 1: 62 case 1:
84 Expect.fail("No close expected");
85 break; 63 break;
86 case 2: 64 case 2:
87 case 3: 65 case 3:
88 _socket.close(); 66 _socket.outputStream.close();
89 proceed(); 67 proceed();
90 break; 68 break;
91 case 4: 69 case 4:
92 proceed(); 70 proceed();
93 break; 71 break;
94 case 5: 72 case 5:
95 _socket.close(); 73 _socket.outputStream.close();
96 proceed(); 74 proceed();
97 break; 75 break;
98 case 6: 76 case 6:
99 proceed(); 77 proceed();
100 break; 78 break;
101 default: 79 default:
102 Expect.fail("Unknown test mode"); 80 Expect.fail("Unknown test mode");
103 } 81 }
104 } 82 }
105 83
106 void errorHandler() { 84 void errorHandler() {
107 _errorEvents++; 85 _errorEvents++;
108 _socket.close(); 86 _socket.close();
109 } 87 }
110 88
111 void connectHandler() { 89 void connectHandler() {
112 _socket.dataHandler = dataHandler; 90 _socket.inputStream.dataHandler = dataHandler;
113 _socket.closeHandler = closeHandler; 91 _socket.inputStream.closeHandler = closeHandler;
114 _socket.errorHandler = errorHandler; 92 _socket.errorHandler = errorHandler;
115 93
116 _iterations++; 94 _iterations++;
117 switch (_mode) { 95 switch (_mode) {
118 case 0: 96 case 0:
119 _socket.close(); 97 _socket.inputStream.close();
120 proceed(); 98 proceed();
121 break; 99 break;
122 case 1: 100 case 1:
123 int bytesWritten = _socket.writeList("Hello".charCodes(), 0, 5); 101 _socket.outputStream.write("Hello".charCodes());
124 Expect.equals(5, bytesWritten); 102 _socket.inputStream.close();
125 _socket.close();
126 proceed(); 103 proceed();
127 break; 104 break;
128 case 2: 105 case 2:
129 case 3: 106 case 3:
130 int bytesWritten = _socket.writeList("Hello".charCodes(), 0, 5); 107 _socket.outputStream.write("Hello".charCodes());
131 Expect.equals(5, bytesWritten);
132 break; 108 break;
133 case 4: 109 case 4:
134 int bytesWritten = _socket.writeList("Hello".charCodes(), 0, 5); 110 _socket.outputStream.write("Hello".charCodes());
135 Expect.equals(5, bytesWritten); 111 _socket.outputStream.close();
136 _socket.close(true);
137 break; 112 break;
138 case 5: 113 case 5:
139 int bytesWritten = _socket.writeList("Hello".charCodes(), 0, 5); 114 _socket.outputStream.write("Hello".charCodes());
140 Expect.equals(5, bytesWritten);
141 break; 115 break;
142 case 6: 116 case 6:
143 int bytesWritten = _socket.writeList("Hello".charCodes(), 0, 5); 117 _socket.outputStream.write("Hello".charCodes());
144 Expect.equals(5, bytesWritten); 118 _socket.outputStream.close();
145 _socket.close(true);
146 break; 119 break;
147 default: 120 default:
148 Expect.fail("Unknown test mode"); 121 Expect.fail("Unknown test mode");
149 } 122 }
150 } 123 }
151 124
152 _socket = new Socket(SocketCloseServer.HOST, _port); 125 _socket = new Socket(SocketCloseServer.HOST, _port);
153 Expect.equals(true, _socket !== null); 126 Expect.equals(true, _socket !== null);
154 _socket.connectHandler = connectHandler; 127 _socket.connectHandler = connectHandler;
155 } 128 }
156 129
157 void start() { 130 void start() {
158 _receivePort.receive((var message, SendPort replyTo) { 131 _receivePort.receive((var message, SendPort replyTo) {
159 _port = message; 132 _port = message;
160 proceed(); 133 proceed();
161 }); 134 });
162 _sendPort.send(_mode, _receivePort.toSendPort()); 135 _sendPort.send(_mode, _receivePort.toSendPort());
163 } 136 }
164 137
165 void shutdown() { 138 void shutdown() {
166 _sendPort.send(SERVERSHUTDOWN, _receivePort.toSendPort()); 139 _sendPort.send(SERVERSHUTDOWN, _receivePort.toSendPort());
167 _receivePort.close(); 140 _receivePort.close();
168 141
169 switch (_mode) { 142 switch (_mode) {
170 case 0: 143 case 0:
171 case 1: 144 case 1:
172 Expect.equals(0, _dataEvents); 145 Expect.equals(0, _dataEvents);
173 Expect.equals(0, _closeEvents); 146 Expect.equals(10, _closeEvents);
174 break; 147 break;
175 case 2: 148 case 2:
176 Expect.equals(0, _dataEvents); 149 Expect.equals(0, _dataEvents);
177 Expect.equals(ITERATIONS, _closeEvents); 150 Expect.equals(ITERATIONS, _closeEvents);
178 break; 151 break;
179 case 3: 152 case 3:
180 case 4: 153 case 4:
181 case 5: 154 case 5:
182 case 6: 155 case 6:
183 Expect.equals(ITERATIONS, _dataEvents); 156 Expect.equals(ITERATIONS, _dataEvents);
(...skipping 27 matching lines...) Expand all
211 184
212 void connectionHandler(Socket connection) { 185 void connectionHandler(Socket connection) {
213 186
214 void dataHandler() { 187 void dataHandler() {
215 _dataEvents++; 188 _dataEvents++;
216 switch (_mode) { 189 switch (_mode) {
217 case 0: 190 case 0:
218 Expect.fail("No data expected"); 191 Expect.fail("No data expected");
219 break; 192 break;
220 case 1: 193 case 1:
221 List<int> b = new List<int>(100); 194 connection.inputStream.read();
222 connection.readList(b, 0, 100);
223 break; 195 break;
224 case 2: 196 case 2:
225 List<int> b = new List<int>(100); 197 connection.inputStream.read();
226 connection.readList(b, 0, 100); 198 connection.inputStream.close();
227 connection.close();
228 break; 199 break;
229 case 3: 200 case 3:
230 List<int> b = new List<int>(100); 201 connection.inputStream.read();
231 connection.readList(b, 0, 100); 202 connection.outputStream.write("Hello".charCodes());
232 connection.writeList("Hello".charCodes(), 0, 5); 203 connection.inputStream.close();
233 connection.close(); 204 //connection.outputStream.close();
234 break; 205 break;
235 case 4: 206 case 4:
236 List<int> b = new List<int>(100); 207 connection.inputStream.read();
237 connection.readList(b, 0, 100); 208 connection.outputStream.write("Hello".charCodes());
238 connection.writeList("Hello".charCodes(), 0, 5);
239 break; 209 break;
240 case 5: 210 case 5:
241 case 6: 211 case 6:
242 List<int> b = new List<int>(100); 212 connection.inputStream.read();
243 connection.readList(b, 0, 100); 213 connection.outputStream.write("Hello".charCodes());
244 connection.writeList("Hello".charCodes(), 0, 5); 214 connection.outputStream.close();
245 connection.close(true);
246 break; 215 break;
247 default: 216 default:
248 Expect.fail("Unknown test mode"); 217 Expect.fail("Unknown test mode");
249 } 218 }
250 } 219 }
251 220
252 void closeHandler() { 221 void closeHandler() {
253 _closeEvents++; 222 _closeEvents++;
254 connection.close(); 223 connection.close();
255 } 224 }
256 225
257 void errorHandler() { 226 void errorHandler() {
258 Expect.fail("Socket error"); 227 Expect.fail("Socket error");
259 } 228 }
260 229
261 _iterations++; 230 _iterations++;
262 231
263 connection.dataHandler = dataHandler; 232 connection.inputStream.dataHandler = dataHandler;
264 connection.closeHandler = closeHandler; 233 connection.inputStream.closeHandler = closeHandler;
265 connection.errorHandler = errorHandler; 234 connection.errorHandler = errorHandler;
266 } 235 }
267 236
268 void errorHandlerServer() { 237 void errorHandlerServer() {
269 Expect.fail("Server socket error"); 238 Expect.fail("Server socket error");
270 } 239 }
271 240
272 waitForResult(Timer timer) { 241 waitForResult(Timer timer) {
273 // Make sure all iterations have been run. For mode 0 and 1 the 242 // Make sure all iterations have been run. For mode 0 and 1 the
274 // client just closes the socket and after the last iteration 243 // client just closes the socket and after the last iteration
275 // signals the server. The server might now be finished just 244 // signals the server. The server might now be finished just
276 // because iterations have reached the limit as this number is 245 // because iterations have reached the limit as this number is
277 // incremented just after accept. In that case wait for the last 246 // incremented just after accept. In that case wait for the last
278 // close event. 247 // close event.
279 if (_iterations == ITERATIONS && 248 if (_iterations == ITERATIONS &&
280 (_mode > 1 || _closeEvents == ITERATIONS)) { 249 (_mode > 1 || _closeEvents == ITERATIONS)) {
281 switch (_mode) { 250 switch (_mode) {
282 case 0: 251 case 0:
283 Expect.equals(0, _dataEvents); 252 Expect.equals(0, _dataEvents);
284 Expect.equals(ITERATIONS, _closeEvents); 253 Expect.equals(ITERATIONS, _closeEvents);
285 break; 254 break;
286 case 1: 255 case 1:
287 Expect.equals(ITERATIONS, _dataEvents); 256 Expect.equals(ITERATIONS, _dataEvents);
288 Expect.equals(ITERATIONS, _closeEvents); 257 Expect.equals(ITERATIONS, _closeEvents);
289 break; 258 break;
290 case 2: 259 case 2:
291 case 3: 260 case 3:
292 Expect.equals(ITERATIONS, _dataEvents); 261 Expect.equals(ITERATIONS, _dataEvents);
293 Expect.equals(0, _closeEvents); 262 Expect.equals(ITERATIONS, _closeEvents);
294 break; 263 break;
295 case 4: 264 case 4:
296 case 5: 265 case 5:
297 case 6: 266 case 6:
298 Expect.equals(ITERATIONS, _dataEvents); 267 Expect.equals(ITERATIONS, _dataEvents);
299 Expect.equals(ITERATIONS, _closeEvents); 268 Expect.equals(ITERATIONS, _closeEvents);
300 break; 269 break;
301 default: 270 default:
302 Expect.fail("Unknown test mode"); 271 Expect.fail("Unknown test mode");
303 } 272 }
(...skipping 26 matching lines...) Expand all
330 ServerSocket _server; 299 ServerSocket _server;
331 int _errorEvents; 300 int _errorEvents;
332 int _dataEvents; 301 int _dataEvents;
333 int _closeEvents; 302 int _closeEvents;
334 int _iterations; 303 int _iterations;
335 int _mode; 304 int _mode;
336 } 305 }
337 306
338 307
339 main() { 308 main() {
340 SocketCloseTest.testMain(); 309 // Run the close test in these different "modes".
310 // 0: Client closes without sending at all.
311 // 1: Client sends and closes.
312 // 2: Client sends. Server closes.
313 // 3: Client sends. Server responds and closes.
314 // 4: Client sends and half-closes. Server responds and closes.
315 // 5: Client sends. Server responds and half closes.
316 // 6: Client sends and half-closes. Server responds and half closes.
317 new SocketClose.start(0);
318 new SocketClose.start(1);
319 new SocketClose.start(2);
320 new SocketClose.start(3);
321 new SocketClose.start(4);
322 new SocketClose.start(5);
323 new SocketClose.start(6);
341 } 324 }
OLDNEW
« no previous file with comments | « tests/standalone/src/SocketCloseTest.dart ('k') | tests/stub-generator/test_config.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698