Skip to content

Commit 779ce85

Browse files
add rough behavior testing
1 parent 9565315 commit 779ce85

File tree

8 files changed

+110
-7
lines changed

8 files changed

+110
-7
lines changed

.evergreen/config.in.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,18 @@ functions:
353353
args:
354354
- "${PROJECT_DIRECTORY}/.evergreen/run-typescript.sh"
355355

356+
"check resource management":
357+
- command: subprocess.exec
358+
type: test
359+
params:
360+
working_dir: "src"
361+
timeout_secs: 60
362+
env:
363+
PROJECT_DIRECTORY: ${PROJECT_DIRECTORY}
364+
binary: bash
365+
args:
366+
- "${PROJECT_DIRECTORY}/.evergreen/run-resource-management.sh"
367+
356368
"compile driver":
357369
- command: subprocess.exec
358370
type: test

.evergreen/config.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,17 @@ functions:
316316
binary: bash
317317
args:
318318
- ${PROJECT_DIRECTORY}/.evergreen/run-typescript.sh
319+
check resource management:
320+
- command: subprocess.exec
321+
type: test
322+
params:
323+
working_dir: src
324+
timeout_secs: 60
325+
env:
326+
PROJECT_DIRECTORY: ${PROJECT_DIRECTORY}
327+
binary: bash
328+
args:
329+
- ${PROJECT_DIRECTORY}/.evergreen/run-resource-management.sh
319330
compile driver:
320331
- command: subprocess.exec
321332
type: test
@@ -3461,6 +3472,30 @@ tasks:
34613472
- {key: NPM_VERSION, value: '9'}
34623473
- func: install dependencies
34633474
- func: run lint checks
3475+
- name: run-resource-management-no-async-dispose
3476+
tags:
3477+
- resource-management
3478+
commands:
3479+
- command: expansions.update
3480+
type: setup
3481+
params:
3482+
updates:
3483+
- {key: NODE_LTS_VERSION, value: v16.20.2}
3484+
- {key: NPM_VERSION, value: '9'}
3485+
- func: install dependencies
3486+
- func: check resource management
3487+
- name: run-resource-management-async-dispose
3488+
tags:
3489+
- resource-management
3490+
commands:
3491+
- command: expansions.update
3492+
type: setup
3493+
params:
3494+
updates:
3495+
- {key: NODE_LTS_VERSION, value: latest}
3496+
- {key: NPM_VERSION, value: '9'}
3497+
- func: install dependencies
3498+
- func: check resource management
34643499
- name: check-types-typescript-next-node-types-20.14.10
34653500
tags:
34663501
- check-types-typescript-next
@@ -5020,6 +5055,7 @@ buildvariants:
50205055
- .unit-tests
50215056
- .lint-checks
50225057
- .typescript-compilation
5058+
- .resource-management
50235059
- name: generate-combined-coverage
50245060
display_name: Generate Combined Coverage
50255061
run_on: rhel80-large

.evergreen/generate_evergreen_tasks.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,30 @@ SINGLETON_TASKS.push(
492492
{ func: 'run lint checks' }
493493
]
494494
},
495+
{
496+
name: 'run-resource-management-no-async-dispose',
497+
tags: ['resource-management'],
498+
commands: [
499+
updateExpansions({
500+
NODE_LTS_VERSION: "v16.20.2",
501+
NPM_VERSION: 9
502+
}),
503+
{ func: 'install dependencies' },
504+
{ func: 'check resource management' }
505+
]
506+
},
507+
{
508+
name: 'run-resource-management-async-dispose',
509+
tags: ['resource-management'],
510+
commands: [
511+
updateExpansions({
512+
NODE_LTS_VERSION: 'latest',
513+
NPM_VERSION: 9
514+
}),
515+
{ func: 'install dependencies' },
516+
{ func: 'check resource management' }
517+
]
518+
},
495519
...Array.from(makeTypescriptTasks())
496520
]
497521
);
@@ -548,7 +572,7 @@ BUILD_VARIANTS.push({
548572
name: 'lint',
549573
display_name: 'lint',
550574
run_on: DEFAULT_OS,
551-
tasks: ['.unit-tests', '.lint-checks', '.typescript-compilation']
575+
tasks: ['.unit-tests', '.lint-checks', '.typescript-compilation', '.resource-management']
552576
});
553577

554578
BUILD_VARIANTS.push({
@@ -755,6 +779,7 @@ fileData.tasks = (fileData.tasks || [])
755779
.concat(AUTH_DISABLED_TASKS)
756780
.concat(AWS_LAMBDA_HANDLER_TASKS)
757781
.concat(MONGOCRYPTD_CSFLE_TASKS);
782+
758783
fileData.buildvariants = (fileData.buildvariants || []).concat(BUILD_VARIANTS);
759784

760785
fs.writeFileSync(

.evergreen/install-dependencies.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#!/usr/bin/env bash
22
set -o errexit # Exit the script with error if any of the commands fail
33

4+
# allowed values:
5+
## a nodejs major version (i.e., 16)
6+
## 'latest'
7+
## a full nodejs version, in the format v<major>.<minor>.patch
48
NODE_LTS_VERSION=${NODE_LTS_VERSION:-16}
59
# npm version can be defined in the environment for cases where we need to install
610
# a version lower than latest to support EOL Node versions.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@
145145
"check:unit": "mocha test/unit",
146146
"check:ts": "node ./node_modules/typescript/bin/tsc -v && node ./node_modules/typescript/bin/tsc --noEmit",
147147
"check:atlas": "mocha --config test/manual/mocharc.json test/manual/atlas_connectivity.test.ts",
148+
"check:resource-management": "mocha --config test/manual/mocharc.json test/manual/resource_management.test.ts",
148149
"check:drivers-atlas-testing": "mocha --config test/mocha_mongodb.json test/atlas/drivers_atlas_testing.test.ts",
149150
"check:adl": "mocha --config test/mocha_mongodb.json test/manual/atlas-data-lake-testing",
150151
"check:aws": "nyc mocha --config test/mocha_mongodb.json test/integration/auth/mongodb_aws.test.ts",

src/resource_management.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import { AbstractCursor, ChangeStream } from './beta';
2+
import { MongoClient } from './mongo_client';
3+
import { ClientSession } from './sessions';
4+
15
/**
26
* @public
37
* @experimental
@@ -6,3 +10,25 @@ export interface AsyncDisposable {
610
/** @beta */
711
[Symbol.asyncDispose]: () => Promise<void>;
812
}
13+
14+
export function load() {
15+
Symbol.asyncDispose &&
16+
(MongoClient.prototype[Symbol.asyncDispose] = async function () {
17+
await this.close();
18+
});
19+
20+
Symbol.asyncDispose &&
21+
(ClientSession.prototype[Symbol.asyncDispose] = async function () {
22+
await this.endSession({ force: true });
23+
});
24+
25+
Symbol.asyncDispose &&
26+
(AbstractCursor.prototype[Symbol.asyncDispose] = async function () {
27+
await this.close();
28+
});
29+
30+
Symbol.asyncDispose &&
31+
(ChangeStream.prototype[Symbol.asyncDispose] = async function () {
32+
await this.close();
33+
});
34+
}

test/manual/resource_management.test.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,9 @@ describe('Explicit Resource Management Tests', function () {
2020
}
2121
});
2222

23-
this.afterEach(function () {});
24-
2523
describe('MongoClient', function () {
2624
it('closes the the client', async function () {
27-
client = new MongoClient(process.env.MONGODB_URI);
25+
client = new MongoClient('mongodb://localhost:27017');
2826

2927
const spy = sinon.spy(client, 'close');
3028
await client[Symbol.asyncDispose]();
@@ -34,7 +32,7 @@ describe('Explicit Resource Management Tests', function () {
3432

3533
describe('ClientSession', function () {
3634
it('ends the session', async function () {
37-
client = new MongoClient(process.env.MONGODB_URI);
35+
client = new MongoClient('mongodb://localhost:27017');
3836
const session = client.startSession();
3937

4038
const spy = sinon.spy(session, 'endSession');
@@ -45,7 +43,7 @@ describe('Explicit Resource Management Tests', function () {
4543

4644
describe('ChangeStreams', function () {
4745
it('closes the change stream', async function () {
48-
client = new MongoClient(process.env.MONGODB_URI);
46+
client = new MongoClient('mongodb://localhost:27017');
4947
const changeStream = client.watch();
5048

5149
const spy = sinon.spy(changeStream, 'close');
@@ -56,7 +54,7 @@ describe('Explicit Resource Management Tests', function () {
5654

5755
describe('cursors', function () {
5856
it('closes the cursor', async function () {
59-
client = new MongoClient(process.env.MONGODB_URI);
57+
client = new MongoClient('mongodb://localhost:27017');
6058
const cursor = client.db('foo').collection('bar').find();
6159

6260
const spy = sinon.spy(cursor, 'close');

test/mongodb.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ export * from '../src/operations/update';
187187
export * from '../src/operations/validate_collection';
188188
export * from '../src/read_concern';
189189
export * from '../src/read_preference';
190+
export * from '../src/resource_management';
190191
export * from '../src/sdam/common';
191192
export * from '../src/sdam/events';
192193
export * from '../src/sdam/monitor';

0 commit comments

Comments
 (0)