Skip to content

Commit dc62173

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

File tree

4 files changed

+144
-5
lines changed

4 files changed

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

test/unit/cursor/find_cursor.test.js

+7-3
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 => {
@@ -71,7 +71,9 @@ describe('Find Cursor', function () {
7171
});
7272

7373
it('does not set the session on the cursor', function (done) {
74-
const topology = new Topology(test.server.hostAddress(), { serverSelectionTimeoutMS: 2 });
74+
const topology = new Topology(test.server.hostAddress(), {
75+
serverSelectionTimeoutMS: 1000
76+
});
7577
const cursor = new FindCursor(topology, MongoDBNamespace.fromString('test.test'), {}, {});
7678
topology.connect(function () {
7779
cursor.next(function () {
@@ -103,7 +105,9 @@ describe('Find Cursor', function () {
103105
});
104106

105107
it('sets the session on the cursor', function (done) {
106-
const topology = new Topology(test.server.hostAddress(), { serverSelectionTimeoutMS: 2 });
108+
const topology = new Topology(test.server.hostAddress(), {
109+
serverSelectionTimeoutMS: 1000
110+
});
107111
const cursor = new FindCursor(topology, MongoDBNamespace.fromString('test.test'), {}, {});
108112
topology.connect(function () {
109113
cursor.next(function () {

0 commit comments

Comments
 (0)