OLD | NEW |
1 // Copyright (c) 2013, 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 // 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 "package:expect/expect.dart"; | 10 import "package:expect/expect.dart"; |
11 import 'dart:io'; | 11 import 'dart:io'; |
12 import 'dart:isolate'; | 12 import 'dart:isolate'; |
13 import 'dart:json' as json; | 13 import "dart:convert"; |
14 import '../../../chat/chat_server_lib.dart'; | 14 import '../../../chat/chat_server_lib.dart'; |
15 | 15 |
16 // Message to start chat test client running in an isolate. | 16 // Message to start chat test client running in an isolate. |
17 class ChatTestClientStart { | 17 class ChatTestClientStart { |
18 ChatTestClientStart(int this.totalClients, | 18 ChatTestClientStart(int this.totalClients, |
19 int this.messagesToSend, | 19 int this.messagesToSend, |
20 int this.messagesToReceive, | 20 int this.messagesToReceive, |
21 int this.port); | 21 int this.port); |
22 | 22 |
23 int totalClients; | 23 int totalClients; |
(...skipping 24 matching lines...) Expand all Loading... |
48 | 48 |
49 String sessionId; // Session id when connected. | 49 String sessionId; // Session id when connected. |
50 int sendMessageNumber; // Number of messages sent. | 50 int sendMessageNumber; // Number of messages sent. |
51 int joinCount; | 51 int joinCount; |
52 int messageCount; | 52 int messageCount; |
53 int receiveMessageNumber; // Number of messages received. | 53 int receiveMessageNumber; // Number of messages received. |
54 | 54 |
55 void leave() { | 55 void leave() { |
56 void leaveResponseHandler(response, String data) { | 56 void leaveResponseHandler(response, String data) { |
57 Expect.equals(HttpStatus.OK, response.statusCode); | 57 Expect.equals(HttpStatus.OK, response.statusCode); |
58 var responseData = json.parse(data); | 58 var responseData = JSON.decode(data); |
59 Expect.equals("leave", responseData["response"]); | 59 Expect.equals("leave", responseData["response"]); |
60 | 60 |
61 // Test done. | 61 // Test done. |
62 statusPort.send("Test succeeded", null); | 62 statusPort.send("Test succeeded", null); |
63 } | 63 } |
64 | 64 |
65 Map leaveRequest = new Map(); | 65 Map leaveRequest = new Map(); |
66 leaveRequest["request"] = "leave"; | 66 leaveRequest["request"] = "leave"; |
67 leaveRequest["sessionId"] = sessionId; | 67 leaveRequest["sessionId"] = sessionId; |
68 httpClient.post("127.0.0.1", port, "/leave") | 68 httpClient.post("127.0.0.1", port, "/leave") |
69 .then((HttpClientRequest request) { | 69 .then((HttpClientRequest request) { |
70 request.write(json.stringify(leaveRequest)); | 70 request.write(JSON.encode(leaveRequest)); |
71 return request.close(); | 71 return request.close(); |
72 }) | 72 }) |
73 .then((HttpClientResponse response) { | 73 .then((HttpClientResponse response) { |
74 StringBuffer body = new StringBuffer(); | 74 StringBuffer body = new StringBuffer(); |
75 response.listen( | 75 response.listen( |
76 (data) => body.write(new String.fromCharCodes(data)), | 76 (data) => body.write(new String.fromCharCodes(data)), |
77 onDone: () => leaveResponseHandler(response, body.toString())); | 77 onDone: () => leaveResponseHandler(response, body.toString())); |
78 }); | 78 }); |
79 } | 79 } |
80 | 80 |
81 void receive() { | 81 void receive() { |
82 void receiveResponseHandler(response, String data) { | 82 void receiveResponseHandler(response, String data) { |
83 Expect.equals(HttpStatus.OK, response.statusCode); | 83 Expect.equals(HttpStatus.OK, response.statusCode); |
84 var responseData = json.parse(data); | 84 var responseData = JSON.decode(data); |
85 Expect.equals("receive", responseData["response"]); | 85 Expect.equals("receive", responseData["response"]); |
86 Expect.equals(null, responseData["disconnect"]); | 86 Expect.equals(null, responseData["disconnect"]); |
87 for (int i = 0; i < responseData["messages"].length; i++) { | 87 for (int i = 0; i < responseData["messages"].length; i++) { |
88 Map message = responseData["messages"][i]; | 88 Map message = responseData["messages"][i]; |
89 if (message["type"] == "join") { | 89 if (message["type"] == "join") { |
90 joinCount++; | 90 joinCount++; |
91 } else if (message["type"] == "message") { | 91 } else if (message["type"] == "message") { |
92 messageCount++; | 92 messageCount++; |
93 } else { | 93 } else { |
94 Expect.equals("leave", message["type"]); | 94 Expect.equals("leave", message["type"]); |
(...skipping 21 matching lines...) Expand all Loading... |
116 leave(); | 116 leave(); |
117 } | 117 } |
118 } | 118 } |
119 | 119 |
120 Map receiveRequest = new Map(); | 120 Map receiveRequest = new Map(); |
121 receiveRequest["request"] = "receive"; | 121 receiveRequest["request"] = "receive"; |
122 receiveRequest["sessionId"] = sessionId; | 122 receiveRequest["sessionId"] = sessionId; |
123 receiveRequest["nextMessage"] = receiveMessageNumber; | 123 receiveRequest["nextMessage"] = receiveMessageNumber; |
124 httpClient.post("127.0.0.1", port, "/receive") | 124 httpClient.post("127.0.0.1", port, "/receive") |
125 .then((HttpClientRequest request) { | 125 .then((HttpClientRequest request) { |
126 request.write(json.stringify(receiveRequest)); | 126 request.write(JSON.encode(receiveRequest)); |
127 return request.close(); | 127 return request.close(); |
128 }) | 128 }) |
129 .then((HttpClientResponse response) { | 129 .then((HttpClientResponse response) { |
130 StringBuffer body = new StringBuffer(); | 130 StringBuffer body = new StringBuffer(); |
131 response.listen( | 131 response.listen( |
132 (data) => body.write(new String.fromCharCodes(data)), | 132 (data) => body.write(new String.fromCharCodes(data)), |
133 onDone: () => receiveResponseHandler(response, body.toString())); | 133 onDone: () => receiveResponseHandler(response, body.toString())); |
134 }); | 134 }); |
135 } | 135 } |
136 | 136 |
137 void sendMessage() { | 137 void sendMessage() { |
138 void sendResponseHandler(response, String data) { | 138 void sendResponseHandler(response, String data) { |
139 Expect.equals(HttpStatus.OK, response.statusCode); | 139 Expect.equals(HttpStatus.OK, response.statusCode); |
140 var responseData = json.parse(data); | 140 var responseData = JSON.decode(data); |
141 Expect.equals("message", responseData["response"]); | 141 Expect.equals("message", responseData["response"]); |
142 sendMessageNumber++; | 142 sendMessageNumber++; |
143 if (sendMessageNumber < messagesToSend) { | 143 if (sendMessageNumber < messagesToSend) { |
144 sendMessage(); | 144 sendMessage(); |
145 } else { | 145 } else { |
146 receive(); | 146 receive(); |
147 } | 147 } |
148 } | 148 } |
149 | 149 |
150 Map messageRequest = new Map(); | 150 Map messageRequest = new Map(); |
151 messageRequest["request"] = "message"; | 151 messageRequest["request"] = "message"; |
152 messageRequest["sessionId"] = sessionId; | 152 messageRequest["sessionId"] = sessionId; |
153 messageRequest["message"] = "message $sendMessageNumber"; | 153 messageRequest["message"] = "message $sendMessageNumber"; |
154 httpClient.post("127.0.0.1", port, "/message") | 154 httpClient.post("127.0.0.1", port, "/message") |
155 .then((HttpClientRequest request) { | 155 .then((HttpClientRequest request) { |
156 request.write(json.stringify(messageRequest)); | 156 request.write(JSON.encode(messageRequest)); |
157 return request.close(); | 157 return request.close(); |
158 }) | 158 }) |
159 .then((HttpClientResponse response) { | 159 .then((HttpClientResponse response) { |
160 StringBuffer body = new StringBuffer(); | 160 StringBuffer body = new StringBuffer(); |
161 response.listen( | 161 response.listen( |
162 (data) => body.write(new String.fromCharCodes(data)), | 162 (data) => body.write(new String.fromCharCodes(data)), |
163 onDone: () => sendResponseHandler(response, body.toString())); | 163 onDone: () => sendResponseHandler(response, body.toString())); |
164 }); | 164 }); |
165 } | 165 } |
166 | 166 |
167 void join() { | 167 void join() { |
168 void joinResponseHandler(response, String data) { | 168 void joinResponseHandler(response, String data) { |
169 Expect.equals(HttpStatus.OK, response.statusCode); | 169 Expect.equals(HttpStatus.OK, response.statusCode); |
170 var responseData = json.parse(data); | 170 var responseData = JSON.decode(data); |
171 Expect.equals("join", responseData["response"]); | 171 Expect.equals("join", responseData["response"]); |
172 sessionId = responseData["sessionId"]; | 172 sessionId = responseData["sessionId"]; |
173 Expect.isTrue(sessionId != null); | 173 Expect.isTrue(sessionId != null); |
174 | 174 |
175 joinCount = 0; | 175 joinCount = 0; |
176 messageCount = 0; | 176 messageCount = 0; |
177 sendMessageNumber = 0; | 177 sendMessageNumber = 0; |
178 receiveMessageNumber = 0; | 178 receiveMessageNumber = 0; |
179 sendMessage(); | 179 sendMessage(); |
180 } | 180 } |
181 | 181 |
182 Map joinRequest = new Map(); | 182 Map joinRequest = new Map(); |
183 joinRequest["request"] = "join"; | 183 joinRequest["request"] = "join"; |
184 joinRequest["handle"] = "test1"; | 184 joinRequest["handle"] = "test1"; |
185 httpClient.post("127.0.0.1", port, "/join") | 185 httpClient.post("127.0.0.1", port, "/join") |
186 .then((HttpClientRequest request) { | 186 .then((HttpClientRequest request) { |
187 request.write(json.stringify(joinRequest)); | 187 request.write(JSON.encode(joinRequest)); |
188 return request.close(); | 188 return request.close(); |
189 }) | 189 }) |
190 .then((HttpClientResponse response) { | 190 .then((HttpClientResponse response) { |
191 StringBuffer body = new StringBuffer(); | 191 StringBuffer body = new StringBuffer(); |
192 response.listen( | 192 response.listen( |
193 (data) => body.write(new String.fromCharCodes(data)), | 193 (data) => body.write(new String.fromCharCodes(data)), |
194 onDone: () => joinResponseHandler(response, body.toString())); | 194 onDone: () => joinResponseHandler(response, body.toString())); |
195 }); | 195 }); |
196 } | 196 } |
197 | 197 |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
311 TestMain testMain = new TestMain.run(10, 2); | 311 TestMain testMain = new TestMain.run(10, 2); |
312 } | 312 } |
313 | 313 |
314 | 314 |
315 void main() { | 315 void main() { |
316 testOneClient(); | 316 testOneClient(); |
317 testTwoClients(); | 317 testTwoClients(); |
318 testTwoClientsMoreMessages(); | 318 testTwoClientsMoreMessages(); |
319 testTenClients(); | 319 testTenClients(); |
320 } | 320 } |
OLD | NEW |