diff --git a/sdk/servicebus/service-bus/src/session/messageSession.ts b/sdk/servicebus/service-bus/src/session/messageSession.ts index 0b11492d4989..aa0ba5364eb8 100644 --- a/sdk/servicebus/service-bus/src/session/messageSession.ts +++ b/sdk/servicebus/service-bus/src/session/messageSession.ts @@ -260,7 +260,7 @@ export class MessageSession extends LinkEntity { this._intermediateLink = link; const receivedSessionId = link.source?.filter?.[Constants.sessionFilterName]; - if (!this._providedSessionId && !receivedSessionId) { + if (this._providedSessionId === undefined && receivedSessionId === undefined) { // When we ask for any sessions (passing option of session-filter: undefined), // but don't receive one back, check whether service has sent any error. if ( @@ -281,7 +281,10 @@ export class MessageSession extends LinkEntity { // Ideally this code path should never be reached as `MessageSession.createReceiver()` should fail instead // TODO: https://github.com/Azure/azure-sdk-for-js/issues/9775 to figure out why this code path indeed gets hit. errorMessage = `Failed to create a receiver. No unlocked sessions available.`; - } else if (this._providedSessionId && receivedSessionId !== this._providedSessionId) { + } else if ( + this._providedSessionId !== undefined && + receivedSessionId !== this._providedSessionId + ) { // This code path is reached if the session is already locked by another receiver. // TODO: Check why the service would not throw an error or just timeout instead of giving a misleading successful receiver errorMessage = `Failed to create a receiver for the requested session '${this._providedSessionId}'. It may be locked by another receiver.`; @@ -321,7 +324,7 @@ export class MessageSession extends LinkEntity { const receivedSessionId = this.link.source?.filter?.[Constants.sessionFilterName]; - if (!this._providedSessionId) this.sessionId = receivedSessionId; + if (this._providedSessionId === undefined) this.sessionId = receivedSessionId; this.sessionLockedUntilUtc = convertTicksToDate( this.link.properties["com.microsoft:locked-until-utc"], ); @@ -347,7 +350,7 @@ export class MessageSession extends LinkEntity { // Fix the unhelpful error messages for the OperationTimeoutError that comes from `rhea-promise`. if ((errObj as MessagingError).code === "OperationTimeoutError") { - if (this._providedSessionId) { + if (this._providedSessionId !== undefined) { errObj.message = `Failed to create a receiver for the requested session '${this._providedSessionId}' within allocated time and retry attempts.`; } else { errObj.message = "Failed to create a receiver within allocated time and retry attempts."; diff --git a/sdk/servicebus/service-bus/test/public/sessionsTests.spec.ts b/sdk/servicebus/service-bus/test/public/sessionsTests.spec.ts index 54e132f5dec5..f2db101c14fa 100644 --- a/sdk/servicebus/service-bus/test/public/sessionsTests.spec.ts +++ b/sdk/servicebus/service-bus/test/public/sessionsTests.spec.ts @@ -300,6 +300,25 @@ describe("session tests", () => { err.name.should.equal("AbortError"); } }); + + ["acceptSession", "acceptNextSession"].forEach((method) => { + it(`${method} on session id of empty string`, async () => { + serviceBusClient = createServiceBusClientForTests(); + const entityNames = await serviceBusClient.test.createTestEntities(testClientType); + const testMessage = TestMessage.getSessionSample(); + testMessage.sessionId = ""; + sender = serviceBusClient.test.addToCleanup( + serviceBusClient.createSender(entityNames.queue ?? entityNames.topic!), + ); + await sender.sendMessages(testMessage); + receiver = + method === "acceptSession" + ? await serviceBusClient.test.acceptSessionWithPeekLock(entityNames, "") + : await serviceBusClient.test.acceptNextSessionWithPeekLock(entityNames); + const msgs = await receiver.receiveMessages(10); + should.equal(msgs.length, 1, "Unexpected number of messages received"); + }); + }); }); });