Skip to content

Commit

Permalink
[test] DRY tests a bit
Browse files Browse the repository at this point in the history
Add macro for creating a worker.
  • Loading branch information
mmalecki committed Dec 9, 2011
1 parent 831f76f commit bbc23e2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 36 deletions.
21 changes: 20 additions & 1 deletion test/helpers/macros.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
var assert = require('assert'),
path = require('path'),
spawn = require('child_process').spawn,
forever = require('../../lib/forever');
nssocket = require('nssocket'),
forever = require('../../lib/forever'),
Worker = require('../../lib/forever/worker').Worker;

var macros = exports;

Expand Down Expand Up @@ -73,3 +75,20 @@ macros.assertList = function (list) {
assert.lengthOf(list, 1);
};

macros.assertWorkerConnected = function (workerOptions, batch) {
return {
topic: function () {
var self = this,
reader = new nssocket.NsSocket(),
worker = new Worker(workerOptions);

worker.start(function (err, sock) {
reader.connect(sock, function () {
self.callback(null, reader, worker, workerOptions);
});
});
},
'worker should connect': batch
};
};

58 changes: 23 additions & 35 deletions test/worker-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,39 @@ var path = require('path'),
assert = require('assert'),
vows = require('vows'),
nssocket = require('nssocket'),
MonitorMock = require('./helpers/mocks/monitor').MonitorMock,
Worker = require('../lib/forever/worker').Worker;
macros = require('./helpers/macros'),
MonitorMock = require('./helpers/mocks/monitor').MonitorMock;

var SOCKET_PATH = path.join(__dirname, 'fixtures');

vows.describe('forever/worker').addBatch({
'When using forever worker': {
'and starting it and pinging it': {
topic: function () {
var self = this,
monitor = new MonitorMock();

var worker = new Worker({ sockPath: SOCKET_PATH, monitor: monitor }),
reader = new nssocket.NsSocket();
'and starting it and pinging it': macros.assertWorkerConnected({
monitor: new MonitorMock(),
sockPath: SOCKET_PATH
}, {
'and respond to pings': {
topic: function (reader) {
reader.send(['ping']);
reader.data(['pong'], this.callback);
},
'with `pong`': function () {}
},
'and when queried for data': {
topic: function (reader, _, options) {
var self = this;

worker.start(function (err, sock) {
reader.connect(sock, function () {
self.callback(null, reader, worker, monitor);
reader.send(['data']);
reader.data(['data'], function (data) {
self.callback(null, { data: data, monitor: options.monitor });
});
});
},
'it should connect': {
'and respond to pings': {
topic: function (reader) {
reader.send(['ping']);
reader.data(['pong'], this.callback);
},
'with `pong`': function () {}
},
'and when queried for data': {
topic: function (reader, _, monitor) {
var self = this;

reader.send(['data']);
reader.data(['data'], function (data) {
self.callback(null, { data: data, monitor: monitor });
});
},
'it should respond with data': function (obj) {
assert.isObject(obj.data);
assert.deepEqual(obj.data, obj.data);
}
'it should respond with data': function (obj) {
assert.isObject(obj.data);
assert.deepEqual(obj.data, obj.monitor.data);
}
}
}
})
}
}).export(module);

0 comments on commit bbc23e2

Please sign in to comment.