-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
find_cursor.test.js
182 lines (167 loc) · 5.61 KB
/
find_cursor.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
'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 { FindCursor } = require('../../../src/cursor/find_cursor');
const test = {};
describe('Find 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.find) {
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 FindCursor(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.find) {
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: 1000
});
const cursor = new FindCursor(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 () {
// Set the count of times hello has been called.
let helloCalls = 0;
test.server.setMessageHandler(request => {
const doc = request.document;
if (isHello(doc)) {
// After the first hello call errors indicating no data bearing server is
// available, any subsequent hello call should succeed after server selection.
// This gives us a data bearing server available for the next call.
request.reply(helloCalls > 0 ? mock.HELLO : { errmsg: 'network error' });
helloCalls++;
} else if (doc.find) {
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: 1000
});
const cursor = new FindCursor(topology, MongoDBNamespace.fromString('test.test'), {}, {});
topology.connect(function () {
cursor.next(function () {
expect(cursor.session).to.exist;
topology.close(done);
});
});
});
});
});
describe('Response', function () {
afterEach(() => mock.cleanup());
beforeEach(() => {
return mock.createServer().then(mockServer => {
test.server = mockServer;
});
});
it('should throw when document is error', function (done) {
const errdoc = {
errmsg: 'Cursor not found (namespace: "liveearth.entityEvents", id: 2018648316188432590).'
};
const client = new Topology(test.server.hostAddress());
test.server.setMessageHandler(request => {
const doc = request.document;
if (isHello(doc)) {
request.reply(
Object.assign({}, mock.HELLO, {
maxWireVersion: 6
})
);
} else if (doc.find) {
request.reply({
cursor: {
id: Long.fromNumber(1),
ns: 'test.test',
firstBatch: []
},
ok: 1
});
} else if (doc.getMore) {
request.reply(errdoc);
} else if (doc.killCursors) {
request.reply({ ok: 1 });
}
});
client.on('error', done);
client.once('connect', () => {
const cursor = new FindCursor(client, MongoDBNamespace.fromString('test.test'), {}, {});
// Execute next
cursor.next(function (err) {
expect(err).to.exist;
expect(err).to.be.instanceof(MongoError);
expect(err.message).to.equal(errdoc.errmsg);
client.close(done);
});
});
client.connect();
});
});
});