mirror of
https://github.com/logos-messaging/js-waku.git
synced 2026-01-05 23:33:08 +00:00
tests: use memLocalHistory for message_channel.spec.ts
This commit is contained in:
parent
581430ab43
commit
d52ea9fd2c
@ -4,6 +4,7 @@ import { expect } from "chai";
|
|||||||
import { DefaultBloomFilter } from "../bloom_filter/bloom.js";
|
import { DefaultBloomFilter } from "../bloom_filter/bloom.js";
|
||||||
|
|
||||||
import { MessageChannelEvent } from "./events.js";
|
import { MessageChannelEvent } from "./events.js";
|
||||||
|
import { MemLocalHistory } from "./mem_local_history.js";
|
||||||
import {
|
import {
|
||||||
ContentMessage,
|
ContentMessage,
|
||||||
HistoryEntry,
|
HistoryEntry,
|
||||||
@ -22,6 +23,28 @@ const callback = (_message: Message): Promise<{ success: boolean }> => {
|
|||||||
return Promise.resolve({ success: true });
|
return Promise.resolve({ success: true });
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test helper to create a MessageChannel with MemLocalHistory.
|
||||||
|
* This avoids localStorage pollution in tests and tests core functionality.
|
||||||
|
*/
|
||||||
|
const createTestChannel = (
|
||||||
|
channelId: string,
|
||||||
|
senderId: string,
|
||||||
|
options: {
|
||||||
|
causalHistorySize?: number;
|
||||||
|
possibleAcksThreshold?: number;
|
||||||
|
timeoutForLostMessagesMs?: number;
|
||||||
|
enableRepair?: boolean;
|
||||||
|
} = {}
|
||||||
|
): MessageChannel => {
|
||||||
|
return new MessageChannel(
|
||||||
|
channelId,
|
||||||
|
senderId,
|
||||||
|
options,
|
||||||
|
new MemLocalHistory()
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
const getBloomFilter = (channel: MessageChannel): DefaultBloomFilter => {
|
const getBloomFilter = (channel: MessageChannel): DefaultBloomFilter => {
|
||||||
return channel["filter"] as DefaultBloomFilter;
|
return channel["filter"] as DefaultBloomFilter;
|
||||||
};
|
};
|
||||||
@ -68,7 +91,7 @@ describe("MessageChannel", function () {
|
|||||||
|
|
||||||
describe("sending a message ", () => {
|
describe("sending a message ", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
channelA = new MessageChannel(channelId, "alice");
|
channelA = createTestChannel(channelId, "alice");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should increase lamport timestamp", async () => {
|
it("should increase lamport timestamp", async () => {
|
||||||
@ -171,8 +194,8 @@ describe("MessageChannel", function () {
|
|||||||
|
|
||||||
describe("receiving a message", () => {
|
describe("receiving a message", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
channelA = new MessageChannel(channelId, "alice");
|
channelA = createTestChannel(channelId, "alice");
|
||||||
channelB = new MessageChannel(channelId, "bob");
|
channelB = createTestChannel(channelId, "bob");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should increase lamport timestamp", async () => {
|
it("should increase lamport timestamp", async () => {
|
||||||
@ -187,8 +210,8 @@ describe("MessageChannel", function () {
|
|||||||
|
|
||||||
// TODO: test is failing in CI, investigate in https://github.com/waku-org/js-waku/issues/2648
|
// TODO: test is failing in CI, investigate in https://github.com/waku-org/js-waku/issues/2648
|
||||||
it.skip("should update lamport timestamp if greater than current timestamp and dependencies are met", async () => {
|
it.skip("should update lamport timestamp if greater than current timestamp and dependencies are met", async () => {
|
||||||
const testChannelA = new MessageChannel(channelId, "alice");
|
const testChannelA = createTestChannel(channelId, "alice");
|
||||||
const testChannelB = new MessageChannel(channelId, "bob");
|
const testChannelB = createTestChannel(channelId, "bob");
|
||||||
|
|
||||||
const timestampBefore = testChannelA["lamportTimestamp"];
|
const timestampBefore = testChannelA["lamportTimestamp"];
|
||||||
|
|
||||||
@ -452,10 +475,10 @@ describe("MessageChannel", function () {
|
|||||||
|
|
||||||
describe("reviewing ack status", () => {
|
describe("reviewing ack status", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
channelA = new MessageChannel(channelId, "alice", {
|
channelA = createTestChannel(channelId, "alice", {
|
||||||
causalHistorySize: 2
|
causalHistorySize: 2
|
||||||
});
|
});
|
||||||
channelB = new MessageChannel(channelId, "bob", { causalHistorySize: 2 });
|
channelB = createTestChannel(channelId, "bob", { causalHistorySize: 2 });
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should mark all messages in causal history as acknowledged", async () => {
|
it("should mark all messages in causal history as acknowledged", async () => {
|
||||||
@ -661,10 +684,10 @@ describe("MessageChannel", function () {
|
|||||||
|
|
||||||
describe("Sweeping incoming buffer", () => {
|
describe("Sweeping incoming buffer", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
channelA = new MessageChannel(channelId, "alice", {
|
channelA = createTestChannel(channelId, "alice", {
|
||||||
causalHistorySize: 2
|
causalHistorySize: 2
|
||||||
});
|
});
|
||||||
channelB = new MessageChannel(channelId, "bob", { causalHistorySize: 2 });
|
channelB = createTestChannel(channelId, "bob", { causalHistorySize: 2 });
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should detect messages with missing dependencies", async () => {
|
it("should detect messages with missing dependencies", async () => {
|
||||||
@ -746,7 +769,7 @@ describe("MessageChannel", function () {
|
|||||||
|
|
||||||
it("should mark a message as irretrievably lost if timeout is exceeded", async () => {
|
it("should mark a message as irretrievably lost if timeout is exceeded", async () => {
|
||||||
// Create a channel with very very short timeout
|
// Create a channel with very very short timeout
|
||||||
const channelC: MessageChannel = new MessageChannel(channelId, "carol", {
|
const channelC = createTestChannel(channelId, "carol", {
|
||||||
timeoutForLostMessagesMs: 10
|
timeoutForLostMessagesMs: 10
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -789,7 +812,7 @@ describe("MessageChannel", function () {
|
|||||||
let lostMessages: HistoryEntry[] = [];
|
let lostMessages: HistoryEntry[] = [];
|
||||||
|
|
||||||
// Create a channel with very short timeout
|
// Create a channel with very short timeout
|
||||||
const channelC: MessageChannel = new MessageChannel(channelId, "carol", {
|
const channelC = createTestChannel(channelId, "carol", {
|
||||||
timeoutForLostMessagesMs: 10
|
timeoutForLostMessagesMs: 10
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -853,7 +876,7 @@ describe("MessageChannel", function () {
|
|||||||
it("should remove messages without delivering if timeout is exceeded", async () => {
|
it("should remove messages without delivering if timeout is exceeded", async () => {
|
||||||
const causalHistorySize = channelA["causalHistorySize"];
|
const causalHistorySize = channelA["causalHistorySize"];
|
||||||
// Create a channel with very very short timeout
|
// Create a channel with very very short timeout
|
||||||
const channelC: MessageChannel = new MessageChannel(channelId, "carol", {
|
const channelC = createTestChannel(channelId, "carol", {
|
||||||
timeoutForLostMessagesMs: 10
|
timeoutForLostMessagesMs: 10
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -1043,10 +1066,10 @@ describe("MessageChannel", function () {
|
|||||||
|
|
||||||
describe("Sweeping outgoing buffer", () => {
|
describe("Sweeping outgoing buffer", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
channelA = new MessageChannel(channelId, "alice", {
|
channelA = createTestChannel(channelId, "alice", {
|
||||||
causalHistorySize: 2
|
causalHistorySize: 2
|
||||||
});
|
});
|
||||||
channelB = new MessageChannel(channelId, "bob", { causalHistorySize: 2 });
|
channelB = createTestChannel(channelId, "bob", { causalHistorySize: 2 });
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should partition messages based on acknowledgement status", async () => {
|
it("should partition messages based on acknowledgement status", async () => {
|
||||||
@ -1088,10 +1111,10 @@ describe("MessageChannel", function () {
|
|||||||
|
|
||||||
describe("Sync messages", () => {
|
describe("Sync messages", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
channelA = new MessageChannel(channelId, "alice", {
|
channelA = createTestChannel(channelId, "alice", {
|
||||||
causalHistorySize: 2
|
causalHistorySize: 2
|
||||||
});
|
});
|
||||||
channelB = new MessageChannel(channelId, "bob", { causalHistorySize: 2 });
|
channelB = createTestChannel(channelId, "bob", { causalHistorySize: 2 });
|
||||||
const message = utf8ToBytes("first message in channel");
|
const message = utf8ToBytes("first message in channel");
|
||||||
channelA["localHistory"].push(
|
channelA["localHistory"].push(
|
||||||
new ContentMessage(
|
new ContentMessage(
|
||||||
@ -1115,7 +1138,7 @@ describe("MessageChannel", function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("should not be sent when there is no history", async () => {
|
it("should not be sent when there is no history", async () => {
|
||||||
const channelC = new MessageChannel(channelId, "carol", {
|
const channelC = createTestChannel(channelId, "carol", {
|
||||||
causalHistorySize: 2
|
causalHistorySize: 2
|
||||||
});
|
});
|
||||||
const res = await channelC.pushOutgoingSyncMessage(async (_msg) => {
|
const res = await channelC.pushOutgoingSyncMessage(async (_msg) => {
|
||||||
@ -1160,7 +1183,7 @@ describe("MessageChannel", function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("should update ack status of messages in outgoing buffer", async () => {
|
it("should update ack status of messages in outgoing buffer", async () => {
|
||||||
const channelC = new MessageChannel(channelId, "carol", {
|
const channelC = createTestChannel(channelId, "carol", {
|
||||||
causalHistorySize: 2
|
causalHistorySize: 2
|
||||||
});
|
});
|
||||||
for (const m of messagesA) {
|
for (const m of messagesA) {
|
||||||
@ -1185,7 +1208,7 @@ describe("MessageChannel", function () {
|
|||||||
|
|
||||||
describe("Ephemeral messages", () => {
|
describe("Ephemeral messages", () => {
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
channelA = new MessageChannel(channelId, "alice");
|
channelA = createTestChannel(channelId, "alice");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should be sent without a timestamp, causal history, or bloom filter", async () => {
|
it("should be sent without a timestamp, causal history, or bloom filter", async () => {
|
||||||
@ -1208,7 +1231,7 @@ describe("MessageChannel", function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("should be delivered immediately if received", async () => {
|
it("should be delivered immediately if received", async () => {
|
||||||
const channelB = new MessageChannel(channelId, "bob");
|
const channelB = createTestChannel(channelId, "bob");
|
||||||
|
|
||||||
// Track initial state
|
// Track initial state
|
||||||
const localHistoryBefore = channelB["localHistory"].length;
|
const localHistoryBefore = channelB["localHistory"].length;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user