local to base node, add register, create functions

This commit is contained in:
oleg 2016-02-28 23:21:08 +03:00
parent baceedd70e
commit 2a7d6dfb98
6 changed files with 48 additions and 18 deletions

View File

@ -2,7 +2,7 @@
var Steppy = require('twostep').Steppy,
_ = require('underscore'),
Node = require('./node').Node,
createNode = require('./node').createNode,
EventEmitter = require('events').EventEmitter,
inherits = require('util').inherits,
logger = require('./logger')('distributor');
@ -36,8 +36,8 @@ inherits(Distributor, EventEmitter);
exports.Distributor = Distributor;
Distributor.prototype._createNode = function(nodeParams) {
return new Node(nodeParams);
Distributor.prototype._createNode = function(params) {
return createNode(params);
};
Distributor.prototype._runNext = function(callback) {

View File

@ -1,7 +1,6 @@
'use strict';
var _ = require('underscore'),
createExecutor = require('./executor').createExecutor;
var _ = require('underscore');
function Node(params) {
@ -76,13 +75,6 @@ Node.prototype.getFreeExecutorsCount = function() {
return this.maxExecutorsCount - _(this.executors).size();
};
Node.prototype._createExecutor = function(project) {
return createExecutor({
type: this.type,
project: project
});
};
Node.prototype.hasScmChanges = function(project, callback) {
this._createExecutor(project).hasScmChanges(callback);
};

18
lib/node/index.js Normal file
View File

@ -0,0 +1,18 @@
'use strict';
var constructors = {
local: require('./local').Node
};
exports.register = function(type, constructor) {
constructor[type] = constructor;
};
exports.createNode = function(params) {
if (params.type in constructors === false) {
throw new Error('Unknown node type: ', params.type);
}
var Constructor = constructors[params.type];
return new Constructor(params);
};

20
lib/node/local.js Normal file
View File

@ -0,0 +1,20 @@
'user strict';
var inherits = require('util').inherits,
ParentNode = require('./base').Node,
LocalExecutor = require('../executor/local').Executor;
function Node(params) {
ParentNode.call(this, params);
}
inherits(Node, ParentNode);
exports.Node = Node;
Node.prototype._createExecutor = function(project) {
return new LocalExecutor({
type: this.type,
project: project
});
};

View File

@ -2,16 +2,16 @@
var _ = require('underscore'),
sinon = require('sinon'),
Node = require('../../lib/node').Node,
createNode = require('../../lib/node').createNode,
EventEmitter = require('events').EventEmitter,
ProjectsCollection = require('../../lib/project').ProjectsCollection,
Distributor = require('../../lib/distributor').Distributor,
Notifier = require('../../lib/notifier').Notifier;
var createNode = function(executorRun) {
var createMockedNode = function(executorRun) {
return function(params) {
var node = new Node(params);
var node = createNode(params);
node._createExecutor = function(project) {
var executor = new EventEmitter();
executor.project = project;
@ -38,7 +38,7 @@ exports.createDistributor = function(params) {
distributorParams.executorRun || sinon.stub().callsArgAsync(1)
);
// patch method which will be called at constructor
sinon.stub(Distributor.prototype, '_createNode', createNode(
sinon.stub(Distributor.prototype, '_createNode', createMockedNode(
executorRun
));
delete distributorParams.executorRun;

View File

@ -1,6 +1,6 @@
'use strict';
var Node = require('../lib/node').Node,
var createNode = require('../lib/node').createNode,
expect = require('expect.js');
@ -19,7 +19,7 @@ describe('Node', function() {
describe('basic', function() {
it('instance should be created without errors', function() {
node = new Node({
node = createNode({
type: 'local',
maxExecutorsCount: 1
});