Skip to content

feat(NODE-3697): reduce serverSession allocation #3171

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

Merged
merged 14 commits into from
Mar 25, 2022
21 changes: 19 additions & 2 deletions test/unit/sessions.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,20 +256,37 @@ describe('Sessions - unit', function () {
describe('from an implicit session', () => {
const session = new ClientSession(topology, serverSessionPool, { explicit: false }); // make an implicit session

it('should throw if the session hasEnded before serverSession was acquired', () => {
it('should throw if the session ended before serverSession was acquired', () => {
expect(session).to.have.property(serverSessionSymbol, null);
session.hasEnded = true;
expect(() => session.serverSession).to.throw(MongoRuntimeError);
});

it('should acquire a serverSession if clientSession.hadEnded is false', () => {
it('should acquire a serverSession if clientSession.hasEnded is false and serverSession is not set', () => {
expect(session).to.have.property(serverSessionSymbol, null);
session.hasEnded = false;
const acquireSpy = sinon.spy(serverSessionPool, 'acquire');
expect(session.serverSession).to.be.instanceOf(ServerSession);
expect(acquireSpy.calledOnce).to.be.true;
acquireSpy.restore();
});

it('should return the existing serverSession and not acquire a new one if one is already set', () => {
expect(session).to.have.property(serverSessionSymbol, null);
const acquireSpy = sinon.spy(serverSessionPool, 'acquire');
expect(session.serverSession).to.be.instanceOf(ServerSession);
expect(acquireSpy.calledOnce).to.be.true;

// call the getter a bunch more times
expect(session.serverSession).to.be.instanceOf(ServerSession);
expect(session.serverSession).to.be.instanceOf(ServerSession);
expect(session.serverSession).to.be.instanceOf(ServerSession);

// acquire never called again
expect(acquireSpy.calledOnce).to.be.true;

acquireSpy.restore();
});
});
});

Expand Down