Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(fix) TopicMessageQuery#unsubscribe() attempts to re-subscribe #2582

Merged
merged 2 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/topic/SubscriptionHandle.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ export default class SubscriptionHandle {
constructor() {
/** @type {{(): void} | null} */
this._call = null;

/** @type {boolean} */
this._unsubscribed = false;
}

/**
Expand All @@ -34,6 +37,7 @@ export default class SubscriptionHandle {

unsubscribe() {
if (this._call != null) {
this._unsubscribed = true;
this._call();
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/topic/TopicMessageQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,10 @@
const message =
error instanceof Error ? error.message : error.details;

if (this._handle?._unsubscribed) {
return;
}

Check warning on line 444 in src/topic/TopicMessageQuery.js

View check run for this annotation

Codecov / codecov/patch

src/topic/TopicMessageQuery.js#L444

Added line #L444 was not covered by tests
if (
this._attempt < this._maxAttempts &&
this._retryHandler(error)
Expand Down
26 changes: 26 additions & 0 deletions test/unit/TopicMessageMocking.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});

});