Skip to content

Commit 4cbdb6c

Browse files
committed
test: refactor unified event capture logic for ease of use
1 parent 648212c commit 4cbdb6c

File tree

2 files changed

+29
-25
lines changed

2 files changed

+29
-25
lines changed

test/tools/unified-spec-runner/entities.ts

+18-15
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,17 @@ export class UnifiedMongoClient extends MongoClient {
140140
return this.ignoredEvents.includes(e.commandName);
141141
}
142142

143+
getCapturedEvents(eventType: string): CommandEvent[] | CmapEvent[] {
144+
switch (eventType) {
145+
case 'command':
146+
return this.commandEvents;
147+
case 'cmap':
148+
return this.cmapEvents;
149+
default:
150+
throw new Error(`Unknown eventType: ${eventType}`);
151+
}
152+
}
153+
143154
// NOTE: pushCommandEvent must be an arrow function
144155
pushCommandEvent: (e: CommandEvent) => void = e => {
145156
if (!this.isIgnored(e)) {
@@ -152,22 +163,14 @@ export class UnifiedMongoClient extends MongoClient {
152163
this.cmapEvents.push(e);
153164
};
154165

155-
stopCapturingEvents(pushFn: PushFunction): void {
156-
const observedEvents = [...this.observedCommandEvents, ...this.observedCmapEvents];
157-
for (const eventName of observedEvents) {
158-
this.off(eventName, pushFn);
159-
}
160-
}
161-
162166
/** Disables command monitoring for the client and returns a list of the captured events. */
163-
stopCapturingCommandEvents(): CommandEvent[] {
164-
this.stopCapturingEvents(this.pushCommandEvent);
165-
return this.commandEvents;
166-
}
167-
168-
stopCapturingCmapEvents(): CmapEvent[] {
169-
this.stopCapturingEvents(this.pushCmapEvent);
170-
return this.cmapEvents;
167+
stopCapturingEvents(): void {
168+
for (const eventName of this.observedCommandEvents) {
169+
this.off(eventName, this.pushCommandEvent);
170+
}
171+
for (const eventName of this.observedCmapEvents) {
172+
this.off(eventName, this.pushCmapEvent);
173+
}
171174
}
172175
}
173176

test/tools/unified-spec-runner/runner.ts

+11-10
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { ReadPreference } from '../../../src/read_preference';
88
import { TopologyType } from '../../../src/sdam/common';
99
import { ns } from '../../../src/utils';
1010
import { ejson } from '../utils';
11-
import { CmapEvent, CommandEvent, EntitiesMap } from './entities';
11+
import { CmapEvent, CommandEvent, EntitiesMap, UnifiedMongoClient } from './entities';
1212
import { matchesEvents } from './match';
1313
import { executeOperationAndCheck } from './operations';
1414
import * as uni from './schema';
@@ -198,13 +198,12 @@ async function runUnifiedTest(
198198
}
199199
}
200200

201-
const clientCommandEvents = new Map<string, CommandEvent[]>();
202-
const clientCmapEvents = new Map<string, CmapEvent[]>();
201+
const clientList = new Map<string, UnifiedMongoClient>();
203202
// If any event listeners were enabled on any client entities,
204203
// the test runner MUST now disable those event listeners.
205204
for (const [id, client] of entities.mapOf('client')) {
206-
clientCommandEvents.set(id, client.stopCapturingCommandEvents());
207-
clientCmapEvents.set(id, client.stopCapturingCmapEvents());
205+
client.stopCapturingEvents();
206+
clientList.set(id, client);
208207
}
209208

210209
if (test.expectEvents) {
@@ -213,11 +212,13 @@ async function runUnifiedTest(
213212
const eventType = expectedEventsForClient.eventType;
214213
// If no event type is provided it defaults to 'command', so just
215214
// check for 'cmap' here for now.
216-
const actualEvents =
217-
eventType === 'cmap' ? clientCmapEvents.get(clientId) : clientCommandEvents.get(clientId);
218-
219-
expect(actualEvents, `No client entity found with id ${clientId}`).to.exist;
220-
matchesEvents(expectedEventsForClient, actualEvents!, entities);
215+
const testClient = clientList.get(clientId);
216+
expect(testClient, `No client entity found with id ${clientId}`).to.exist;
217+
matchesEvents(
218+
expectedEventsForClient,
219+
testClient!.getCapturedEvents(eventType ?? 'command'),
220+
entities
221+
);
221222
}
222223
}
223224

0 commit comments

Comments
 (0)