|
9 | 9 | type FindCursor,
|
10 | 10 | MongoAPIError,
|
11 | 11 | type MongoClient,
|
| 12 | + MongoCursorExhaustedError, |
12 | 13 | MongoServerError
|
13 | 14 | } from '../../mongodb';
|
14 | 15 |
|
@@ -193,7 +194,9 @@ describe('class AbstractCursor', function () {
|
193 | 194 | const error = await cursor.toArray().catch(e => e);
|
194 | 195 |
|
195 | 196 | expect(error).be.instanceOf(MongoAPIError);
|
196 |
| - expect(cursor.closed).to.be.true; |
| 197 | + expect(cursor.id.isZero()).to.be.true; |
| 198 | + // The first batch exhausted the cursor, the only thing to clean up is the session |
| 199 | + expect(cursor.session.hasEnded).to.be.true; |
197 | 200 | });
|
198 | 201 | });
|
199 | 202 |
|
@@ -225,7 +228,9 @@ describe('class AbstractCursor', function () {
|
225 | 228 | }
|
226 | 229 | } catch (error) {
|
227 | 230 | expect(error).to.be.instanceOf(MongoAPIError);
|
228 |
| - expect(cursor.closed).to.be.true; |
| 231 | + expect(cursor.id.isZero()).to.be.true; |
| 232 | + // The first batch exhausted the cursor, the only thing to clean up is the session |
| 233 | + expect(cursor.session.hasEnded).to.be.true; |
229 | 234 | }
|
230 | 235 | });
|
231 | 236 | });
|
@@ -259,7 +264,9 @@ describe('class AbstractCursor', function () {
|
259 | 264 |
|
260 | 265 | const error = await cursor.forEach(iterator).catch(e => e);
|
261 | 266 | expect(error).to.be.instanceOf(MongoAPIError);
|
262 |
| - expect(cursor.closed).to.be.true; |
| 267 | + expect(cursor.id.isZero()).to.be.true; |
| 268 | + // The first batch exhausted the cursor, the only thing to clean up is the session |
| 269 | + expect(cursor.session.hasEnded).to.be.true; |
263 | 270 | });
|
264 | 271 | });
|
265 | 272 | });
|
@@ -299,4 +306,59 @@ describe('class AbstractCursor', function () {
|
299 | 306 | expect(error).to.be.instanceof(MongoServerError);
|
300 | 307 | });
|
301 | 308 | });
|
| 309 | + |
| 310 | + describe('cursor end state', function () { |
| 311 | + let client: MongoClient; |
| 312 | + let cursor: FindCursor; |
| 313 | + |
| 314 | + beforeEach(async function () { |
| 315 | + client = this.configuration.newClient(); |
| 316 | + const test = client.db().collection('test'); |
| 317 | + await test.deleteMany({}); |
| 318 | + await test.insertMany([{ a: 1 }, { a: 2 }, { a: 3 }, { a: 4 }]); |
| 319 | + }); |
| 320 | + |
| 321 | + afterEach(async function () { |
| 322 | + await cursor.close(); |
| 323 | + await client.close(); |
| 324 | + }); |
| 325 | + |
| 326 | + describe('when the last batch has been received', () => { |
| 327 | + it('has a zero id and is not closed and is never killed', async function () { |
| 328 | + cursor = client.db().collection('test').find({}); |
| 329 | + expect(cursor).to.have.property('closed', false); |
| 330 | + await cursor.tryNext(); |
| 331 | + expect(cursor.id.isZero()).to.be.true; |
| 332 | + expect(cursor).to.have.property('closed', false); |
| 333 | + expect(cursor).to.have.property('killed', false); |
| 334 | + }); |
| 335 | + }); |
| 336 | + |
| 337 | + describe('when the last document has been iterated', () => { |
| 338 | + it('has a zero id and is closed and is never killed', async function () { |
| 339 | + cursor = client.db().collection('test').find({}); |
| 340 | + await cursor.next(); |
| 341 | + await cursor.next(); |
| 342 | + await cursor.next(); |
| 343 | + await cursor.next(); |
| 344 | + expect(await cursor.next()).to.be.null; |
| 345 | + expect(cursor.id.isZero()).to.be.true; |
| 346 | + expect(cursor).to.have.property('closed', true); |
| 347 | + expect(cursor).to.have.property('killed', false); |
| 348 | + }); |
| 349 | + }); |
| 350 | + |
| 351 | + describe('when some documents have been iterated and the cursor is closed', () => { |
| 352 | + it('has a zero id and is not closed and is killed', async function () { |
| 353 | + cursor = client.db().collection('test').find({}, { batchSize: 2 }); |
| 354 | + await cursor.next(); |
| 355 | + await cursor.close(); |
| 356 | + expect(cursor).to.have.property('closed', false); |
| 357 | + expect(cursor).to.have.property('killed', true); |
| 358 | + expect(cursor.id.isZero()).to.be.true; |
| 359 | + const error = await cursor.next().catch(error => error); |
| 360 | + expect(error).to.be.instanceOf(MongoCursorExhaustedError); |
| 361 | + }); |
| 362 | + }); |
| 363 | + }); |
302 | 364 | });
|
0 commit comments