mirror of
https://github.com/waku-org/js-waku.git
synced 2025-02-23 09:38:24 +00:00
updates v2 tests
This commit is contained in:
parent
833b910915
commit
a07437d81e
@ -21,7 +21,7 @@ const TestEncoder = createEncoder({ contentTopic: TestContentTopic });
|
|||||||
const TestDecoder = createDecoder(TestContentTopic);
|
const TestDecoder = createDecoder(TestContentTopic);
|
||||||
|
|
||||||
describe("Waku Filter: V2", () => {
|
describe("Waku Filter: V2", () => {
|
||||||
let waku: LightNode;
|
let waku: LightNode<true>;
|
||||||
let nwaku: Nwaku;
|
let nwaku: Nwaku;
|
||||||
|
|
||||||
afterEach(async function () {
|
afterEach(async function () {
|
||||||
@ -67,9 +67,31 @@ describe("Waku Filter: V2", () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
await waku.filter.subscribe([TestDecoder], callback);
|
await waku.filter.subscribe([TestDecoder], callback);
|
||||||
// As the filter protocol does not cater for an ack of subscription
|
|
||||||
// we cannot know whether the subscription happened. Something we want to
|
await waku.lightPush.send(TestEncoder, message);
|
||||||
// correct in future versions of the protocol.
|
while (messageCount === 0) {
|
||||||
|
await delay(250);
|
||||||
|
}
|
||||||
|
expect(messageCount).to.eq(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("modifies subscription", async function () {
|
||||||
|
this.timeout(10000);
|
||||||
|
|
||||||
|
let messageCount = 0;
|
||||||
|
const messageText = "Filtering works!";
|
||||||
|
const message = { payload: utf8ToBytes(messageText) };
|
||||||
|
|
||||||
|
const callback = (msg: DecodedMessage): void => {
|
||||||
|
log("Got a message");
|
||||||
|
messageCount++;
|
||||||
|
expect(msg.contentTopic).to.eq(TestContentTopic);
|
||||||
|
expect(msg.pubSubTopic).to.eq(DefaultPubSubTopic);
|
||||||
|
expect(bytesToUtf8(msg.payload)).to.eq(messageText);
|
||||||
|
};
|
||||||
|
|
||||||
|
await waku.filter.subscribe([TestDecoder], callback);
|
||||||
|
|
||||||
await delay(200);
|
await delay(200);
|
||||||
|
|
||||||
await waku.lightPush.send(TestEncoder, message);
|
await waku.lightPush.send(TestEncoder, message);
|
||||||
@ -77,6 +99,30 @@ describe("Waku Filter: V2", () => {
|
|||||||
await delay(250);
|
await delay(250);
|
||||||
}
|
}
|
||||||
expect(messageCount).to.eq(1);
|
expect(messageCount).to.eq(1);
|
||||||
|
|
||||||
|
// Modify subscription
|
||||||
|
messageCount = 0;
|
||||||
|
const newMessageText = "Filtering still works!";
|
||||||
|
const newMessage = { payload: utf8ToBytes(newMessageText) };
|
||||||
|
|
||||||
|
const newContentTopic = "/test/2/waku-filter";
|
||||||
|
const newEncoder = createEncoder({ contentTopic: newContentTopic });
|
||||||
|
const newDecoder = createDecoder(newContentTopic);
|
||||||
|
const newCallback = (msg: DecodedMessage): void => {
|
||||||
|
log("Got a message");
|
||||||
|
messageCount++;
|
||||||
|
expect(msg.contentTopic).to.eq(newContentTopic);
|
||||||
|
expect(msg.pubSubTopic).to.eq(DefaultPubSubTopic);
|
||||||
|
expect(bytesToUtf8(msg.payload)).to.eq(newMessageText);
|
||||||
|
};
|
||||||
|
|
||||||
|
await waku.filter.subscribe([newDecoder], newCallback);
|
||||||
|
|
||||||
|
await waku.lightPush.send(newEncoder, newMessage);
|
||||||
|
while (messageCount === 0) {
|
||||||
|
await delay(250);
|
||||||
|
}
|
||||||
|
expect(messageCount).to.eq(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("handles multiple messages", async function () {
|
it("handles multiple messages", async function () {
|
||||||
@ -89,7 +135,6 @@ describe("Waku Filter: V2", () => {
|
|||||||
};
|
};
|
||||||
await waku.filter.subscribe(TestDecoder, callback);
|
await waku.filter.subscribe(TestDecoder, callback);
|
||||||
|
|
||||||
await delay(200);
|
|
||||||
await waku.lightPush.send(TestEncoder, {
|
await waku.lightPush.send(TestEncoder, {
|
||||||
payload: utf8ToBytes("Filtering works!"),
|
payload: utf8ToBytes("Filtering works!"),
|
||||||
});
|
});
|
||||||
@ -115,7 +160,46 @@ describe("Waku Filter: V2", () => {
|
|||||||
});
|
});
|
||||||
await delay(100);
|
await delay(100);
|
||||||
await unsubscribe();
|
await unsubscribe();
|
||||||
|
await waku.lightPush.send(TestEncoder, {
|
||||||
|
payload: utf8ToBytes("This should not be received"),
|
||||||
|
});
|
||||||
|
await delay(100);
|
||||||
|
expect(messageCount).to.eq(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("tests ping", async function () {
|
||||||
|
let messageCount = 0;
|
||||||
|
const callback = (): void => {
|
||||||
|
messageCount++;
|
||||||
|
};
|
||||||
|
await waku.filter.subscribe([TestDecoder], callback);
|
||||||
|
|
||||||
await delay(200);
|
await delay(200);
|
||||||
|
await waku.lightPush.send(TestEncoder, {
|
||||||
|
payload: utf8ToBytes("This should be received"),
|
||||||
|
});
|
||||||
|
await delay(100);
|
||||||
|
await waku.filter.ping();
|
||||||
|
await waku.lightPush.send(TestEncoder, {
|
||||||
|
payload: utf8ToBytes("This should be received"),
|
||||||
|
});
|
||||||
|
await delay(100);
|
||||||
|
expect(messageCount).to.eq(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("unsubscribes all", async function () {
|
||||||
|
let messageCount = 0;
|
||||||
|
const callback = (): void => {
|
||||||
|
messageCount++;
|
||||||
|
};
|
||||||
|
await waku.filter.subscribe([TestDecoder], callback);
|
||||||
|
|
||||||
|
await delay(200);
|
||||||
|
await waku.lightPush.send(TestEncoder, {
|
||||||
|
payload: utf8ToBytes("This should be received"),
|
||||||
|
});
|
||||||
|
await delay(100);
|
||||||
|
await waku.filter.unsubscribeAll();
|
||||||
await waku.lightPush.send(TestEncoder, {
|
await waku.lightPush.send(TestEncoder, {
|
||||||
payload: utf8ToBytes("This should not be received"),
|
payload: utf8ToBytes("This should not be received"),
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user