2018-10-24 21:42:33 +08:00
|
|
|
'use strict';
|
|
|
|
|
2019-10-04 00:24:39 +08:00
|
|
|
var Channel = require('../../../lib/batch/pubsub/channel');
|
|
|
|
var JobSubscriber = require('../../../lib/batch/pubsub/job-subscriber');
|
2016-01-23 02:31:25 +08:00
|
|
|
var assert = require('assert');
|
|
|
|
|
|
|
|
describe('batch API job subscriber', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
var self = this;
|
2016-07-07 20:14:46 +08:00
|
|
|
|
2016-01-23 02:31:25 +08:00
|
|
|
this.onMessageListener = function () {};
|
|
|
|
this.redis = {
|
|
|
|
createClient: function () {
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
subscribe: function () {
|
2016-10-13 00:44:46 +08:00
|
|
|
var isValidFirstArg = arguments[0] === Channel.NAME;
|
2016-01-23 02:31:25 +08:00
|
|
|
self.redis.subscribeIsCalledWithValidArgs = isValidFirstArg;
|
|
|
|
},
|
|
|
|
on: function () {
|
2016-07-07 16:44:17 +08:00
|
|
|
if (arguments[0] === 'message') {
|
|
|
|
self.redis.onIsCalledWithValidArgs = true;
|
|
|
|
}
|
2016-01-23 02:31:25 +08:00
|
|
|
},
|
|
|
|
unsubscribe: function () {
|
2016-10-13 00:44:46 +08:00
|
|
|
var isValidFirstArg = arguments[0] === Channel.NAME;
|
2016-01-23 02:31:25 +08:00
|
|
|
self.redis.unsubscribeIsCalledWithValidArgs = isValidFirstArg;
|
2016-04-05 02:05:58 +08:00
|
|
|
},
|
2016-10-12 01:01:39 +08:00
|
|
|
scan: function(params, callback) {
|
|
|
|
return callback(null, ['0']);
|
|
|
|
},
|
2016-04-05 02:05:58 +08:00
|
|
|
removeAllListeners: function () {
|
|
|
|
return this;
|
2016-07-07 20:14:46 +08:00
|
|
|
},
|
2017-03-31 20:30:33 +08:00
|
|
|
smembers: function (key, callback) {
|
|
|
|
callback(null, []);
|
|
|
|
},
|
|
|
|
connected: true,
|
2016-07-07 20:14:46 +08:00
|
|
|
};
|
|
|
|
this.pool = {
|
|
|
|
acquire: function (db, cb) {
|
|
|
|
cb(null, self.redis);
|
2016-10-12 01:01:39 +08:00
|
|
|
},
|
|
|
|
release: function(/*db, client*/) {
|
|
|
|
|
2016-01-23 02:31:25 +08:00
|
|
|
}
|
|
|
|
};
|
2016-04-04 22:05:33 +08:00
|
|
|
this.queueSeeker = {
|
|
|
|
seek: function () {
|
|
|
|
var callback = arguments[1];
|
2016-01-23 02:31:25 +08:00
|
|
|
|
2016-03-31 23:37:35 +08:00
|
|
|
callback(null, []);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-07-07 20:14:46 +08:00
|
|
|
this.jobSubscriber = new JobSubscriber(this.pool, 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 () {
|
2016-07-07 20:14:46 +08:00
|
|
|
this.jobSubscriber.subscribe(this.onMessageListener);
|
2016-01-23 02:31:25 +08:00
|
|
|
this.jobSubscriber.unsubscribe();
|
|
|
|
assert.ok(this.redis.unsubscribeIsCalledWithValidArgs);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|