Skip to content

Commit 97470e4

Browse files
add shared mongocryptd spawn and lifecycle management hooks
1 parent 031a373 commit 97470e4

File tree

5 files changed

+42
-85
lines changed

5 files changed

+42
-85
lines changed

test/integration/client-side-operations-timeout/client_side_operations_timeout.prose.test.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
/* Specification prose tests */
22

3-
import { type ChildProcess, spawn } from 'node:child_process';
43
import { Readable } from 'node:stream';
54

65
import { expect } from 'chai';
7-
import * as os from 'os';
8-
import * as path from 'path';
96
import * as semver from 'semver';
107
import * as sinon from 'sinon';
118
import { pipeline } from 'stream/promises';
@@ -27,6 +24,7 @@ import {
2724
import {
2825
clearFailPoint,
2926
configureFailPoint,
27+
configureMongocryptdSpawnHooks,
3028
type FailCommandFailPoint,
3129
makeMultiBatchWrite,
3230
measureDuration
@@ -127,28 +125,16 @@ describe('CSOT spec prose tests', function () {
127125

128126
let client: MongoClient;
129127
const mongocryptdTestPort = '23000';
130-
let childProcess: ChildProcess;
128+
configureMongocryptdSpawnHooks({ port: mongocryptdTestPort });
131129

132130
beforeEach(async function () {
133-
const pidFile = path.join(os.tmpdir(), new ObjectId().toHexString());
134-
childProcess = spawn(
135-
'mongocryptd',
136-
['--port', mongocryptdTestPort, '--ipv6', '--pidfilepath', pidFile],
137-
{
138-
stdio: 'ignore',
139-
detached: true
140-
}
141-
);
142-
143-
childProcess.on('error', error => console.warn(this.currentTest?.fullTitle(), error));
144131
client = new MongoClient(`mongodb://localhost:${mongocryptdTestPort}/?timeoutMS=1000`, {
145132
monitorCommands: true
146133
});
147134
});
148135

149136
afterEach(async function () {
150137
await client.close();
151-
childProcess.kill('SIGKILL');
152138
sinon.restore();
153139
});
154140

test/integration/node-specific/client_close.test.ts

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import { type ChildProcess, spawn } from 'node:child_process';
21
import * as events from 'node:events';
3-
import { tmpdir } from 'node:os';
4-
import { join } from 'node:path';
52

63
import { expect } from 'chai';
74

@@ -10,9 +7,9 @@ import {
107
type Collection,
118
type CommandStartedEvent,
129
type FindCursor,
13-
type MongoClient,
14-
ObjectId
10+
type MongoClient
1511
} from '../../mongodb';
12+
import { configureMongocryptdSpawnHooks } from '../../tools/utils';
1613
import { filterForCommands } from '../shared';
1714
import { runScriptAndGetProcessInfo } from './resource_tracking_script_builder';
1815

@@ -520,25 +517,10 @@ describe('MongoClient.close() Integration', () => {
520517

521518
describe('when sessions are not supported', function () {
522519
const mongocryptdTestPort = '27022';
523-
let childProcess: ChildProcess;
524520
let client: MongoClient;
525521
const commands: Array<CommandStartedEvent> = [];
526522

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-
});
523+
configureMongocryptdSpawnHooks({ port: mongocryptdTestPort });
542524

543525
beforeEach('configure cryptd client and prepopulate session pool', async function () {
544526
client = this.configuration.newClient(`mongodb://localhost:${mongocryptdTestPort}`, {
@@ -553,10 +535,6 @@ describe('MongoClient.close() Integration', () => {
553535
expect(client.s.sessionPool.sessions).to.have.length.greaterThan(0);
554536
});
555537

556-
afterEach(() => {
557-
childProcess.kill();
558-
});
559-
560538
it('does not execute endSessions', async function () {
561539
await client.close();
562540

test/integration/server-discovery-and-monitoring/server_description.test.ts

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,20 @@
1-
import { type ChildProcess, spawn } from 'node:child_process';
2-
31
import { expect } from 'chai';
4-
import * as os from 'os';
5-
import * as path from 'path';
62

7-
import { MongoClient, ObjectId } from '../../mongodb';
3+
import { MongoClient } from '../../mongodb';
4+
import { configureMongocryptdSpawnHooks } from '../../tools/utils';
85

96
describe('class ServerDescription', function () {
107
describe('when connecting to mongocryptd', { requires: { mongodb: '>=4.4' } }, function () {
118
let client: MongoClient;
12-
const mongocryptdTestPort = '27022';
13-
let childProcess: ChildProcess;
9+
10+
const { port: mongocryptdTestPort } = configureMongocryptdSpawnHooks();
1411

1512
beforeEach(async function () {
16-
const pidFile = path.join(os.tmpdir(), new ObjectId().toHexString());
17-
childProcess = spawn(
18-
'mongocryptd',
19-
['--port', mongocryptdTestPort, '--ipv6', '--pidfilepath', pidFile],
20-
{
21-
stdio: 'ignore',
22-
detached: true
23-
}
24-
);
25-
26-
childProcess.on('error', error => console.warn(this.currentTest?.fullTitle(), error));
2713
client = new MongoClient(`mongodb://localhost:${mongocryptdTestPort}`);
2814
});
2915

3016
afterEach(async function () {
3117
await client?.close();
32-
childProcess.kill('SIGKILL');
3318
});
3419

3520
it('iscryptd is set to true ', async function () {

test/integration/sessions/sessions.prose.test.ts

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
import { ObjectId } from 'bson';
21
import { expect } from 'chai';
3-
import { type ChildProcess, spawn } from 'child_process';
42
import { once } from 'events';
5-
import * as os from 'os';
6-
import * as path from 'path';
73

84
import { type CommandStartedEvent } from '../../../src/cmap/command_monitoring_events';
95
import { type Collection } from '../../../src/collection';
106
import { MongoDriverError, MongoInvalidArgumentError } from '../../../src/error';
117
import { MongoClient } from '../../../src/mongo_client';
12-
import { sleep } from '../../tools/utils';
8+
import { configureMongocryptdSpawnHooks, sleep } from '../../tools/utils';
139

1410
describe('Sessions Prose Tests', () => {
1511
describe('5. Session argument is for the right client', () => {
@@ -128,23 +124,8 @@ describe('Sessions Prose Tests', () => {
128124
*/
129125
const mongocryptdTestPort = '27022';
130126
let client: MongoClient;
131-
let childProcess: ChildProcess;
132-
133-
before(() => {
134-
const pidFile = path.join(os.tmpdir(), new ObjectId().toHexString());
135-
childProcess = spawn(
136-
'mongocryptd',
137-
['--port', mongocryptdTestPort, '--ipv6', '--pidfilepath', pidFile],
138-
{
139-
stdio: 'ignore',
140-
detached: true
141-
}
142-
);
143127

144-
childProcess.on('error', err => {
145-
console.warn('Sessions prose mongocryptd error:', err);
146-
});
147-
});
128+
configureMongocryptdSpawnHooks({ port: mongocryptdTestPort });
148129

149130
beforeEach(async () => {
150131
client = new MongoClient(`mongodb://localhost:${mongocryptdTestPort}`, {
@@ -160,10 +141,6 @@ describe('Sessions Prose Tests', () => {
160141
await client?.close();
161142
});
162143

163-
after(() => {
164-
childProcess.kill();
165-
});
166-
167144
it(
168145
'18. Implicit session is ignored if connection does not support sessions',
169146
{

test/tools/utils.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as child_process from 'node:child_process';
22
import { on, once } from 'node:events';
33
import * as fs from 'node:fs/promises';
4+
import { tmpdir } from 'node:os';
45
import * as path from 'node:path';
56

67
import { EJSON } from 'bson';
@@ -528,3 +529,33 @@ export const DOMException: {
528529
ac.abort();
529530
return ac.signal.reason.constructor;
530531
})();
532+
533+
export function configureMongocryptdSpawnHooks(
534+
options: { port?: string; pidfilepath?: string } = {}
535+
): { port: string } {
536+
const port = options.port ?? '27022';
537+
const pidfilepath = options.pidfilepath ?? path.join(tmpdir(), new BSON.ObjectId().toHexString());
538+
539+
let childProcess: child_process.ChildProcess;
540+
541+
beforeEach(async function () {
542+
childProcess = child_process.spawn(
543+
'mongocryptd',
544+
['--port', port, '--ipv6', '--pidfilepath', pidfilepath],
545+
{
546+
stdio: 'ignore',
547+
detached: false
548+
}
549+
);
550+
551+
childProcess.on('error', error => console.warn(this.currentTest?.fullTitle(), error));
552+
});
553+
554+
afterEach(function () {
555+
childProcess.kill('SIGKILL');
556+
});
557+
558+
return {
559+
port
560+
};
561+
}

0 commit comments

Comments
 (0)