deno.land / x / mongoose@6.7.5 / test / helpers / cursor.eachAsync.test.js

cursor.eachAsync.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
'use strict';
const assert = require('assert');const eachAsync = require('../../lib/helpers/cursor/eachAsync');
describe('eachAsync()', function() { it('exhausts large cursor without parallel calls (gh-8235)', function() { this.timeout(10000);
let numInProgress = 0; let num = 0; const max = 1000; let processed = 0;
function next(cb) { assert.equal(numInProgress, 0); ++numInProgress; setTimeout(function() { --numInProgress; if (num++ >= max) { return cb(null, null); } cb(null, { name: `doc${num}` }); }, 0); }
return eachAsync(next, () => Promise.resolve(++processed), { parallel: 8 }). then(() => assert.equal(processed, max)); });
it('waits until the end before resolving the promise (gh-8352)', function() { const max = 2; let numCalled = 0; let numDone = 0; function next(cb) { setImmediate(() => { if (++numCalled > max) { return cb(null, null); } cb(null, { num: numCalled }); }); }
function fn() { return new Promise(resolve => { setTimeout(() => { ++numDone; resolve(); }, 100); }); }
return eachAsync(next, fn, { parallel: 3 }). then(() => assert.equal(numDone, max)). then(() => { numCalled = 0; numDone = 0; }). then(() => eachAsync(next, fn, { parallel: 2 })). then(() => assert.equal(numDone, max)); });
it('it processes the documents in batches successfully', () => { const batchSize = 3; let numberOfDocuments = 0; const maxNumberOfDocuments = 9; let numberOfBatchesProcessed = 0;
function next(cb) { setTimeout(() => { if (++numberOfDocuments > maxNumberOfDocuments) { cb(null, null); } return cb(null, { id: numberOfDocuments }); }, 0); }
const fn = (docs, index) => { assert.equal(docs.length, batchSize); assert.equal(index, numberOfBatchesProcessed++); };
return eachAsync(next, fn, { batchSize }); });
it('it processes the documents in batches even if the batch size % document count is not zero successfully', () => { const batchSize = 3; let numberOfDocuments = 0; const maxNumberOfDocuments = 10; let numberOfBatchesProcessed = 0;
function next(cb) { setTimeout(() => { if (++numberOfDocuments > maxNumberOfDocuments) { cb(null, null); } return cb(null, { id: numberOfDocuments }); }, 0); }
const fn = (docs, index) => { assert.equal(index, numberOfBatchesProcessed++); if (index == 3) { assert.equal(docs.length, 1); } else { assert.equal(docs.length, batchSize); } };
return eachAsync(next, fn, { batchSize }); });
it('it processes the documents in batches with the parallel option provided', () => { const batchSize = 3; const parallel = 3; let numberOfDocuments = 0; const maxNumberOfDocuments = 9; let numberOfBatchesProcessed = 0;
function next(cb) { setTimeout(() => { if (++numberOfDocuments > maxNumberOfDocuments) { cb(null, null); } return cb(null, { id: numberOfDocuments }); }, 0); }
const fn = (docs, index) => { assert.equal(index, numberOfBatchesProcessed++); assert.equal(docs.length, batchSize); };
return eachAsync(next, fn, { batchSize, parallel }); });
it('executes all documents and aggregates errors if continueOnError set (gh-6355)', async() => { const max = 3; let numCalled = 0; function next(cb) { setImmediate(() => { if (++numCalled > max) { return cb(null, null); } cb(null, { num: numCalled }); }); }
function fn(doc) { return doc.num % 2 === 1 ? Promise.reject(new Error(`Doc number ${doc.num}`)) : null; }
const err = await eachAsync(next, fn, { continueOnError: true }).then(() => null, err => err); assert.ok(err); assert.equal(err.name, 'EachAsyncMultiError'); assert.ok(err.message.includes('Doc number 1')); assert.ok(err.message.includes('Doc number 3')); assert.equal(err.errors.length, 2); assert.equal(err.errors[0].message, 'Doc number 1'); assert.equal(err.errors[1].message, 'Doc number 3'); });
it('returns aggregated error fetching documents with continueOnError (gh-6355)', async() => { const max = 3; let numCalled = 0; function next(cb) { setImmediate(() => { if (++numCalled > max) { return cb(null, null); } if (numCalled % 2 === 1) { return cb(new Error(`Fetching doc ${numCalled}`)); } cb(null, { num: numCalled }); }); }
function fn() { return null; }
const err = await eachAsync(next, fn, { continueOnError: true }).then(() => null, err => err); assert.ok(err); assert.equal(err.name, 'EachAsyncMultiError', err); assert.ok(err.message.includes('Fetching doc 1')); assert.equal(err.errors.length, 1); assert.equal(err.errors[0].message, 'Fetching doc 1'); assert.equal(numCalled, 1); });
it('avoids mutating document batch with parallel (gh-12652)', async() => { const max = 100; let numCalled = 0; function next(cb) { setImmediate(() => { if (++numCalled > max) { return cb(null, null); } cb(null, { num: numCalled }); }); }
let numDocsProcessed = 0; async function fn(batch) { numDocsProcessed += batch.length; const length = batch.length; await new Promise(resolve => setTimeout(resolve, 50)); assert.equal(batch.length, length); }
await eachAsync(next, fn, { parallel: 7, batchSize: 10 }); assert.equal(numDocsProcessed, max); });
it('using AbortSignal (gh-12173)', async function() { if (typeof AbortController === 'undefined') { return this.skip(); }
const ac = new AbortController(); const signal = ac.signal;
let numCalled = 0; const abortAfter = 2; let numNextCalls = 0; let numFnCalls = 0; function next(cb) { ++numNextCalls; setImmediate(() => { if (numCalled++ === abortAfter) { ac.abort();
return setImmediate(() => { cb(null, { num: numCalled }); }); }
cb(null, { num: numCalled }); }); }
await eachAsync(next, () => ++numFnCalls, { signal });
assert.equal(numNextCalls, 3); assert.equal(numCalled, 3); assert.equal(numFnCalls, 2); });});
mongoose

Version Info

Tagged at
a year ago