Skip to content

Commit c460580

Browse files
migrate killCursors
1 parent ff9a785 commit c460580

File tree

2 files changed

+30
-75
lines changed

2 files changed

+30
-75
lines changed

src/operations/kill_cursors.ts

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import type { Long } from '../bson';
2-
import { MongoRuntimeError } from '../error';
3-
import type { Server } from '../sdam/server';
2+
import { type Connection } from '../cmap/connection';
3+
import { MongoDBResponse } from '../cmap/wire_protocol/responses';
4+
import { type MongoError, MongoRuntimeError } from '../error';
5+
import type { Server, ServerCommandOptions } from '../sdam/server';
46
import type { ClientSession } from '../sessions';
57
import { type TimeoutContext } from '../timeout';
6-
import { type MongoDBNamespace, squashError } from '../utils';
7-
import { AbstractOperation, Aspect, defineAspects, type OperationOptions } from './operation';
8+
import { type MongoDBNamespace } from '../utils';
9+
import { Aspect, defineAspects, ModernizedOperation, type OperationOptions } from './operation';
810

911
/**
1012
* https://www.mongodb.com/docs/manual/reference/command/killCursors/
@@ -16,7 +18,8 @@ interface KillCursorsCommand {
1618
comment?: unknown;
1719
}
1820

19-
export class KillCursorsOperation extends AbstractOperation {
21+
export class KillCursorsOperation extends ModernizedOperation<void> {
22+
override SERVER_COMMAND_RESPONSE_TYPE = MongoDBResponse;
2023
cursorId: Long;
2124

2225
constructor(cursorId: Long, ns: MongoDBNamespace, server: Server, options: OperationOptions) {
@@ -30,15 +33,7 @@ export class KillCursorsOperation extends AbstractOperation {
3033
return 'killCursors' as const;
3134
}
3235

33-
override async execute(
34-
server: Server,
35-
session: ClientSession | undefined,
36-
timeoutContext: TimeoutContext
37-
): Promise<void> {
38-
if (server !== this.server) {
39-
throw new MongoRuntimeError('Killcursor must run on the same server operation began on');
40-
}
41-
36+
override buildCommand(_connection: Connection, _session?: ClientSession): KillCursorsCommand {
4237
const killCursors = this.ns.collection;
4338
if (killCursors == null) {
4439
// Cursors should have adopted the namespace returned by MongoDB
@@ -50,15 +45,19 @@ export class KillCursorsOperation extends AbstractOperation {
5045
killCursors,
5146
cursors: [this.cursorId]
5247
};
53-
try {
54-
await server.command(this.ns, killCursorsCommand, {
55-
session,
56-
timeoutContext
57-
});
58-
} catch (error) {
59-
// The driver should never emit errors from killCursors, this is spec-ed behavior
60-
squashError(error);
61-
}
48+
49+
return killCursorsCommand;
50+
}
51+
52+
override buildOptions(timeoutContext: TimeoutContext): ServerCommandOptions {
53+
return {
54+
session: this.session,
55+
timeoutContext
56+
};
57+
}
58+
59+
override handleError(_error: MongoError): void {
60+
// The driver should never emit errors from killCursors, this is spec-ed behavior
6261
}
6362
}
6463

Lines changed: 8 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
11
import { expect } from 'chai';
2-
import * as sinon from 'sinon';
32

43
import {
54
KillCursorsOperation,
65
Long,
76
MongoDBNamespace,
8-
MongoRuntimeError,
97
ns,
108
Server,
119
ServerDescription
1210
} from '../../mongodb';
1311
import { topologyWithPlaceholderClient } from '../../tools/utils';
1412

1513
describe('class KillCursorsOperation', () => {
16-
afterEach(function () {
17-
sinon.restore();
18-
});
19-
2014
describe('constructor()', () => {
2115
const cursorId = Long.fromBigInt(0xffff_ffffn);
2216
const namespace = ns('db.collection');
@@ -43,60 +37,22 @@ describe('class KillCursorsOperation', () => {
4337

4438
describe('execute()', () => {
4539
const cursorId = Long.fromBigInt(0xffff_ffffn);
46-
const namespace = ns('db.collection');
4740
const server = new Server(
4841
topologyWithPlaceholderClient([], {} as any),
4942
new ServerDescription('a:1'),
5043
{} as any
5144
);
52-
const differentServer = new Server(
53-
topologyWithPlaceholderClient([], {} as any),
54-
new ServerDescription('a:1'),
55-
{} as any
56-
);
5745
const options = {};
5846

59-
it('should throw if the server defined from the constructor changes', async () => {
60-
const killCursorsOperation = new KillCursorsOperation(
61-
cursorId,
62-
namespace,
63-
server,
64-
options
65-
) as any;
66-
67-
const error = await killCursorsOperation
68-
.execute(differentServer, undefined)
69-
.catch(error => error);
70-
71-
expect(error).to.be.instanceOf(MongoRuntimeError);
72-
});
73-
7447
it('should throw if the namespace does not define a collection', async () => {
75-
const killCursorsOperation = new KillCursorsOperation(
76-
cursorId,
77-
ns('db'),
78-
server,
79-
options
80-
) as any;
81-
82-
const error = await killCursorsOperation.execute(server, undefined).catch(error => error);
83-
84-
expect(error).to.be.instanceOf(MongoRuntimeError);
85-
});
86-
87-
it('should construct a killCursors command', async () => {
88-
const killCursorsOperation = new KillCursorsOperation(
89-
cursorId,
90-
namespace,
91-
server,
92-
options
93-
) as any;
94-
const stub = sinon.stub(server, 'command').resolves({});
95-
await killCursorsOperation.execute(server, undefined);
96-
expect(stub).to.have.been.calledOnceWith(namespace, {
97-
killCursors: namespace.collection,
98-
cursors: [cursorId]
99-
});
48+
const killCursorsOperation = new KillCursorsOperation(cursorId, ns('db'), server, options);
49+
50+
const connection = {
51+
description: {}
52+
} as any;
53+
expect(() => {
54+
killCursorsOperation.buildCommand(connection);
55+
}).to.throw(/A collection name must be determined before killCursors/);
10056
});
10157
});
10258
});

0 commit comments

Comments
 (0)