Skip to content

NODE-3008: Dont pass session to cloned cursor #2725

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/cursor/aggregation_cursor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AggregateOperation, AggregateOptions } from '../operations/aggregate';
import { AbstractCursor, assertUninitialized } from './abstract_cursor';
import { executeOperation, ExecutionResult } from '../operations/execute_operation';
import { mergeOptions } from '../utils';
import type { Document } from '../bson';
import type { Sort } from '../sort';
import type { Topology } from '../sdam/topology';
Expand Down Expand Up @@ -52,9 +53,9 @@ export class AggregationCursor extends AbstractCursor {
}

clone(): AggregationCursor {
const clonedOptions = mergeOptions({}, this[kOptions]);
return new AggregationCursor(this[kParent], this.topology, this.namespace, this[kPipeline], {
...this[kOptions],
...this.cursorOptions
...clonedOptions
});
}

Expand Down
5 changes: 3 additions & 2 deletions src/cursor/find_cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { ExplainVerbosityLike } from '../explain';
import { CountOperation, CountOptions } from '../operations/count';
import { executeOperation, ExecutionResult } from '../operations/execute_operation';
import { FindOperation, FindOptions } from '../operations/find';
import { mergeOptions } from '../utils';
import type { Hint } from '../operations/operation';
import type { CollationOptions } from '../operations/command';
import type { Topology } from '../sdam/topology';
Expand Down Expand Up @@ -52,9 +53,9 @@ export class FindCursor extends AbstractCursor {
}

clone(): FindCursor {
const clonedOptions = mergeOptions({}, this[kBuiltOptions]);
return new FindCursor(this.topology, this.namespace, this[kFilter], {
...this[kBuiltOptions],
...this.cursorOptions
...clonedOptions
});
}

Expand Down
59 changes: 59 additions & 0 deletions test/functional/cursor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3755,6 +3755,65 @@ describe('Cursor', function () {
}
);

describe('#clone', function () {
let client;
let db;
let collection;

beforeEach(function () {
client = this.configuration.newClient({ w: 1 });

return client.connect().then(client => {
db = client.db(this.configuration.db);
collection = db.collection('test_coll');
});
});

afterEach(function () {
return client.close();
});

context('when executing on a find cursor', function () {
it('removes the existing session from the cloned cursor', function () {
const docs = [{ name: 'test1' }, { name: 'test2' }];
return collection.insertMany(docs).then(() => {
const cursor = collection.find({}, { batchSize: 1 });
return cursor
.next()
.then(doc => {
expect(doc).to.exist;
const clonedCursor = cursor.clone();
expect(clonedCursor.cursorOptions.session).to.not.exist;
expect(clonedCursor.session).to.not.exist;
})
.finally(() => {
return cursor.close();
});
});
});
});

context('when executing on an aggregation cursor', function () {
it('removes the existing session from the cloned cursor', function () {
const docs = [{ name: 'test1' }, { name: 'test2' }];
return collection.insertMany(docs).then(() => {
const cursor = collection.aggregate([{ $match: {} }], { batchSize: 1 });
return cursor
.next()
.then(doc => {
expect(doc).to.exist;
const clonedCursor = cursor.clone();
expect(clonedCursor.cursorOptions.session).to.not.exist;
expect(clonedCursor.session).to.not.exist;
})
.finally(() => {
return cursor.close();
});
});
});
});
});

it('should return a promise when no callback supplied to forEach method', function () {
const configuration = this.configuration;
const client = configuration.newClient({ w: 1 }, { maxPoolSize: 1 });
Expand Down