@@ -35,179 +35,58 @@ describe('explicit resource management feature integration tests', function () {
35
35
} )
36
36
describe ( 'MongoClient' , function ( ) {
37
37
it ( 'does not crash or error when used with await-using syntax' , async function ( ) {
38
- await using client = new MongoClient ( process . env . MONGODB_URI ! ) ;
39
- await client . connect ( ) ;
40
- } )
41
-
42
- it ( 'always cleans up the client, regardless of thrown errors' , async function ( ) {
43
- const error = await ( async ( ) => {
38
+ {
44
39
await using client = new MongoClient ( process . env . MONGODB_URI ! ) ;
45
40
await client . connect ( ) ;
46
-
47
- throw new Error ( 'error thrown' ) ;
48
- } ) ( ) . catch ( e => e ) ;
49
-
50
- expect ( error ) . to . match ( / e r r o r t h r o w n / ) ;
41
+ }
51
42
expect ( clientDisposeSpy . called ) . to . be . true ;
52
- } ) ;
53
-
54
- it ( 'works if client is explicitly closed' , async function ( ) {
55
- const expected = await ( async ( ) => {
56
- await using client = new MongoClient ( process . env . MONGODB_URI ! ) ;
57
- await client . connect ( ) ;
58
- await client . close ( ) ;
59
-
60
- return 'not error' ;
61
- } ) ( ) ;
62
-
63
- expect ( expected ) . to . equal ( 'not error' ) ;
64
43
} )
65
44
} )
66
45
67
46
describe ( 'Cursors' , function ( ) {
68
47
it ( 'does not crash or error when used with await-using syntax' , async function ( ) {
69
- await using client = new MongoClient ( process . env . MONGODB_URI ! ) ;
70
- await client . connect ( ) ;
71
-
72
- const collection = await setUpCollection ( client ) ;
73
-
74
- await using cursor = collection . find ( ) ;
75
- await cursor . next ( ) ;
76
- } )
77
-
78
- it ( 'always cleans up the cursor, regardless of thrown errors' , async function ( ) {
79
- const error = await ( async ( ) => {
48
+ {
80
49
await using client = new MongoClient ( process . env . MONGODB_URI ! ) ;
81
50
await client . connect ( ) ;
82
51
83
52
const collection = await setUpCollection ( client ) ;
84
53
85
54
await using cursor = collection . find ( ) ;
86
55
await cursor . next ( ) ;
87
-
88
- throw new Error ( 'error thrown' ) ;
89
- } ) ( ) . catch ( e => e ) ;
90
-
91
- expect ( error ) . to . match ( / e r r o r t h r o w n / ) ;
56
+ }
92
57
expect ( cursorDisposeSpy . called ) . to . be . true ;
93
- } ) ;
94
-
95
- it ( 'works if cursor is explicitly closed' , async function ( ) {
96
- const expected = await ( async ( ) => {
97
- await using client = new MongoClient ( process . env . MONGODB_URI ! ) ;
98
- await client . connect ( ) ;
99
-
100
- const collection = await setUpCollection ( client ) ;
101
-
102
- await using cursor = collection . find ( ) ;
103
- await cursor . next ( ) ;
104
-
105
- await cursor . close ( ) ;
106
-
107
- return 'not error' ;
108
- } ) ( ) ;
109
-
110
- expect ( expected ) . to . equal ( 'not error' ) ;
111
58
} )
112
59
113
60
describe ( 'cursor streams' , function ( ) {
114
61
it ( 'does not crash or error when used with await-using syntax' , async function ( ) {
115
- await using client = new MongoClient ( process . env . MONGODB_URI ! ) ;
116
- await client . connect ( ) ;
117
-
118
- const collection = await setUpCollection ( client ) ;
119
-
120
- await using readable = collection . find ( ) . stream ( ) ;
121
- } )
122
-
123
- it ( 'always cleans up the stream, regardless of thrown errors' , async function ( ) {
124
- const error = await ( async ( ) => {
62
+ {
125
63
await using client = new MongoClient ( process . env . MONGODB_URI ! ) ;
126
64
await client . connect ( ) ;
127
65
128
66
const collection = await setUpCollection ( client ) ;
129
67
130
68
await using readable = collection . find ( ) . stream ( ) ;
131
-
132
- throw new Error ( 'error thrown' ) ;
133
- } ) ( ) . catch ( e => e ) ;
134
-
135
- expect ( error ) . to . match ( / e r r o r t h r o w n / ) ;
69
+ }
136
70
expect ( readableDisposeSpy . called ) . to . be . true ;
137
- } ) ;
138
-
139
- it ( 'works if stream is explicitly closed' , async function ( ) {
140
- const expected = await ( async ( ) => {
141
- await using client = new MongoClient ( process . env . MONGODB_URI ! ) ;
142
- await client . connect ( ) ;
143
-
144
- const collection = await setUpCollection ( client ) ;
145
-
146
- await using readable = collection . find ( ) . stream ( ) ;
147
-
148
- readable . destroy ( ) ;
149
-
150
- return 'not error' ;
151
- } ) ( ) ;
152
-
153
- expect ( expected ) . to . equal ( 'not error' ) ;
154
71
} )
155
-
156
72
} )
157
73
} )
158
74
159
75
describe ( 'Sessions' , function ( ) {
160
76
it ( 'does not crash or error when used with await-using syntax' , async function ( ) {
161
- await using client = new MongoClient ( process . env . MONGODB_URI ! ) ;
162
- await client . connect ( ) ;
163
-
164
- await using session = client . startSession ( ) ;
165
- } )
166
-
167
- it ( 'always cleans up the session, regardless of thrown errors' , async function ( ) {
168
- const error = await ( async ( ) => {
77
+ {
169
78
await using client = new MongoClient ( process . env . MONGODB_URI ! ) ;
170
79
await client . connect ( ) ;
171
80
172
81
await using session = client . startSession ( ) ;
173
-
174
- throw new Error ( 'error thrown' ) ;
175
- } ) ( ) . catch ( e => e ) ;
176
-
177
- expect ( error ) . to . match ( / e r r o r t h r o w n / ) ;
82
+ }
178
83
expect ( sessionDisposeSpy . called ) . to . be . true ;
179
- } ) ;
180
-
181
- it ( 'works if session is explicitly closed' , async function ( ) {
182
- const expected = await ( async ( ) => {
183
- await using client = new MongoClient ( process . env . MONGODB_URI ! ) ;
184
- await client . connect ( ) ;
185
-
186
- await using session = client . startSession ( ) ;
187
-
188
- await session . endSession ( ) ;
189
-
190
- return 'not error' ;
191
- } ) ( ) ;
192
-
193
- expect ( expected ) . to . equal ( 'not error' ) ;
194
84
} )
195
85
} )
196
86
197
87
describe ( 'ChangeStreams' , function ( ) {
198
88
it ( 'does not crash or error when used with await-using syntax' , async function ( ) {
199
- await using client = new MongoClient ( process . env . MONGODB_URI ! ) ;
200
- await client . connect ( ) ;
201
-
202
- const collection = await setUpCollection ( client ) ;
203
- await using cs = collection . watch ( ) ;
204
-
205
- setTimeout ( 1000 ) . then ( ( ) => collection . insertOne ( { name : 'bailey' } ) ) ;
206
- await cs . next ( ) ;
207
- } )
208
-
209
- it ( 'always cleans up the change stream, regardless of thrown errors' , async function ( ) {
210
- const error = await ( async ( ) => {
89
+ {
211
90
await using client = new MongoClient ( process . env . MONGODB_URI ! ) ;
212
91
await client . connect ( ) ;
213
92
@@ -216,47 +95,14 @@ describe('explicit resource management feature integration tests', function () {
216
95
217
96
setTimeout ( 1000 ) . then ( ( ) => collection . insertOne ( { name : 'bailey' } ) ) ;
218
97
await cs . next ( ) ;
219
-
220
- throw new Error ( 'error thrown' ) ;
221
- } ) ( ) . catch ( e => e ) ;
222
-
223
- expect ( error ) . to . match ( / e r r o r t h r o w n / ) ;
98
+ }
224
99
expect ( changeStreamDisposeSpy . called ) . to . be . true ;
225
- } ) ;
226
-
227
- it ( 'works if change stream is explicitly closed' , async function ( ) {
228
- const expected = await ( async ( ) => {
229
- await using client = new MongoClient ( process . env . MONGODB_URI ! ) ;
230
- await client . connect ( ) ;
231
-
232
- const collection = await setUpCollection ( client ) ;
233
- await using cs = collection . watch ( ) ;
234
-
235
- setTimeout ( 1000 ) . then ( ( ) => collection . insertOne ( { name : 'bailey' } ) ) ;
236
- await cs . next ( ) ;
237
- await cs . close ( ) ;
238
-
239
- return 'not error' ;
240
- } ) ( ) ;
241
-
242
- expect ( expected ) . to . equal ( 'not error' ) ;
243
100
} )
244
101
} ) ;
245
102
246
103
describe ( 'GridFSDownloadStream' , function ( ) {
247
104
it ( 'does not crash or error when used with await-using syntax' , async function ( ) {
248
- await using client = new MongoClient ( process . env . MONGODB_URI ! ) ;
249
- await client . connect ( ) ;
250
-
251
- const bucket = new GridFSBucket ( client . db ( 'foo' ) ) ;
252
- const uploadStream = bucket . openUploadStream ( 'foo.txt' )
253
- await pipeline ( Readable . from ( "AAAAAAA" . split ( '' ) ) , uploadStream ) ;
254
-
255
- await using downloadStream = bucket . openDownloadStreamByName ( 'foo.txt' ) ;
256
- } )
257
-
258
- it ( 'always cleans up the stream, regardless of thrown errors' , async function ( ) {
259
- const error = await ( async ( ) => {
105
+ {
260
106
await using client = new MongoClient ( process . env . MONGODB_URI ! ) ;
261
107
await client . connect ( ) ;
262
108
@@ -265,66 +111,9 @@ describe('explicit resource management feature integration tests', function () {
265
111
await pipeline ( Readable . from ( "AAAAAAA" . split ( '' ) ) , uploadStream ) ;
266
112
267
113
await using downloadStream = bucket . openDownloadStreamByName ( 'foo.txt' ) ;
114
+ }
268
115
269
- throw new Error ( 'error thrown' ) ;
270
- } ) ( ) . catch ( e => e ) ;
271
-
272
- expect ( error ) . to . match ( / e r r o r t h r o w n / ) ;
273
116
expect ( readableDisposeSpy . called ) . to . be . true ;
274
- } ) ;
275
-
276
- it ( 'works if stream is explicitly closed' , async function ( ) {
277
- const expected = await ( async ( ) => {
278
- await using client = new MongoClient ( process . env . MONGODB_URI ! ) ;
279
- await client . connect ( ) ;
280
-
281
- const bucket = new GridFSBucket ( client . db ( 'foo' ) ) ;
282
- const uploadStream = bucket . openUploadStream ( 'foo.txt' )
283
- await pipeline ( Readable . from ( "AAAAAAA" . split ( '' ) ) , uploadStream ) ;
284
-
285
- await using downloadStream = bucket . openDownloadStreamByName ( 'foo.txt' ) ;
286
-
287
- await downloadStream . abort ( ) ;
288
-
289
- return 'not error' ;
290
- } ) ( ) ;
291
-
292
- expect ( expected ) . to . equal ( 'not error' ) ;
293
- } )
294
-
295
- it ( 'throws premature close error if explicitly destroyed early' , async function ( ) {
296
- // Gridfs streams inherit their _destroy() and Symbol.asyncDispose implementations from
297
- // Nodejs' readable implementation. This behavior matches the behavior for other readable streams
298
- // (see the below test).
299
- const expected = await ( async ( ) => {
300
- await using client = new MongoClient ( process . env . MONGODB_URI ! ) ;
301
- await client . connect ( ) ;
302
-
303
- const bucket = new GridFSBucket ( client . db ( 'foo' ) ) ;
304
- const uploadStream = bucket . openUploadStream ( 'foo.txt' )
305
- await pipeline ( Readable . from ( "AAAAAAA" . split ( '' ) ) , uploadStream ) ;
306
-
307
- await using downloadStream = bucket . openDownloadStreamByName ( 'foo.txt' ) ;
308
-
309
- downloadStream . destroy ( ) ;
310
-
311
- return 'not error' ;
312
- } ) ( ) . catch ( e => e ) ;
313
-
314
- expect ( expected ) . to . match ( / P r e m a t u r e c l o s e / ) ;
315
- } )
316
-
317
- it ( 'throws premature close error if explicitly destroyed early (builtin stream)' , async function ( ) {
318
- // Gridfs streams inherit their _destroy() and Symbol.asyncDispose implementations from
319
- // Nodejs' readable implementation. This behavior matches the behavior for other readable streams (ie - ReadFileStream)
320
- const expected = await ( async ( ) => {
321
- await using readStream = createReadStream ( join ( __dirname , 'main.test.ts' ) ) ;
322
- readStream . destroy ( ) ;
323
-
324
- return 'not error' ;
325
- } ) ( ) . catch ( e => e ) ;
326
-
327
- expect ( expected ) . to . match ( / P r e m a t u r e c l o s e / ) ;
328
117
} )
329
118
} ) ;
330
119
} )
0 commit comments