Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
alkatrivedi committed Dec 26, 2024
1 parent 4024beb commit ae9c1fe
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 26 deletions.
53 changes: 35 additions & 18 deletions src/session-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,27 @@ export interface GetSessionCallback {
* @interface SessionFactoryInterface
*/
export interface SessionFactoryInterface {
/**
* When called returns a session.
*
* @name SessionFactoryInterface#getSession
* @param {GetSessionCallback} callback The callback function.
*/
getSession(callback: GetSessionCallback): void;

/**
* When called returns the pool object.
*
* @name SessionFactoryInterface#getPool
*/
getPool(): SessionPoolInterface;

/**
* To be called when releasing a session.
*
* @name SessionFactoryInterface#release
* @param {Session} session The session to be released.
*/
release(session: Session): void;
}

Expand Down Expand Up @@ -86,20 +105,20 @@ export class SessionFactory
this.pool_.on('error', this.emit.bind(database, 'error'));
this.pool_.open();
this.multiplexedSession_ = new MultiplexedSession(database);
// multiplexed session should only get created if the env variable is enabled
// Multiplexed sessions should only be created if its enabled.
if ((this.multiplexedSession_ as MultiplexedSession).isMultiplexedEnabled) {
this.multiplexedSession_.on('error', this.emit.bind(database, 'error'));
this.multiplexedSession_.createSession();
}
}

/**
* Retrieves the session, either the regular session or the multiplexed session based upon the environment varibale
* If the environment variable GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS is set to `true` the method will attempt to
* retrieve the multiplexed session. Otherwise it will retrieve the session from the pool.
* Retrieves a session, either a regular session or a multiplexed session, based on the environment variable configuration.
*
* If the environment variable `GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS` is set to `true`, the method will attempt to
* retrieve a multiplexed session. Otherwise, it will retrieve a session from the regular pool.
*
* The session is returned asynchronously via the provided callback, which will receive either an error or the session object.
* @param callback
* @param {GetSessionCallback} callback The callback function.
*/

getSession(callback: GetSessionCallback): void {
Expand All @@ -112,32 +131,30 @@ export class SessionFactory
}

/**
* Returns the SessionPoolInterface used by the current instance, which provide access to the session pool
* for obtaining database sessions.
* Returns the regular session pool object.
*
* @returns {SessionPoolInterface} The session pool used by current instance.
* This object allows interaction with the pool for acquiring and managing sessions.
*/

getPool(): SessionPoolInterface {
return this.pool_;
}

/**
* Releases the session back to the pool.
*
* This method is used to return the session back to the pool after it is no longer needed.
* Releases a session back to the session pool.
*
* It only performs the operation if the Multiplexed Session is disabled which is controlled via the
* env variable GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS.
* This method returns a session to the pool after it is no longer needed.
* It is a no-op for multiplexed sessions.
*
* @param session The session to be released. This should be an instance of `Session` that was previously
* acquired from the session pool.
* @param {Session} session - The session to be released. This should be an instance of `Session` that was
* previously acquired from the session pool.
*
* @throws {Error} Throws an error if the session is invalid or cannot be released.
* @throws {Error} If the session is invalid or cannot be released.
*/
release(session: Session): void {
if (!this.isMuxCreated) {
if (
!(this.multiplexedSession_ as MultiplexedSession).isMultiplexedEnabled
) {
this.pool_.release(session);
}
}
Expand Down
12 changes: 11 additions & 1 deletion test/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export class FakeSessionFactory extends EventEmitter {
this.calledWith_ = arguments;
}
getSession(): FakeSession | FakeMultiplexedSession {
if (process.env.GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS) {
if (process.env.GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS === 'false') {
return new FakeSession();
} else {
return new FakeMultiplexedSession();
Expand Down Expand Up @@ -327,6 +327,16 @@ describe('Database', () => {
assert(database.formattedName_, formattedName);
});

it('should accept a custom Pool class', () => {
function FakePool() {}
const database = new Database(
INSTANCE,
NAME,
FakePool as {} as db.SessionPoolConstructor
);
assert(database.pool_ instanceof FakeSessionPool);
});

it('should re-emit SessionPool errors', done => {
const error = new Error('err');

Expand Down
14 changes: 7 additions & 7 deletions test/session-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ describe('SessionFactory', () => {
assert.strictEqual(openStub.callCount, 1);
});

describe('when env GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS is enabled', () => {
describe('when GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS is enabled', () => {
before(() => {
process.env.GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS = 'true';
});
Expand All @@ -133,12 +133,12 @@ describe('SessionFactory', () => {
});

describe('getSession', () => {
describe('for regular session', () => {
describe('when GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS is disabled', () => {
before(() => {
process.env.GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS = 'false';
});

it('should return the regular session if GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS env is disabled', done => {
it('should retrieve a regular session from the pool', done => {
(
sandbox.stub(sessionFactory.pool_, 'getSession') as sinon.SinonStub
).callsFake(callback => callback(null, fakeSession));
Expand All @@ -149,7 +149,7 @@ describe('SessionFactory', () => {
});
});

it('should return the error from getSession if GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS env is disabled and regular session creation get failed', done => {
it('should propagate errors when regular session retrieval fails', done => {
const fakeError = new Error();
(
sandbox.stub(sessionFactory.pool_, 'getSession') as sinon.SinonStub
Expand All @@ -162,12 +162,12 @@ describe('SessionFactory', () => {
});
});

describe('for multiplexed session', () => {
describe('when GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS is enabled', () => {
before(() => {
process.env.GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS = 'true';
});

it('should return the multiplexed session if GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS env is enabled', done => {
it('should return the multiplexed session', done => {
(
sandbox.stub(
sessionFactory.multiplexedSession_,
Expand All @@ -183,7 +183,7 @@ describe('SessionFactory', () => {
});
});

it('should return the error from getSession if GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS env is enabled and multiplexed session creation get failed', done => {
it('should propagate error when multiplexed session return fails', done => {
const fakeError = new Error();
(
sandbox.stub(
Expand Down

0 comments on commit ae9c1fe

Please sign in to comment.