CartoDB-SQL-API/test/unit/batch/job_subscriber.js

52 lines
1.7 KiB
JavaScript
Raw Normal View History

2016-01-23 02:31:25 +08:00
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 () {
if (arguments[0] === 'message') {
self.redis.onIsCalledWithValidArgs = true;
}
2016-01-23 02:31:25 +08:00
},
unsubscribe: function () {
var isValidFirstArg = arguments[0] === 'batch:hosts';
self.redis.unsubscribeIsCalledWithValidArgs = isValidFirstArg;
2016-04-05 02:05:58 +08:00
},
removeAllListeners: function () {
return this;
2016-01-23 02:31:25 +08:00
}
};
this.queueSeeker = {
seek: function () {
var callback = arguments[1];
2016-01-23 02:31:25 +08:00
callback(null, []);
}
};
this.jobSubscriber = new JobSubscriber(this.redis, this.queueSeeker);
2016-01-23 02:31:25 +08:00
});
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);
});
});