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

Side by Side Diff: tests/standalone/src/EchoServerTest.dart

Issue 9572005: Move all tests using dart:io to a separate directory (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Addressed review comments Created 8 years, 9 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 #import("dart:io");
13 #import("dart:isolate");
14 #source("TestingServer.dart");
15
16 class EchoServerTest {
17
18 static void testMain() {
19 EchoServerGame echoServerGame = new EchoServerGame.start();
20 }
21 }
22
23 class EchoServerGame {
24
25 static final MSGSIZE = 10;
26 static final MESSAGES = 100;
27 static final FIRSTCHAR = 65;
28
29 EchoServerGame.start()
30 : _receivePort = new ReceivePort(),
31 _sendPort = null,
32 _buffer = new List<int>(MSGSIZE),
33 _messages = 0 {
34 for (int i = 0; i < MSGSIZE; i++) {
35 _buffer[i] = FIRSTCHAR + i;
36 }
37 new EchoServer().spawn().then((SendPort port) {
38 _sendPort = port;
39 start();
40 });
41 }
42
43 void sendData() {
44 Socket _socket;
45
46 void messageHandler() {
47
48 List<int> bufferReceived = new List<int>(MSGSIZE);
49 int bytesRead = 0;
50
51 void handleRead() {
52 bytesRead += _socket.readList(
53 bufferReceived, bytesRead, MSGSIZE - bytesRead);
54 if (bytesRead < MSGSIZE) {
55 // We check every time the whole buffer to verify data integrity.
56 for (int i = 0; i < bytesRead; i++) {
57 Expect.equals(FIRSTCHAR + i, bufferReceived[i]);
58 }
59 _socket.onData = handleRead;
60 } else {
61 // We check every time the whole buffer to verify data integrity.
62 for (int i = 0; i < MSGSIZE; i++) {
63 Expect.equals(FIRSTCHAR + i, bufferReceived[i]);
64 }
65 _messages++;
66 _socket.close();
67 if (_messages < MESSAGES) {
68 sendData();
69 } else {
70 shutdown();
71 }
72 }
73 }
74
75 handleRead();
76 }
77
78 void errorHandler() {
79 Expect.fail("Socket error");
80 }
81
82 void connectHandler() {
83
84 void writeMessage() {
85 int bytesWritten = 0;
86
87 void handleWrite() {
88 bytesWritten += _socket.writeList(
89 _buffer, bytesWritten, MSGSIZE - bytesWritten);
90 if (bytesWritten < MSGSIZE) {
91 _socket.onWrite = handleWrite;
92 }
93 }
94
95 handleWrite();
96 }
97
98 _socket.onData = messageHandler;
99 _socket.onError = errorHandler;
100 writeMessage();
101 }
102
103 _socket = new Socket(TestingServer.HOST, _port);
104 if (_socket !== null) {
105 _socket.onConnect = connectHandler;
106 } else {
107 Expect.fail("Socket creation failed");
108 }
109 }
110
111 void start() {
112 _receivePort.receive((var message, SendPort replyTo) {
113 _port = message;
114 sendData();
115 });
116 _sendPort.send(TestingServer.INIT, _receivePort.toSendPort());
117 }
118
119 void shutdown() {
120 _sendPort.send(TestingServer.SHUTDOWN, _receivePort.toSendPort());
121 _receivePort.close();
122 }
123
124 int _port;
125 ReceivePort _receivePort;
126 SendPort _sendPort;
127 List<int> _buffer;
128 int _messages;
129 }
130
131 class EchoServer extends TestingServer {
132
133 static final msgSize = EchoServerGame.MSGSIZE;
134
135 void onConnection(Socket connection) {
136
137 void messageHandler() {
138
139 List<int> buffer = new List<int>(msgSize);
140 int bytesRead = 0;
141
142 void handleRead() {
143 int read = connection.readList(buffer, bytesRead, msgSize - bytesRead);
144 if (read > 0) {
145 bytesRead += read;
146 if (bytesRead < msgSize) {
147 // We check every time the whole buffer to verify data integrity.
148 for (int i = 0; i < bytesRead; i++) {
149 Expect.equals(EchoServerGame.FIRSTCHAR + i, buffer[i]);
150 }
151 connection.onData = handleRead;
152 } else {
153 // We check every time the whole buffer to verify data integrity.
154 for (int i = 0; i < msgSize; i++) {
155 Expect.equals(EchoServerGame.FIRSTCHAR + i, buffer[i]);
156 }
157
158 void writeMessage() {
159
160 int bytesWritten = 0;
161
162 void handleWrite() {
163 int written = connection.writeList(
164 buffer, bytesWritten, msgSize - bytesWritten);
165 bytesWritten += written;
166 if (bytesWritten < msgSize) {
167 connection.onWrite = handleWrite;
168 } else {
169 connection.close(true);
170 }
171 }
172 handleWrite();
173 }
174 writeMessage();
175 }
176 }
177 }
178
179 handleRead();
180 }
181
182 void closeHandler() {
183 connection.close();
184 }
185
186 void errorHandler() {
187 Expect.fail("Socket error");
188 }
189
190 connection.onData = messageHandler;
191 connection.onClosed = closeHandler;
192 connection.onError = errorHandler;
193 }
194 }
195
196 main() {
197 EchoServerTest.testMain();
198 }
OLDNEW
« no previous file with comments | « tests/standalone/src/EchoServerStreamTest.dart ('k') | tests/standalone/src/FileInputStreamTest.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698