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

Commit 406a000

Browse files
daprahamianmbroadst
authored andcommitted
test(OP_MSG): adding killCursor tests
1 parent 33a5407 commit 406a000

File tree

1 file changed

+146
-0
lines changed

1 file changed

+146
-0
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
'use strict';
2+
3+
const expect = require('chai').expect;
4+
const makeSinonSandbox = require('./utils').makeSinonSandbox;
5+
const errors = require('../../../../../lib/error');
6+
const MongoError = errors.MongoError;
7+
const MongoNetworkError = errors.MongoNetworkError;
8+
const executeKillCursor = require('../../../../../lib/wireprotocol/3_6_support/execute_kill_cursor');
9+
const Pool = require('../../../../../lib/connection/pool');
10+
const BSON = require('bson');
11+
12+
describe('Wire Protocol 3.6 Kill Cursor', function() {
13+
const sinon = makeSinonSandbox();
14+
let bson, pool, callback;
15+
16+
const $db = 'darmok';
17+
const collection = 'jalad';
18+
const namespace = `${$db}.${collection}`;
19+
20+
const cursorState = {
21+
cursorId: 8675309
22+
};
23+
24+
beforeEach(() => {
25+
bson = sinon.createStubInstance(BSON);
26+
pool = sinon.createStubInstance(Pool);
27+
pool.isConnected.returns(true);
28+
callback = sinon.stub();
29+
});
30+
31+
it('should not call to pool if pool is not there, or pool is not connected', function() {
32+
executeKillCursor(bson, namespace, cursorState, undefined, callback);
33+
expect(pool.write).to.not.have.been.called;
34+
expect(callback).to.have.been.calledOnce.and.calledWithExactly(null, null);
35+
callback.reset();
36+
37+
pool.isConnected.returns(false);
38+
39+
executeKillCursor(bson, namespace, cursorState, pool, callback);
40+
expect(pool.write).to.not.have.been.called;
41+
expect(callback).to.have.been.calledOnce.and.calledWithExactly(null, null);
42+
});
43+
44+
it('should catch any errors throw by pool.write, and pass them along to callback', function() {
45+
const err = new Error('this is a test error');
46+
pool.write.throws(err);
47+
48+
expect(() => executeKillCursor(bson, namespace, cursorState, pool, callback)).to.not.throw();
49+
50+
expect(callback).to.have.been.calledOnce.and.calledWithExactly(err, undefined);
51+
});
52+
53+
it('should properly format command for killing cursor', function() {
54+
executeKillCursor(bson, namespace, cursorState, pool, callback);
55+
56+
expect(pool.write).to.have.been.calledOnce.and.calledWithExactly(
57+
sinon.match({ query: sinon.match.any }),
58+
sinon.match.object,
59+
sinon.match.func
60+
);
61+
62+
const msg = pool.write.lastCall.args[0];
63+
64+
expect(msg)
65+
.to.have.property('query')
66+
.with.lengthOf(1)
67+
.that.has.property(0)
68+
.that.deep.includes({
69+
$db,
70+
cursors: [cursorState.cursorId],
71+
killCursors: collection
72+
});
73+
});
74+
75+
describe('killCursorCallback', function() {
76+
let killCursorCallback;
77+
beforeEach(() => {
78+
executeKillCursor(bson, namespace, cursorState, pool, callback);
79+
killCursorCallback = pool.write.lastCall.args[2];
80+
});
81+
82+
it('should take any errors from pool callback and pass them along', function() {
83+
const err = new Error('this is a test error');
84+
killCursorCallback(err);
85+
86+
expect(callback).to.have.been.calledOnce.and.to.have.been.calledWithExactly(err, undefined);
87+
});
88+
89+
it('should throw MongoNetworkError if responseFlag is nonzero', function() {
90+
const err = null;
91+
const response = {
92+
message: {
93+
documents: undefined,
94+
responseFlags: 1
95+
}
96+
};
97+
98+
killCursorCallback(err, response);
99+
100+
expect(callback).to.have.been.calledOnce;
101+
102+
const returnedError = callback.lastCall.args[0];
103+
104+
expect(returnedError)
105+
.to.be.an.instanceOf(MongoNetworkError)
106+
.and.to.have.property('message')
107+
.that.equals('cursor killed or timed out');
108+
});
109+
110+
it('should throw MongoError if an invalid response comes back', function() {
111+
const err = null;
112+
const response = {
113+
message: {
114+
documents: [],
115+
responseFlags: 0
116+
}
117+
};
118+
119+
killCursorCallback(err, response);
120+
121+
expect(callback).to.have.been.calledOnce;
122+
123+
const returnedError = callback.lastCall.args[0];
124+
125+
expect(returnedError)
126+
.to.be.an.instanceOf(MongoError)
127+
.and.to.have.property('message')
128+
.that.equals(`invalid killCursors result returned for cursor id ${cursorState.cursorId}`);
129+
});
130+
131+
it('should return first returned document if everything is fine', function() {
132+
const doc = {};
133+
const err = null;
134+
const response = {
135+
message: {
136+
documents: [doc],
137+
responseFlags: 0
138+
}
139+
};
140+
141+
killCursorCallback(err, response);
142+
143+
expect(callback).to.have.been.calledOnce.and.to.have.been.calledWithExactly(null, doc);
144+
});
145+
});
146+
});

0 commit comments

Comments
 (0)