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

Side by Side Diff: tests/standalone/src/EchoServerStreamTest.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 | « runtime/bin/stream_util.dart ('k') | tests/standalone/src/FileInputStreamTest.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 // Echo server test program to test socket streams. 5 // Echo server test program to test socket streams.
6 // 6 //
7 // VMOptions= 7 // VMOptions=
8 // VMOptions=--short_socket_read 8 // VMOptions=--short_socket_read
9 // VMOptions=--short_socket_write 9 // VMOptions=--short_socket_write
10 // VMOptions=--short_socket_read --short_socket_write 10 // VMOptions=--short_socket_read --short_socket_write
(...skipping 16 matching lines...) Expand all
27 _buffer[i] = FIRSTCHAR + i; 27 _buffer[i] = FIRSTCHAR + i;
28 } 28 }
29 new EchoServer().spawn().then((SendPort port) { 29 new EchoServer().spawn().then((SendPort port) {
30 _sendPort = port; 30 _sendPort = port;
31 start(); 31 start();
32 }); 32 });
33 } 33 }
34 34
35 void sendData() { 35 void sendData() {
36 36
37 void closeHandler() {
38 _socket.close();
39 }
40
41 void errorHandler() { 37 void errorHandler() {
42 Expect.fail("Socket error"); 38 Expect.fail("Socket error");
43 } 39 }
44 40
45 void connectHandler() { 41 void connectHandler() {
46 42
47 SocketOutputStream stream = _socket.outputStream; 43 SocketOutputStream stream = _socket.outputStream;
48 44
49 void dataSent() { 45 void dataSent() {
50 InputStream inputStream = _socket.inputStream; 46 InputStream inputStream = _socket.inputStream;
51 int offset = 0; 47 int offset = 0;
52 List<int> data; 48 List<int> data;
53 49
54 void dataReceived() { 50 void onClose() {
51 Expect.equals(MSGSIZE, offset);
52 _messages++;
53 if (_messages < MESSAGES) {
54 sendData();
55 } else {
56 shutdown();
57 }
58 }
59
60 void onData() {
55 // Test both read and readInto. 61 // Test both read and readInto.
56 int bytesRead = 0; 62 int bytesRead = 0;
57 if (_messages % 2 == 0) { 63 if (_messages % 2 == 0) {
58 bytesRead = inputStream.readInto(data, offset, MSGSIZE - offset); 64 bytesRead = inputStream.readInto(data, offset, MSGSIZE - offset);
59 for (int i = 0; i < offset + bytesRead; i++) { 65 for (int i = 0; i < offset + bytesRead; i++) {
60 Expect.equals(FIRSTCHAR + i, data[i]); 66 Expect.equals(FIRSTCHAR + i, data[i]);
61 } 67 }
62 } else { 68 } else {
63 data = inputStream.read(); 69 data = inputStream.read();
64 bytesRead = data.length; 70 bytesRead = data.length;
65 for (int i = 0; i < data.length; i++) { 71 for (int i = 0; i < data.length; i++) {
66 Expect.equals(FIRSTCHAR + i + offset, data[i]); 72 Expect.equals(FIRSTCHAR + i + offset, data[i]);
67 } 73 }
68 } 74 }
69 75
70 offset += bytesRead; 76 offset += bytesRead;
71 if (offset == MSGSIZE) {
72 _messages++;
73 _socket.close();
74 if (_messages < MESSAGES) {
75 sendData();
76 } else {
77 shutdown();
78 }
79 }
80 } 77 }
81 78
82 if (_messages % 2 == 0) data = new List<int>(MSGSIZE); 79 if (_messages % 2 == 0) data = new List<int>(MSGSIZE);
83 inputStream.dataHandler = dataReceived; 80 inputStream.dataHandler = onData;
81 inputStream.closeHandler = onClose;
84 } 82 }
85 83
86 _socket.closeHandler = closeHandler;
87 _socket.errorHandler = errorHandler; 84 _socket.errorHandler = errorHandler;
88 85
89 // Test both write and writeFrom in different forms. 86 // Test both write and writeFrom in different forms.
90 switch (_messages % 2) { 87 switch (_messages % 4) {
91 case 0: 88 case 0:
92 stream.write(_buffer); 89 stream.write(_buffer);
93 break; 90 break;
94 case 1: 91 case 1:
95 stream.write(_buffer, false); 92 stream.write(_buffer, false);
96 break; 93 break;
97 case 2: 94 case 2:
98 stream.writeFrom(_buffer); 95 stream.writeFrom(_buffer);
99 break; 96 break;
100 case 3: 97 case 3:
98 Expect.equals(0, _buffer.length % 2);
101 stream.writeFrom(_buffer, len: _buffer.length ~/ 2); 99 stream.writeFrom(_buffer, len: _buffer.length ~/ 2);
102 stream.writeFrom(_buffer, _buffer.length ~/ 2); 100 stream.writeFrom(_buffer, _buffer.length ~/ 2);
103 break; 101 break;
104 } 102 }
105 stream.close(); 103 stream.close();
106 dataSent(); 104 dataSent();
107 } 105 }
108 106
109 _socket = new Socket(TestingServer.HOST, _port); 107 _socket = new Socket(TestingServer.HOST, _port);
110 if (_socket !== null) { 108 if (_socket !== null) {
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
168 166
169 inputStream = connection.inputStream; 167 inputStream = connection.inputStream;
170 inputStream.dataHandler = dataReceived; 168 inputStream.dataHandler = dataReceived;
171 connection.errorHandler = errorHandler; 169 connection.errorHandler = errorHandler;
172 } 170 }
173 } 171 }
174 172
175 main() { 173 main() {
176 EchoServerGame echoServerGame = new EchoServerGame.start(); 174 EchoServerGame echoServerGame = new EchoServerGame.start();
177 } 175 }
OLDNEW
« no previous file with comments | « runtime/bin/stream_util.dart ('k') | tests/standalone/src/FileInputStreamTest.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698