Skip to content
This repository was archived by the owner on May 17, 2021. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/data-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
16 changes: 16 additions & 0 deletions lib/native-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -547,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');
Expand Down
34 changes: 34 additions & 0 deletions test/data-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,40 @@ describe('DataService', function() {
service.disconnect(done);
});

// Each test gets its own service so that we can connect/disconnect it freely
describe('#isConnected', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about the other way around?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got a little lazy, added in 2eb3803

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);
}
});
});

describe('#deleteOne', function() {
it('deletes the document from the collection', function(done) {
service.insertOne(
Expand Down