Skip to content

Commit 6be9726

Browse files
committed
chore(NODE-3521): find/agg cursor session always there
1 parent ddc4d24 commit 6be9726

File tree

4 files changed

+120
-3
lines changed

4 files changed

+120
-3
lines changed

src/cursor/aggregation_cursor.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export class AggregationCursor<TSchema = Document> extends AbstractCursor<TSchem
6161
}
6262

6363
/** @internal */
64-
_initialize(session: ClientSession | undefined, callback: Callback<ExecutionResult>): void {
64+
_initialize(session: ClientSession, callback: Callback<ExecutionResult>): void {
6565
const aggregateOperation = new AggregateOperation(this.namespace, this[kPipeline], {
6666
...this[kOptions],
6767
...this.cursorOptions,

src/cursor/find_cursor.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export class FindCursor<TSchema = Document> extends AbstractCursor<TSchema> {
6868
}
6969

7070
/** @internal */
71-
_initialize(session: ClientSession | undefined, callback: Callback<ExecutionResult>): void {
71+
_initialize(session: ClientSession, callback: Callback<ExecutionResult>): void {
7272
const findOperation = new FindOperation(undefined, this.namespace, this[kFilter], {
7373
...this[kBuiltOptions], // NOTE: order matters here, we may need to refine this
7474
...this.cursorOptions,
+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
'use strict';
2+
3+
const expect = require('chai').expect;
4+
const { MongoError } = require('../../../src/error');
5+
const mock = require('../../tools/mongodb-mock/index');
6+
const { Topology } = require('../../../src/sdam/topology');
7+
const { Long } = require('bson');
8+
const { MongoDBNamespace, isHello } = require('../../../src/utils');
9+
const { AggregationCursor } = require('../../../src/cursor/aggregation_cursor');
10+
11+
const test = {};
12+
describe('Aggregation Cursor', function () {
13+
describe('#next', function () {
14+
afterEach(function () {
15+
mock.cleanup();
16+
});
17+
beforeEach(function () {
18+
return mock.createServer().then(mockServer => {
19+
test.server = mockServer;
20+
});
21+
});
22+
23+
context('when there is a data bearing server', function () {
24+
beforeEach(function () {
25+
test.server.setMessageHandler(request => {
26+
const doc = request.document;
27+
if (isHello(doc)) {
28+
request.reply(mock.HELLO);
29+
} else if (doc.aggregate) {
30+
request.reply({
31+
cursor: {
32+
id: Long.fromNumber(1),
33+
ns: 'test.test',
34+
firstBatch: [{ _id: 1, name: 'test' }]
35+
},
36+
ok: 1
37+
});
38+
}
39+
});
40+
});
41+
42+
it('sets the session on the cursor', function (done) {
43+
const topology = new Topology(test.server.hostAddress());
44+
const cursor = new AggregationCursor(topology, MongoDBNamespace.fromString('test.test'), [], {});
45+
topology.connect(function () {
46+
cursor.next(function () {
47+
expect(cursor.session).to.exist;
48+
topology.close(done);
49+
});
50+
});
51+
});
52+
});
53+
54+
context('when there is no data bearing server', function () {
55+
beforeEach(function () {
56+
test.server.setMessageHandler(request => {
57+
const doc = request.document;
58+
if (isHello(doc)) {
59+
request.reply({ errmsg: 'network error' });
60+
} else if (doc.aggregate) {
61+
request.reply({
62+
cursor: {
63+
id: Long.fromNumber(1),
64+
ns: 'test.test',
65+
firstBatch: [{ _id: 1, name: 'test' }]
66+
},
67+
ok: 1
68+
});
69+
}
70+
});
71+
});
72+
73+
it('does not set the session on the cursor', function (done) {
74+
const topology = new Topology(test.server.hostAddress(), { serverSelectionTimeoutMS: 2 });
75+
const cursor = new AggregationCursor(topology, MongoDBNamespace.fromString('test.test'), [], {});
76+
topology.connect(function () {
77+
cursor.next(function () {
78+
expect(cursor.session).to.not.exist;
79+
topology.close(done);
80+
});
81+
});
82+
});
83+
});
84+
85+
context('when a data bearing server becomes available', function () {
86+
beforeEach(function () {
87+
let helloCalls = 0;
88+
test.server.setMessageHandler(request => {
89+
const doc = request.document;
90+
if (isHello(doc)) {
91+
request.reply(helloCalls > 0 ? { errmsg: 'network error' } : mock.HELLO);
92+
} else if (doc.aggregate) {
93+
request.reply({
94+
cursor: {
95+
id: Long.fromNumber(1),
96+
ns: 'test.test',
97+
firstBatch: [{ _id: 1, name: 'test' }]
98+
},
99+
ok: 1
100+
});
101+
}
102+
});
103+
});
104+
105+
it('sets the session on the cursor', function (done) {
106+
const topology = new Topology(test.server.hostAddress(), { serverSelectionTimeoutMS: 2 });
107+
const cursor = new AggregationCursor(topology, MongoDBNamespace.fromString('test.test'), [], {});
108+
topology.connect(function () {
109+
cursor.next(function () {
110+
expect(cursor.session).to.exist;
111+
topology.close(done);
112+
});
113+
});
114+
});
115+
});
116+
});
117+
});

test/unit/cursor/find_cursor.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const test = {};
1212
describe('Find Cursor', function () {
1313
describe('#next', function () {
1414
afterEach(function () {
15-
mock.cleanup()
15+
mock.cleanup();
1616
});
1717
beforeEach(function () {
1818
return mock.createServer().then(mockServer => {

0 commit comments

Comments
 (0)