Skip to content

Commit

Permalink
chore(NODE-3521): find/agg cursor session always there
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Mar 1, 2022
1 parent ddc4d24 commit 6be9726
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/cursor/aggregation_cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class AggregationCursor<TSchema = Document> extends AbstractCursor<TSchem
}

/** @internal */
_initialize(session: ClientSession | undefined, callback: Callback<ExecutionResult>): void {
_initialize(session: ClientSession, callback: Callback<ExecutionResult>): void {
const aggregateOperation = new AggregateOperation(this.namespace, this[kPipeline], {
...this[kOptions],
...this.cursorOptions,
Expand Down
2 changes: 1 addition & 1 deletion src/cursor/find_cursor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class FindCursor<TSchema = Document> extends AbstractCursor<TSchema> {
}

/** @internal */
_initialize(session: ClientSession | undefined, callback: Callback<ExecutionResult>): void {
_initialize(session: ClientSession, callback: Callback<ExecutionResult>): void {
const findOperation = new FindOperation(undefined, this.namespace, this[kFilter], {
...this[kBuiltOptions], // NOTE: order matters here, we may need to refine this
...this.cursorOptions,
Expand Down
117 changes: 117 additions & 0 deletions test/unit/cursor/aggregation_cursor.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
'use strict';

const expect = require('chai').expect;
const { MongoError } = require('../../../src/error');
const mock = require('../../tools/mongodb-mock/index');
const { Topology } = require('../../../src/sdam/topology');
const { Long } = require('bson');
const { MongoDBNamespace, isHello } = require('../../../src/utils');
const { AggregationCursor } = require('../../../src/cursor/aggregation_cursor');

const test = {};
describe('Aggregation Cursor', function () {
describe('#next', function () {
afterEach(function () {
mock.cleanup();
});
beforeEach(function () {
return mock.createServer().then(mockServer => {
test.server = mockServer;
});
});

context('when there is a data bearing server', function () {
beforeEach(function () {
test.server.setMessageHandler(request => {
const doc = request.document;
if (isHello(doc)) {
request.reply(mock.HELLO);
} else if (doc.aggregate) {
request.reply({
cursor: {
id: Long.fromNumber(1),
ns: 'test.test',
firstBatch: [{ _id: 1, name: 'test' }]
},
ok: 1
});
}
});
});

it('sets the session on the cursor', function (done) {
const topology = new Topology(test.server.hostAddress());
const cursor = new AggregationCursor(topology, MongoDBNamespace.fromString('test.test'), [], {});
topology.connect(function () {
cursor.next(function () {
expect(cursor.session).to.exist;
topology.close(done);
});
});
});
});

context('when there is no data bearing server', function () {
beforeEach(function () {
test.server.setMessageHandler(request => {
const doc = request.document;
if (isHello(doc)) {
request.reply({ errmsg: 'network error' });
} else if (doc.aggregate) {
request.reply({
cursor: {
id: Long.fromNumber(1),
ns: 'test.test',
firstBatch: [{ _id: 1, name: 'test' }]
},
ok: 1
});
}
});
});

it('does not set the session on the cursor', function (done) {
const topology = new Topology(test.server.hostAddress(), { serverSelectionTimeoutMS: 2 });
const cursor = new AggregationCursor(topology, MongoDBNamespace.fromString('test.test'), [], {});
topology.connect(function () {
cursor.next(function () {
expect(cursor.session).to.not.exist;
topology.close(done);
});
});
});
});

context('when a data bearing server becomes available', function () {
beforeEach(function () {
let helloCalls = 0;
test.server.setMessageHandler(request => {
const doc = request.document;
if (isHello(doc)) {
request.reply(helloCalls > 0 ? { errmsg: 'network error' } : mock.HELLO);
} else if (doc.aggregate) {
request.reply({
cursor: {
id: Long.fromNumber(1),
ns: 'test.test',
firstBatch: [{ _id: 1, name: 'test' }]
},
ok: 1
});
}
});
});

it('sets the session on the cursor', function (done) {
const topology = new Topology(test.server.hostAddress(), { serverSelectionTimeoutMS: 2 });
const cursor = new AggregationCursor(topology, MongoDBNamespace.fromString('test.test'), [], {});
topology.connect(function () {
cursor.next(function () {
expect(cursor.session).to.exist;
topology.close(done);
});
});
});
});
});
});
2 changes: 1 addition & 1 deletion test/unit/cursor/find_cursor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const test = {};
describe('Find Cursor', function () {
describe('#next', function () {
afterEach(function () {
mock.cleanup()
mock.cleanup();
});
beforeEach(function () {
return mock.createServer().then(mockServer => {
Expand Down

0 comments on commit 6be9726

Please sign in to comment.