From bd9cb2f851cfae9ff442d88641108bcc2edc41e1 Mon Sep 17 00:00:00 2001 From: Alka Trivedi Date: Thu, 26 Dec 2024 11:18:31 +0530 Subject: [PATCH] refactor --- src/session-factory.ts | 2 +- test/spanner.ts | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/session-factory.ts b/src/session-factory.ts index 812a32ff8..14c7af571 100644 --- a/src/session-factory.ts +++ b/src/session-factory.ts @@ -107,7 +107,7 @@ export class SessionFactory process.env.GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS === 'true' ? this.multiplexedSession_ : this.pool_; - sessionHandler?.getSession((err, session) => callback(err, session)); + sessionHandler!.getSession((err, session) => callback(err, session)); } /** diff --git a/test/spanner.ts b/test/spanner.ts index dcc8a3300..aca33a9a3 100644 --- a/test/spanner.ts +++ b/test/spanner.ts @@ -66,6 +66,7 @@ import protobuf = google.spanner.v1; import Priority = google.spanner.v1.RequestOptions.Priority; import TypeCode = google.spanner.v1.TypeCode; import NullValue = google.protobuf.NullValue; +import {SessionFactory} from '../src/session-factory'; const { AlwaysOnSampler, @@ -5075,6 +5076,25 @@ describe('Spanner with mock server', () => { done(); }); }); + + describe('session-factory', () => { + after(() => { + process.env.GOOGLE_CLOUD_SPANNER_MULTIPLEXED_SESSIONS = 'false'; + }); + it('should execute table mutations without leaking sessions', () => { + const database = newTestDatabase(); + 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')" + ); + } + }); + }); }); function executeSimpleUpdate(