Skip to content

Commit d1a3d43

Browse files
committed
consolidate cs/cursor getting
1 parent 9e94b16 commit d1a3d43

File tree

2 files changed

+17
-35
lines changed

2 files changed

+17
-35
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,16 @@ export class EntitiesMap<E = Entity> extends Map<string, E> {
417417
return new EntitiesMap(Array.from(this.entries()).filter(([, e]) => e instanceof ctor));
418418
}
419419

420+
getChangeStreamOrCursor(key: string): UnifiedChangeStream | AbstractCursor {
421+
try {
422+
const cs = this.getEntity('stream', key);
423+
return cs;
424+
} catch {
425+
const cursor = this.getEntity('cursor', key);
426+
return cursor;
427+
}
428+
}
429+
420430
getEntity(type: 'client', key: string, assertExists?: boolean): UnifiedMongoClient;
421431
getEntity(type: 'db', key: string, assertExists?: boolean): Db;
422432
getEntity(type: 'collection', key: string, assertExists?: boolean): Collection;

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

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ import {
2121
} from '../../mongodb';
2222
import { getSymbolFrom, sleep } from '../../tools/utils';
2323
import { TestConfiguration } from '../runner/config';
24-
import { EntitiesMap, UnifiedChangeStream } from './entities';
24+
import { EntitiesMap } from './entities';
2525
import { expectErrorCheck, resultCheck } from './match';
26-
import type { ExpectedEvent, ExpectedLogMessage, OperationDescription } from './schema';
26+
import type { ExpectedEvent, OperationDescription } from './schema';
2727
import { getMatchingEventCount, translateOptions } from './unified-utils';
2828

2929
interface OperationFunctionParams {
@@ -349,40 +349,14 @@ operations.set('insertMany', async ({ entities, operation }) => {
349349
return collection.insertMany(documents, opts);
350350
});
351351

352-
function getChangeStream({ entities, operation }): UnifiedChangeStream | null {
353-
try {
354-
const changeStream = entities.getEntity('stream', operation.object);
355-
return changeStream;
356-
} catch (e) {
357-
return null;
358-
}
359-
}
360352
operations.set('iterateUntilDocumentOrError', async ({ entities, operation }) => {
361-
const changeStream = getChangeStream({ entities, operation });
362-
if (changeStream == null) {
363-
// iterateUntilDocumentOrError is used for changes streams and regular cursors.
364-
// we have no other way to distinguish which scenario we are testing when we run an
365-
// iterateUntilDocumentOrError operation, so we first try to get the changeStream and
366-
// if that fails, we know we need to get a cursor
367-
const cursor = entities.getEntity('cursor', operation.object);
368-
return cursor.next();
369-
}
370-
371-
return changeStream.next();
353+
const iterable = entities.getChangeStreamOrCursor(operation.object);
354+
return iterable.next();
372355
});
373356

374357
operations.set('iterateOnce', async ({ entities, operation }) => {
375-
const changeStream = getChangeStream({ entities, operation });
376-
if (changeStream == null) {
377-
// iterateOnce is used for changes streams and regular cursors.
378-
// we have no other way to distinguish which scenario we are testing when we run an
379-
// iterateOnce operation, so we first try to get the changeStream and
380-
// if that fails, we know we need to get a cursor
381-
const cursor = entities.getEntity('cursor', operation.object);
382-
return cursor.tryNext();
383-
}
384-
385-
return changeStream.tryNext();
358+
const iterable = entities.getChangeStreamOrCursor(operation.object);
359+
return iterable.tryNext();
386360
});
387361

388362
operations.set('listCollections', async ({ entities, operation }) => {
@@ -701,9 +675,7 @@ operations.set('createCommandCursor', async ({ entities, operation }: OperationF
701675

702676
// The spec dictates that we create the cursor and force the find command
703677
// to execute, but the first document must still be returned for the first iteration.
704-
const result = await cursor.tryNext();
705-
const kDocuments = getSymbolFrom(cursor, 'documents');
706-
if (result) cursor[kDocuments].unshift(result);
678+
await cursor.hasNext();
707679

708680
return cursor;
709681
});

0 commit comments

Comments
 (0)