|
| 1 | +import { expect } from 'chai'; |
| 2 | +import * as sinon from 'sinon'; |
| 3 | + |
| 4 | +import { AbstractCursor, ChangeStream, ClientSession, load, MongoClient } from '../mongodb'; |
| 5 | + |
| 6 | +describe('Explicit Resource Management Tests', function () { |
| 7 | + let client: MongoClient; |
| 8 | + |
| 9 | + before(load); |
| 10 | + |
| 11 | + afterEach(async function () { |
| 12 | + await client?.close(); |
| 13 | + }); |
| 14 | + |
| 15 | + describe('Symbol.asyncDispose defined', function () { |
| 16 | + beforeEach(function () { |
| 17 | + if (!('asyncDispose' in Symbol)) { |
| 18 | + this.currentTest.skipReason = 'Test must run with asyncDispose available.'; |
| 19 | + this.skip(); |
| 20 | + } |
| 21 | + }); |
| 22 | + |
| 23 | + this.afterEach(function () {}); |
| 24 | + |
| 25 | + describe('MongoClient', function () { |
| 26 | + it('closes the the client', async function () { |
| 27 | + client = new MongoClient(process.env.MONGODB_URI); |
| 28 | + |
| 29 | + const spy = sinon.spy(client, 'close'); |
| 30 | + await client[Symbol.asyncDispose](); |
| 31 | + expect(spy.called).to.be.true; |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + describe('ClientSession', function () { |
| 36 | + it('ends the session', async function () { |
| 37 | + client = new MongoClient(process.env.MONGODB_URI); |
| 38 | + const session = client.startSession(); |
| 39 | + |
| 40 | + const spy = sinon.spy(session, 'endSession'); |
| 41 | + await session[Symbol.asyncDispose](); |
| 42 | + expect(spy.called).to.be.true; |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + describe('ChangeStreams', function () { |
| 47 | + it('closes the change stream', async function () { |
| 48 | + client = new MongoClient(process.env.MONGODB_URI); |
| 49 | + const changeStream = client.watch(); |
| 50 | + |
| 51 | + const spy = sinon.spy(changeStream, 'close'); |
| 52 | + await changeStream[Symbol.asyncDispose](); |
| 53 | + expect(spy.called).to.be.true; |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + describe('cursors', function () { |
| 58 | + it('closes the cursor', async function () { |
| 59 | + client = new MongoClient(process.env.MONGODB_URI); |
| 60 | + const cursor = client.db('foo').collection('bar').find(); |
| 61 | + |
| 62 | + const spy = sinon.spy(cursor, 'close'); |
| 63 | + await cursor[Symbol.asyncDispose](); |
| 64 | + expect(spy.called).to.be.true; |
| 65 | + }); |
| 66 | + }); |
| 67 | + }); |
| 68 | + |
| 69 | + describe('Symbol.asyncDispose not defined', function () { |
| 70 | + beforeEach(function () { |
| 71 | + if ('asyncDispose' in Symbol) { |
| 72 | + this.currentTest.skipReason = 'Test must run without asyncDispose available.'; |
| 73 | + this.skip(); |
| 74 | + } |
| 75 | + }); |
| 76 | + |
| 77 | + it('does not define symbol.asyncDispose on MongoClient', function () { |
| 78 | + expect(MongoClient[Symbol.asyncDispose]).not.to.exist; |
| 79 | + }); |
| 80 | + |
| 81 | + it('does not define symbol.asyncDispose on ClientSession', function () { |
| 82 | + expect(ClientSession[Symbol.asyncDispose]).not.to.exist; |
| 83 | + }); |
| 84 | + |
| 85 | + it('does not define symbol.asyncDispose on ChangeStream', function () { |
| 86 | + expect(ChangeStream[Symbol.asyncDispose]).not.to.exist; |
| 87 | + }); |
| 88 | + |
| 89 | + it('does not define symbol.asyncDispose on cursors', function () { |
| 90 | + expect(AbstractCursor[Symbol.asyncDispose]).not.to.exist; |
| 91 | + }); |
| 92 | + }); |
| 93 | +}); |
0 commit comments