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

Commit 59f6112

Browse files
committed
use async/await for connect, update tests
1 parent 02fb427 commit 59f6112

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": 0
913
},

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
@@ -94,39 +94,36 @@ class NativeClient extends EventEmitter {
9494
/**
9595
* Connect to the server.
9696
*
97-
* @param {function} done - The callback function.
9897
* @return {NativeClient}
9998
*/
100-
connect(done) {
99+
async connect() {
101100
debug('connecting...');
102101

103102
this.connectionOptions = null;
104103
this.isWritable = false;
105104
this.isMongos = false;
106105

107-
connect(
106+
const [
107+
_client, // The client is set in the `setupListeners` method.
108+
connectionOptions
109+
] = await connect(
108110
this.model,
109-
this.setupListeners.bind(this),
110-
(err, _client, connectionOptions) => {
111-
if (err) {
112-
return done(this._translateMessage(err));
113-
}
111+
this.setupListeners.bind(this)
112+
);
114113

115-
this.connectionOptions = connectionOptions;
114+
this.connectionOptions = connectionOptions;
116115

117-
this.isWritable = this.client.isWritable;
118-
this.isMongos = this.client.isMongos;
116+
this.isWritable = this.client.isWritable;
117+
this.isMongos = this.client.isMongos;
119118

120-
debug('connected!', {
121-
isWritable: this.isWritable,
122-
isMongos: this.isMongos
123-
});
119+
debug('connected!', {
120+
isWritable: this.isWritable,
121+
isMongos: this.isMongos
122+
});
123+
124+
this.client.on('status', (evt) => this.emit('status', evt));
125+
this.database = this.client.db(this.model.ns || ADMIN);
124126

125-
this.client.on('status', (evt) => this.emit('status', evt));
126-
this.database = this.client.db(this.model.ns || ADMIN);
127-
done(null, this);
128-
}
129-
);
130127
return this;
131128
}
132129

test/data-service.test.js

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

15-
before(function(done) {
16-
service.connect(done);
15+
before(async() => {
16+
await service.connect();
1717
});
1818
after(function(done) {
1919
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)