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 689c26e
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 37 deletions.
8 changes: 6 additions & 2 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 @@ -104,9 +107,10 @@ export class SessionFactory

getSession(callback: GetSessionCallback): void {
const sessionHandler =

Check failure on line 109 in src/session-factory.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `⏎·····`
process.env.GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS === 'true'
this.isMuxCreated
? this.multiplexedSession_

Check failure on line 111 in src/session-factory.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `··`
: this.pool_;

Check failure on line 112 in src/session-factory.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `··`

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

Expand Down Expand Up @@ -136,7 +140,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
59 changes: 33 additions & 26 deletions test/session-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,44 +78,51 @@ describe('SessionFactory', () => {
});

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

Check failure on line 81 in test/session-factory.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `⏎`
it('should create a SessionPool object', () => {
assert(sessionFactory.pool_ instanceof SessionPool);
});
before(() => {
process.env.GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS = 'false';
});

it('should accept a custom Pool class', () => {
function FakePool() {}
FakePool.prototype.on = util.noop;
FakePool.prototype.open = util.noop;
it('should create a SessionPool object', () => {
assert(sessionFactory.pool_ instanceof SessionPool);
});

const sessionFactory = new SessionFactory(
DATABASE,
NAME,
FakePool as {} as db.SessionPoolConstructor
);
assert(sessionFactory.pool_ instanceof FakePool);
});
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);
});

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);
});

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

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
19 changes: 10 additions & 9 deletions test/spanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5078,21 +5078,22 @@ describe('Spanner with mock server', () => {
});

describe('session-factory', () => {

Check failure on line 5081 in test/spanner.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `⏎`
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) => {

Check failure on line 5086 in test/spanner.ts

View workflow job for this annotation

GitHub Actions / lint

Replace `(done)·=>·{⏎` with `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();
});

Check failure on line 5095 in test/spanner.ts

View workflow job for this annotation

GitHub Actions / lint

Delete `⏎`

});
});
});
Expand Down

0 comments on commit 689c26e

Please sign in to comment.