Skip to content
This repository was archived by the owner on May 17, 2021. It is now read-only.

Commit 9a4ff05

Browse files
authored
feat: Add isConnected method to dataService to allow easy access to connection status of the internal MongoClient COMPASS-4741 (#320)
* feat: Add isConnected method to dataService to allow easy access to connection status of the internal MongoClient * test: Add a few other tests cases; Handle disconnect better in native client
1 parent 456f2e0 commit 9a4ff05

File tree

3 files changed

+54
-0
lines changed

3 files changed

+54
-0
lines changed

lib/data-service.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,10 @@ class DataService extends EventEmitter {
582582
return this.client.killSession(...args);
583583
}
584584

585+
isConnected() {
586+
return this.client.isConnected();
587+
}
588+
585589
/**
586590
* When Node supports ES6 default values for arguments, this can go away.
587591
*

lib/native-client.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,13 @@ class NativeClient extends EventEmitter {
101101
this._isConnected = false;
102102
}
103103

104+
isConnected() {
105+
// This is better than just returning internal `_isConnected` as this
106+
// actually shows when the client is available on the NativeClient instance
107+
// and connected
108+
return !!(this.client && this.client.isConnected());
109+
}
110+
104111
/**
105112
* Connect to the server.
106113
*
@@ -547,6 +554,15 @@ class NativeClient extends EventEmitter {
547554
* @param {Function} callback
548555
*/
549556
disconnect(callback) {
557+
// This follows MongoClient behavior where calling `close` on client that is
558+
// not connected
559+
if (!this.client) {
560+
setImmediate(() => {
561+
callback();
562+
});
563+
return;
564+
}
565+
550566
this.client.close(true, err => {
551567
if (this.tunnel) {
552568
debug('mongo client closed. shutting down ssh tunnel');

test/data-service.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,40 @@ describe('DataService', function() {
1717
service.disconnect(done);
1818
});
1919

20+
// Each test gets its own service so that we can connect/disconnect it freely
21+
describe('#isConnected', () => {
22+
let _service;
23+
24+
it('returns false when not connected initially', () => {
25+
_service = new DataService(helper.connection);
26+
expect(_service.isConnected()).to.equal(false);
27+
});
28+
29+
it('returns true if client is connected', (done) => {
30+
_service = new DataService(helper.connection);
31+
_service.connect(() => {
32+
expect(_service.isConnected()).to.equal(true);
33+
done();
34+
});
35+
});
36+
37+
it('returns false if client is disconnected', (done) => {
38+
_service = new DataService(helper.connection);
39+
_service.connect(() => {
40+
_service.disconnect(() => {
41+
expect(_service.isConnected()).to.equal(false);
42+
done();
43+
});
44+
});
45+
});
46+
47+
afterEach((done) => {
48+
if (_service) {
49+
_service.disconnect(done);
50+
}
51+
});
52+
});
53+
2054
describe('#deleteOne', function() {
2155
it('deletes the document from the collection', function(done) {
2256
service.insertOne(

0 commit comments

Comments
 (0)