Skip to content
This repository was archived by the owner on Feb 4, 2022. It is now read-only.

Commit a912ee0

Browse files
daprahamianmbroadst
authored andcommitted
test(OP_MSG): adding tests for getMore
1 parent 57bd8bc commit a912ee0

File tree

3 files changed

+178
-3
lines changed

3 files changed

+178
-3
lines changed

lib/wireprotocol/3_6_support/execute_get_more.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ const retrieveBSON = require('../../connection/utils').retrieveBSON;
88

99
const BSON = retrieveBSON();
1010

11-
function executeGetMore(bson, ns, cursorState, batchSize, raw, connection, options, callback) {
12-
options = options || {};
11+
function executeGetMore(bson, ns, cursorState, batchSize, raw, connection, callback) {
1312
// Build command namespace
1413
const parts = ns.split(/\./);
1514

lib/wireprotocol/3_6_support/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class WireProtocol {
2929
}
3030

3131
getMore(bson, ns, cursorState, batchSize, raw, connection, options, callback) {
32-
executeGetMore(bson, ns, cursorState, batchSize, raw, connection, options, callback);
32+
executeGetMore(bson, ns, cursorState, batchSize, raw, connection, callback);
3333
}
3434

3535
command(bson, ns, cmd, cursorState, topology, options) {
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
'use strict';
2+
3+
const expect = require('chai').expect;
4+
const TestHarness = require('./utils').TestHarness;
5+
const expectMsgToHaveSingleQuery = require('./utils').expectMsgToHaveSingleQuery;
6+
const executeGetMore = require('../../../../../lib/wireprotocol/3_6_support/execute_get_more');
7+
8+
describe('Wire Protocol 3.6 GetMore', function() {
9+
const test = new TestHarness();
10+
11+
const $db = 'darmok';
12+
const collection = 'jalad';
13+
const namespace = `${$db}.${collection}`;
14+
15+
let connection;
16+
17+
beforeEach(() => {
18+
connection = { write: test.sinon.stub() };
19+
});
20+
21+
it('should reflect the basic options on to the getMoreCommand', function() {
22+
const cursorState = {
23+
cmd: {},
24+
cursorId: 8675309
25+
};
26+
27+
const BATCH_SIZE = -23000;
28+
29+
executeGetMore(
30+
test.bson,
31+
namespace,
32+
cursorState,
33+
BATCH_SIZE,
34+
undefined,
35+
connection,
36+
test.callback
37+
);
38+
39+
expect(connection.write).to.have.been.calledOnce;
40+
41+
expectMsgToHaveSingleQuery(connection.write.lastCall.args[0]).to.deep.equal({
42+
$db,
43+
collection,
44+
batchSize: 23000,
45+
getMore: cursorState.cursorId
46+
});
47+
});
48+
49+
it('should attach maxTimeMS', function() {
50+
const cursorState = {
51+
cmd: {
52+
tailable: true,
53+
maxAwaitTimeMS: 1001
54+
},
55+
cursorId: 8675309
56+
};
57+
58+
const BATCH_SIZE = -23000;
59+
60+
executeGetMore(
61+
test.bson,
62+
namespace,
63+
cursorState,
64+
BATCH_SIZE,
65+
undefined,
66+
connection,
67+
test.callback
68+
);
69+
70+
expect(connection.write).to.have.been.calledOnce;
71+
72+
expectMsgToHaveSingleQuery(connection.write.lastCall.args[0]).to.deep.equal({
73+
$db,
74+
collection,
75+
batchSize: 23000,
76+
getMore: cursorState.cursorId,
77+
maxTimeMS: cursorState.cmd.maxAwaitTimeMS
78+
});
79+
});
80+
81+
describe('callback tests', function() {
82+
let queryCallback;
83+
84+
beforeEach(() => {
85+
const cursorState = {
86+
cmd: {
87+
tailable: true,
88+
maxAwaitTimeMS: 1001
89+
},
90+
cursorId: 8675309
91+
};
92+
93+
const BATCH_SIZE = -23000;
94+
95+
executeGetMore(
96+
test.bson,
97+
namespace,
98+
cursorState,
99+
BATCH_SIZE,
100+
undefined,
101+
connection,
102+
test.callback
103+
);
104+
105+
queryCallback = connection.write.lastCall.args[2];
106+
});
107+
108+
it('should pass along any errors to the callback', function() {
109+
const err = new Error('test error 1');
110+
queryCallback(err);
111+
112+
expect(test.callback).to.have.been.calledOnce.and.calledWithExactly(err);
113+
});
114+
115+
it('should throw MongoNetworkError if responseFlag is nonzero', function() {
116+
const err = null;
117+
const response = {
118+
message: {
119+
documents: undefined,
120+
responseFlags: 1
121+
}
122+
};
123+
124+
queryCallback(err, response);
125+
126+
expect(test.callback).to.have.been.calledOnce;
127+
128+
const returnedError = test.callback.lastCall.args[0];
129+
130+
expect(returnedError)
131+
.to.be.an.instanceOf(test.MongoNetworkError)
132+
.and.to.have.property('message')
133+
.that.equals('cursor killed or timed out');
134+
});
135+
136+
it('should return a MongoError if ok is 0', function() {
137+
const err = null;
138+
const badMsg = { ok: 0 };
139+
const response = {
140+
message: {
141+
documents: [badMsg]
142+
}
143+
};
144+
145+
queryCallback(err, response);
146+
147+
expect(test.callback).to.have.been.calledOnce;
148+
149+
const returnedError = test.callback.lastCall.args[0];
150+
151+
expect(returnedError)
152+
.to.be.an.instanceOf(test.MongoError)
153+
.and.to.have.property('message')
154+
.that.equals('n/a');
155+
});
156+
157+
it('should return a valid document and connection to the callback', function() {
158+
const validDoc = { ok: 1, cursor: { id: 8675309 } };
159+
const err = null;
160+
const response = {
161+
message: {
162+
documents: [validDoc],
163+
connection: {}
164+
}
165+
};
166+
167+
queryCallback(err, response);
168+
169+
expect(test.callback).to.have.been.calledOnce.and.to.have.been.calledWithExactly(
170+
null,
171+
response.message.documents[0],
172+
response.message.connection
173+
);
174+
});
175+
});
176+
});

0 commit comments

Comments
 (0)