| OLD | NEW |
| 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'; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 45 int messagesToReceive; // Numbe rof messages expected to be received. | 45 int messagesToReceive; // Numbe rof messages expected to be received. |
| 46 int port; // TCP/IP port for server. | 46 int port; // TCP/IP port for server. |
| 47 | 47 |
| 48 String sessionId; // Session id when connected. | 48 String sessionId; // Session id when connected. |
| 49 int sendMessageNumber; // Number of messages sent. | 49 int sendMessageNumber; // Number of messages sent. |
| 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 void leaveResponseHandler(response, String data) { |
| 56 HttpClientResponse response; | |
| 57 | |
| 58 void leaveResponseHandler(String data) { | |
| 59 Expect.equals(HttpStatus.OK, response.statusCode); | 56 Expect.equals(HttpStatus.OK, response.statusCode); |
| 60 var responseData = json.parse(data); | 57 var responseData = json.parse(data); |
| 61 Expect.equals("leave", responseData["response"]); | 58 Expect.equals("leave", responseData["response"]); |
| 62 | 59 |
| 63 // Test done. | 60 // Test done. |
| 64 statusPort.send("Test succeeded", null); | 61 statusPort.send("Test succeeded", null); |
| 65 } | 62 } |
| 66 | 63 |
| 67 Map leaveRequest = new Map(); | 64 Map leaveRequest = new Map(); |
| 68 leaveRequest["request"] = "leave"; | 65 leaveRequest["request"] = "leave"; |
| 69 leaveRequest["sessionId"] = sessionId; | 66 leaveRequest["sessionId"] = sessionId; |
| 70 HttpClientConnection conn = httpClient.post("127.0.0.1", port, "/leave"); | 67 httpClient.post("127.0.0.1", port, "/leave") |
| 71 conn.onRequest = (HttpClientRequest request) { | 68 .then((HttpClientRequest request) { |
| 72 request.outputStream.writeString(json.stringify(leaveRequest)); | 69 request.addString(json.stringify(leaveRequest)); |
| 73 request.outputStream.close(); | 70 return request.close(); |
| 74 }; | 71 }) |
| 75 conn.onResponse = (HttpClientResponse r) { | 72 .then((HttpClientResponse response) { |
| 76 response = r; | 73 StringBuffer body = new StringBuffer(); |
| 77 StringInputStream stream = new StringInputStream(response.inputStream); | 74 response.listen( |
| 78 StringBuffer body = new StringBuffer(); | 75 (data) => body.add(new String.fromCharCodes(data)), |
| 79 stream.onData = () => body.add(stream.read()); | 76 onDone: () => leaveResponseHandler(response, body.toString())); |
| 80 stream.onClosed = () => leaveResponseHandler(body.toString()); | 77 }); |
| 81 }; | |
| 82 } | 78 } |
| 83 | 79 |
| 84 void receive() { | 80 void receive() { |
| 85 HttpClientRequest request; | 81 void receiveResponseHandler(response, String data) { |
| 86 HttpClientResponse response; | |
| 87 | |
| 88 void receiveResponseHandler(String data) { | |
| 89 Expect.equals(HttpStatus.OK, response.statusCode); | 82 Expect.equals(HttpStatus.OK, response.statusCode); |
| 90 var responseData = json.parse(data); | 83 var responseData = json.parse(data); |
| 91 Expect.equals("receive", responseData["response"]); | 84 Expect.equals("receive", responseData["response"]); |
| 92 Expect.equals(null, responseData["disconnect"]); | 85 Expect.equals(null, responseData["disconnect"]); |
| 93 for (int i = 0; i < responseData["messages"].length; i++) { | 86 for (int i = 0; i < responseData["messages"].length; i++) { |
| 94 Map message = responseData["messages"][i]; | 87 Map message = responseData["messages"][i]; |
| 95 if (message["type"] == "join") { | 88 if (message["type"] == "join") { |
| 96 joinCount++; | 89 joinCount++; |
| 97 } else if (message["type"] == "message") { | 90 } else if (message["type"] == "message") { |
| 98 messageCount++; | 91 messageCount++; |
| (...skipping 21 matching lines...) Expand all Loading... |
| 120 Expect.equals(messagesToReceive, messageCount); | 113 Expect.equals(messagesToReceive, messageCount); |
| 121 Expect.equals(totalClients, joinCount); | 114 Expect.equals(totalClients, joinCount); |
| 122 leave(); | 115 leave(); |
| 123 } | 116 } |
| 124 } | 117 } |
| 125 | 118 |
| 126 Map receiveRequest = new Map(); | 119 Map receiveRequest = new Map(); |
| 127 receiveRequest["request"] = "receive"; | 120 receiveRequest["request"] = "receive"; |
| 128 receiveRequest["sessionId"] = sessionId; | 121 receiveRequest["sessionId"] = sessionId; |
| 129 receiveRequest["nextMessage"] = receiveMessageNumber; | 122 receiveRequest["nextMessage"] = receiveMessageNumber; |
| 130 HttpClientConnection conn = | 123 httpClient.post("127.0.0.1", port, "/receive") |
| 131 httpClient.post("127.0.0.1", port, "/receive"); | 124 .then((HttpClientRequest request) { |
| 132 conn.onRequest = (HttpClientRequest request) { | 125 request.addString(json.stringify(receiveRequest)); |
| 133 request.outputStream.writeString(json.stringify(receiveRequest)); | 126 return request.close(); |
| 134 request.outputStream.close(); | 127 }) |
| 135 }; | 128 .then((HttpClientResponse response) { |
| 136 conn.onResponse = (HttpClientResponse r) { | 129 StringBuffer body = new StringBuffer(); |
| 137 response = r; | 130 response.listen( |
| 138 StringInputStream stream = new StringInputStream(response.inputStream); | 131 (data) => body.add(new String.fromCharCodes(data)), |
| 139 StringBuffer body = new StringBuffer(); | 132 onDone: () => receiveResponseHandler(response, body.toString())); |
| 140 stream.onData = () => body.add(stream.read()); | 133 }); |
| 141 stream.onClosed = () => receiveResponseHandler(body.toString()); | |
| 142 }; | |
| 143 } | 134 } |
| 144 | 135 |
| 145 void sendMessage() { | 136 void sendMessage() { |
| 146 HttpClientRequest request; | 137 void sendResponseHandler(response, String data) { |
| 147 HttpClientResponse response; | |
| 148 | |
| 149 void sendResponseHandler(String data) { | |
| 150 Expect.equals(HttpStatus.OK, response.statusCode); | 138 Expect.equals(HttpStatus.OK, response.statusCode); |
| 151 var responseData = json.parse(data); | 139 var responseData = json.parse(data); |
| 152 Expect.equals("message", responseData["response"]); | 140 Expect.equals("message", responseData["response"]); |
| 153 sendMessageNumber++; | 141 sendMessageNumber++; |
| 154 if (sendMessageNumber < messagesToSend) { | 142 if (sendMessageNumber < messagesToSend) { |
| 155 sendMessage(); | 143 sendMessage(); |
| 156 } else { | 144 } else { |
| 157 receive(); | 145 receive(); |
| 158 } | 146 } |
| 159 } | 147 } |
| 160 | 148 |
| 161 Map messageRequest = new Map(); | 149 Map messageRequest = new Map(); |
| 162 messageRequest["request"] = "message"; | 150 messageRequest["request"] = "message"; |
| 163 messageRequest["sessionId"] = sessionId; | 151 messageRequest["sessionId"] = sessionId; |
| 164 messageRequest["message"] = "message $sendMessageNumber"; | 152 messageRequest["message"] = "message $sendMessageNumber"; |
| 165 HttpClientConnection conn = | 153 httpClient.post("127.0.0.1", port, "/message") |
| 166 httpClient.post("127.0.0.1", port, "/message"); | 154 .then((HttpClientRequest request) { |
| 167 conn.onRequest = (HttpClientRequest request) { | 155 request.addString(json.stringify(messageRequest)); |
| 168 request.outputStream.writeString(json.stringify(messageRequest)); | 156 return request.close(); |
| 169 request.outputStream.close(); | 157 }) |
| 170 }; | 158 .then((HttpClientResponse response) { |
| 171 conn.onResponse = (HttpClientResponse r) { | 159 StringBuffer body = new StringBuffer(); |
| 172 response = r; | 160 response.listen( |
| 173 StringInputStream stream = new StringInputStream(response.inputStream); | 161 (data) => body.add(new String.fromCharCodes(data)), |
| 174 StringBuffer body = new StringBuffer(); | 162 onDone: () => sendResponseHandler(response, body.toString())); |
| 175 stream.onData = () => body.add(stream.read()); | 163 }); |
| 176 stream.onClosed = () => sendResponseHandler(body.toString()); | |
| 177 }; | |
| 178 } | 164 } |
| 179 | 165 |
| 180 void join() { | 166 void join() { |
| 181 HttpClientRequest request; | 167 void joinResponseHandler(response, String data) { |
| 182 HttpClientResponse response; | |
| 183 | |
| 184 void joinResponseHandler(String data) { | |
| 185 Expect.equals(HttpStatus.OK, response.statusCode); | 168 Expect.equals(HttpStatus.OK, response.statusCode); |
| 186 var responseData = json.parse(data); | 169 var responseData = json.parse(data); |
| 187 Expect.equals("join", responseData["response"]); | 170 Expect.equals("join", responseData["response"]); |
| 188 sessionId = responseData["sessionId"]; | 171 sessionId = responseData["sessionId"]; |
| 189 Expect.isTrue(sessionId != null); | 172 Expect.isTrue(sessionId != null); |
| 190 | 173 |
| 191 joinCount = 0; | 174 joinCount = 0; |
| 192 messageCount = 0; | 175 messageCount = 0; |
| 193 sendMessageNumber = 0; | 176 sendMessageNumber = 0; |
| 194 receiveMessageNumber = 0; | 177 receiveMessageNumber = 0; |
| 195 sendMessage(); | 178 sendMessage(); |
| 196 } | 179 } |
| 197 | 180 |
| 198 Map joinRequest = new Map(); | 181 Map joinRequest = new Map(); |
| 199 joinRequest["request"] = "join"; | 182 joinRequest["request"] = "join"; |
| 200 joinRequest["handle"] = "test1"; | 183 joinRequest["handle"] = "test1"; |
| 201 HttpClientConnection conn = httpClient.post("127.0.0.1", port, "/join"); | 184 httpClient.post("127.0.0.1", port, "/join") |
| 202 conn.onRequest = (HttpClientRequest request) { | 185 .then((HttpClientRequest request) { |
| 203 request.outputStream.writeString(json.stringify(joinRequest)); | 186 request.addString(json.stringify(joinRequest)); |
| 204 request.outputStream.close(); | 187 return request.close(); |
| 205 }; | 188 }) |
| 206 conn.onResponse = (HttpClientResponse r) { | 189 .then((HttpClientResponse response) { |
| 207 response = r; | 190 StringBuffer body = new StringBuffer(); |
| 208 StringInputStream stream = new StringInputStream(response.inputStream); | 191 response.listen( |
| 209 StringBuffer body = new StringBuffer(); | 192 (data) => body.add(new String.fromCharCodes(data)), |
| 210 stream.onData = () => body.add(stream.read()); | 193 onDone: () => joinResponseHandler(response, body.toString())); |
| 211 stream.onClosed = () => joinResponseHandler(body.toString()); | 194 }); |
| 212 }; | |
| 213 } | 195 } |
| 214 | 196 |
| 215 void dispatch(message, replyTo) { | 197 void dispatch(message, replyTo) { |
| 216 totalClients = message.totalClients; | 198 totalClients = message.totalClients; |
| 217 messagesToSend = message.messagesToSend; | 199 messagesToSend = message.messagesToSend; |
| 218 messagesToReceive = message.messagesToReceive; | 200 messagesToReceive = message.messagesToReceive; |
| 219 port = message.port; | 201 port = message.port; |
| 220 statusPort = replyTo; | 202 statusPort = replyTo; |
| 221 | 203 |
| 222 // Create a HTTP client factory. | 204 // Create a HTTP client factory. |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 | 265 |
| 284 clientStatusPorts[i] = statusPort; | 266 clientStatusPorts[i] = statusPort; |
| 285 clientPorts[i] = spawnFunction(startChatTestClient); | 267 clientPorts[i] = spawnFunction(startChatTestClient); |
| 286 liveClientsCount++; | 268 liveClientsCount++; |
| 287 if (liveClientsCount == clientCount) { | 269 if (liveClientsCount == clientCount) { |
| 288 // Once all clients are running send server start message to | 270 // Once all clients are running send server start message to |
| 289 // the server. Use port 0 for an ephemeral port. The actual | 271 // the server. Use port 0 for an ephemeral port. The actual |
| 290 // port will be returned with the server started | 272 // port will be returned with the server started |
| 291 // message. Use a backlog equal to the client count to avoid | 273 // message. Use a backlog equal to the client count to avoid |
| 292 // connection issues. | 274 // connection issues. |
| 293 serverPort.send(new ChatServerCommand.start("127.0.0.1", | 275 serverPort.send(new ChatServerCommand.start("127.0.0.1", 0), |
| 294 0, | |
| 295 backlog: clientCount), | |
| 296 serverStatusPort.toSendPort()); | 276 serverStatusPort.toSendPort()); |
| 297 } | 277 } |
| 298 } | 278 } |
| 299 } | 279 } |
| 300 | 280 |
| 301 int clientCount; // Number of clients to run. | 281 int clientCount; // Number of clients to run. |
| 302 int messageCount; // Number of messages per clients to send. | 282 int messageCount; // Number of messages per clients to send. |
| 303 int finishedClients; // Number of clients finished. | 283 int finishedClients; // Number of clients finished. |
| 304 | 284 |
| 305 // Ports for communicating with the server. | 285 // Ports for communicating with the server. |
| (...skipping 24 matching lines...) Expand all Loading... |
| 330 TestMain testMain = new TestMain.run(10, 2); | 310 TestMain testMain = new TestMain.run(10, 2); |
| 331 } | 311 } |
| 332 | 312 |
| 333 | 313 |
| 334 void main() { | 314 void main() { |
| 335 testOneClient(); | 315 testOneClient(); |
| 336 testTwoClients(); | 316 testTwoClients(); |
| 337 testTwoClientsMoreMessages(); | 317 testTwoClientsMoreMessages(); |
| 338 testTenClients(); | 318 testTenClients(); |
| 339 } | 319 } |
| OLD | NEW |