1
1
2
2
import { describe , it } from 'mocha' ;
3
- import { GridFSBucket , MongoClient } from 'mongodb/lib/beta' ;
3
+ import { AbstractCursor , ChangeStream , ClientSession , GridFSBucket , MongoClient } from 'mongodb/lib/beta' ;
4
4
import { Readable } from 'stream' ;
5
5
import { pipeline } from 'stream/promises' ;
6
6
import { expect } from 'chai' ;
7
7
import { setTimeout } from 'timers/promises' ;
8
8
import { createReadStream } from 'fs' ;
9
9
import { join } from 'path' ;
10
10
11
+ import * as sinon from 'sinon' ;
12
+
11
13
async function setUpCollection ( client : MongoClient ) {
12
14
const collection = client . db ( 'foo' ) . collection < { name : string } > ( 'bar' ) ;
13
15
const documents : Array < { name : string } > = Array . from ( { length : 5 } ) . map ( i => ( {
@@ -18,6 +20,19 @@ async function setUpCollection(client: MongoClient) {
18
20
}
19
21
20
22
describe ( 'explicit resource management feature integration tests' , function ( ) {
23
+ const clientDisposeSpy = sinon . spy ( MongoClient . prototype , Symbol . asyncDispose ) ;
24
+ const sessionDisposeSpy = sinon . spy ( ClientSession . prototype , Symbol . asyncDispose ) ;
25
+ const changeStreamDisposeSpy = sinon . spy ( ChangeStream . prototype , Symbol . asyncDispose ) ;
26
+ const cursorDisposeSpy = sinon . spy ( AbstractCursor . prototype , Symbol . asyncDispose ) ;
27
+ const readableDisposeSpy = sinon . spy ( Readable . prototype , Symbol . asyncDispose ) ;
28
+
29
+ afterEach ( function ( ) {
30
+ clientDisposeSpy . resetHistory ( ) ;
31
+ sessionDisposeSpy . resetHistory ( ) ;
32
+ changeStreamDisposeSpy . resetHistory ( ) ;
33
+ cursorDisposeSpy . resetHistory ( ) ;
34
+ readableDisposeSpy . resetHistory ( ) ;
35
+ } )
21
36
describe ( 'MongoClient' , function ( ) {
22
37
it ( 'does not crash or error when used with await-using syntax' , async function ( ) {
23
38
await using client = new MongoClient ( process . env . MONGODB_URI ! ) ;
@@ -33,6 +48,7 @@ describe('explicit resource management feature integration tests', function () {
33
48
} ) ( ) . catch ( e => e ) ;
34
49
35
50
expect ( error ) . to . match ( / e r r o r t h r o w n / ) ;
51
+ expect ( clientDisposeSpy . called ) . to . be . true ;
36
52
} ) ;
37
53
38
54
it ( 'works if client is explicitly closed' , async function ( ) {
@@ -73,6 +89,7 @@ describe('explicit resource management feature integration tests', function () {
73
89
} ) ( ) . catch ( e => e ) ;
74
90
75
91
expect ( error ) . to . match ( / e r r o r t h r o w n / ) ;
92
+ expect ( cursorDisposeSpy . called ) . to . be . true ;
76
93
} ) ;
77
94
78
95
it ( 'works if cursor is explicitly closed' , async function ( ) {
@@ -116,6 +133,7 @@ describe('explicit resource management feature integration tests', function () {
116
133
} ) ( ) . catch ( e => e ) ;
117
134
118
135
expect ( error ) . to . match ( / e r r o r t h r o w n / ) ;
136
+ expect ( readableDisposeSpy . called ) . to . be . true ;
119
137
} ) ;
120
138
121
139
it ( 'works if stream is explicitly closed' , async function ( ) {
@@ -157,6 +175,7 @@ describe('explicit resource management feature integration tests', function () {
157
175
} ) ( ) . catch ( e => e ) ;
158
176
159
177
expect ( error ) . to . match ( / e r r o r t h r o w n / ) ;
178
+ expect ( sessionDisposeSpy . called ) . to . be . true ;
160
179
} ) ;
161
180
162
181
it ( 'works if session is explicitly closed' , async function ( ) {
@@ -202,6 +221,7 @@ describe('explicit resource management feature integration tests', function () {
202
221
} ) ( ) . catch ( e => e ) ;
203
222
204
223
expect ( error ) . to . match ( / e r r o r t h r o w n / ) ;
224
+ expect ( changeStreamDisposeSpy . called ) . to . be . true ;
205
225
} ) ;
206
226
207
227
it ( 'works if change stream is explicitly closed' , async function ( ) {
@@ -250,6 +270,7 @@ describe('explicit resource management feature integration tests', function () {
250
270
} ) ( ) . catch ( e => e ) ;
251
271
252
272
expect ( error ) . to . match ( / e r r o r t h r o w n / ) ;
273
+ expect ( readableDisposeSpy . called ) . to . be . true ;
253
274
} ) ;
254
275
255
276
it ( 'works if stream is explicitly closed' , async function ( ) {
0 commit comments