Skip to content

Commit 9109360

Browse files
committed
refactor: make event names consistent with spec, remove error types
1 parent 6ba409a commit 9109360

File tree

4 files changed

+28
-25
lines changed

4 files changed

+28
-25
lines changed

lib/cmap/connection_pool.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ const PoolClosedError = errors.PoolClosedError;
1414
const WaitQueueTimeoutError = errors.WaitQueueTimeoutError;
1515

1616
const events = require('./events');
17-
const PoolCreatedEvent = events.PoolCreatedEvent;
18-
const PoolClosedEvent = events.PoolClosedEvent;
17+
const ConnectionPoolCreatedEvent = events.ConnectionPoolCreatedEvent;
18+
const ConnectionPoolClosedEvent = events.ConnectionPoolClosedEvent;
1919
const ConnectionCreatedEvent = events.ConnectionCreatedEvent;
2020
const ConnectionReadyEvent = events.ConnectionReadyEvent;
2121
const ConnectionClosedEvent = events.ConnectionClosedEvent;
2222
const ConnectionCheckOutStartedEvent = events.ConnectionCheckOutStartedEvent;
2323
const ConnectionCheckOutFailedEvent = events.ConnectionCheckOutFailedEvent;
2424
const ConnectionCheckedOutEvent = events.ConnectionCheckedOutEvent;
2525
const ConnectionCheckedInEvent = events.ConnectionCheckedInEvent;
26-
const PoolClearedEvent = events.PoolClearedEvent;
26+
const ConnectionPoolClearedEvent = events.ConnectionPoolClearedEvent;
2727

2828
const kConnections = Symbol('connections');
2929
const kPermits = Symbol('permits');
@@ -147,7 +147,7 @@ class ConnectionPool extends EventEmitter {
147147
this[kWaitQueue] = new Denque();
148148

149149
process.nextTick(() => {
150-
this.emit('connectionPoolCreated', new PoolCreatedEvent(this));
150+
this.emit('connectionPoolCreated', new ConnectionPoolCreatedEvent(this));
151151
ensureMinPoolSize(this);
152152
});
153153
}
@@ -223,7 +223,7 @@ class ConnectionPool extends EventEmitter {
223223
*/
224224
clear() {
225225
this[kGeneration] += 1;
226-
this.emit('connectionPoolCleared', new PoolClearedEvent(this));
226+
this.emit('connectionPoolCleared', new ConnectionPoolClearedEvent(this));
227227
}
228228

229229
/**
@@ -276,7 +276,7 @@ class ConnectionPool extends EventEmitter {
276276
},
277277
err => {
278278
this[kConnections].clear();
279-
this.emit('connectionPoolClosed', new PoolClosedEvent(this));
279+
this.emit('connectionPoolClosed', new ConnectionPoolClosedEvent(this));
280280
callback(err);
281281
}
282282
);

lib/cmap/errors.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,15 @@ const MongoError = require('../core/error').MongoError;
44
class PoolClosedError extends MongoError {
55
constructor(pool) {
66
super('Attempted to check out a connection from closed connection pool');
7-
Error.captureStackTrace(this, this.constructor);
8-
this.type = 'PoolClosedError';
7+
this.name = 'MongoPoolClosedError';
98
this.address = pool.address;
109
}
1110
}
1211

1312
class WaitQueueTimeoutError extends MongoError {
1413
constructor(pool) {
1514
super('Timed out while checking out a connection from connection pool');
16-
Error.captureStackTrace(this, this.constructor);
17-
this.type = 'WaitQueueTimeoutError';
15+
this.name = 'MongoWaitQueueTimeoutError';
1816
this.address = pool.address;
1917
}
2018
}

lib/cmap/events.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,90 @@
11
'use strict';
22

3-
class PoolMonitoringEvent {
3+
class ConnectionPoolMonitoringEvent {
44
constructor(type, pool) {
55
this.time = new Date();
66
this.type = type;
77
this.address = pool.address;
88
}
99
}
1010

11-
class PoolCreatedEvent extends PoolMonitoringEvent {
11+
class ConnectionPoolCreatedEvent extends ConnectionPoolMonitoringEvent {
1212
constructor(pool) {
1313
super('ConnectionPoolCreated', pool);
1414
this.options = pool.options;
1515
}
1616
}
1717

18-
class PoolClosedEvent extends PoolMonitoringEvent {
18+
class ConnectionPoolClosedEvent extends ConnectionPoolMonitoringEvent {
1919
constructor(pool) {
2020
super('ConnectionPoolClosed', pool);
2121
}
2222
}
2323

24-
class ConnectionCreatedEvent extends PoolMonitoringEvent {
24+
class ConnectionCreatedEvent extends ConnectionPoolMonitoringEvent {
2525
constructor(pool, connection) {
2626
super('ConnectionCreated', pool);
2727
this.connectionId = connection.id;
2828
}
2929
}
3030

31-
class ConnectionReadyEvent extends PoolMonitoringEvent {
31+
class ConnectionReadyEvent extends ConnectionPoolMonitoringEvent {
3232
constructor(pool, connection) {
3333
super('ConnectionReady', pool);
3434
this.connectionId = connection.id;
3535
}
3636
}
3737

38-
class ConnectionClosedEvent extends PoolMonitoringEvent {
38+
class ConnectionClosedEvent extends ConnectionPoolMonitoringEvent {
3939
constructor(pool, connection, reason) {
4040
super('ConnectionClosed', pool);
4141
this.connectionId = connection.id;
4242
this.reason = reason || 'unknown';
4343
}
4444
}
4545

46-
class ConnectionCheckOutStartedEvent extends PoolMonitoringEvent {
46+
class ConnectionCheckOutStartedEvent extends ConnectionPoolMonitoringEvent {
4747
constructor(pool) {
4848
super('ConnectionCheckOutStarted', pool);
4949
}
5050
}
5151

52-
class ConnectionCheckOutFailedEvent extends PoolMonitoringEvent {
52+
class ConnectionCheckOutFailedEvent extends ConnectionPoolMonitoringEvent {
5353
constructor(pool, reason) {
5454
super('ConnectionCheckOutFailed', pool);
5555
this.reason = reason;
5656
}
5757
}
5858

59-
class ConnectionCheckedOutEvent extends PoolMonitoringEvent {
59+
class ConnectionCheckedOutEvent extends ConnectionPoolMonitoringEvent {
6060
constructor(pool, connection) {
6161
super('ConnectionCheckedOut', pool);
6262
this.connectionId = connection.id;
6363
}
6464
}
6565

66-
class ConnectionCheckedInEvent extends PoolMonitoringEvent {
66+
class ConnectionCheckedInEvent extends ConnectionPoolMonitoringEvent {
6767
constructor(pool, connection) {
6868
super('ConnectionCheckedIn', pool);
6969
this.connectionId = connection.id;
7070
}
7171
}
7272

73-
class PoolClearedEvent extends PoolMonitoringEvent {
73+
class ConnectionPoolClearedEvent extends ConnectionPoolMonitoringEvent {
7474
constructor(pool) {
7575
super('ConnectionPoolCleared', pool);
7676
}
7777
}
7878

7979
module.exports = {
80-
PoolCreatedEvent,
81-
PoolClosedEvent,
80+
ConnectionPoolCreatedEvent,
81+
ConnectionPoolClosedEvent,
8282
ConnectionCreatedEvent,
8383
ConnectionReadyEvent,
8484
ConnectionClosedEvent,
8585
ConnectionCheckOutStartedEvent,
8686
ConnectionCheckOutFailedEvent,
8787
ConnectionCheckedOutEvent,
8888
ConnectionCheckedInEvent,
89-
PoolClearedEvent
89+
ConnectionPoolClearedEvent
9090
};

test/unit/cmap/connection_pool.test.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,11 @@ describe('Connection Pool', function() {
250250
return thread;
251251
}
252252

253+
function eventType(event) {
254+
const eventName = event.constructor.name;
255+
return eventName.substring(0, eventName.lastIndexOf('Event'));
256+
}
257+
253258
const OPERATION_FUNCTIONS = {
254259
checkOut: function(op) {
255260
return PROMISIFIED_POOL_FUNCTIONS.checkOut.call(pool).then(connection => {
@@ -307,7 +312,7 @@ describe('Connection Pool', function() {
307312
const count = options.count;
308313
return new Promise(resolve => {
309314
function run() {
310-
if (poolEvents.filter(ev => ev.type === event).length >= count) {
315+
if (poolEvents.filter(ev => eventType(ev) === event).length >= count) {
311316
return resolve();
312317
}
313318

0 commit comments

Comments
 (0)