Refactor, end to end integration, tests failing

Most event emitters are now merged with the exports object, instead of having a dedicated "events" property, preventing an uncecessarily verbose API. Sockets are now held inside an object labeled by their port number to allow for multiple sockets open at once. That's important for the reporters, who'll likely be operating on a separate reporter socket.

Basic server test on `index.js` to ensure everything is working from server to client. So far, success! However, by integrating everything, there were many things that had to change, breaking the tests.

Next steps: implement client/server asynchronous discourse model, and relay socket data through the test event emitter.
This commit is contained in:
Jesse Gibson
2016-03-18 12:41:48 -06:00
parent f9224d9898
commit 3e9daf366b
5 changed files with 70 additions and 26 deletions

View File

@@ -2,7 +2,7 @@
"name": "panic",
"version": "0.1.0",
"description": "Test gun against a storm of requests",
"main": "index.js",
"main": "src/index.js",
"scripts": {
"test": "jasmine",
"start": "node server",

View File

@@ -2,14 +2,14 @@
'use strict';
var Emitter = require('events');
var assign = require('object-assign-deep');
var io = require('socket.io');
var server;
function subscribe(socket) {
socket.on('connection', function (client) {
server.events.emit('join', client);
server.emit('join', client);
});
}
@@ -18,22 +18,31 @@ function open(port) {
// set default port
port = port || 8080;
// close old socket
if (server.socket) {
server.socket.close();
// don't try to re-open
if (server.sockets[port]) {
return server.sockets[port];
}
// update state
server.port = port;
server.socket = io(port);
var socket = io(port);
server.sockets[port] = socket;
subscribe(server.socket);
return server.socket;
subscribe(socket);
return socket;
}
module.exports = server = {
events: new Emitter(),
socket: null,
function close(port) {
var socket = server.sockets[port];
if (socket) {
socket.close();
}
return socket;
}
server = module.exports = new Emitter();
assign(module.exports, {
sockets: {},
port: null,
open: open
};
open: open,
close: close
});

View File

@@ -39,6 +39,11 @@ Test.prototype = new Emitter();
assign(Test.prototype, {
constructor: Test,
env: function (obj) {
assign(this.config.env, obj);
return this;
},
server: function (cb, args) {
this.config.cbs.push({
conditional: 'typeof global !== "undefined"',
@@ -47,12 +52,12 @@ assign(Test.prototype, {
return this;
},
client: function (cb, args) {
client: function (cb) {
this.config.cbs.push({
args: args,
conditional: 'typeof window !== "undefined"',
cb: cb
});
return this;
}
});

View File

@@ -7,6 +7,8 @@ var server = require('../../server');
var stack;
function push(test) {
test.on('done', stack.shift);
if (!stack.current) {
stack.current = test;
} else {
@@ -14,15 +16,7 @@ function push(test) {
}
}
module.exports = stack = new Emitter();
assign(module.exports, {
current: null,
next: [],
completed: [],
push: push
});
stack.on('done', function () {
function shift() {
var test = stack.next.shift();
if (stack.current) {
stack.completed.push(stack.current);
@@ -31,4 +25,23 @@ stack.on('done', function () {
if (test) {
stack.current = test;
}
stack.emit('shift', stack.current);
}
module.exports = stack = new Emitter();
assign(module.exports, {
current: null,
next: [],
completed: [],
push: push,
shift: shift
});
/*
Our server won't be running
if there are no tests.
*/
server.on('join', function (socket) {
console.log('Sending', stack.current);
socket.emit('test', stack.current);
});

View File

@@ -2,6 +2,23 @@
'use strict';
var Test = require('./framework/Test');
var server = require('../server');
global.test = Test;
module.exports = Test;
global.test('Panic client', function () {
this.env({
msg: "You are"
});
this.env({
status: 'amazing'
});
this.client(function () {
console.log("We're online!");
});
});
server.open(8080);