forked from mongodb/node-mongodb-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.test.ts
More file actions
119 lines (100 loc) · 4.06 KB
/
main.test.ts
File metadata and controls
119 lines (100 loc) · 4.06 KB
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
import { expect } from 'chai';
import { describe, it } from 'mocha';
import { AbstractCursor, ChangeStream, ClientSession, GridFSBucket, MongoClient } from 'mongodb';
import * as process from 'process';
import * as sinon from 'sinon';
import { Readable } from 'stream';
import { pipeline } from 'stream/promises';
import { setTimeout } from 'timers/promises';
async function setUpCollection(client: MongoClient) {
const collection = client.db('foo').collection<{ name: string }>('bar');
const documents: Array<{ name: string }> = Array.from({ length: 5 }).map(i => ({
name: String(i)
}));
await collection.insertMany(documents);
return collection;
}
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const MONGODB_URI = process.env.MONGODB_URI!;
describe('explicit resource management feature integration tests', function () {
const clientDisposeSpy = sinon.spy(MongoClient.prototype, Symbol.asyncDispose);
const sessionDisposeSpy = sinon.spy(ClientSession.prototype, Symbol.asyncDispose);
const changeStreamDisposeSpy = sinon.spy(ChangeStream.prototype, Symbol.asyncDispose);
const cursorDisposeSpy = sinon.spy(AbstractCursor.prototype, Symbol.asyncDispose);
const readableDisposeSpy = sinon.spy(Readable.prototype, Symbol.asyncDispose);
afterEach(function () {
clientDisposeSpy.resetHistory();
sessionDisposeSpy.resetHistory();
changeStreamDisposeSpy.resetHistory();
cursorDisposeSpy.resetHistory();
readableDisposeSpy.resetHistory();
});
describe('MongoClient', function () {
it('does not crash or error when used with await-using syntax', async function () {
{
await using client = new MongoClient(MONGODB_URI);
await client.connect();
}
expect(clientDisposeSpy.called).to.be.true;
});
});
describe('Cursors', function () {
it('does not crash or error when used with await-using syntax', async function () {
{
await using client = new MongoClient(MONGODB_URI);
await client.connect();
const collection = await setUpCollection(client);
await using cursor = collection.find();
await cursor.next();
}
expect(cursorDisposeSpy.called).to.be.true;
});
describe('cursor streams', function () {
it('does not crash or error when used with await-using syntax', async function () {
{
await using client = new MongoClient(MONGODB_URI);
await client.connect();
const collection = await setUpCollection(client);
await using _readable = collection.find().stream();
}
expect(readableDisposeSpy.called).to.be.true;
});
});
});
describe('Sessions', function () {
it('does not crash or error when used with await-using syntax', async function () {
{
await using client = new MongoClient(MONGODB_URI);
await client.connect();
await using _session = client.startSession();
}
expect(sessionDisposeSpy.called).to.be.true;
});
});
describe('ChangeStreams', function () {
it('does not crash or error when used with await-using syntax', async function () {
{
await using client = new MongoClient(MONGODB_URI);
await client.connect();
const collection = await setUpCollection(client);
await using cs = collection.watch();
setTimeout(1000).then(() => collection.insertOne({ name: 'bailey' }));
await cs.next();
}
expect(changeStreamDisposeSpy.called).to.be.true;
});
});
describe('GridFSDownloadStream', function () {
it('does not crash or error when used with await-using syntax', async function () {
{
await using client = new MongoClient(MONGODB_URI);
await client.connect();
const bucket = new GridFSBucket(client.db('foo'));
const uploadStream = bucket.openUploadStream('foo.txt');
await pipeline(Readable.from('AAAAAAA'.split('')), uploadStream);
await using _downloadStream = bucket.openDownloadStreamByName('foo.txt');
}
expect(readableDisposeSpy.called).to.be.true;
});
});
});