From 83a64d5f593ecabd6e7d63cbd2aae5ae0369cc8c Mon Sep 17 00:00:00 2001 From: Jessy Ssebuliba Date: Tue, 15 Oct 2024 13:28:07 +0300 Subject: [PATCH 1/2] unsubscribe should prevent retries Signed-off-by: Jessy Ssebuliba --- src/topic/SubscriptionHandle.js | 4 ++++ src/topic/TopicMessageQuery.js | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/src/topic/SubscriptionHandle.js b/src/topic/SubscriptionHandle.js index ed4b37813..c3032acbe 100644 --- a/src/topic/SubscriptionHandle.js +++ b/src/topic/SubscriptionHandle.js @@ -22,6 +22,9 @@ export default class SubscriptionHandle { constructor() { /** @type {{(): void} | null} */ this._call = null; + + /** @type {boolean} */ + this._unsubscribed = false; } /** @@ -34,6 +37,7 @@ export default class SubscriptionHandle { unsubscribe() { if (this._call != null) { + this._unsubscribed = true; this._call(); } } diff --git a/src/topic/TopicMessageQuery.js b/src/topic/TopicMessageQuery.js index cc681c975..c36238980 100644 --- a/src/topic/TopicMessageQuery.js +++ b/src/topic/TopicMessageQuery.js @@ -438,6 +438,10 @@ export default class TopicMessageQuery extends Query { const message = error instanceof Error ? error.message : error.details; + if (this._handle?._unsubscribed) { + return; + } + if ( this._attempt < this._maxAttempts && this._retryHandler(error) From d74190bce447b483a1bdee98e0d2d09a4bf676a9 Mon Sep 17 00:00:00 2001 From: Jessy Ssebuliba Date: Wed, 16 Oct 2024 12:29:50 +0300 Subject: [PATCH 2/2] dd unit test Signed-off-by: Jessy Ssebuliba --- test/unit/TopicMessageMocking.js | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/test/unit/TopicMessageMocking.js b/test/unit/TopicMessageMocking.js index 0126c2fd6..533ab9a43 100644 --- a/test/unit/TopicMessageMocking.js +++ b/test/unit/TopicMessageMocking.js @@ -58,4 +58,30 @@ describe("TopicMessageMocking", function () { expect(finished).to.be.true; }); + + it("should stop processing messages after unsubscription", async function () { + ({ client, servers } = await Mocker.withResponses([ + [{ response: TOPIC_MESSAGE }], + [{ response: TOPIC_MESSAGE }], + ])); + + let messageProcessed = false; + + handle = new TopicMessageQuery() + .setTopicId("0.0.3") + .subscribe(client, () => { + messageProcessed = true; + }); + + handle.unsubscribe(); + + const startTime = Date.now(); + + while (!messageProcessed && Date.now() < startTime + 5000) { + await new Promise((resolved) => setTimeout(resolved, 2000)); + } + + expect(messageProcessed).to.be.false; + }); + });