-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
cursors.test.ts
95 lines (83 loc) · 3.05 KB
/
cursors.test.ts
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
import { expect } from 'chai';
import { inspect } from 'util';
import {
Collection,
CommandStartedEvent,
MongoClient,
MongoError,
MongoServerError
} from '../../../src';
import { runLater, sleep } from '../../tools/utils';
describe('MongoDB Cursors', () => {
describe('tailable cursors', () => {
const insertedDocs = [{ _id: 1 }];
let client: MongoClient;
let cappedCollection: Collection<{ _id: number }>;
beforeEach(async function () {
client = this.configuration.newClient({ monitorCommands: true });
await client
.db()
.dropCollection('cappedAt3')
.catch(() => null);
await sleep(500);
cappedCollection = await client
.db()
.createCollection('cappedAt3', { capped: true, size: 4096, max: 3 });
cappedCollection.insertMany(insertedDocs);
});
afterEach(async function () {
await client?.close();
});
const tailableValues = [true, false, undefined];
const awaitDataValues = [true, false, undefined];
const maxTimeMSValues = [800, 0, undefined];
const maxAwaitTimeMSValues = [800, 0, undefined];
const tests = tailableValues.flatMap(tailable =>
awaitDataValues.flatMap(awaitData =>
maxAwaitTimeMSValues.flatMap(maxAwaitTimeMS =>
maxTimeMSValues.flatMap(maxTimeMS => {
const awaitDataSet = Boolean(awaitData) === true;
const tailableSet = Boolean(tailable) === true;
const timeIsSet = typeof maxAwaitTimeMS === 'number' || typeof maxTimeMS === 'number';
return [
{
options: { tailable, awaitData, maxAwaitTimeMS, maxTimeMS },
outcome: {
isError:
// Cannot set 'awaitData' without also setting 'tailable'
(awaitDataSet && !tailableSet) ||
// cannot set maxTimeMS on getMore command for a non-awaitData cursor
(timeIsSet && awaitData === false && !tailableSet)
}
}
];
})
)
)
);
for (const { options, outcome } of tests) {
let optionsString = inspect(options, { breakLength: Infinity });
optionsString = optionsString
.slice(2, optionsString.length - 2)
.split('undefined')
.join('omit');
it(`should create find cursor with ${optionsString}`, async () => {
const events: CommandStartedEvent[] = [];
client.on('commandStarted', event => events.push(event));
const cursor = cappedCollection.find({ _id: { $gt: 0 } }, { ...options, batchSize: 1 });
const docOrError: { _id: number } | Error = await cursor.next().catch(error => error);
if (outcome.isError) {
expect(docOrError).to.be.instanceOf(MongoServerError);
} else {
if (docOrError instanceof Error) {
throw docOrError;
}
const doc = docOrError;
expect(doc).to.have.property('_id', 1);
await cursor.tryNext();
await cursor.close();
}
});
}
});
});