From 63c605df747d7e1b80e059acf94dc026b7a7ac2a Mon Sep 17 00:00:00 2001 From: janehe Date: Tue, 19 Nov 2024 01:11:13 +0000 Subject: [PATCH] fix inconsistenly failiing test --- lib/control-connection.js | 14 ++++++++----- .../short/control-connection-tests.js | 21 +++++++++++++++++++ test/test-helper.js | 4 ++-- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/lib/control-connection.js b/lib/control-connection.js index 54b3e617..b8ddf217 100644 --- a/lib/control-connection.js +++ b/lib/control-connection.js @@ -490,7 +490,9 @@ class ControlConnection extends events.EventEmitter { await this._registerToConnectionEvents(); } catch (err) { this.log('error', 'ControlConnection failed to retrieve topology and keyspaces information', err); - this._triedHosts[this.connection.endpoint] = err; + if (this.connection != null) { + this._triedHosts[this.connection.endpoint] = err; + } if (err.isSocketError && this.host) { this.host.removeFromPool(this.connection); @@ -501,10 +503,12 @@ class ControlConnection extends events.EventEmitter { } this._reconnectionSchedule = this._reconnectionPolicy.newSchedule(); - this._setHealthListeners(this.host, this.connection); - this.emit('newConnection', null, this.connection, this.host); - - this.log('info', `ControlConnection connected to ${this.connection.endpointFriendlyName} and up to date`); + if (this.host != null && this.connection != null) { + this._setHealthListeners(this.host, this.connection); + this.emit('newConnection', null, this.connection, this.host); + + this.log('info', `ControlConnection connected to ${this.connection.endpointFriendlyName} and up to date`); + } // else, someone else started a _refresh or _initializeConnection, ignoring the current result } /** diff --git a/test/integration/short/control-connection-tests.js b/test/integration/short/control-connection-tests.js index b4810216..b4552ddf 100644 --- a/test/integration/short/control-connection-tests.js +++ b/test/integration/short/control-connection-tests.js @@ -151,6 +151,27 @@ describe('ControlConnection', function () { assert.strictEqual(cc.hosts.length, 1); }); + it('should not break when refreshing concurrently', async () => { + const cc = newInstance(); + cc.options.policies.loadBalancing = new policies.loadBalancing.RoundRobinPolicy(); + disposeAfter(cc); + + await cc.init(); + await new Promise(r => cc.options.policies.loadBalancing.init(null, cc.hosts, r)); + + /** @type {Array} */ + const refreshPromises = []; + // randomly emit 200 times cc._refresh + for (let i = 0; i < 200; i++) { + refreshPromises.push(cc._refresh()); + // interval of random number + await helper.delayAsync(Math.floor(Math.random() * 200)); + } + Promise.all(refreshPromises); + assert.ok(cc.host); + assert.ok(cc.connection); + }); + it('should reconnect when host used goes down', async () => { const options = clientOptions.extend( utils.extend({ pooling: helper.getPoolingOptions(1, 1, 500) }, helper.baseOptions)); diff --git a/test/test-helper.js b/test/test-helper.js index c342d1e3..013844b5 100644 --- a/test/test-helper.js +++ b/test/test-helper.js @@ -1441,10 +1441,10 @@ helper.ads._spawnAndWait = function(processName, params, cb) { cb = utils.noop; }; - // If process hasn't completed in 10 seconds. + // If process hasn't completed in 20 seconds. timeout = setTimeout(function() { callbackOnce(new Error("Timed out while waiting for " + processName + " to complete.")); - }, 10000); + }, 20000); const p = spawn(processName, params, { env: Object.assign({ KRB5_CONFIG: this.getKrb5ConfigPath()}, process.env)