Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(NODE-6626): implement integration tests for improved client.close() - server-side #4367

Open
wants to merge 1 commit into
base: NODE-6620/sockets
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 103 additions & 5 deletions test/integration/node-specific/client_close.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* eslint-disable @typescript-eslint/no-empty-function */
import { expect } from 'chai';

import { type TestConfiguration } from '../../tools/runner/config';
import { runScriptAndGetProcessInfo } from './resource_tracking_script_builder';

Expand Down Expand Up @@ -500,29 +502,85 @@ describe.skip('MongoClient.close() Integration', () => {
});

describe('ClientSession (Implicit)', () => {
let idleSessionsBeforeClose;
let idleSessionsAfterClose;

beforeEach(async function () {
const client = this.configuration.newClient();
await client.connect();
const session = client.startSession({ explicit: false });
session.startTransaction();
await client.db('db').collection('collection').insertOne({ x: 1 }, { session });

const opBefore = await client.db().admin().command({ currentOp: 1 });
idleSessionsBeforeClose = opBefore.inprog.filter(s => s.type === 'idleSession');

await client.close();
await client.connect();

const opAfter = await client.db().admin().command({ currentOp: 1 });
idleSessionsAfterClose = opAfter.inprog.filter(s => s.type === 'idleSession');

await client.close();
});

describe('Server resource: LSID/ServerSession', () => {
describe('after a clientSession is implicitly created and used', () => {
it.skip('the server-side ServerSession is cleaned up by client.close()', async function () {});
it('the server-side ServerSession is cleaned up by client.close()', async function () {
expect(idleSessionsBeforeClose).to.not.be.empty;
expect(idleSessionsAfterClose).to.be.empty;
});
});
});

describe('Server resource: Transactions', () => {
describe('after a clientSession is implicitly created and used', () => {
it.skip('the server-side transaction is cleaned up by client.close()', async function () {});
it('the server-side transaction is cleaned up by client.close()', async function () {
expect(idleSessionsBeforeClose[0].transaction.txnNumber).to.not.null;
expect(idleSessionsAfterClose).to.be.empty;
});
});
});
});

describe('ClientSession (Explicit)', () => {
let idleSessionsBeforeClose;
let idleSessionsAfterClose;

beforeEach(async function () {
const client = this.configuration.newClient();
await client.connect();
const session = client.startSession();
session.startTransaction();
await client.db('db').collection('collection').insertOne({ x: 1 }, { session });

const opBefore = await client.db().admin().command({ currentOp: 1 });
idleSessionsBeforeClose = opBefore.inprog.filter(s => s.type === 'idleSession');

await client.close();
await client.connect();

const opAfter = await client.db().admin().command({ currentOp: 1 });
idleSessionsAfterClose = opAfter.inprog.filter(s => s.type === 'idleSession');

await client.close();
});

describe('Server resource: LSID/ServerSession', () => {
describe('after a clientSession is created and used', () => {
it.skip('the server-side ServerSession is cleaned up by client.close()', async function () {});
it('the server-side ServerSession is cleaned up by client.close()', async function () {
expect(idleSessionsBeforeClose).to.not.be.empty;
expect(idleSessionsAfterClose).to.be.empty;
});
});
});

describe('Server resource: Transactions', () => {
describe('after a clientSession is created and used', () => {
it.skip('the server-side transaction is cleaned up by client.close()', async function () {});
it('the server-side transaction is cleaned up by client.close()', async function () {
expect(idleSessionsBeforeClose[0].transaction.txnNumber).to.not.null;
expect(idleSessionsAfterClose).to.be.empty;
});
});
});
});
Expand Down Expand Up @@ -632,7 +690,47 @@ describe.skip('MongoClient.close() Integration', () => {

describe('Server resource: Cursor', () => {
describe('after cursors are created', () => {
it.skip('all active server-side cursors are closed by client.close()', async function () {});
let client;
let coll;
let cursor;

beforeEach(async function () {
client = this.configuration.newClient();
coll = client.db('db').collection('coll');
});

afterEach(async function () {
await client?.close();
await cursor?.close();
});

it('all active server-side cursors are closed by client.close()', async function () {
const getCursors = async () => {
const res = await client
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optional: this is the sort of thing for which we'd usually use a utilClient. If we use a separate client, we keep the client used for testing separate from the client we use to collect cluster data. It also avoids reopening the client after closing it in the test (line 729).

.db()
.admin()
.command({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(optional): Neal added runCursorCommand last year to simplify running cursor commands manually:

const result = await client.db('admin').runCursorCommand({ aggregate: .... }).toArray();
return result.filter(
  r => r.type === 'idleCursor' || (r.type === 'op' && r.desc === 'getMore')
);

aggregate: 1,
cursor: { batchSize: 10 },
pipeline: [{ $currentOp: { idleCursors: true } }]
});
return res.cursor.firstBatch.filter(
r => r.type === 'idleCursor' || (r.type === 'op' && r.desc === 'getMore')
);
};

await coll.insertMany([{ a: 1 }, { b: 2 }, { c: 3 }]);
cursor = await coll.find();

// assert creation
expect(await getCursors()).to.not.be.empty;

await client.close();
await client.connect();

// assert clean-up
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: We pass all cases already except cursors already.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the other test cases be unskipped then?

expect(await getCursors()).to.be.empty;
});
});
});
});