Skip to content

Commit

Permalink
Test new setup structure
Browse files Browse the repository at this point in the history
  • Loading branch information
RafaelVidaurre committed Apr 15, 2015
1 parent 498faf4 commit 20d6210
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 231 deletions.
45 changes: 8 additions & 37 deletions agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ function Agent (id) {
*/
this.__applied = false;

/**
* List of functions which modify the Agent's configuration (provided by setup())
* @private
*/
this.__configCallbacks = [];

/**
* Agent's configuration object (set by running all configCallback functions)
* @private
Expand Down Expand Up @@ -58,17 +52,6 @@ function Agent (id) {
this.id = id;
}

/**
* Run functions passed via config(), thus applying their config logic
* @private
*/
Agent.prototype.__applyConfigCallbacks = function () {
var _this = this;
_.each(_this.__configCallbacks, function (configCallback) {
configCallback(_this.__config);
});
};

/**
* Turns every element in the execution plan into an array for type consistency
* @private
Expand Down Expand Up @@ -108,41 +91,29 @@ Agent.prototype.__formatPlan = function () {
this._plan = formattedPlan;
};

/**
* Applies all task definitions
* @private
*/
Agent.prototype.__applyTaskDefinitions = function () {
_.each(this._taskDefinitions, function (taskDefinition) {
taskDefinition._applySetup();
});
};

/**
* Applies all necessary processes regarding the setup stage of the agent
*/
Agent.prototype._applySetup = function () {
if (this.__applied) {
return;
}
this.__applyConfigCallbacks();
this.__applyTaskDefinitions();

this.__formatPlan();
this.__applied = true;
};

/**
* Saves a configuration function into the config callbacks array
* @param {function} cbConfig method which modifies the agent's config object (passed as argument)
* Sets the task's execution plan
* @param {Array} executionPlan array representing the execution plan for this agent
*/
Agent.prototype.setup = function (cbConfig) {
if (!_.isFunction(cbConfig)) {
throw new Error('Setup argument must be a function');
Agent.prototype.plan = function (executionPlan) {
// TODO: Validate execution plan format right away
if (!_.isArray(executionPlan)) {
throw new Error('Agent plan must be an array of task ids');
}

this.__configCallbacks.push(cbConfig);

return this;
this.__config.plan = executionPlan;
};

/**
Expand Down
51 changes: 23 additions & 28 deletions spec/agent.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,37 +20,36 @@ beforeEach(function () {
});

describe('Agent', function () {
describe('#setup', function () {
var error;
describe('#plan', function () {
it('should set the execution plan', function () {
var agent;

error = 'Setup argument must be a function';
agent = yakuza.agent('Scraper', 'Agent');

agent.plan([
'Task1'
]);

agent.__config.plan.should.eql(['Task1']);
});

it('should throw if argument is not an array', function () {
var error;

error = 'Agent plan must be an array of task ids';

it('should throw if argument is not a function', function () {
(function () {
yakuza.agent('Scraper', 'Agent').setup('foo');
yakuza.agent('Scraper', 'Agent').plan(123);
}).should.throw(error);

(function () {
yakuza.agent('Scraper', 'Agent').setup(['foo']);
yakuza.agent('Scraper', 'Agent').plan({foo: 'bar'});
}).should.throw(error);

(function () {
yakuza.agent('Scraper', 'Agent').setup(123);
yakuza.agent('Scraper', 'Agent').plan('foo');
}).should.throw(error);
});

it('it should add a config callback', function (done) {
yakuza.agent('Scraper', 'Agent').setup(function (config) {
config.plan = [
'Task1'
];
done();
});

yakuza.task('Scraper', 'Agent', 'Task1').main(function (task) {
task.success();
});

yakuza.ready();
});
});

describe('#task', function () {
Expand All @@ -65,13 +64,9 @@ describe('Agent', function () {
beforeEach(function () {
agent = yakuza.agent('Scraper', 'Agent');
});

it('should create an agent-level routine', function () {
agent.setup(function (config) {
config.plan = [
'Task1',
'Task2'
];
});
agent.plan(['Task1', 'Task2']);
agent.routine('OnlyOne', ['Task1']);
yakuza.job('Scraper', 'Agent').routine('OnlyOne');
});
Expand Down
38 changes: 14 additions & 24 deletions spec/job.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,11 @@ beforeEach(function () {
yakuza = new YakuzaBase();

yakuza.scraper('Scraper');
yakuza.agent('Scraper', 'Parallel').setup(function (config) {
config.plan = [
'Task1',
['Task2', 'Task3'],
'Task4'
];
});
yakuza.agent('Scraper', 'Parallel').plan([
'Task1',
['Task2', 'Task3'],
'Task4'
]);

yakuza.task('Scraper', 'Parallel', 'Task1').main(function (task) {
task.success(1);
Expand Down Expand Up @@ -182,12 +180,10 @@ describe('Job', function () {

beforeEach(function () {
newYakuza = new YakuzaBase();
newYakuza.agent('FooScraper', 'FooAgent').setup(function (config) {
config.plan = [
'FailTask',
'SuccessTask'
];
});
newYakuza.agent('FooScraper', 'FooAgent').plan([
'FailTask',
'SuccessTask'
]);
newYakuza.task('FooScraper', 'FooAgent', 'FailTask').main(function (task) {
task.fail(new Error('Error!'));
});
Expand Down Expand Up @@ -360,11 +356,7 @@ describe('Job', function () {
it('should throw if enqueued tasks are not defined', function () {
var invalidJob;

yakuza.agent('Scraper', 'InvalidAgent').setup(function (config) {
config.plan = [
'FakeTask'
];
});
yakuza.agent('Scraper', 'InvalidAgent').plan(['FakeTask']);

invalidJob = yakuza.job('Scraper', 'InvalidAgent');
invalidJob.enqueue('FakeTask');
Expand All @@ -377,12 +369,10 @@ describe('Job', function () {

describe('execution queue', function () {
beforeEach(function () {
yakuza.scraper('QueueTest').agent('SyncTest').setup(function (config) {
config.plan = [
{taskId: 'SyncTask', selfSync: true},
'AsyncTask'
];
});
yakuza.scraper('QueueTest').agent('SyncTest').plan([
{taskId: 'SyncTask', selfSync: true},
'AsyncTask'
]);
yakuza.task('QueueTest', 'SyncTest', 'SyncTask').builder(function () {
return [1, 2];
})
Expand Down
18 changes: 7 additions & 11 deletions spec/scraper.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ chai.use(sinonChai);
beforeEach(function () {
yakuza = new YakuzaBase();
yakuza.scraper('Scraper');
yakuza.agent('Scraper', 'Agent').setup(function (config) {
config.plan = [
'Task1'
];
});
yakuza.agent('Scraper', 'Agent').plan([
'Task1'
]);
yakuza.task('Scraper', 'Agent', 'Task1').main(function (task) {
task.success();
});
Expand Down Expand Up @@ -113,12 +111,10 @@ describe('Scraper', function () {
return newValue;
});

yakuza.agent('Scraper', 'OtherAgent').setup(function (config) {
config.plan = [
'ConcatTask',
'FinalTask'
];
});
yakuza.agent('Scraper', 'OtherAgent').plan([
'ConcatTask',
'FinalTask'
]);

yakuza.task('Scraper', 'OtherAgent', 'ConcatTask').builder(function () {
return ['this', ' is', ' con', 'catenated'];
Expand Down
Loading

0 comments on commit 20d6210

Please sign in to comment.