Implemented some new unit test
This commit is contained in:
parent
cbe16cf8c4
commit
e6f714a51b
@ -21,6 +21,7 @@ var StatsD = require('node-statsd').StatsD;
|
||||
var _ = require('underscore');
|
||||
var LRU = require('lru-cache');
|
||||
|
||||
var redis = require('redis');
|
||||
var UserDatabaseService = require('./services/user_database_service');
|
||||
var JobPublisher = require('../batch/job_publisher');
|
||||
var JobQueue = require('../batch/job_queue');
|
||||
@ -178,7 +179,7 @@ function App() {
|
||||
var userDatabaseService = new UserDatabaseService(metadataBackend);
|
||||
|
||||
var jobQueue = new JobQueue(metadataBackend);
|
||||
var jobPublisher = new JobPublisher();
|
||||
var jobPublisher = new JobPublisher(redis);
|
||||
var userIndexer = new UserIndexer(metadataBackend);
|
||||
var jobBackend = new JobBackend(metadataBackend, jobQueue, jobPublisher, userIndexer);
|
||||
var userDatabaseMetadataService = new UserDatabaseMetadataService(metadataBackend);
|
||||
|
@ -1,5 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
var redis = require('redis');
|
||||
var JobRunner = require('./job_runner');
|
||||
var JobCanceller = require('./job_canceller');
|
||||
var JobQueuePool = require('./job_queue_pool');
|
||||
@ -12,9 +13,9 @@ var JobBackend = require('./job_backend');
|
||||
var Batch = require('./batch');
|
||||
|
||||
module.exports = function batchFactory (metadataBackend) {
|
||||
var jobSubscriber = new JobSubscriber();
|
||||
var jobSubscriber = new JobSubscriber(redis);
|
||||
var jobQueuePool = new JobQueuePool(metadataBackend);
|
||||
var jobPublisher = new JobPublisher();
|
||||
var jobPublisher = new JobPublisher(redis);
|
||||
var jobQueue = new JobQueue(metadataBackend);
|
||||
var userIndexer = new UserIndexer(metadataBackend);
|
||||
var jobBackend = new JobBackend(metadataBackend, jobQueue, jobPublisher, userIndexer);
|
||||
|
@ -1,8 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
var redis = require('redis');
|
||||
|
||||
function JobPublisher() {
|
||||
function JobPublisher(redis) {
|
||||
this.channel = 'batch:hosts';
|
||||
this.client = redis.createClient(global.settings.redis_port, global.settings.redis_host);
|
||||
}
|
||||
|
@ -1,8 +1,6 @@
|
||||
'use strict';
|
||||
|
||||
var redis = require('redis');
|
||||
|
||||
function JobSubscriber() {
|
||||
function JobSubscriber(redis) {
|
||||
this.channel = 'batch:hosts';
|
||||
this.client = redis.createClient(global.settings.redis_port, global.settings.redis_host);
|
||||
}
|
||||
|
@ -1,4 +1,6 @@
|
||||
var _ = require('underscore');
|
||||
|
||||
var redis = require('redis');
|
||||
var queue = require('queue-async');
|
||||
var batchFactory = require('../../batch');
|
||||
var JobPublisher = require('../../batch/job_publisher');
|
||||
@ -17,7 +19,7 @@ describe('batch module', function() {
|
||||
var dbInstance = 'localhost';
|
||||
var username = 'vizzuality';
|
||||
var jobQueue = new JobQueue(metadataBackend);
|
||||
var jobPublisher = new JobPublisher();
|
||||
var jobPublisher = new JobPublisher(redis);
|
||||
var userIndexer = new UserIndexer(metadataBackend);
|
||||
var jobBackend = new JobBackend(metadataBackend, jobQueue, jobPublisher, userIndexer);
|
||||
|
||||
|
27
test/unit/batch/job_publisher.js
Normal file
27
test/unit/batch/job_publisher.js
Normal file
@ -0,0 +1,27 @@
|
||||
var JobPublisher = require('../../../batch/job_publisher');
|
||||
var assert = require('assert');
|
||||
|
||||
describe('batch API job publisher', function () {
|
||||
beforeEach(function () {
|
||||
var self = this;
|
||||
this.host = 'irrelevantHost';
|
||||
this.redis = {
|
||||
createClient: function () {
|
||||
return this;
|
||||
},
|
||||
publish: function () {
|
||||
var isValidFirstArg = arguments[0] === 'batch:hosts';
|
||||
var isValidSecondArg = arguments[1] === self.host;
|
||||
self.redis.publishIsCalledWithValidArgs = isValidFirstArg && isValidSecondArg;
|
||||
}
|
||||
};
|
||||
|
||||
this.jobPublisher = new JobPublisher(this.redis);
|
||||
});
|
||||
|
||||
it('.publish() should publish new messages', function () {
|
||||
this.jobPublisher.publish(this.host);
|
||||
assert.ok(this.redis.publishIsCalledWithValidArgs);
|
||||
});
|
||||
|
||||
});
|
@ -14,7 +14,7 @@ describe('batch API job queue', function () {
|
||||
this.jobQueue = new JobQueue(this.metadataBackend);
|
||||
});
|
||||
|
||||
it('.enqueue(username) should enqueue the provided job', function (done) {
|
||||
it('.enqueue() should enqueue the provided job', function (done) {
|
||||
this.jobQueue.enqueue('irrelevantJob', 'irrelevantHost', function (err, username) {
|
||||
assert.ok(!err);
|
||||
assert.equal(username, 'irrelevantJob');
|
||||
@ -22,7 +22,7 @@ describe('batch API job queue', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('.dequeue(username) should dequeue the next job', function (done) {
|
||||
it('.dequeue() should dequeue the next job', function (done) {
|
||||
this.jobQueue.dequeue('irrelevantHost', function (err, username) {
|
||||
assert.ok(!err);
|
||||
assert.equal(username, 'irrelevantJob');
|
||||
@ -30,4 +30,12 @@ describe('batch API job queue', function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('.enqueueFirst() should dequeue the next job', function (done) {
|
||||
this.jobQueue.enqueueFirst('irrelevantJob', 'irrelevantHost', function (err, username) {
|
||||
assert.ok(!err);
|
||||
assert.equal(username, 'irrelevantJob');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
41
test/unit/batch/job_subscriber.js
Normal file
41
test/unit/batch/job_subscriber.js
Normal file
@ -0,0 +1,41 @@
|
||||
var JobSubscriber = require('../../../batch/job_subscriber');
|
||||
var assert = require('assert');
|
||||
|
||||
describe('batch API job subscriber', function () {
|
||||
beforeEach(function () {
|
||||
var self = this;
|
||||
this.onMessageListener = function () {};
|
||||
this.redis = {
|
||||
createClient: function () {
|
||||
return this;
|
||||
},
|
||||
subscribe: function () {
|
||||
var isValidFirstArg = arguments[0] === 'batch:hosts';
|
||||
self.redis.subscribeIsCalledWithValidArgs = isValidFirstArg;
|
||||
},
|
||||
on: function () {
|
||||
var isValidFirstArg = arguments[0] === 'message';
|
||||
var isValidSecondArg = arguments[1] === self.onMessageListener;
|
||||
self.redis.onIsCalledWithValidArgs = isValidFirstArg && isValidSecondArg;
|
||||
},
|
||||
unsubscribe: function () {
|
||||
var isValidFirstArg = arguments[0] === 'batch:hosts';
|
||||
self.redis.unsubscribeIsCalledWithValidArgs = isValidFirstArg;
|
||||
}
|
||||
};
|
||||
|
||||
this.jobSubscriber = new JobSubscriber(this.redis);
|
||||
});
|
||||
|
||||
it('.subscribe() should listen for incoming messages', function () {
|
||||
this.jobSubscriber.subscribe(this.onMessageListener);
|
||||
assert.ok(this.redis.onIsCalledWithValidArgs);
|
||||
assert.ok(this.redis.subscribeIsCalledWithValidArgs);
|
||||
});
|
||||
|
||||
it('.unsubscribe() should stop listening for incoming messages', function () {
|
||||
this.jobSubscriber.unsubscribe();
|
||||
assert.ok(this.redis.unsubscribeIsCalledWithValidArgs);
|
||||
});
|
||||
|
||||
});
|
Loading…
Reference in New Issue
Block a user