Skip to content

Commit

Permalink
test: add test for getMaxOfflineRequests
Browse files Browse the repository at this point in the history
  • Loading branch information
nabdelgadir committed Aug 2, 2019
1 parent 797387e commit fc10a11
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/datasource.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ util.inherits(DataSource, EventEmitter);
// allow child classes to supply a data access object
DataSource.DataAccessObject = DataAccessObject;

/**
* Global maximum number of event listeners
*/
DataSource.prototype.DEFAULT_MAX_OFFLINE_REQUESTS = 16;

/**
* Set up the connector instance for backward compatibility with JugglingDB schema/adapter
* @private
Expand All @@ -209,7 +214,6 @@ DataSource.prototype._setupConnector = function() {
const dataSource = this;

// Set max listeners to a default/configured value
// solves bug https://github.com/strongloop/loopback-next/issues/2198
dataSource.setMaxListeners(dataSource.getMaxOfflineRequests());

this.connector.log = function(query, start) {
Expand Down Expand Up @@ -2711,9 +2715,13 @@ DataSource.prototype.getMaxOfflineRequests = function() {
// Set max listeners to a default value
// Override this default value with a datasource setting
// 'maxOfflineRequests' from an application's datasources.json
// solves bug https://github.com/strongloop/loopback-next/issues/2198
let maxOfflineRequests = 16;
if (this.settings && this.settings.maxOfflineRequests) {

let maxOfflineRequests = this.DEFAULT_MAX_OFFLINE_REQUESTS;
if (
this.settings &&
this.settings.maxOfflineRequests &&
typeof this.settings.maxOfflineRequests === 'number'
) {
if (this.settings.maxOfflineRequests > maxOfflineRequests)
maxOfflineRequests = this.settings.maxOfflineRequests;
}
Expand Down
24 changes: 24 additions & 0 deletions test/datasource.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,30 @@ describe('DataSource', function() {
ds.connector.should.equal(connector);
});
});

describe('getMaxOfflineRequests', () => {
let ds;
beforeEach(() => ds = new DataSource('ds', {connector: 'memory'}));

it('sets the default maximum number of event listeners to 16', () => {
ds.getMaxOfflineRequests().should.be.eql(16);
});

it('uses the default max number of event listeners if the provided one is less than 16', () => {
ds.settings.maxOfflineRequests = 15;
ds.getMaxOfflineRequests().should.be.eql(16);
});

it('uses provided number of listeners if it is greater than 16', () => {
ds.settings.maxOfflineRequests = 17;
ds.getMaxOfflineRequests().should.be.eql(17);
});

it('uses default if a non-number is provided for the max number of listeners', () => {
ds.settings.maxOfflineRequests = '17';
ds.getMaxOfflineRequests().should.be.eql(16);
});
});
});

function givenMockConnector(props) {
Expand Down

0 comments on commit fc10a11

Please sign in to comment.