Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow using the same server/client in different tests #121

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 1 addition & 5 deletions .travis.yml
@@ -1,8 +1,4 @@
language: node_js
before_install:
- nvm install 0.10.12
- nvm use 0.10.12
- node -v
node_js:
- "0.8"
- "0.10.12"
- "0.10"
65 changes: 63 additions & 2 deletions bin/_laika
Expand Up @@ -5,7 +5,8 @@ var colors = require('colors');
var Injector = require('../lib/injector');
var App = require('../lib/app');
var AppPool = require('../lib/app_pool');
var testLogic = require('../lib/test_logic');
var testLogic = require('../lib/test_logic').testLogic;
var setupApp = require('../lib/test_logic').setupApp;
var helpers = require('../lib/helpers');
var logger = require('../lib/logger');
var qbox = require('qbox');
Expand All @@ -23,6 +24,7 @@ var ended = false;

module.exports = {
run: function(argv) {

if(argv.debug) {
logger.log('\n NOTICE: '.yellow + 'if you are looking for debug logs(-d or -D), use -V instead'.bold);
}
Expand Down Expand Up @@ -85,6 +87,18 @@ module.exports = {
}
}

function repeat(times, value) {
var array = [];
while (--times >= 0) {
if (typeof value === 'function') {
array.push(value());
} else {
array.push(value);
}
}
return array;
}

deps.ready(function() {
logger.info(' loading initial app pool...');
appPool = new AppPool({
Expand All @@ -94,13 +108,60 @@ module.exports = {
settingsFile: (argv.settings)? path.resolve(argv.settings) : null
});
appPool.on('ready', onAppPoolReady);

if(argv.ui == 'tdd') {
test = testLogic(appPool, phantom);
ltest = ltestWithWarning(test);
} else if(argv.ui == 'bdd') {
it = testLogic(appPool, phantom);
}
// see issue #62
setup = function (callback, clientOptions) {
var nClients = callback.length - 1;
var connectors;
var app;
if (arguments.length === 0) {
return;
}
// fix options
// check and fix options
if (!Array.isArray(clientOptions)) {
if (typeof clientOptions === 'object') {
// use clientOptions for each client
clientOptions = repeat(nClients, function () {
// XXX maybe it would be better to copy these options?
return Object.create(clientOptions);
});
} else if (typeof clientOptions === 'string') {
clientOptions = repeat(nClients, clientOptions);
} else {
clientOptions = repeat(nClients, undefined);
}
} else {
// make sure that we won't spawn unnecessary clients ...
clientOptions = clientOptions.slice(0, nClients);
// ... and that we have enough ...
while (clientOptions.length < nClients) {
// if no options is set, client won't be spawned correctly
// so we need to push an empty object at least
clientOptions.push(undefined);
}
}
// define before/after hooks
before(function (done) {
app = appPool.get();
setupApp(app, phantom, clientOptions, function () {
connectors = Array.prototype.slice.call(arguments);
callback.apply(null, arguments);
done();
});
});
after(function () {
connectors.forEach(function (connector) {
connector.close();
});
app.close();
});
}; // setup
});

function onAppPoolReady() {
Expand Down
26 changes: 21 additions & 5 deletions lib/connectors/client.js
Expand Up @@ -5,11 +5,19 @@ var logger = require('../logger');
var Connector = require('./connector');
var Future = require('fibers/future');

function ClientConnector(phantom, appUrl) {
Connector.call(this)
appUrl = appUrl || "http://localhost:3000";
function ClientConnector(phantom, options) {
Connector.call(this);
// check and fix options
if (typeof options === 'string') {
// for compatibility with older API
options = { appUrl: options };
} else if (typeof options !== 'object') {
options = options || {};
}
options.appUrl = options.appUrl || "http://localhost:3000";
//---------------------------------------------------------
var self = this;
self.appUrl = appUrl;
self.appUrl = options.appUrl;
var errorFired = false;
var pageOpenedCallbackFired = false;

Expand All @@ -21,8 +29,16 @@ function ClientConnector(phantom, appUrl) {
throw err;
} else {
page = p;
// apply different options
if (options.userAgent) {
page.set('settings.userAgent', options.userAgent);
}
if (options.viewport) {
page.set('viewportSize', options.viewport);
}
//---------------------------------------
page.onConsoleMessage = onConsoleMessage;
page.open(appUrl, afterOpened);
page.open(options.appUrl, afterOpened);
}
});

Expand Down
58 changes: 37 additions & 21 deletions lib/test_logic.js
Expand Up @@ -3,7 +3,37 @@ var ClientConnector = require('./connectors/client.js');
var Fiber = require('fibers');
var logger = require('./logger');

module.exports = function(appPool, phantom) {
var setupApp = function (app, phantom, listOfOptions, callback) {
var appPort = app.port;
var hostnames = ["localhost", "127.0.0.1", "0.0.0.0"];
app.ready(function(injectPort) {
var args = [new ServerConnector(injectPort)];
var noClients = listOfOptions.length;
var hostnameIssueWarned = false;
for (var lc = 0; lc < noClients; lc++) {
if (lc >= hostnames.length && !hostnameIssueWarned) {
logger.error(' WARN: It is recommended to use 3 clients only. see more - http://goo.gl/MMX3A');
hostnameIssueWarned = true;
}
var hostname = hostnames[lc] || "localhost";
// check and fix options
var clientOptions = listOfOptions[lc];
if (typeof clientOptions === 'string') {
clientOptions = { appUrl: clientOptions };
} else if (typeof clientOptions !== 'object') {
clientOptions = {};
}
clientOptions.appUrl = "http://" + hostname + ":" + appPort + (clientOptions.appUrl || "");
//-----------------------------------------------------------------------------------------
args.push(new ClientConnector(phantom, clientOptions));
}
callback.apply(null, args);
});
};

module.exports.setupApp = setupApp;

module.exports.testLogic = function(appPool, phantom) {
return function testLogic(message, callback) {

if(this._test) {
Expand All @@ -13,7 +43,7 @@ module.exports = function(appPool, phantom) {
}

function mockTest(done) {
logger.laika('start running test');
logger.laika('start running test');
var completed = false;
var args = [];
var app;
Expand All @@ -26,24 +56,10 @@ module.exports = function(appPool, phantom) {
} else {
//create new server with different db and port
app = appPool.get();
var appPort = app.port
var mongoDbname = app.dbname;
var hostnames = ["localhost", "127.0.0.1", "0.0.0.0"];

app.ready(function(injectPort) {
args = [cleanAndDone, new ServerConnector(injectPort)];
var noClients = callback.length - 2;
var hostnameIssueWarned = false;
for(var lc = 0; lc<noClients; lc++) {
if(lc >= hostnames.length && !hostnameIssueWarned) {
logger.error(' WARN: It is recommended to use 3 clients only. see more - http://goo.gl/MMX3A');
hostnameIssueWarned = true;
}
var hostname = hostnames[lc] || "localhost";
var appUrl = "http://" + hostname + ":" + appPort;
args.push(new ClientConnector(phantom, appUrl));
}

var emptyOptions = new Array(callback.length - 2);
setupApp(app, phantom, emptyOptions, function () {
args = Array.prototype.slice.apply(arguments); // copy argumetns
args.unshift(cleanAndDone);
Fiber(function() {
logger.laika('running test');
callback.apply(null, args);
Expand All @@ -56,7 +72,7 @@ module.exports = function(appPool, phantom) {
args.slice(1).forEach(function(connector) {
connector.close();
});
completeTest();
completeTest();
}
}

Expand Down