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

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

Issue 12316036: Merge IO v2 branch to bleeding edge (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 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) 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 // 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
11 11
(...skipping 15 matching lines...) Expand all
27 _buffer = new List<int>.fixedLength(MSGSIZE), 27 _buffer = new List<int>.fixedLength(MSGSIZE),
28 _messages = 0 { 28 _messages = 0 {
29 for (int i = 0; i < MSGSIZE; i++) { 29 for (int i = 0; i < MSGSIZE; i++) {
30 _buffer[i] = FIRSTCHAR + i; 30 _buffer[i] = FIRSTCHAR + i;
31 } 31 }
32 _sendPort = spawnFunction(startEchoServer); 32 _sendPort = spawnFunction(startEchoServer);
33 initialize(); 33 initialize();
34 } 34 }
35 35
36 void sendData() { 36 void sendData() {
37 int offset = 0;
38 List<int> data;
37 39
38 void errorHandler(Exception e) { 40 void onData(List<int> data) {
41 int bytesRead = data.length;
42 for (int i = 0; i < data.length; i++) {
43 Expect.equals(FIRSTCHAR + i + offset, data[i]);
44 }
45 offset += bytesRead;
46 }
47
48 void onClosed() {
49 Expect.equals(MSGSIZE, offset);
50 _messages++;
51 if (_messages < MESSAGES) {
52 sendData();
53 } else {
54 shutdown();
55 }
56 }
57
58 void errorHandler(e) {
39 Expect.fail("Socket error $e"); 59 Expect.fail("Socket error $e");
40 } 60 }
41 61
42 void connectHandler() { 62 void connectHandler() {
43 63 _socket.listen(onData,
44 OutputStream stream = _socket.outputStream; 64 onError: errorHandler,
45 65 onDone: onClosed);
46 void dataSent() { 66 _socket.add(_buffer);
47 InputStream inputStream = _socket.inputStream; 67 _socket.close();
48 int offset = 0; 68 data = new List<int>.fixedLength(MSGSIZE);
49 List<int> data;
50
51 void onClosed() {
52 Expect.equals(MSGSIZE, offset);
53 _messages++;
54 if (_messages < MESSAGES) {
55 sendData();
56 } else {
57 shutdown();
58 }
59 }
60
61 void onData() {
62 // Test both read and readInto.
63 int bytesRead = 0;
64 if (_messages % 2 == 0) {
65 bytesRead = inputStream.readInto(data, offset, MSGSIZE - offset);
66 for (int i = 0; i < offset + bytesRead; i++) {
67 Expect.equals(FIRSTCHAR + i, data[i]);
68 }
69 } else {
70 data = inputStream.read();
71 bytesRead = data.length;
72 for (int i = 0; i < data.length; i++) {
73 Expect.equals(FIRSTCHAR + i + offset, data[i]);
74 }
75 }
76
77 offset += bytesRead;
78 }
79
80 if (_messages % 2 == 0) data = new List<int>.fixedLength(MSGSIZE);
81 inputStream.onData = onData;
82 inputStream.onClosed = onClosed;
83 }
84
85 _socket.onError = errorHandler;
86
87 // Test both write and writeFrom in different forms.
88 switch (_messages % 4) {
89 case 0:
90 stream.write(_buffer);
91 break;
92 case 1:
93 stream.write(_buffer, false);
94 break;
95 case 2:
96 stream.writeFrom(_buffer);
97 break;
98 case 3:
99 Expect.equals(0, _buffer.length % 2);
100 stream.writeFrom(_buffer, 0, _buffer.length ~/ 2);
101 stream.writeFrom(_buffer, _buffer.length ~/ 2);
102 break;
103 }
104 stream.close();
105 dataSent();
106 } 69 }
107 70
108 _socket = new Socket(TestingServer.HOST, _port); 71 Socket.connect(TestingServer.HOST, _port).then((s) {
109 if (_socket != null) { 72 _socket = s;
110 _socket.onConnect = connectHandler; 73 connectHandler();
111 } else { 74 });
112 Expect.fail("socket creation failed");
113 }
114 } 75 }
115 76
116 void initialize() { 77 void initialize() {
117 _receivePort.receive((var message, SendPort replyTo) { 78 _receivePort.receive((var message, SendPort replyTo) {
118 _port = message; 79 _port = message;
119 sendData(); 80 sendData();
120 }); 81 });
121 _sendPort.send(TestingServer.INIT, _receivePort.toSendPort()); 82 _sendPort.send(TestingServer.INIT, _receivePort.toSendPort());
122 } 83 }
123 84
(...skipping 15 matching lines...) Expand all
139 var server = new EchoServer(); 100 var server = new EchoServer();
140 port.receive(server.dispatch); 101 port.receive(server.dispatch);
141 } 102 }
142 103
143 104
144 class EchoServer extends TestingServer { 105 class EchoServer extends TestingServer {
145 106
146 static const int MSGSIZE = EchoServerGame.MSGSIZE; 107 static const int MSGSIZE = EchoServerGame.MSGSIZE;
147 108
148 void onConnection(Socket connection) { 109 void onConnection(Socket connection) {
149 InputStream inputStream;
150 List<int> buffer = new List<int>.fixedLength(MSGSIZE); 110 List<int> buffer = new List<int>.fixedLength(MSGSIZE);
151 int offset = 0; 111 int offset = 0;
152 112
153 void dataReceived() { 113 void dataReceived(List<int> data) {
154 int bytesRead; 114 int bytesRead;
155 OutputStream outputStream = connection.outputStream; 115 bytesRead = data.length;
156 bytesRead = inputStream.readInto(buffer, offset, MSGSIZE - offset);
157 if (bytesRead > 0) { 116 if (bytesRead > 0) {
117 buffer.setRange(offset, data.length, data);
158 offset += bytesRead; 118 offset += bytesRead;
159 for (int i = 0; i < offset; i++) { 119 for (int i = 0; i < offset; i++) {
160 Expect.equals(EchoServerGame.FIRSTCHAR + i, buffer[i]); 120 Expect.equals(EchoServerGame.FIRSTCHAR + i, buffer[i]);
161 } 121 }
162 if (offset == MSGSIZE) { 122 if (offset == MSGSIZE) {
163 outputStream.write(buffer); 123 connection.add(buffer);
164 outputStream.close(); 124 connection.close();
165 } 125 }
166 } 126 }
167 } 127 }
168 128
169 void errorHandler(Exception e) { 129 void errorHandler(e) {
170 Expect.fail("Socket error $e"); 130 Expect.fail("Socket error $e");
171 } 131 }
172 132
173 inputStream = connection.inputStream; 133 connection.listen(dataReceived, onError: errorHandler);
174 inputStream.onData = dataReceived;
175 connection.onError = errorHandler;
176 } 134 }
177 } 135 }
178 136
179 main() { 137 main() {
180 EchoServerGame echoServerGame = new EchoServerGame.start(); 138 EchoServerGame echoServerGame = new EchoServerGame.start();
181 } 139 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698