Skip to content

Commit

Permalink
refactor: dynamic fetching of env to in-memory state
Browse files Browse the repository at this point in the history
  • Loading branch information
alkatrivedi committed Dec 26, 2024
1 parent fb6a5bb commit a5d9666
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 40 deletions.
13 changes: 8 additions & 5 deletions src/session-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export class SessionFactory
{
multiplexedSession_?: MultiplexedSessionInterface;
pool_: SessionPoolInterface;
isMuxCreated: boolean;
constructor(
database: Database,
name: String,
Expand All @@ -85,8 +86,10 @@ export class SessionFactory
: new SessionPool(database, poolOptions);
this.pool_.on('error', this.emit.bind(database, 'error'));
this.pool_.open();
this.isMuxCreated = false;
// multiplexed session should only get created if the env variable is enabled
if (process.env.GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS === 'true') {
this.isMuxCreated = true;
this.multiplexedSession_ = new MultiplexedSession(database);
this.multiplexedSession_.on('error', this.emit.bind(database, 'error'));
this.multiplexedSession_.createSession();
Expand All @@ -103,10 +106,10 @@ export class SessionFactory
*/

getSession(callback: GetSessionCallback): void {
const sessionHandler =
process.env.GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS === 'true'
? this.multiplexedSession_
: this.pool_;
const sessionHandler = this.isMuxCreated
? this.multiplexedSession_
: this.pool_;

sessionHandler!.getSession((err, session) => callback(err, session));
}

Expand Down Expand Up @@ -136,7 +139,7 @@ export class SessionFactory
* @throws {Error} Throws an error if the session is invalid or cannot be released.
*/
release(session: Session): void {
if (process.env.GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS === 'false') {
if (!this.isMuxCreated) {
this.pool_.release(session);
}
}
Expand Down
58 changes: 32 additions & 26 deletions test/session-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,44 +78,50 @@ describe('SessionFactory', () => {
});

describe('instantiation', () => {
describe('SessionPool', () => {
before(() => {
process.env.GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS = 'false';
});
before(() => {
process.env.GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS = 'false';
});

it('should create a SessionPool object', () => {
assert(sessionFactory.pool_ instanceof SessionPool);
});
it('should create a SessionPool object', () => {
assert(sessionFactory.pool_ instanceof SessionPool);
});

it('should accept a custom Pool class', () => {
function FakePool() {}
FakePool.prototype.on = util.noop;
FakePool.prototype.open = util.noop;
it('should accept a custom Pool class', () => {
function FakePool() {}
FakePool.prototype.on = util.noop;
FakePool.prototype.open = util.noop;

const sessionFactory = new SessionFactory(
DATABASE,
NAME,
FakePool as {} as db.SessionPoolConstructor
);
assert(sessionFactory.pool_ instanceof FakePool);
});

const sessionFactory = new SessionFactory(
DATABASE,
NAME,
FakePool as {} as db.SessionPoolConstructor
);
assert(sessionFactory.pool_ instanceof FakePool);
});
it('should open the pool', () => {
const openStub = sandbox
.stub(SessionPool.prototype, 'open')
.callsFake(() => {});

it('should open the pool', () => {
const openStub = sandbox
.stub(SessionPool.prototype, 'open')
.callsFake(() => {});
new SessionFactory(DATABASE, NAME, POOL_OPTIONS);

new SessionFactory(DATABASE, NAME, POOL_OPTIONS);
assert.strictEqual(openStub.callCount, 1);
});

assert.strictEqual(openStub.callCount, 1);
});
it('should set the isMuxCreated to be false if env is disabled', () => {
assert.strictEqual(sessionFactory.isMuxCreated, false);
});

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

it('should set the isMuxCreated to be true if env is enabled', () => {
assert.strictEqual(sessionFactory.isMuxCreated, true);
});

it('should create a MultiplexedSession object', () => {
assert(
sessionFactory.multiplexedSession_ instanceof MultiplexedSession
Expand Down
16 changes: 7 additions & 9 deletions test/spanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5081,18 +5081,16 @@ describe('Spanner with mock server', () => {
after(() => {
process.env.GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS = 'false';
});
it('should execute table mutations without leaking sessions', () => {

it('should not throw error when enabling env GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS after client initialization', done => {
const database = newTestDatabase();
// enable env after database creation
process.env.GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS = 'true';
const sessionFactory = database.sessionFactory_ as SessionFactory;
try {
sessionFactory.getSession(() => {});
} catch (e) {
assert.strictEqual(
(e as TypeError).message,
"Cannot read properties of undefined (reading 'getSession')"
);
}
sessionFactory.getSession((err, _) => {
assert.ifError(err);
done();
});
});
});
});
Expand Down

0 comments on commit a5d9666

Please sign in to comment.