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

Side by Side Diff: tests/standalone/io/echo_server_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: Rebased to r18818 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
(Empty)
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
3 // BSD-style license that can be found in the LICENSE file.
4 //
5 // Echo server test program for testing sockets.
6 //
7 // VMOptions=
8 // VMOptions=--short_socket_read
9 // VMOptions=--short_socket_write
10 // VMOptions=--short_socket_read --short_socket_write
11
12 library ServerTest;
13 import "dart:io";
14 import "dart:isolate";
15 part "testing_server.dart";
16
17 class EchoServerTest {
18
19 static void testMain() {
20 EchoServerGame echoServerGame = new EchoServerGame.start();
21 }
22 }
23
24 class EchoServerGame {
25
26 static const MSGSIZE = 10;
27 static const MESSAGES = 100;
28 static const FIRSTCHAR = 65;
29
30 EchoServerGame.start()
31 : _receivePort = new ReceivePort(),
32 _sendPort = null,
33 _buffer = new List<int>.fixedLength(MSGSIZE),
34 _messages = 0 {
35 for (int i = 0; i < MSGSIZE; i++) {
36 _buffer[i] = FIRSTCHAR + i;
37 }
38 _sendPort = spawnFunction(startEchoServer);
39 initialize();
40 }
41
42 void sendData() {
43 Socket _socket;
44
45 void messageHandler() {
46
47 List<int> bufferReceived = new List<int>.fixedLength(MSGSIZE);
48 int bytesRead = 0;
49
50 void handleRead() {
51 bytesRead += _socket.readList(
52 bufferReceived, bytesRead, MSGSIZE - bytesRead);
53 if (bytesRead < MSGSIZE) {
54 // We check every time the whole buffer to verify data integrity.
55 for (int i = 0; i < bytesRead; i++) {
56 Expect.equals(FIRSTCHAR + i, bufferReceived[i]);
57 }
58 _socket.onData = handleRead;
59 } else {
60 // We check every time the whole buffer to verify data integrity.
61 for (int i = 0; i < MSGSIZE; i++) {
62 Expect.equals(FIRSTCHAR + i, bufferReceived[i]);
63 }
64 _messages++;
65 _socket.close();
66 if (_messages < MESSAGES) {
67 sendData();
68 } else {
69 shutdown();
70 }
71 }
72 }
73
74 handleRead();
75 }
76
77 void errorHandler(Exception e) {
78 Expect.fail("Socket error $e");
79 }
80
81 void connectHandler() {
82
83 void writeMessage() {
84 int bytesWritten = 0;
85
86 void handleWrite() {
87 bytesWritten += _socket.writeList(
88 _buffer, bytesWritten, MSGSIZE - bytesWritten);
89 if (bytesWritten < MSGSIZE) {
90 _socket.onWrite = handleWrite;
91 }
92 }
93
94 handleWrite();
95 }
96
97 _socket.onData = messageHandler;
98 _socket.onError = errorHandler;
99 writeMessage();
100 }
101
102 _socket = new Socket(TestingServer.HOST, _port);
103 if (_socket != null) {
104 _socket.onConnect = connectHandler;
105 } else {
106 Expect.fail("Socket creation failed");
107 }
108 }
109
110 void initialize() {
111 _receivePort.receive((var message, SendPort replyTo) {
112 _port = message;
113 sendData();
114 });
115 _sendPort.send(TestingServer.INIT, _receivePort.toSendPort());
116 }
117
118 void shutdown() {
119 _sendPort.send(TestingServer.SHUTDOWN, _receivePort.toSendPort());
120 _receivePort.close();
121 }
122
123 int _port;
124 ReceivePort _receivePort;
125 SendPort _sendPort;
126 List<int> _buffer;
127 int _messages;
128 }
129
130
131 void startEchoServer() {
132 var server = new EchoServer();
133 port.receive(server.dispatch);
134 }
135
136 class EchoServer extends TestingServer {
137
138 static const msgSize = EchoServerGame.MSGSIZE;
139
140 void onConnection(Socket connection) {
141
142 void messageHandler() {
143
144 List<int> buffer = new List<int>.fixedLength(msgSize);
145 int bytesRead = 0;
146
147 void handleRead() {
148 int read = connection.readList(buffer, bytesRead, msgSize - bytesRead);
149 if (read > 0) {
150 bytesRead += read;
151 if (bytesRead < msgSize) {
152 // We check every time the whole buffer to verify data integrity.
153 for (int i = 0; i < bytesRead; i++) {
154 Expect.equals(EchoServerGame.FIRSTCHAR + i, buffer[i]);
155 }
156 connection.onData = handleRead;
157 } else {
158 // We check every time the whole buffer to verify data integrity.
159 for (int i = 0; i < msgSize; i++) {
160 Expect.equals(EchoServerGame.FIRSTCHAR + i, buffer[i]);
161 }
162
163 void writeMessage() {
164
165 int bytesWritten = 0;
166
167 void handleWrite() {
168 int written = connection.writeList(
169 buffer, bytesWritten, msgSize - bytesWritten);
170 bytesWritten += written;
171 if (bytesWritten < msgSize) {
172 connection.onWrite = handleWrite;
173 } else {
174 connection.close(true);
175 }
176 }
177 handleWrite();
178 }
179 writeMessage();
180 }
181 }
182 }
183
184 handleRead();
185 }
186
187 void closeHandler() {
188 connection.close();
189 }
190
191 void errorHandler(Exception e) {
192 Expect.fail("Socket error $e");
193 }
194
195 connection.onData = messageHandler;
196 connection.onClosed = closeHandler;
197 connection.onError = errorHandler;
198 }
199 }
200
201 main() {
202 EchoServerTest.testMain();
203 }
OLDNEW
« no previous file with comments | « tests/standalone/io/echo_server_stream_test.dart ('k') | tests/standalone/io/file_fuzz_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698