make workers private, add getter, add destroy test
This commit is contained in:
11
src/index.js
11
src/index.js
@@ -19,8 +19,8 @@ export default class Elastique extends events.EventEmitter {
|
|||||||
timeout: 10000,
|
timeout: 10000,
|
||||||
}, omit(options, [ 'client' ]));
|
}, omit(options, [ 'client' ]));
|
||||||
this.client = createClient(options.client || {});
|
this.client = createClient(options.client || {});
|
||||||
this.workers = [];
|
|
||||||
|
|
||||||
|
this._workers = [];
|
||||||
this._initTasks().catch((err) => this.emit('error', err));
|
this._initTasks().catch((err) => this.emit('error', err));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,11 +48,16 @@ export default class Elastique extends events.EventEmitter {
|
|||||||
|
|
||||||
registerWorker(type, workerFn, opts) {
|
registerWorker(type, workerFn, opts) {
|
||||||
const worker = new Worker(this, type, workerFn, opts);
|
const worker = new Worker(this, type, workerFn, opts);
|
||||||
this.workers.push(worker);
|
this._workers.push(worker);
|
||||||
return worker;
|
return worker;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getWorkers() {
|
||||||
|
return this._workers.map((fn) => fn);
|
||||||
|
}
|
||||||
|
|
||||||
destroy() {
|
destroy() {
|
||||||
this.workers.forEach((worker) => worker.destroy());
|
const workers = this._workers.filter((worker) => worker.destroy());
|
||||||
|
this._workers = workers;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import events from 'events';
|
import events from 'events';
|
||||||
import expect from 'expect.js';
|
import expect from 'expect.js';
|
||||||
import sinon from 'sinon';
|
import sinon from 'sinon';
|
||||||
import { noop } from 'lodash';
|
import { noop, times } from 'lodash';
|
||||||
import elasticsearchMock from '../fixtures/elasticsearch';
|
import elasticsearchMock from '../fixtures/elasticsearch';
|
||||||
import Elastique from '../../lib/index';
|
import Elastique from '../../lib/index';
|
||||||
|
|
||||||
@@ -51,13 +51,26 @@ describe('Elastique class', function () {
|
|||||||
describe('Registering workers', function () {
|
describe('Registering workers', function () {
|
||||||
it('should keep track of workers', function () {
|
it('should keep track of workers', function () {
|
||||||
const queue = new Elastique('elastique', { client });
|
const queue = new Elastique('elastique', { client });
|
||||||
expect(queue.workers).to.eql([]);
|
expect(queue.getWorkers()).to.eql([]);
|
||||||
expect(queue.workers).to.have.length(0);
|
expect(queue.getWorkers()).to.have.length(0);
|
||||||
|
|
||||||
queue.registerWorker('test', noop);
|
queue.registerWorker('test', noop);
|
||||||
queue.registerWorker('test', noop);
|
queue.registerWorker('test', noop);
|
||||||
queue.registerWorker('test2', noop);
|
queue.registerWorker('test2', noop);
|
||||||
expect(queue.workers).to.have.length(3);
|
expect(queue.getWorkers()).to.have.length(3);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Destroy', function () {
|
||||||
|
it('should destroy workers', function () {
|
||||||
|
const queue = new Elastique('elastique', { client });
|
||||||
|
const stubs = times(3, () => { return { destroy: sinon.stub() }; });
|
||||||
|
stubs.forEach((stub) => queue._workers.push(stub));
|
||||||
|
expect(queue.getWorkers()).to.have.length(3);
|
||||||
|
|
||||||
|
queue.destroy();
|
||||||
|
stubs.forEach((stub) => sinon.assert.calledOnce(stub.destroy));
|
||||||
|
expect(queue.getWorkers()).to.have.length(0);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user