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); this.pass(node);
var queueItem = self.queue[queueItemIndex]; var queueItem = self.queue.splice(queueItemIndex, 1)[0];
this.pass(queueItemIndex, queueItem);
self._updateBuild( self._updateBuild(
queueItem.build, queueItem.build,
@ -59,11 +58,9 @@ Distributor.prototype._runNext = function(callback) {
this.slot() this.slot()
); );
}, },
function(err, node, queueItemIndex, queueItem, build) { function(err, node, build) {
self.queue.splice(queueItemIndex, 1);
var stepCallback = this.slot(); 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( self._updateBuild(
build, build,
{ {

View File

@ -43,9 +43,10 @@
"gulp-less": "3.0.3", "gulp-less": "3.0.3",
"gulp-nodemon": "2.0.3", "gulp-nodemon": "2.0.3",
"gulp-react-jade-amd": "git://github.com/vladimir-polyakov/gulp-react-jade-amd", "gulp-react-jade-amd": "git://github.com/vladimir-polyakov/gulp-react-jade-amd",
"memdown": "1.0.0",
"main-bower-files": "2.7.0", "main-bower-files": "2.7.0",
"memdown": "1.0.0",
"mocha": "1.18.2", "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, var Distributor = require('../lib/distributor').Distributor,
Node = require('../lib/node').Node, Node = require('../lib/node').Node,
expect = require('expect.js'), expect = require('expect.js'),
EventEmitter = require('events').EventEmitter; EventEmitter = require('events').EventEmitter,
sinon = require('sinon')
describe('Distributor', function() { describe('Distributor', function() {
@ -31,102 +32,117 @@ describe('Distributor', function() {
} }
}; };
describe('with sucess project', function() { describe('with success project', function() {
var originalCreateNode;
before(function() { before(function() {
originalCreateNode = Distributor.prototype._createNode; var executorRun = function(params, callback) {
Distributor.prototype._createNode = createNodeMock( setTimeout(callback, 1);
function(params, callback) { };
setTimeout(callback, 10); sinon.stub(Distributor.prototype, '_createNode', createNodeMock(
} executorRun
); ));
}); });
var updateBuildSpy;
it('instance should be created without errors', function() { 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({ distributor = new Distributor({
nodes: [{type: 'local', maxExecutorsCount: 1}], nodes: [{type: 'local', maxExecutorsCount: 1}]
saveBuild: saveBuild
}); });
updateBuildSpy = sinon.spy(distributor, '_updateBuild');
}); });
it('should run without errors', function() { it('should run without errors', function(done) {
distributor.run(project1, {}, function(err) { distributor.run(project1, {}, function(err) {
expect(err).not.ok(); expect(err).not.ok();
done();
}); });
}); });
it('wait for project done (should no errors)', function(done) { it('build should be queued', function() {
setTimeout(done, 20); 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() { after(function() {
Distributor.prototype._createNode = originalCreateNode; Distributor.prototype._createNode.restore();
}); });
}); });
describe('with fail project', function() { describe('with fail project', function() {
var originalCreateNode;
before(function() { before(function() {
originalCreateNode = Distributor.prototype._createNode; var executorRun = function(params, callback) {
Distributor.prototype._createNode = createNodeMock(
function(params, callback) {
setTimeout(function() { setTimeout(function() {
callback(new Error('Some error')); callback(new Error('Some error'));
}, 10); }, 1);
} };
); sinon.stub(Distributor.prototype, '_createNode', createNodeMock(
executorRun
));
}); });
var updateBuildSpy;
it('instance should be created without errors', function() { 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({ distributor = new Distributor({
nodes: [{type: 'local', maxExecutorsCount: 1}], nodes: [{type: 'local', maxExecutorsCount: 1}]
saveBuild: saveBuild
}); });
updateBuildSpy = sinon.spy(distributor, '_updateBuild');
}); });
it('should run with errors', function() { it('should run without errors', function(done) {
distributor.run(project1, {}, function(err) { distributor.run(project1, {}, function(err) {
expect(err).not.ok(); expect(err).not.ok();
done();
}); });
}); });
it('wait for project done (should no errors)', function(done) { it('build should be queued', function() {
setTimeout(done, 20); 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() { after(function() {
Distributor.prototype._createNode = originalCreateNode; Distributor.prototype._createNode.restore();
}); });
}); });
}); });