fix: filter subscription with `pubsubTopic1` and decoder with `pubsubTopic2` (#1675)

* add a test for a failing case

* add error handling & update test

* remove only

* rename test
This commit is contained in:
Danish Arora 2023-10-23 14:17:02 +05:30 committed by GitHub
parent 81b2bf46ab
commit 491366bd4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 0 deletions

View File

@ -74,6 +74,16 @@ class Subscription {
callback: Callback<T>
): Promise<void> {
const decodersArray = Array.isArray(decoders) ? decoders : [decoders];
// check that all decoders are configured for the same pubsub topic as this subscription
decodersArray.forEach((decoder) => {
if (decoder.pubsubTopic !== this.pubsubTopic) {
throw new Error(
`Pubsub topic not configured: decoder is configured for pubsub topic ${decoder.pubsubTopic} but this subscription is for pubsub topic ${this.pubsubTopic}. Please create a new Subscription for the different pubsub topic.`
);
}
});
const decodersGroupedByCT = groupByContentTopic(decodersArray);
const contentTopics = Array.from(decodersGroupedByCT.keys());

View File

@ -146,4 +146,15 @@ describe("Waku Filter V2: Multiple PubSubtopics", function () {
expectedMessageText: "M2"
});
});
it("Should fail to subscribe with decoder with wrong pubsubTopic", async function () {
// this subscription object is set up with the `customPubsubTopic` but we're passing it a Decoder with the `DefaultPubsubTopic`
try {
await subscription.subscribe([TestDecoder], messageCollector.callback);
} catch (error) {
expect((error as Error).message).to.include(
"Pubsub topic not configured"
);
}
});
});