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

Side by Side Diff: samples/tests/samples/chat/chat_server_test.dart

Issue 11783009: Big merge from experimental to bleeding edge. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 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 | « samples/swarm/swarmlib.dart ('k') | samples/third_party/dromaeo/Dromaeo.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) 2012, the Dart project authors. Please see the AUTHORS file 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 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 // VMOptions= 5 // VMOptions=
6 // VMOptions=--short_socket_read 6 // VMOptions=--short_socket_read
7 // VMOptions=--short_socket_write 7 // VMOptions=--short_socket_write
8 // VMOptions=--short_socket_read --short_socket_write 8 // VMOptions=--short_socket_read --short_socket_write
9 9
10 import 'dart:io'; 10 import 'dart:io';
11 import 'dart:isolate'; 11 import 'dart:isolate';
12 import 'dart:json'; 12 import 'dart:json' as json;
13 import '../../../chat/chat_server_lib.dart'; 13 import '../../../chat/chat_server_lib.dart';
14 14
15 // Message to start chat test client running in an isolate. 15 // Message to start chat test client running in an isolate.
16 class ChatTestClientStart { 16 class ChatTestClientStart {
17 ChatTestClientStart(int this.totalClients, 17 ChatTestClientStart(int this.totalClients,
18 int this.messagesToSend, 18 int this.messagesToSend,
19 int this.messagesToReceive, 19 int this.messagesToReceive,
20 int this.port); 20 int this.port);
21 21
22 int totalClients; 22 int totalClients;
(...skipping 27 matching lines...) Expand all
50 int joinCount; 50 int joinCount;
51 int messageCount; 51 int messageCount;
52 int receiveMessageNumber; // Number of messages received. 52 int receiveMessageNumber; // Number of messages received.
53 53
54 void leave() { 54 void leave() {
55 HttpClientRequest request; 55 HttpClientRequest request;
56 HttpClientResponse response; 56 HttpClientResponse response;
57 57
58 void leaveResponseHandler(String data) { 58 void leaveResponseHandler(String data) {
59 Expect.equals(HttpStatus.OK, response.statusCode); 59 Expect.equals(HttpStatus.OK, response.statusCode);
60 var responseData = JSON.parse(data); 60 var responseData = json.parse(data);
61 Expect.equals("leave", responseData["response"]); 61 Expect.equals("leave", responseData["response"]);
62 62
63 // Test done. 63 // Test done.
64 statusPort.send("Test succeeded", null); 64 statusPort.send("Test succeeded", null);
65 } 65 }
66 66
67 Map leaveRequest = new Map(); 67 Map leaveRequest = new Map();
68 leaveRequest["request"] = "leave"; 68 leaveRequest["request"] = "leave";
69 leaveRequest["sessionId"] = sessionId; 69 leaveRequest["sessionId"] = sessionId;
70 HttpClientConnection conn = httpClient.post("127.0.0.1", port, "/leave"); 70 HttpClientConnection conn = httpClient.post("127.0.0.1", port, "/leave");
71 conn.onRequest = (HttpClientRequest request) { 71 conn.onRequest = (HttpClientRequest request) {
72 request.outputStream.writeString(JSON.stringify(leaveRequest)); 72 request.outputStream.writeString(json.stringify(leaveRequest));
73 request.outputStream.close(); 73 request.outputStream.close();
74 }; 74 };
75 conn.onResponse = (HttpClientResponse r) { 75 conn.onResponse = (HttpClientResponse r) {
76 response = r; 76 response = r;
77 StringInputStream stream = new StringInputStream(response.inputStream); 77 StringInputStream stream = new StringInputStream(response.inputStream);
78 StringBuffer body = new StringBuffer(); 78 StringBuffer body = new StringBuffer();
79 stream.onData = () => body.add(stream.read()); 79 stream.onData = () => body.add(stream.read());
80 stream.onClosed = () => leaveResponseHandler(body.toString()); 80 stream.onClosed = () => leaveResponseHandler(body.toString());
81 }; 81 };
82 } 82 }
83 83
84 void receive() { 84 void receive() {
85 HttpClientRequest request; 85 HttpClientRequest request;
86 HttpClientResponse response; 86 HttpClientResponse response;
87 87
88 void receiveResponseHandler(String data) { 88 void receiveResponseHandler(String data) {
89 Expect.equals(HttpStatus.OK, response.statusCode); 89 Expect.equals(HttpStatus.OK, response.statusCode);
90 var responseData = JSON.parse(data); 90 var responseData = json.parse(data);
91 Expect.equals("receive", responseData["response"]); 91 Expect.equals("receive", responseData["response"]);
92 Expect.equals(null, responseData["disconnect"]); 92 Expect.equals(null, responseData["disconnect"]);
93 for (int i = 0; i < responseData["messages"].length; i++) { 93 for (int i = 0; i < responseData["messages"].length; i++) {
94 Map message = responseData["messages"][i]; 94 Map message = responseData["messages"][i];
95 if (message["type"] == "join") { 95 if (message["type"] == "join") {
96 joinCount++; 96 joinCount++;
97 } else if (message["type"] == "message") { 97 } else if (message["type"] == "message") {
98 messageCount++; 98 messageCount++;
99 } else { 99 } else {
100 Expect.equals("leave", message["type"]); 100 Expect.equals("leave", message["type"]);
(...skipping 22 matching lines...) Expand all
123 } 123 }
124 } 124 }
125 125
126 Map receiveRequest = new Map(); 126 Map receiveRequest = new Map();
127 receiveRequest["request"] = "receive"; 127 receiveRequest["request"] = "receive";
128 receiveRequest["sessionId"] = sessionId; 128 receiveRequest["sessionId"] = sessionId;
129 receiveRequest["nextMessage"] = receiveMessageNumber; 129 receiveRequest["nextMessage"] = receiveMessageNumber;
130 HttpClientConnection conn = 130 HttpClientConnection conn =
131 httpClient.post("127.0.0.1", port, "/receive"); 131 httpClient.post("127.0.0.1", port, "/receive");
132 conn.onRequest = (HttpClientRequest request) { 132 conn.onRequest = (HttpClientRequest request) {
133 request.outputStream.writeString(JSON.stringify(receiveRequest)); 133 request.outputStream.writeString(json.stringify(receiveRequest));
134 request.outputStream.close(); 134 request.outputStream.close();
135 }; 135 };
136 conn.onResponse = (HttpClientResponse r) { 136 conn.onResponse = (HttpClientResponse r) {
137 response = r; 137 response = r;
138 StringInputStream stream = new StringInputStream(response.inputStream); 138 StringInputStream stream = new StringInputStream(response.inputStream);
139 StringBuffer body = new StringBuffer(); 139 StringBuffer body = new StringBuffer();
140 stream.onData = () => body.add(stream.read()); 140 stream.onData = () => body.add(stream.read());
141 stream.onClosed = () => receiveResponseHandler(body.toString()); 141 stream.onClosed = () => receiveResponseHandler(body.toString());
142 }; 142 };
143 } 143 }
144 144
145 void sendMessage() { 145 void sendMessage() {
146 HttpClientRequest request; 146 HttpClientRequest request;
147 HttpClientResponse response; 147 HttpClientResponse response;
148 148
149 void sendResponseHandler(String data) { 149 void sendResponseHandler(String data) {
150 Expect.equals(HttpStatus.OK, response.statusCode); 150 Expect.equals(HttpStatus.OK, response.statusCode);
151 var responseData = JSON.parse(data); 151 var responseData = json.parse(data);
152 Expect.equals("message", responseData["response"]); 152 Expect.equals("message", responseData["response"]);
153 sendMessageNumber++; 153 sendMessageNumber++;
154 if (sendMessageNumber < messagesToSend) { 154 if (sendMessageNumber < messagesToSend) {
155 sendMessage(); 155 sendMessage();
156 } else { 156 } else {
157 receive(); 157 receive();
158 } 158 }
159 } 159 }
160 160
161 Map messageRequest = new Map(); 161 Map messageRequest = new Map();
162 messageRequest["request"] = "message"; 162 messageRequest["request"] = "message";
163 messageRequest["sessionId"] = sessionId; 163 messageRequest["sessionId"] = sessionId;
164 messageRequest["message"] = "message $sendMessageNumber"; 164 messageRequest["message"] = "message $sendMessageNumber";
165 HttpClientConnection conn = 165 HttpClientConnection conn =
166 httpClient.post("127.0.0.1", port, "/message"); 166 httpClient.post("127.0.0.1", port, "/message");
167 conn.onRequest = (HttpClientRequest request) { 167 conn.onRequest = (HttpClientRequest request) {
168 request.outputStream.writeString(JSON.stringify(messageRequest)); 168 request.outputStream.writeString(json.stringify(messageRequest));
169 request.outputStream.close(); 169 request.outputStream.close();
170 }; 170 };
171 conn.onResponse = (HttpClientResponse r) { 171 conn.onResponse = (HttpClientResponse r) {
172 response = r; 172 response = r;
173 StringInputStream stream = new StringInputStream(response.inputStream); 173 StringInputStream stream = new StringInputStream(response.inputStream);
174 StringBuffer body = new StringBuffer(); 174 StringBuffer body = new StringBuffer();
175 stream.onData = () => body.add(stream.read()); 175 stream.onData = () => body.add(stream.read());
176 stream.onClosed = () => sendResponseHandler(body.toString()); 176 stream.onClosed = () => sendResponseHandler(body.toString());
177 }; 177 };
178 } 178 }
179 179
180 void join() { 180 void join() {
181 HttpClientRequest request; 181 HttpClientRequest request;
182 HttpClientResponse response; 182 HttpClientResponse response;
183 183
184 void joinResponseHandler(String data) { 184 void joinResponseHandler(String data) {
185 Expect.equals(HttpStatus.OK, response.statusCode); 185 Expect.equals(HttpStatus.OK, response.statusCode);
186 var responseData = JSON.parse(data); 186 var responseData = json.parse(data);
187 Expect.equals("join", responseData["response"]); 187 Expect.equals("join", responseData["response"]);
188 sessionId = responseData["sessionId"]; 188 sessionId = responseData["sessionId"];
189 Expect.isTrue(sessionId != null); 189 Expect.isTrue(sessionId != null);
190 190
191 joinCount = 0; 191 joinCount = 0;
192 messageCount = 0; 192 messageCount = 0;
193 sendMessageNumber = 0; 193 sendMessageNumber = 0;
194 receiveMessageNumber = 0; 194 receiveMessageNumber = 0;
195 sendMessage(); 195 sendMessage();
196 } 196 }
197 197
198 Map joinRequest = new Map(); 198 Map joinRequest = new Map();
199 joinRequest["request"] = "join"; 199 joinRequest["request"] = "join";
200 joinRequest["handle"] = "test1"; 200 joinRequest["handle"] = "test1";
201 HttpClientConnection conn = httpClient.post("127.0.0.1", port, "/join"); 201 HttpClientConnection conn = httpClient.post("127.0.0.1", port, "/join");
202 conn.onRequest = (HttpClientRequest request) { 202 conn.onRequest = (HttpClientRequest request) {
203 request.outputStream.writeString(JSON.stringify(joinRequest)); 203 request.outputStream.writeString(json.stringify(joinRequest));
204 request.outputStream.close(); 204 request.outputStream.close();
205 }; 205 };
206 conn.onResponse = (HttpClientResponse r) { 206 conn.onResponse = (HttpClientResponse r) {
207 response = r; 207 response = r;
208 StringInputStream stream = new StringInputStream(response.inputStream); 208 StringInputStream stream = new StringInputStream(response.inputStream);
209 StringBuffer body = new StringBuffer(); 209 StringBuffer body = new StringBuffer();
210 stream.onData = () => body.add(stream.read()); 210 stream.onData = () => body.add(stream.read());
211 stream.onClosed = () => joinResponseHandler(body.toString()); 211 stream.onClosed = () => joinResponseHandler(body.toString());
212 }; 212 };
213 } 213 }
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
251 message.port); 251 message.port);
252 clientPorts[i].send(command, clientStatusPorts[i].toSendPort()); 252 clientPorts[i].send(command, clientStatusPorts[i].toSendPort());
253 } 253 }
254 } else if (message.isError) { 254 } else if (message.isError) {
255 print("Could not start server - probably error \"Address already in use\" from bind."); 255 print("Could not start server - probably error \"Address already in use\" from bind.");
256 serverStatusPort.close(); 256 serverStatusPort.close();
257 } 257 }
258 }); 258 });
259 259
260 // Prepare the requested number of clients. 260 // Prepare the requested number of clients.
261 clientPorts = new List<SendPort>(clientCount); 261 clientPorts = new List<SendPort>.fixedLength(clientCount);
262 int liveClientsCount = 0; 262 int liveClientsCount = 0;
263 clientStatusPorts = new List<ReceivePort>(clientCount); 263 clientStatusPorts = new List<ReceivePort>.fixedLength(clientCount);
264 for (int i = 0; i < clientCount; i++) { 264 for (int i = 0; i < clientCount; i++) {
265 ReceivePort statusPort = new ReceivePort(); 265 ReceivePort statusPort = new ReceivePort();
266 statusPort.receive((var message, SendPort replyTo) { 266 statusPort.receive((var message, SendPort replyTo) {
267 // First and only message from the client indicates that 267 // First and only message from the client indicates that
268 // the test is done. 268 // the test is done.
269 Expect.equals("Test succeeded", message); 269 Expect.equals("Test succeeded", message);
270 statusPort.close(); 270 statusPort.close();
271 finishedClients++; 271 finishedClients++;
272 272
273 // If all clients are finished shutdown the server. 273 // If all clients are finished shutdown the server.
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
330 TestMain testMain = new TestMain.run(10, 2); 330 TestMain testMain = new TestMain.run(10, 2);
331 } 331 }
332 332
333 333
334 void main() { 334 void main() {
335 testOneClient(); 335 testOneClient();
336 testTwoClients(); 336 testTwoClients();
337 testTwoClientsMoreMessages(); 337 testTwoClientsMoreMessages();
338 testTenClients(); 338 testTenClients();
339 } 339 }
OLDNEW
« no previous file with comments | « samples/swarm/swarmlib.dart ('k') | samples/third_party/dromaeo/Dromaeo.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698