Skip to content

Commit fbad7ba

Browse files
committed
test: check that event emitters have error listeners in the same tick of their creation
1 parent 654069f commit fbad7ba

File tree

21 files changed

+54
-49
lines changed

21 files changed

+54
-49
lines changed

.mocharc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"source-map-support/register",
55
"ts-node/register",
66
"test/tools/runner/chai_addons.ts",
7-
"test/tools/runner/hooks/unhandled_checker.ts"
7+
"test/tools/runner/ee_checker.ts"
88
],
99
"extension": [
1010
"js",

src/change_stream.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,11 @@ export class ChangeStream<
630630
options: ChangeStreamOptions = {}
631631
) {
632632
super();
633+
const ctorCallSite = new Error('ChangeStream must add an error listener synchronously');
634+
ctorCallSite.stack;
635+
636+
const squishErrorListenerRequirement = () => null;
637+
this.on('error', squishErrorListenerRequirement);
633638

634639
this.pipeline = pipeline;
635640
this.options = { ...options };
@@ -667,7 +672,11 @@ export class ChangeStream<
667672
// Listen for any `change` listeners being added to ChangeStream
668673
this.on('newListener', eventName => {
669674
if (eventName === 'change' && this.cursor && this.listenerCount('change') === 0) {
675+
this.off('error', squishErrorListenerRequirement);
670676
this._streamEvents(this.cursor);
677+
process.nextTick(() => {
678+
if (this.listenerCount('error') === 0) throw ctorCallSite;
679+
});
671680
}
672681
});
673682

src/cmap/connection.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
229229

230230
constructor(stream: Stream, options: ConnectionOptions) {
231231
super();
232+
this.on('error', () => null);
232233

233234
this.socket = stream;
234235
this.id = options.id;

src/cmap/connection_pool.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ export class ConnectionPool extends TypedEventEmitter<ConnectionPoolEvents> {
200200

201201
constructor(server: Server, options: ConnectionPoolOptions) {
202202
super();
203+
this.on('error', () => null);
203204

204205
this.options = Object.freeze({
205206
connectionType: Connection,

src/cursor/abstract_cursor.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ export abstract class AbstractCursor<
267267
options: AbstractCursorOptions & Abortable = {}
268268
) {
269269
super();
270+
this.on('error', () => null);
270271

271272
if (!client.s.isMongoClient) {
272273
throw new MongoRuntimeError('Cursor must be constructed with MongoClient');

src/gridfs/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ export class GridFSBucket extends TypedEventEmitter<GridFSBucketEvents> {
8787

8888
constructor(db: Db, options?: GridFSBucketOptions) {
8989
super();
90+
this.on('error', () => null);
9091
this.setMaxListeners(0);
9192
const privateOptions = resolveOptions(db, {
9293
...DEFAULT_GRIDFS_BUCKET_OPTIONS,

src/mongo_client.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ export class MongoClient extends TypedEventEmitter<MongoClientEvents> implements
384384

385385
constructor(url: string, options?: MongoClientOptions) {
386386
super();
387+
this.on('error', () => null);
387388

388389
this.options = parseOptions(url, this, options);
389390

src/mongo_types.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,12 @@ export class TypedEventEmitter<Events extends EventsDescription> extends EventEm
472472
}
473473

474474
/** @public */
475-
export class CancellationToken extends TypedEventEmitter<{ cancel(): void }> {}
475+
export class CancellationToken extends TypedEventEmitter<{ cancel(): void }> {
476+
constructor(...args: any[]) {
477+
super(...args);
478+
this.on('error', () => null);
479+
}
480+
}
476481

477482
/** @public */
478483
export type Abortable = {

src/sdam/monitor.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ export class Monitor extends TypedEventEmitter<MonitorEvents> {
102102

103103
constructor(server: Server, options: MonitorOptions) {
104104
super();
105+
this.on('error', () => null);
105106

106107
this.server = server;
107108
this.connection = null;

src/sdam/server.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ export class Server extends TypedEventEmitter<ServerEvents> {
142142
*/
143143
constructor(topology: Topology, description: ServerDescription, options: ServerOptions) {
144144
super();
145+
this.on('error', () => null);
145146

146147
this.serverApi = options.serverApi;
147148

0 commit comments

Comments
 (0)