Skip to content

Commit 5757fcd

Browse files
ljhaywarnbbeeken
andauthored
refactor(NODE-3273): remove undefined error param from topology open event (#3033)
* refactor(NODE-3273): remove undefined error param from topology open event * rename test * Update test/functional/connection.test.js Change vars to consts Co-authored-by: Neal Beeken <[email protected]> * Fix up test based on code review * Update tests in connection.test.js to use afterEach * Fix tests after code review * Fix hanging test issue Co-authored-by: Neal Beeken <[email protected]>
1 parent b6c73bf commit 5757fcd

File tree

2 files changed

+72
-36
lines changed

2 files changed

+72
-36
lines changed

src/sdam/topology.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ export type TopologyEvents = {
182182
topologyOpening(event: TopologyOpeningEvent): void;
183183
topologyDescriptionChanged(event: TopologyDescriptionChangedEvent): void;
184184
error(error: Error): void;
185-
/** TODO(NODE-3273) - remove error @internal */
186-
open(error: undefined, topology: Topology): void;
185+
/** @internal */
186+
open(topology: Topology): void;
187187
close(): void;
188188
timeout(): void;
189189
} & Omit<ServerEvents, 'connect'> &
@@ -456,8 +456,7 @@ export class Topology extends TypedEventEmitter<TopologyEvents> {
456456
}
457457

458458
stateTransition(this, STATE_CONNECTED);
459-
// TODO(NODE-3273) - remove err
460-
this.emit(Topology.OPEN, err, this);
459+
this.emit(Topology.OPEN, this);
461460
this.emit(Topology.CONNECT, this);
462461

463462
if (typeof callback === 'function') callback(undefined, this);
@@ -467,8 +466,7 @@ export class Topology extends TypedEventEmitter<TopologyEvents> {
467466
}
468467

469468
stateTransition(this, STATE_CONNECTED);
470-
// TODO(NODE-3273) - remove err
471-
this.emit(Topology.OPEN, err, this);
469+
this.emit(Topology.OPEN, this);
472470
this.emit(Topology.CONNECT, this);
473471

474472
if (typeof callback === 'function') callback(undefined, this);

test/functional/connection.test.js

Lines changed: 68 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,42 @@ const { withClient, setupDatabase } = require('./shared');
33
const test = require('./shared').assert;
44
const { expect } = require('chai');
55
const { ServerHeartbeatStartedEvent, MongoClient } = require('../../src');
6+
const { Topology } = require('../../src/sdam/topology');
67

78
describe('Connection - functional', function () {
9+
let client;
10+
let testClient;
11+
812
before(function () {
913
return setupDatabase(this.configuration);
1014
});
1115

16+
afterEach(async () => {
17+
let savedError;
18+
if (client) {
19+
try {
20+
await client.close();
21+
} catch (err) {
22+
savedError = err;
23+
}
24+
}
25+
if (testClient) {
26+
try {
27+
await testClient.close();
28+
} catch (err) {
29+
savedError = err;
30+
}
31+
}
32+
if (savedError) {
33+
throw savedError;
34+
}
35+
});
36+
1237
it('should correctly start monitoring for single server connection', {
1338
metadata: { requires: { topology: 'single', os: '!win32' } },
14-
test() {
39+
test: function (done) {
1540
var configuration = this.configuration;
16-
var client = configuration.newClient(
41+
client = configuration.newClient(
1742
`mongodb://${encodeURIComponent('/tmp/mongodb-27017.sock')}?w=1`,
1843
{
1944
maxPoolSize: 1,
@@ -27,12 +52,10 @@ describe('Connection - functional', function () {
2752
isMonitoring = event instanceof ServerHeartbeatStartedEvent;
2853
});
2954

30-
return client
31-
.connect()
32-
.then(() => {
33-
expect(isMonitoring);
34-
})
35-
.finally(() => client.close());
55+
client.connect().then(() => {
56+
expect(isMonitoring);
57+
done();
58+
});
3659
}
3760
});
3861

@@ -41,7 +64,7 @@ describe('Connection - functional', function () {
4164

4265
test: function (done) {
4366
var configuration = this.configuration;
44-
var client = configuration.newClient(
67+
client = configuration.newClient(
4568
`mongodb://${encodeURIComponent('/tmp/mongodb-27017.sock')}?w=1`,
4669
{ maxPoolSize: 1 }
4770
);
@@ -62,22 +85,37 @@ describe('Connection - functional', function () {
6285
expect(err).to.not.exist;
6386
test.equal(1, items.length);
6487

65-
client.close(done);
88+
done();
6689
});
6790
}
6891
);
6992
});
7093
}
7194
});
7295

96+
it('should only pass one argument (topology and not error) for topology "open" events', function (done) {
97+
const configuration = this.configuration;
98+
client = configuration.newClient({ w: 1 }, { maxPoolSize: 1 });
99+
100+
client.on('topologyOpening', () => {
101+
client.topology.on('open', (...args) => {
102+
expect(args).to.have.lengthOf(1);
103+
expect(args[0]).to.be.instanceOf(Topology);
104+
done();
105+
});
106+
});
107+
108+
client.connect();
109+
});
110+
73111
it('should correctly connect to server using just events', function (done) {
74112
var configuration = this.configuration;
75-
var client = configuration.newClient({ w: 1 }, { maxPoolSize: 1 });
113+
client = configuration.newClient({ w: 1 }, { maxPoolSize: 1 });
76114

77115
client.on('open', clientFromEvent => {
78116
expect(clientFromEvent).to.be.instanceOf(MongoClient);
79117
expect(clientFromEvent).to.equal(client);
80-
clientFromEvent.close(done);
118+
done();
81119
});
82120

83121
client.connect();
@@ -90,9 +128,9 @@ describe('Connection - functional', function () {
90128

91129
test: function (done) {
92130
var configuration = this.configuration;
93-
var client = configuration.newClient({ w: 1 }, { maxPoolSize: 2000 });
131+
client = configuration.newClient({ w: 1 }, { maxPoolSize: 2000 });
94132
client.on('open', function () {
95-
client.close(done);
133+
done();
96134
});
97135

98136
client.connect();
@@ -130,11 +168,11 @@ describe('Connection - functional', function () {
130168

131169
test: function (done) {
132170
const configuration = this.configuration;
133-
const client = configuration.newClient();
171+
client = configuration.newClient();
134172

135173
client.connect(
136-
connectionTester(configuration, 'testConnectNoOptions', function (client) {
137-
client.close(done);
174+
connectionTester(configuration, 'testConnectNoOptions', function () {
175+
done();
138176
})
139177
);
140178
}
@@ -148,24 +186,24 @@ describe('Connection - functional', function () {
148186
const username = 'testConnectGoodAuth';
149187
const password = 'password';
150188

151-
const setupClient = configuration.newClient();
189+
client = configuration.newClient();
152190

153191
// First add a user.
154-
setupClient.connect(function (err, client) {
192+
client.connect(function (err, client) {
155193
expect(err).to.not.exist;
156194
var db = client.db(configuration.db);
157195

158196
db.addUser(username, password, function (err) {
159197
expect(err).to.not.exist;
160-
client.close(restOfTest);
198+
restOfTest();
161199
});
162200
});
163201

164202
function restOfTest() {
165-
const testClient = configuration.newClient(configuration.url({ username, password }));
203+
testClient = configuration.newClient(configuration.url({ username, password }));
166204
testClient.connect(
167-
connectionTester(configuration, 'testConnectGoodAuth', function (client) {
168-
client.close(done);
205+
connectionTester(configuration, 'testConnectGoodAuth', function () {
206+
done();
169207
})
170208
);
171209
}
@@ -181,25 +219,25 @@ describe('Connection - functional', function () {
181219
const password = 'password';
182220

183221
// First add a user.
184-
const setupClient = configuration.newClient();
185-
setupClient.connect(function (err, client) {
222+
client = configuration.newClient();
223+
client.connect(function (err, client) {
186224
expect(err).to.not.exist;
187225
var db = client.db(configuration.db);
188226

189227
db.addUser(username, password, { roles: ['read'] }, function (err) {
190228
expect(err).to.not.exist;
191-
client.close(restOfTest);
229+
restOfTest();
192230
});
193231
});
194232

195233
function restOfTest() {
196234
var opts = { auth: { username, password }, authSource: configuration.db };
197235

198-
const testClient = configuration.newClient(opts);
236+
testClient = configuration.newClient(opts);
199237

200238
testClient.connect(
201-
connectionTester(configuration, 'testConnectGoodAuthAsOption', function (client) {
202-
client.close(done);
239+
connectionTester(configuration, 'testConnectGoodAuthAsOption', function () {
240+
done();
203241
})
204242
);
205243
}
@@ -211,7 +249,7 @@ describe('Connection - functional', function () {
211249

212250
test: function (done) {
213251
var configuration = this.configuration;
214-
const client = configuration.newClient(
252+
client = configuration.newClient(
215253
configuration.url({ username: 'slithy', password: 'toves' })
216254
);
217255
client.connect(function (err, client) {

0 commit comments

Comments
 (0)