mirror of
https://gitlab.silvrtree.co.uk/martind2000/nci.git
synced 2025-02-10 19:19:16 +00:00
more distributor tests
This commit is contained in:
parent
62533701c2
commit
eede98c665
@ -13,6 +13,10 @@ function Distributor(params) {
|
||||
});
|
||||
// queued projects to build
|
||||
self.queue = [];
|
||||
|
||||
self.onBuildUpdate = params.onBuildUpdate || function(err, build) {
|
||||
callback(err, build);
|
||||
};
|
||||
}
|
||||
|
||||
exports.Distributor = Distributor;
|
||||
@ -53,6 +57,7 @@ Distributor.prototype._runNext = function(callback) {
|
||||
var stepCallback = this.slot();
|
||||
node.run(queueItem.project, build.params, function(err) {
|
||||
build.status = err ? 'error' : 'done';
|
||||
build.error = err;
|
||||
self._updateBuild(build, stepCallback);
|
||||
});
|
||||
},
|
||||
@ -61,7 +66,7 @@ Distributor.prototype._runNext = function(callback) {
|
||||
};
|
||||
|
||||
Distributor.prototype._updateBuild = function(build, callback) {
|
||||
callback(null, build);
|
||||
this.onBuildUpdate(build, callback);
|
||||
};
|
||||
|
||||
Distributor.prototype.run = function(project, params, callback) {
|
||||
|
@ -9,46 +9,54 @@ describe('Distributor', function() {
|
||||
var distributor,
|
||||
project1 = {name: 'project1'};
|
||||
|
||||
var createNodeMock = function(executorRun) {
|
||||
return function(params) {
|
||||
var node = new Node(params);
|
||||
node._createExecutor = function() {
|
||||
return {run: executorRun};
|
||||
};
|
||||
return node;
|
||||
};
|
||||
};
|
||||
|
||||
var expectUpdateBuild = function(distributor, build, number, conditionsHash) {
|
||||
var conditions = conditionsHash[number];
|
||||
expect(distributor.queue).length(conditions.queue.length);
|
||||
expect(build.status).equal(conditions.build.status);
|
||||
if (build.status === 'error') {
|
||||
expect(build.error.message).eql(conditions.build.error.message);
|
||||
}
|
||||
};
|
||||
|
||||
describe('with sucess project', function() {
|
||||
var originalCreateNode,
|
||||
originalUpdateBuild;
|
||||
var originalCreateNode;
|
||||
|
||||
before(function() {
|
||||
originalCreateNode = Distributor.prototype._createNode;
|
||||
Distributor.prototype._createNode = function(params) {
|
||||
var node = new Node(params);
|
||||
node._createExecutor = function() {
|
||||
return {run: function(params, callback) {
|
||||
setTimeout(callback, 100);
|
||||
}};
|
||||
};
|
||||
return node;
|
||||
};
|
||||
|
||||
originalUpdateBuild = Distributor.prototype._updateBuild;
|
||||
|
||||
var updateBuildNumber = 1;
|
||||
Distributor.prototype._updateBuild = function(build, callback) {
|
||||
if (updateBuildNumber === 1) {
|
||||
expect(distributor.queue).length(0);
|
||||
expect(build.status).equal('waiting');
|
||||
} else if (updateBuildNumber === 2) {
|
||||
expect(distributor.queue).length(1);
|
||||
expect(build.status).equal('in-progress');
|
||||
} else if (updateBuildNumber === 3) {
|
||||
expect(distributor.queue).length(0);
|
||||
expect(build.status).equal('done');
|
||||
} else {
|
||||
throw new Error('Should never happend');
|
||||
Distributor.prototype._createNode = createNodeMock(
|
||||
function(params, callback) {
|
||||
setTimeout(callback, 10);
|
||||
}
|
||||
updateBuildNumber++;
|
||||
callback(null, build)
|
||||
};
|
||||
);
|
||||
});
|
||||
|
||||
it('instance should be created without errors', function() {
|
||||
var number = 1;
|
||||
var conditionsHash = {
|
||||
1: {queue: {length: 0}, build: {status: 'waiting'}},
|
||||
2: {queue: {length: 1}, build: {status: 'in-progress'}},
|
||||
3: {queue: {length: 0}, build: {status: 'done'}},
|
||||
4: 'Should never happend'
|
||||
};
|
||||
var onBuildUpdate = function(build, callback) {
|
||||
expectUpdateBuild(distributor, build, number, conditionsHash);
|
||||
number++;
|
||||
callback(null, build)
|
||||
};
|
||||
|
||||
distributor = new Distributor({
|
||||
nodes: [{type: 'local', maxExecutorsCount: 1}]
|
||||
nodes: [{type: 'local', maxExecutorsCount: 1}],
|
||||
onBuildUpdate: onBuildUpdate
|
||||
});
|
||||
});
|
||||
|
||||
@ -59,12 +67,63 @@ describe('Distributor', function() {
|
||||
});
|
||||
|
||||
it('wait for project done (should no errors)', function(done) {
|
||||
setTimeout(done, 100);
|
||||
setTimeout(done, 20);
|
||||
});
|
||||
|
||||
after(function() {
|
||||
Distributor.prototype._createNode = originalCreateNode;
|
||||
});
|
||||
});
|
||||
|
||||
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);
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
it('instance should be created without errors', function() {
|
||||
var number = 1;
|
||||
var conditionsHash = {
|
||||
1: {queue: {length: 0}, build: {status: 'waiting'}},
|
||||
2: {queue: {length: 1}, build: {status: 'in-progress'}},
|
||||
3: {
|
||||
queue: {length: 0},
|
||||
build: {status: 'error', error: {message: 'Some error'}}
|
||||
},
|
||||
4: 'Should never happend'
|
||||
};
|
||||
var onBuildUpdate = function(build, callback) {
|
||||
expectUpdateBuild(distributor, build, number, conditionsHash);
|
||||
number++;
|
||||
callback(null, build)
|
||||
};
|
||||
|
||||
distributor = new Distributor({
|
||||
nodes: [{type: 'local', maxExecutorsCount: 1}],
|
||||
onBuildUpdate: onBuildUpdate
|
||||
});
|
||||
});
|
||||
|
||||
it('should run with errors', function() {
|
||||
distributor.run(project1, {}, function(err) {
|
||||
expect(err).not.ok();
|
||||
});
|
||||
});
|
||||
|
||||
it('wait for project done (should no errors)', function(done) {
|
||||
setTimeout(done, 20);
|
||||
});
|
||||
|
||||
after(function() {
|
||||
Distributor.prototype._createNode = originalCreateNode;
|
||||
Distributor.prototype._updateBuild = originalUpdateBuild;
|
||||
});
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user