spies and mocks are coming, sinon is here

This commit is contained in:
oleg 2015-05-24 02:22:09 +03:00
parent 4261892083
commit fa48a5269a
3 changed files with 83 additions and 69 deletions

View File

@ -50,8 +50,7 @@ Distributor.prototype._runNext = function(callback) {
this.pass(node);
var queueItem = self.queue[queueItemIndex];
this.pass(queueItemIndex, queueItem);
var queueItem = self.queue.splice(queueItemIndex, 1)[0];
self._updateBuild(
queueItem.build,
@ -59,11 +58,9 @@ Distributor.prototype._runNext = function(callback) {
this.slot()
);
},
function(err, node, queueItemIndex, queueItem, build) {
self.queue.splice(queueItemIndex, 1);
function(err, node, build) {
var stepCallback = this.slot();
var executor = node.run(queueItem.project, build.params, function(err) {
var executor = node.run(build.project, build.params, function(err) {
self._updateBuild(
build,
{

View File

@ -43,9 +43,10 @@
"gulp-less": "3.0.3",
"gulp-nodemon": "2.0.3",
"gulp-react-jade-amd": "git://github.com/vladimir-polyakov/gulp-react-jade-amd",
"memdown": "1.0.0",
"main-bower-files": "2.7.0",
"memdown": "1.0.0",
"mocha": "1.18.2",
"nodemon": "1.3.7"
"nodemon": "1.3.7",
"sinon": "1.14.1"
}
}

View File

@ -3,7 +3,8 @@
var Distributor = require('../lib/distributor').Distributor,
Node = require('../lib/node').Node,
expect = require('expect.js'),
EventEmitter = require('events').EventEmitter;
EventEmitter = require('events').EventEmitter,
sinon = require('sinon')
describe('Distributor', function() {
@ -31,102 +32,117 @@ describe('Distributor', function() {
}
};
describe('with sucess project', function() {
var originalCreateNode;
describe('with success project', function() {
before(function() {
originalCreateNode = Distributor.prototype._createNode;
Distributor.prototype._createNode = createNodeMock(
function(params, callback) {
setTimeout(callback, 10);
}
);
var executorRun = function(params, callback) {
setTimeout(callback, 1);
};
sinon.stub(Distributor.prototype, '_createNode', createNodeMock(
executorRun
));
});
var updateBuildSpy;
it('instance should be created without errors', function() {
var number = 1;
var conditionsHash = {
1: {queue: {length: 0}, build: {status: 'queued'}},
2: {queue: {length: 1}, build: {status: 'in-progress'}},
3: {queue: {length: 0}, build: {status: 'done'}},
4: 'Should never happend'
};
var saveBuild = function(build, callback) {
expectUpdateBuild(distributor, build, number, conditionsHash);
number++;
callback(null, build)
};
distributor = new Distributor({
nodes: [{type: 'local', maxExecutorsCount: 1}],
saveBuild: saveBuild
nodes: [{type: 'local', maxExecutorsCount: 1}]
});
updateBuildSpy = sinon.spy(distributor, '_updateBuild');
});
it('should run without errors', function() {
it('should run without errors', function(done) {
distributor.run(project1, {}, function(err) {
expect(err).not.ok();
done();
});
});
it('wait for project done (should no errors)', function(done) {
setTimeout(done, 20);
it('build should be queued', function() {
var changes = updateBuildSpy.getCall(0).args[1];
expect(changes).only.have.keys(
'project', 'params', 'createDate', 'status', 'completed'
);
expect(changes.status).equal('queued');
expect(changes.completed).equal(false);
});
it('build should be in-progress', function() {
var changes = updateBuildSpy.getCall(1).args[1];
expect(changes).only.have.keys('startDate', 'status');
expect(changes.status).equal('in-progress');
});
it('build should be done', function() {
var changes = updateBuildSpy.getCall(2).args[1];
expect(changes).only.have.keys(
'endDate', 'status', 'completed', 'error'
);
expect(changes.status).equal('done');
expect(changes.completed).equal(true);
expect(changes.error).equal(null);
});
it('update build called 3 times in total', function() {
expect(updateBuildSpy.callCount).equal(3);
});
after(function() {
Distributor.prototype._createNode = originalCreateNode;
Distributor.prototype._createNode.restore();
});
});
describe('with fail project', function() {
var originalCreateNode;
before(function() {
originalCreateNode = Distributor.prototype._createNode;
Distributor.prototype._createNode = createNodeMock(
function(params, callback) {
setTimeout(function() {
callback(new Error('Some error'));
}, 10);
}
);
var executorRun = function(params, callback) {
setTimeout(function() {
callback(new Error('Some error'));
}, 1);
};
sinon.stub(Distributor.prototype, '_createNode', createNodeMock(
executorRun
));
});
var updateBuildSpy;
it('instance should be created without errors', function() {
var number = 1;
var conditionsHash = {
1: {queue: {length: 0}, build: {status: 'queued'}},
2: {queue: {length: 1}, build: {status: 'in-progress'}},
3: {
queue: {length: 0},
build: {status: 'error', error: {message: 'Some error'}}
},
4: 'Should never happend'
};
var saveBuild = function(build, callback) {
expectUpdateBuild(distributor, build, number, conditionsHash);
number++;
callback(null, build)
};
distributor = new Distributor({
nodes: [{type: 'local', maxExecutorsCount: 1}],
saveBuild: saveBuild
nodes: [{type: 'local', maxExecutorsCount: 1}]
});
updateBuildSpy = sinon.spy(distributor, '_updateBuild');
});
it('should run with errors', function() {
it('should run without errors', function(done) {
distributor.run(project1, {}, function(err) {
expect(err).not.ok();
done();
});
});
it('wait for project done (should no errors)', function(done) {
setTimeout(done, 20);
it('build should be queued', function() {
var changes = updateBuildSpy.getCall(0).args[1];
expect(changes.status).equal('queued');
});
it('build should be in-progress', function() {
var changes = updateBuildSpy.getCall(1).args[1];
expect(changes.status).equal('in-progress');
});
it('build should be fail', function() {
var changes = updateBuildSpy.getCall(2).args[1];
expect(changes.status).equal('error');
expect(changes.completed).equal(true);
expect(changes.error).equal('Some error');
});
it('update build called 3 times in total', function() {
expect(updateBuildSpy.callCount).equal(3);
});
after(function() {
Distributor.prototype._createNode = originalCreateNode;
Distributor.prototype._createNode.restore();
});
});
});