From fc10a115ab9c87db4fa532043b0e3966f847a69f Mon Sep 17 00:00:00 2001 From: Nora Date: Fri, 2 Aug 2019 17:01:28 -0400 Subject: [PATCH] test: add test for getMaxOfflineRequests --- lib/datasource.js | 16 ++++++++++++---- test/datasource.test.js | 24 ++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/lib/datasource.js b/lib/datasource.js index e47559ff6..f5f647f29 100644 --- a/lib/datasource.js +++ b/lib/datasource.js @@ -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 @@ -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) { @@ -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; } diff --git a/test/datasource.test.js b/test/datasource.test.js index a3795cd66..ae158506c 100644 --- a/test/datasource.test.js +++ b/test/datasource.test.js @@ -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) {