Browse files

phantoms test for demo & run engine.io tests

  • Loading branch information...
1 parent 2897907 commit 684d4f7a182c5a28cdefe49590c3c3a7540219ee @cadorn cadorn committed Sep 12, 2012
Showing with 353 additions and 12 deletions.
  1. +21 −11 demo/server.js
  2. +4 −1 package.json
  3. +127 −0 test/all.js
  4. +103 −0 test/demo-ui.js
  5. +98 −0 test/demo.js
View
32 demo/server.js
@@ -3,7 +3,8 @@
const PATH = require("path");
const ARCHITECT = require("architect");
-function startServer(port) {
+
+exports.main = function main(port, callback) {
var plugins = [
{
@@ -50,15 +51,15 @@ function startServer(port) {
// Fires once for every *new* client connection (not reconnects).
TRANSPORT.on("connect", function(connection) {
- console.log("[port: " + port + "] Connected:", connection.id);
+ console.log("Connected:", connection.id);
// Fires once after reconnect attempts have failed and a timeout has passed.
connection.once("disconnect", function(reason) {
- console.log("[port: " + port + "] Disconnected:", connection.id, reason);
+ console.log("Disconnected:", connection.id, reason);
});
connection.on("message", function(message) {
- console.log("[port: " + port + "] Got message:", message);
+ console.log("Got message:", message);
if (typeof message === "string" && message.indexOf("ping:") === 0) {
connection.send("pong: " + message.match(/\d+$/)[0]);
}
@@ -67,12 +68,12 @@ function startServer(port) {
connection.send({say:"Connected"});
connection.on("away", function() {
- console.log("[port: " + port + "] Away:", connection.id);
+ console.log("Away:", connection.id);
connection.send({say:"While server away"});
});
connection.on("back", function() {
- console.log("[port: " + port + "] Back:", connection.id);
+ console.log("Back:", connection.id);
connection.send({say:"Server back"});
});
});
@@ -84,12 +85,21 @@ function startServer(port) {
ARCHITECT.createApp(ARCHITECT.resolveConfig(plugins, __dirname), function (err, app) {
if (err) {
- console.error("While starting!");
- throw err;
+ return callback(err);
}
- console.log("Started!");
+ callback(null, app);
});
}
-var port = parseInt(process.env.PORT || 8080, 10);
-startServer(port);
+if (require.main === module) {
+
+ var port = parseInt(process.env.PORT || 8080, 10);
+
+ exports.main(port, function(err) {
+ if (err) {
+ console.error(err.stack);
+ process.exit(1);
+ }
+ // Server should now be running.
+ });
+}
View
5 package.json
@@ -15,6 +15,9 @@
"pm": "npm",
"dependencies": {
"smith": "~0.1.7",
- "engine.io": "https://github.com/c9/engine.io/tarball/37f7d4f23f0462731ca65f905b7d8e8ce1336dde"
+ "engine.io": "https://github.com/c9/engine.io/tarball/5bc3682e6a438ba913c996627989f1134c26efb2"
+ },
+ "scripts": {
+ "test": "node test/all.js"
}
}
View
127 test/all.js
@@ -0,0 +1,127 @@
+
+const PATH = require("path");
+const FS = require("fs");
+const SPAWN = require("child_process").spawn;
+
+
+function main(callback) {
+
+ console.log("Running `./demo`:");
+
+ require("./demo").main(function(err) {
+ if (err) return callback(err);
+
+ return runEngineIoTests(callback);
+ });
+
+
+ // @see http://stackoverflow.com/questions/1187518/javascript-array-difference
+ function diff(a, b) {
+ return a.filter(function(i) {return !(b.indexOf(i) > -1);});
+ };
+
+ function runEngineIoTests(callback) {
+
+ // TODO: Use `sm` for this: `sm test <packagePath>`
+ function ensureDevDependenciesInstalled(packagePath, callback) {
+
+ var keys = Object.keys(JSON.parse(FS.readFileSync(PATH.join(packagePath, "package.json"))).devDependencies);
+ var dirs = FS.readdirSync(PATH.join(packagePath, "node_modules"));
+
+ // See if dev dependencies are missing.
+ if (diff(keys, dirs).length > 0) {
+ var proc = SPAWN("npm", [
+ "install"
+ ], {
+ cwd: packagePath
+ });
+
+ proc.on("error", function(err) {
+ callback(err);
+ });
+
+ proc.stdout.on("data", function(data) {
+ process.stdout.write(data.toString());
+ });
+ proc.stderr.on("data", function(data) {
+ process.stderr.write(data.toString());
+ });
+ proc.on("exit", function(code) {
+ if (code !== 0) {
+ callback(new Error("Did not get `status === 0`!"));
+ return;
+ }
+ callback(null);
+ });
+ } else {
+ callback(null);
+ }
+ }
+
+ // TODO: Use `sm` for this: `sm test <packagePath>`
+ function testPackage(packagePath, callback) {
+
+ var proc = SPAWN("npm", [
+ "test"
+ ], {
+ cwd: packagePath
+ });
+
+ proc.on("error", function(err) {
+ callback(err);
+ });
+
+ proc.stdout.on("data", function(data) {
+ process.stdout.write(data.toString());
+ });
+ proc.stderr.on("data", function(data) {
+ process.stderr.write(data.toString());
+ });
+ proc.on("exit", function(code) {
+ if (code !== 0) {
+ callback(new Error("Did not get `status === 0`!"));
+ return;
+ }
+ callback(null);
+ });
+ }
+
+
+ console.log("Running `engine.io` and `engine.io-client` tests:")
+
+ var path = PATH.join(__dirname, "../node_modules/engine.io");
+
+ ensureDevDependenciesInstalled(path, function(err) {
+ if (err) return callback(err);
+
+ testPackage(path, function(err) {
+ if (err) return callback(err);
+
+ path = PATH.join(__dirname, "../demo/node_modules/engine.io-client");
+
+ ensureDevDependenciesInstalled(path, function(err) {
+ if (err) return callback(err);
+
+ testPackage(path, function(err) {
+ if (err) return callback(err);
+
+ callback(null);
+ });
+ });
+ });
+ });
+ }
+
+}
+
+
+if (require.main === module) {
+ main(function(err) {
+ if (err) {
+ console.error(err.stack);
+ process.exit(1);
+ }
+ console.log("OK");
+ process.exit(0);
+ });
+}
View
103 test/demo-ui.js
@@ -0,0 +1,103 @@
+//#!/usr/bin/env phantomjs
+
+var SYSTEM = require("system");
+var WEBPAGE = require("webpage");
+var FS = require("fs");
+
+
+function main(callback) {
+
+ function error(err) {
+ callback(err);
+ }
+
+ var messages = [];
+ var steps = [];
+ var page = WEBPAGE.create();
+
+ page.onLoadStarted = function () {
+ console.log("Start loading ...");
+ };
+
+ page.onConsoleMessage = function (msg) {
+ if (/^:error:/.test(msg)) {
+ error(msg.substring(7));
+ return;
+ } else
+ if (/^:/.test(msg)) {
+ steps[msg.substring(1)]();
+ return;
+ }
+
+ messages.push(msg);
+ console.log("[phantomjs]", msg);
+ };
+
+ page.onLoadFinished = function (status) {
+ console.log("Loading finished. Running tests ...");
+
+ try {
+
+ // Check some basic things.
+ if (status !== "success") {
+ return error("Page did not load with 'success'!");
+ }
+
+ steps["start"] = function() {
+ page.evaluate(function () {
+ console.log(":wait-a-bit");
+ });
+ }
+
+ steps["wait-a-bit"] = function() {
+ setTimeout(function() {
+
+ if (messages[0] !== "Connecting") {
+ return error('`message[0] !== "Connecting"');
+ }
+ if (messages[1] !== "Connected") {
+ return error('`message[0] !== "Connected"');
+ }
+ if (messages[2].indexOf("Relaying message") !== 0) {
+ return error('`message[0].indexOf("Relaying message") !== 0"');
+ }
+ if (messages[3] !== "Send ping: 1") {
+ return error('`message[0] !== "Send ping: 1"');
+ }
+ if (messages[4] !== "Received pong: 1") {
+ return error('`message[0] !== "Received pong: 1"');
+ }
+ if (messages[5] !== "Send ping: 2") {
+ return error('`message[0] !== "Send ping: 2"');
+ }
+ if (messages[6] !== "Received pong: 2") {
+ return error('`message[0] !== "Received pong: 2"');
+ }
+
+ steps["done"]();
+
+ }, 3000);
+ }
+
+ steps["done"] = function() {
+ callback(null);
+ }
+
+ steps["start"]();
+
+ } catch(err) {
+ callback(err);
+ }
+ };
+
+ page.open("http://localhost:" + SYSTEM.args[1] + "/");
+}
+
+main(function(err) {
+ if (err) {
+ console.error((typeof err === "object" && err.stack)?err.stack:err);
+ console.error("ERROR");
+ phantom.exit(1);
+ }
+ phantom.exit(0);
+});
View
98 test/demo.js
@@ -0,0 +1,98 @@
+
+const ASSERT = require("assert");
+const DEMO_SERVER = require("../demo/server");
+const SPAWN = require("child_process").spawn;
+
+
+exports.main = function (callback) {
+
+ var port = parseInt(process.env.PORT || 8080, 10);
+
+ DEMO_SERVER.main(port, function(err, app) {
+ if (err) return callback(err);
+
+
+ var messages = [];
+
+ var TRANSPORT = app.getService("smith.transport.server");
+
+ TRANSPORT.on("connect", function(connection) {
+
+ messages.push("Connected");
+
+ // Fires once after reconnect attempts have failed and a timeout has passed.
+ connection.once("disconnect", function(reason) {
+ messages.push(["Disconnected:", reason].join(" "));
+ });
+
+ connection.on("message", function(message) {
+ messages.push(["Got message:", JSON.stringify(message)].join(" "));
+ });
+
+ connection.on("away", function() {
+ messages.push("Away");
+ });
+
+ connection.on("back", function() {
+ messages.push("Back");
+ });
+ });
+
+ console.log("Running `./demo-ui.js` via phantomjs.");
+
+ var proc = SPAWN("phantomjs", [
+ "demo-ui.js",
+ port
+ ], {
+ cwd: __dirname
+ });
+
+ proc.on("error", function(err) {
+ callback(err);
+ });
+
+ var buffer = [];
+ proc.stdout.on("data", function(data) {
+ buffer.push(data.toString());
+ process.stdout.write(data.toString());
+ });
+ proc.stderr.on("data", function(data) {
+ buffer.push(data.toString());
+ process.stderr.write(data.toString());
+ });
+ proc.on("exit", function(code) {
+ if (code !== 0) {
+ callback(new Error("Did not get `status === 0`!"));
+ return;
+ }
+ else if (/ERROR\n?$/.test(buffer.join(""))) {
+ callback(new Error("Got demo-ui error: " + buffer));
+ return;
+ }
+
+ console.log("messages", messages);
+
+ ASSERT.equal(messages.length, 5);
+
+ ASSERT.equal(messages[0], 'Connected');
+ ASSERT.equal(messages[1], 'Got message: {"say":"Connected"}');
+ ASSERT.equal(messages[2], 'Got message: "ping: 1"');
+ ASSERT.equal(messages[3], 'Got message: "ping: 2"');
+ ASSERT.equal(messages[4], 'Away');
+
+ callback(null);
+ });
+ });
+}
+
+
+if (require.main === module) {
+ exports.main(function(err) {
+ if (err) {
+ console.error(err.stack);
+ process.exit(1);
+ }
+ console.log("OK");
+ process.exit(0);
+ });
+}

0 comments on commit 684d4f7

Please sign in to comment.