-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathcursor_async_iterator.test.js
131 lines (102 loc) · 3.73 KB
/
cursor_async_iterator.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
'use strict';
const { expect } = require('chai');
const sinon = require('sinon');
describe('Cursor Async Iterator Tests', function () {
context('default promise library', function () {
let client, collection;
before(async function () {
client = this.configuration.newClient();
await client.connect();
const docs = Array.from({ length: 1000 }).map((_, index) => ({ foo: index, bar: 1 }));
collection = client.db(this.configuration.db).collection('async_cursor_tests');
await collection.deleteMany({});
await collection.insertMany(docs);
await client.close();
});
beforeEach(async function () {
client = this.configuration.newClient();
await client.connect();
collection = client.db(this.configuration.db).collection('async_cursor_tests');
});
afterEach(() => client.close());
it('should be able to use a for-await loop on a find command cursor', async function () {
const cursor = collection.find({ bar: 1 });
let counter = 0;
for await (const doc of cursor) {
expect(doc).to.have.property('bar', 1);
counter += 1;
}
expect(counter).to.equal(1000);
expect(cursor.closed).to.be.true;
});
it('should be able to use a for-await loop on an aggregation cursor', async function () {
const cursor = collection.aggregate([{ $match: { bar: 1 } }]);
let counter = 0;
for await (const doc of cursor) {
expect(doc).to.have.property('bar', 1);
counter += 1;
}
expect(counter).to.equal(1000);
expect(cursor.closed).to.be.true;
});
it('should be able to use a for-await loop on a command cursor', {
metadata: { requires: { mongodb: '>=3.0.0' } },
test: async function () {
const cursor1 = collection.listIndexes();
const cursor2 = collection.listIndexes();
const indexes = await cursor1.toArray();
let counter = 0;
for await (const doc of cursor2) {
expect(doc).to.exist;
counter += 1;
}
expect(counter).to.equal(indexes.length);
expect(cursor1.closed).to.be.true;
expect(cursor2.closed).to.be.true;
}
});
it('should not iterate if closed immediately', async function () {
const cursor = collection.find();
await cursor.close();
let count = 0;
// eslint-disable-next-line no-unused-vars
for await (const _ of cursor) count++;
expect(count).to.equal(0);
expect(cursor.closed).to.be.true;
});
it('should properly stop when cursor is closed', async function () {
const cursor = collection.find();
let count = 0;
for await (const doc of cursor) {
expect(doc).to.exist;
count++;
await cursor.close();
}
expect(count).to.equal(1);
expect(cursor.killed).to.be.true;
});
it('cleans up cursor when breaking out of for await of loops', async function () {
const cursor = collection.find();
for await (const doc of cursor) {
expect(doc).to.exist;
break;
}
// The expectation is that we have "cleaned" up the cursor on the server side
expect(cursor.killed).to.be.true;
});
it('returns when attempting to reuse the cursor after a break', async function () {
const cursor = collection.find();
const spy = sinon.spy(cursor);
for await (const doc of cursor) {
expect(doc).to.exist;
break;
}
expect(cursor.killed).to.be.true;
for await (const doc of cursor) {
expect.fail('Async generator returns immediately if cursor is closed', doc);
}
// cursor.close() should only be called once.
expect(spy.close.calledOnce).to.be.true;
});
});
});