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

Commit 3f896f7

Browse files
committed
use async/await for connect, update tests
1 parent 791ba8a commit 3f896f7

File tree

6 files changed

+136
-138
lines changed

6 files changed

+136
-138
lines changed

.eslintrc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
"node": true,
55
"es6": true
66
},
7+
"parserOptions": {
8+
"ecmaVersion": 2018,
9+
"sourceType": "module"
10+
},
711
"rules": {
812
"strict": "off",
913
"no-undef": "error",

lib/data-service.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const EventEmitter = require('events');
2+
const debug = require('debug')('mongodb-data-service:data-service');
23

34
/**
45
* Instantiate a new DataService object.
@@ -31,6 +32,7 @@ class DataService extends EventEmitter {
3132
.on('topologyOpening', (evt) => this.emit('topologyOpening', evt))
3233
.on('topologyClosed', (evt) => this.emit('topologyClosed', evt))
3334
.on('topologyDescriptionChanged', (evt) => {
35+
debug('got new topologyDescriptionChanged', evt.newDescription);
3436
this.lastSeenTopology = evt.newDescription;
3537
this.emit('topologyDescriptionChanged', evt);
3638
});
@@ -151,17 +153,13 @@ class DataService extends EventEmitter {
151153

152154
/**
153155
* Connect to the server.
154-
*
155-
* @param {function} done - The callback function.
156156
*/
157-
connect(done) {
158-
this.client.connect((err) => {
159-
if (err) {
160-
return done(err);
161-
}
162-
done(null, this);
163-
this.emit('readable');
164-
});
157+
async connect() {
158+
await this.client.connect();
159+
160+
this.emit('readable');
161+
162+
return this;
165163
}
166164

167165
/**

lib/native-client.js

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -98,39 +98,36 @@ class NativeClient extends EventEmitter {
9898
/**
9999
* Connect to the server.
100100
*
101-
* @param {function} done - The callback function.
102101
* @return {NativeClient}
103102
*/
104-
connect(done) {
103+
async connect() {
105104
debug('connecting...');
106105

107106
this.connectionOptions = null;
108107
this.isWritable = false;
109108
this.isMongos = false;
110109

111-
connect(
110+
const [
111+
_client, // The client is set in the `setupListeners` method.
112+
connectionOptions
113+
] = await connect(
112114
this.model,
113-
this.setupListeners.bind(this),
114-
(err, _client, connectionOptions) => {
115-
if (err) {
116-
return done(this._translateMessage(err));
117-
}
115+
this.setupListeners.bind(this)
116+
);
118117

119-
this.connectionOptions = connectionOptions;
118+
this.connectionOptions = connectionOptions;
120119

121-
this.isWritable = this.client.isWritable;
122-
this.isMongos = this.client.isMongos;
120+
this.isWritable = this.client.isWritable;
121+
this.isMongos = this.client.isMongos;
123122

124-
debug('connected!', {
125-
isWritable: this.isWritable,
126-
isMongos: this.isMongos
127-
});
123+
debug('connected!', {
124+
isWritable: this.isWritable,
125+
isMongos: this.isMongos
126+
});
127+
128+
this.client.on('status', (evt) => this.emit('status', evt));
129+
this.database = this.client.db(this.model.ns || ADMIN);
128130

129-
this.client.on('status', (evt) => this.emit('status', evt));
130-
this.database = this.client.db(this.model.ns || ADMIN);
131-
done(null, this);
132-
}
133-
);
134131
return this;
135132
}
136133

test/data-service.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ describe('DataService', function() {
1010
this.timeout(20000);
1111
const service = new DataService(helper.connection);
1212

13-
before(function(done) {
14-
service.connect(done);
13+
before(async() => {
14+
await service.connect();
1515
});
1616
after(function(done) {
1717
service.disconnect(done);

test/instance-detail-helper.test.js

Lines changed: 53 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,60 +4,72 @@ const connect = Connection.connect;
44
const { getInstance } = require('../lib/instance-detail-helper');
55
const helper = require('./helper');
66
const DataService = require('../lib/data-service');
7+
const { promisify } = require('util');
78
const _ = require('lodash');
89

910
describe('mongodb-data-service#instance', function() {
1011
describe('local', function() {
1112
let client;
12-
let db;
13-
after(function(done) {
14-
client.close(true, done);
15-
});
16-
it('should connect to `localhost:27018`', function(done) {
17-
Connection.from(
18-
'mongodb://localhost:27018/data-service',
19-
function(error, model) {
20-
assert.equal(error, null);
21-
connect(
22-
model,
23-
null,
24-
function(err, _client) {
25-
if (err) {
26-
return done(err);
27-
}
28-
client = _client;
29-
db = client.db('data-service');
30-
done();
31-
}
32-
);
33-
}
13+
let model;
14+
15+
before(async() => {
16+
model = await Connection.from(
17+
'mongodb://localhost:27018/data-service'
3418
);
3519
});
36-
it('should not close the db after getting instance details', function(done) {
37-
assert(db);
38-
getInstance(client, db, function(err) {
39-
if (err) {
40-
return done(err);
41-
}
42-
db.admin().ping(function(_err, pingResult) {
43-
if (_err) {
44-
done(_err);
20+
21+
describe('connecting', () => {
22+
after(function(done) {
23+
client.close(true, done);
24+
});
25+
it('should connect to `localhost:27018`', async() => {
26+
const [ _client ] = await connect(
27+
model,
28+
null
29+
);
30+
client = _client;
31+
});
32+
});
33+
34+
describe('after connecting', () => {
35+
before(async() => {
36+
const [ _client ] = await connect(
37+
model,
38+
null
39+
);
40+
client = _client;
41+
});
42+
after(function(done) {
43+
client.close(true, done);
44+
});
45+
46+
it('should not close the db after getting instance details', (done) => {
47+
const db = client.db('data-service');
48+
49+
assert(db);
50+
getInstance(client, db, function(err) {
51+
if (err) {
52+
return done(err);
4553
}
46-
done(null, pingResult);
54+
db.admin().ping(function(_err, pingResult) {
55+
if (_err) {
56+
done(_err);
57+
}
58+
done(null, pingResult);
59+
});
4760
});
4861
});
4962
});
63+
5064
if (process.env.MONGODB_TOPOLOGY !== 'cluster') {
5165
describe('views', function() {
52-
var service = new DataService(helper.connection);
53-
var instanceDetails = null;
54-
before(function(done) {
55-
service.connect(function(err) {
56-
if (err) return done(err);
57-
helper.insertTestDocuments(service.client, function() {
58-
done();
59-
});
60-
});
66+
const service = new DataService(helper.connection);
67+
let instanceDetails;
68+
before(async() => {
69+
await service.connect();
70+
71+
const runInsertDocuments = promisify(helper.insertTestDocuments);
72+
await runInsertDocuments(service.client);
6173
});
6274

6375
after(function(done) {

0 commit comments

Comments
 (0)