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

Unified Diff: samples/tests/samples/chat/chat_server_test.dart

Issue 23717003: Reorganize tests for samples so the test is discoverable from the sample (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 4 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 side-by-side diff with in-line comments
Download patch
Index: samples/tests/samples/chat/chat_server_test.dart
diff --git a/samples/tests/samples/chat/chat_server_test.dart b/samples/tests/samples/chat/chat_server_test.dart
deleted file mode 100644
index 7e7d1effd28fa0a46e6e9f1106d85d8d0590fa42..0000000000000000000000000000000000000000
--- a/samples/tests/samples/chat/chat_server_test.dart
+++ /dev/null
@@ -1,320 +0,0 @@
-// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
-// for details. All rights reserved. Use of this source code is governed by a
-// BSD-style license that can be found in the LICENSE file.
-//
-// VMOptions=
-// VMOptions=--short_socket_read
-// VMOptions=--short_socket_write
-// VMOptions=--short_socket_read --short_socket_write
-
-import "package:expect/expect.dart";
-import 'dart:io';
-import 'dart:isolate';
-import "dart:convert";
-import '../../../chat/chat_server_lib.dart';
-
-// Message to start chat test client running in an isolate.
-class ChatTestClientStart {
- ChatTestClientStart(int this.totalClients,
- int this.messagesToSend,
- int this.messagesToReceive,
- int this.port);
-
- int totalClients;
- int messagesToSend;
- int messagesToReceive;
- int port;
-}
-
-
-void startChatTestClient() {
- var client = new ChatTestClient();
- port.receive(client.dispatch);
-}
-
-
-// Chat server test client for running in a separate isolate. When
-// this test client is started it will join the chat topic, send a
-// number of messages, receive the expected number of messages and
-// leave the topic.
-class ChatTestClient {
- SendPort statusPort; // Port to reply to when test has finished.
- HttpClient httpClient; // HTTP client connection factory.
-
- int totalClients; // Total number of clients in the test.
- int messagesToSend; // Number of messages to send.
- int messagesToReceive; // Numbe rof messages expected to be received.
- int port; // TCP/IP port for server.
-
- String sessionId; // Session id when connected.
- int sendMessageNumber; // Number of messages sent.
- int joinCount;
- int messageCount;
- int receiveMessageNumber; // Number of messages received.
-
- void leave() {
- void leaveResponseHandler(response, String data) {
- Expect.equals(HttpStatus.OK, response.statusCode);
- var responseData = JSON.decode(data);
- Expect.equals("leave", responseData["response"]);
-
- // Test done.
- statusPort.send("Test succeeded", null);
- }
-
- Map leaveRequest = new Map();
- leaveRequest["request"] = "leave";
- leaveRequest["sessionId"] = sessionId;
- httpClient.post("127.0.0.1", port, "/leave")
- .then((HttpClientRequest request) {
- request.write(JSON.encode(leaveRequest));
- return request.close();
- })
- .then((HttpClientResponse response) {
- StringBuffer body = new StringBuffer();
- response.listen(
- (data) => body.write(new String.fromCharCodes(data)),
- onDone: () => leaveResponseHandler(response, body.toString()));
- });
- }
-
- void receive() {
- void receiveResponseHandler(response, String data) {
- Expect.equals(HttpStatus.OK, response.statusCode);
- var responseData = JSON.decode(data);
- Expect.equals("receive", responseData["response"]);
- Expect.equals(null, responseData["disconnect"]);
- for (int i = 0; i < responseData["messages"].length; i++) {
- Map message = responseData["messages"][i];
- if (message["type"] == "join") {
- joinCount++;
- } else if (message["type"] == "message") {
- messageCount++;
- } else {
- Expect.equals("leave", message["type"]);
- }
- if (totalClients == 1) {
- // Test the exact messages when this is the only client.
- Expect.equals(messagesToSend + 1, responseData["messages"].length);
- Expect.equals(i, message["number"]);
- if (i == 0) {
- Expect.equals("join", message["type"]);
- } else {
- Expect.equals("message", message["type"]);
- Expect.equals("message ${i - 1}", message["message"]);
- }
- }
- receiveMessageNumber = message["number"] + 1;
- }
-
- // Receive all expected messages then leave.
- if (messageCount < messagesToReceive) {
- receive();
- } else {
- Expect.equals(messagesToReceive, messageCount);
- Expect.equals(totalClients, joinCount);
- leave();
- }
- }
-
- Map receiveRequest = new Map();
- receiveRequest["request"] = "receive";
- receiveRequest["sessionId"] = sessionId;
- receiveRequest["nextMessage"] = receiveMessageNumber;
- httpClient.post("127.0.0.1", port, "/receive")
- .then((HttpClientRequest request) {
- request.write(JSON.encode(receiveRequest));
- return request.close();
- })
- .then((HttpClientResponse response) {
- StringBuffer body = new StringBuffer();
- response.listen(
- (data) => body.write(new String.fromCharCodes(data)),
- onDone: () => receiveResponseHandler(response, body.toString()));
- });
- }
-
- void sendMessage() {
- void sendResponseHandler(response, String data) {
- Expect.equals(HttpStatus.OK, response.statusCode);
- var responseData = JSON.decode(data);
- Expect.equals("message", responseData["response"]);
- sendMessageNumber++;
- if (sendMessageNumber < messagesToSend) {
- sendMessage();
- } else {
- receive();
- }
- }
-
- Map messageRequest = new Map();
- messageRequest["request"] = "message";
- messageRequest["sessionId"] = sessionId;
- messageRequest["message"] = "message $sendMessageNumber";
- httpClient.post("127.0.0.1", port, "/message")
- .then((HttpClientRequest request) {
- request.write(JSON.encode(messageRequest));
- return request.close();
- })
- .then((HttpClientResponse response) {
- StringBuffer body = new StringBuffer();
- response.listen(
- (data) => body.write(new String.fromCharCodes(data)),
- onDone: () => sendResponseHandler(response, body.toString()));
- });
- }
-
- void join() {
- void joinResponseHandler(response, String data) {
- Expect.equals(HttpStatus.OK, response.statusCode);
- var responseData = JSON.decode(data);
- Expect.equals("join", responseData["response"]);
- sessionId = responseData["sessionId"];
- Expect.isTrue(sessionId != null);
-
- joinCount = 0;
- messageCount = 0;
- sendMessageNumber = 0;
- receiveMessageNumber = 0;
- sendMessage();
- }
-
- Map joinRequest = new Map();
- joinRequest["request"] = "join";
- joinRequest["handle"] = "test1";
- httpClient.post("127.0.0.1", port, "/join")
- .then((HttpClientRequest request) {
- request.write(JSON.encode(joinRequest));
- return request.close();
- })
- .then((HttpClientResponse response) {
- StringBuffer body = new StringBuffer();
- response.listen(
- (data) => body.write(new String.fromCharCodes(data)),
- onDone: () => joinResponseHandler(response, body.toString()));
- });
- }
-
- void dispatch(message, replyTo) {
- totalClients = message.totalClients;
- messagesToSend = message.messagesToSend;
- messagesToReceive = message.messagesToReceive;
- port = message.port;
- statusPort = replyTo;
-
- // Create a HTTP client factory.
- httpClient = new HttpClient();
-
- // Start the client by joining the chat topic.
- join();
- }
-}
-
-
-class TestMain {
- TestMain.run(int this.clientCount, int this.messageCount)
- : serverStatusPort = new ReceivePort(),
- serverPort = null,
- finishedClients = 0 {
- serverPort = spawnFunction(startChatServer);
- start();
- }
-
- void start() {
- // Handle status messages from the server.
- serverStatusPort.receive(
- (var message, SendPort replyTo) {
- if (message.isStarted) {
- // When the server is started start all test clients.
- for (int i = 0; i < clientCount; i++) {
- ChatTestClientStart command =
- new ChatTestClientStart(clientCount,
- messageCount,
- messageCount * this.clientCount,
- message.port);
- clientPorts[i].send(command, clientStatusPorts[i].toSendPort());
- }
- } else if (message.isError) {
- print("Could not start server - probably error \"Address already in use\" from bind.");
- serverStatusPort.close();
- }
- });
-
- // Prepare the requested number of clients.
- clientPorts = new List<SendPort>(clientCount);
- int liveClientsCount = 0;
- clientStatusPorts = new List<ReceivePort>(clientCount);
- for (int i = 0; i < clientCount; i++) {
- ReceivePort statusPort = new ReceivePort();
- statusPort.receive((var message, SendPort replyTo) {
- // First and only message from the client indicates that
- // the test is done.
- Expect.equals("Test succeeded", message);
- statusPort.close();
- finishedClients++;
-
- // If all clients are finished shutdown the server.
- if (finishedClients == clientCount) {
- // Send server stop message to the server.
- serverPort.send(new ChatServerCommand.stop(),
- serverStatusPort.toSendPort());
-
- // Close the last port to terminate the test.
- serverStatusPort.close();
- }
- });
-
- clientStatusPorts[i] = statusPort;
- clientPorts[i] = spawnFunction(startChatTestClient);
- liveClientsCount++;
- if (liveClientsCount == clientCount) {
- // Once all clients are running send server start message to
- // the server. Use port 0 for an ephemeral port. The actual
- // port will be returned with the server started
- // message. Use a backlog equal to the client count to avoid
- // connection issues.
- serverPort.send(new ChatServerCommand.start("127.0.0.1", 0),
- serverStatusPort.toSendPort());
- }
- }
- }
-
- int clientCount; // Number of clients to run.
- int messageCount; // Number of messages per clients to send.
- int finishedClients; // Number of clients finished.
-
- // Ports for communicating with the server.
- SendPort serverPort;
- ReceivePort serverStatusPort;
- // Ports for communicating with the clients.
- List<SendPort> clientPorts;
- List<ReceivePort> clientStatusPorts;
-}
-
-
-void testOneClient() {
- TestMain testMain = new TestMain.run(1, 5);
-}
-
-
-void testTwoClients() {
- TestMain testMain = new TestMain.run(2, 5);
-}
-
-
-void testTwoClientsMoreMessages() {
- TestMain testMain = new TestMain.run(2, 10);
-}
-
-
-void testTenClients() {
- TestMain testMain = new TestMain.run(10, 2);
-}
-
-
-void main() {
- testOneClient();
- testTwoClients();
- testTwoClientsMoreMessages();
- testTenClients();
-}

Powered by Google App Engine
This is Rietveld 408576698