From fa10adad5590992a711c0e334d82b0ff842fbe5b Mon Sep 17 00:00:00 2001 From: Sergey Petushkov Date: Tue, 30 Mar 2021 16:50:14 +0200 Subject: [PATCH 1/2] feat: Add isConnected method to dataService to allow easy access to connection status of the internal MongoClient --- lib/data-service.js | 4 ++++ lib/native-client.js | 7 +++++++ test/data-service.test.js | 6 ++++++ 3 files changed, 17 insertions(+) diff --git a/lib/data-service.js b/lib/data-service.js index d01481eb..a1f7e594 100644 --- a/lib/data-service.js +++ b/lib/data-service.js @@ -582,6 +582,10 @@ class DataService extends EventEmitter { return this.client.killSession(...args); } + isConnected() { + return this.client.isConnected(); + } + /** * When Node supports ES6 default values for arguments, this can go away. * diff --git a/lib/native-client.js b/lib/native-client.js index c0ad49de..aee6b7d0 100644 --- a/lib/native-client.js +++ b/lib/native-client.js @@ -101,6 +101,13 @@ class NativeClient extends EventEmitter { this._isConnected = false; } + isConnected() { + // This is better than just returning internal `_isConnected` as this + // actually shows when the client is available on the NativeClient instance + // and connected + return this.client && this.client.isConnected(); + } + /** * Connect to the server. * diff --git a/test/data-service.test.js b/test/data-service.test.js index 4031009b..1d4dd75d 100644 --- a/test/data-service.test.js +++ b/test/data-service.test.js @@ -17,6 +17,12 @@ describe('DataService', function() { service.disconnect(done); }); + describe('#isConnected', () => { + it('returns true if client is connected', () => { + expect(service.isConnected()).to.equal(true); + }); + }); + describe('#deleteOne', function() { it('deletes the document from the collection', function(done) { service.insertOne( From 2eb3803f379f57ae9d765ec546165346990c05c2 Mon Sep 17 00:00:00 2001 From: Sergey Petushkov Date: Tue, 30 Mar 2021 18:09:43 +0200 Subject: [PATCH 2/2] test: Add a few other tests cases; Handle disconnect better in native client --- lib/native-client.js | 11 ++++++++++- test/data-service.test.js | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/lib/native-client.js b/lib/native-client.js index aee6b7d0..b5e6f110 100644 --- a/lib/native-client.js +++ b/lib/native-client.js @@ -105,7 +105,7 @@ class NativeClient extends EventEmitter { // This is better than just returning internal `_isConnected` as this // actually shows when the client is available on the NativeClient instance // and connected - return this.client && this.client.isConnected(); + return !!(this.client && this.client.isConnected()); } /** @@ -554,6 +554,15 @@ class NativeClient extends EventEmitter { * @param {Function} callback */ disconnect(callback) { + // This follows MongoClient behavior where calling `close` on client that is + // not connected + if (!this.client) { + setImmediate(() => { + callback(); + }); + return; + } + this.client.close(true, err => { if (this.tunnel) { debug('mongo client closed. shutting down ssh tunnel'); diff --git a/test/data-service.test.js b/test/data-service.test.js index 1d4dd75d..ddb8260b 100644 --- a/test/data-service.test.js +++ b/test/data-service.test.js @@ -17,9 +17,37 @@ describe('DataService', function() { service.disconnect(done); }); + // Each test gets its own service so that we can connect/disconnect it freely describe('#isConnected', () => { - it('returns true if client is connected', () => { - expect(service.isConnected()).to.equal(true); + let _service; + + it('returns false when not connected initially', () => { + _service = new DataService(helper.connection); + expect(_service.isConnected()).to.equal(false); + }); + + it('returns true if client is connected', (done) => { + _service = new DataService(helper.connection); + _service.connect(() => { + expect(_service.isConnected()).to.equal(true); + done(); + }); + }); + + it('returns false if client is disconnected', (done) => { + _service = new DataService(helper.connection); + _service.connect(() => { + _service.disconnect(() => { + expect(_service.isConnected()).to.equal(false); + done(); + }); + }); + }); + + afterEach((done) => { + if (_service) { + _service.disconnect(done); + } }); });