|
| 1 | +import { type ChildProcess, spawn } from 'node:child_process'; |
1 | 2 | import * as events from 'node:events'; |
| 3 | +import { tmpdir } from 'node:os'; |
| 4 | +import { join } from 'node:path'; |
2 | 5 |
|
3 | 6 | import { expect } from 'chai'; |
4 | 7 |
|
5 | 8 | import { getCSFLEKMSProviders } from '../../csfle-kms-providers'; |
6 | | -import { type Collection, type FindCursor, type MongoClient } from '../../mongodb'; |
| 9 | +import { |
| 10 | + type Collection, |
| 11 | + type CommandStartedEvent, |
| 12 | + type FindCursor, |
| 13 | + type MongoClient, |
| 14 | + ObjectId |
| 15 | +} from '../../mongodb'; |
| 16 | +import { filterForCommands } from '../shared'; |
7 | 17 | import { runScriptAndGetProcessInfo } from './resource_tracking_script_builder'; |
8 | 18 |
|
9 | 19 | describe('MongoClient.close() Integration', () => { |
@@ -490,20 +500,68 @@ describe('MongoClient.close() Integration', () => { |
490 | 500 | }); |
491 | 501 |
|
492 | 502 | describe('when MongoClient.close is called', function () { |
493 | | - it('sends an endSessions command', async function () { |
494 | | - await client.db('a').collection('a').insertOne({ a: 1 }); |
495 | | - await client.db('a').collection('a').insertOne({ a: 1 }); |
496 | | - await client.db('a').collection('a').insertOne({ a: 1 }); |
497 | | - const endSessionsStarted = events.once(client, 'commandStarted'); |
498 | | - const willEndSessions = events.once(client, 'commandSucceeded'); |
| 503 | + describe('when sessions are supported', function () { |
| 504 | + it('sends an endSessions command', async function () { |
| 505 | + await client.db('a').collection('a').insertOne({ a: 1 }); |
| 506 | + await client.db('a').collection('a').insertOne({ a: 1 }); |
| 507 | + await client.db('a').collection('a').insertOne({ a: 1 }); |
| 508 | + const endSessionsStarted = events.once(client, 'commandStarted'); |
| 509 | + const willEndSessions = events.once(client, 'commandSucceeded'); |
499 | 510 |
|
500 | | - await client.close(); |
| 511 | + await client.close(); |
| 512 | + |
| 513 | + const [startedEv] = await endSessionsStarted; |
| 514 | + expect(startedEv).to.have.nested.property('command.endSessions').that.has.lengthOf(1); |
| 515 | + |
| 516 | + const [commandEv] = await willEndSessions; |
| 517 | + expect(commandEv).to.have.property('commandName', 'endSessions'); |
| 518 | + }); |
| 519 | + }); |
501 | 520 |
|
502 | | - const [startedEv] = await endSessionsStarted; |
503 | | - expect(startedEv).to.have.nested.property('command.endSessions').that.has.lengthOf(1); |
| 521 | + describe('when sessions are not supported', function () { |
| 522 | + const mongocryptdTestPort = '27022'; |
| 523 | + let childProcess: ChildProcess; |
| 524 | + let client: MongoClient; |
| 525 | + const commands: Array<CommandStartedEvent> = []; |
| 526 | + |
| 527 | + beforeEach(async function () { |
| 528 | + const pidFile = join(tmpdir(), new ObjectId().toHexString()); |
| 529 | + childProcess = spawn( |
| 530 | + 'mongocryptd', |
| 531 | + ['--port', mongocryptdTestPort, '--ipv6', '--pidfilepath', pidFile], |
| 532 | + { |
| 533 | + stdio: 'ignore', |
| 534 | + detached: true |
| 535 | + } |
| 536 | + ); |
| 537 | + |
| 538 | + childProcess.on('error', err => { |
| 539 | + console.warn('Sessions prose mongocryptd error:', err); |
| 540 | + }); |
| 541 | + }); |
504 | 542 |
|
505 | | - const [commandEv] = await willEndSessions; |
506 | | - expect(commandEv).to.have.property('commandName', 'endSessions'); |
| 543 | + beforeEach('configure cryptd client and prepopulate session pool', async function () { |
| 544 | + client = this.configuration.newClient(`mongodb://localhost:${mongocryptdTestPort}`, { |
| 545 | + monitorCommands: true |
| 546 | + }); |
| 547 | + |
| 548 | + client.on('commandStarted', filterForCommands('endSessions', commands)); |
| 549 | + |
| 550 | + // run an operation to instantiate an implicit session (which should be omitted) from the |
| 551 | + // actual command but still instantiated by the client. See session prose test 18. |
| 552 | + await client.db().command({ hello: true }); |
| 553 | + expect(client.s.sessionPool.sessions).to.have.length.greaterThan(0); |
| 554 | + }); |
| 555 | + |
| 556 | + afterEach(() => { |
| 557 | + childProcess.kill(); |
| 558 | + }); |
| 559 | + |
| 560 | + it('does not execute endSessions', async function () { |
| 561 | + await client.close(); |
| 562 | + |
| 563 | + expect(commands).to.deep.equal([]); |
| 564 | + }); |
507 | 565 | }); |
508 | 566 | }); |
509 | 567 | }); |
|
0 commit comments