Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
benzekrimaha committed Oct 17, 2024
1 parent 319863d commit 44c229c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 20 deletions.
4 changes: 2 additions & 2 deletions lib/api/BackbeatAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -1314,13 +1314,13 @@ class BackbeatAPI {
});
}

_setZookeeper(cb) {
_setZookeeper(cb, ZookeeperManagerClass = ZookeeperManager) {
const { connectionString, autoCreateNamespace } = this._zkConfig;
const zookeeperUrl = this._queuePopulator.mongo
? connectionString
: `${connectionString}${this._queuePopulator.zookeeperPath}`;

const zkClient = new ZookeeperManager(zookeeperUrl, { autoCreateNamespace }, this._logger);
const zkClient = new ZookeeperManagerClass(zookeeperUrl, { autoCreateNamespace }, this._logger);

zkClient.once('error', cb);
zkClient.once('connected', () => {
Expand Down
59 changes: 41 additions & 18 deletions tests/unit/api/BackbeatAPI.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const assert = require('assert');
const redis = require('ioredis');
const sinon = require('sinon');

const BackbeatAPI = require('../../../lib/api/BackbeatAPI');
const BackbeatRequest = require('../../../lib/api/BackbeatRequest');
Expand All @@ -19,6 +21,14 @@ describe('BackbeatAPI', () => {
before(() => {
setupIngestionSiteMock();
bbapi = new BackbeatAPI(config, fakeLogger, { timer: true });

sinon.stub(redis.prototype, 'connect').returns(Promise.resolve());
sinon.stub(redis.prototype, 'on').returnsThis();
sinon.stub(redis.prototype, 'quit').returns(Promise.resolve());
});

after(() => {
sinon.restore();
});

// valid routes
Expand Down Expand Up @@ -212,25 +222,38 @@ describe('BackbeatAPI', () => {
});

describe('_setZookeeper', () => {
describe('_setZookeeper', () => {

it('should use connectionString directly if this._queuePopulator.mongo exists', done => {
bbapi._setZookeeper(err => {
assert.ifError(err);
assert.strictEqual(bbapi.zkClient.url, 'localhost:1');
done();
});
});

it('should append zookeeperPath to connectionString if this._queuePopulator.mongo does not exist', done => {
delete bbapi._queuePopulator.mongo;
bbapi._zkConfig = { connectionString: 'localhost:1', autoCreateNamespace: true };
bbapi._setZookeeper(err => {
assert.ifError(err);
assert.strictEqual(bbapi.zkClient.url, 'localhost:1/test/path');
done();
let zkManagerArgs;
class MockZookeeperManager {
constructor(url, options, logger) {
zkManagerArgs = { url, options, logger };
this.once = sinon.stub().callsFake((event, callback) => {
if (event === 'connected') {
callback();
}
});
});
this.removeAllListeners = sinon.stub();
}
}

after(() => {
sinon.restore();
});

it('should use connectionString directly if this._queuePopulator.mongo exists', done => {
bbapi._setZookeeper(() => {
assert(zkManagerArgs);
assert.strictEqual(zkManagerArgs.url, '127.0.0.1:2181/backbeat');
done();
}, MockZookeeperManager);
});

it('should append zookeeperPath to connectionString if this._queuePopulator.mongo does not exist', done => {
delete bbapi._queuePopulator.mongo;
bbapi._setZookeeper(() => {
assert(zkManagerArgs);
assert.strictEqual(zkManagerArgs.url, '127.0.0.1:2181/backbeat/queue-populator');
done();
}, MockZookeeperManager);
});
});
});

0 comments on commit 44c229c

Please sign in to comment.