Skip to content
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 packages/core/src/lib/stream_manager/stream_manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ describe("StreamManager", () => {
} as any as Libp2pComponents);
});

afterEach(() => {
sinon.restore();
});

it("should return usable stream attached to connection", async () => {
for (const writeStatus of ["ready", "writing"]) {
const con1 = createMockConnection();
Expand Down
5 changes: 5 additions & 0 deletions packages/sdk/src/light_push/light_push.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Libp2p, LightPushError, LightPushStatusCode } from "@waku/interfaces";
import { createRoutingInfo } from "@waku/utils";
import { utf8ToBytes } from "@waku/utils/bytes";
import { expect } from "chai";
import { afterEach } from "mocha";
import sinon, { SinonSpy } from "sinon";

import { PeerManager } from "../peer_manager/index.js";
Expand Down Expand Up @@ -38,6 +39,10 @@ describe("LightPush SDK", () => {
lightPush = mockLightPush({ libp2p });
});

afterEach(() => {
sinon.restore();
});

it("should fail to send if no connected peers found", async () => {
const result = await lightPush.send(encoder, {
payload: utf8ToBytes("test")
Expand Down
4 changes: 3 additions & 1 deletion packages/sdk/src/light_push/retry_manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ describe("RetryManager", () => {
sinon.restore();
});

it("should start and stop interval correctly", () => {
// TODO: Skipped because the global state is not being restored and it breaks
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why so? test is passing on master

Copy link
Collaborator Author

@fryorcraken fryorcraken Nov 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My new tests aren't yet on master and they don't pass with this enabled because setInterval does not work any more once with the code in this test.

Tests on master aren't passing either: #2648

I have spent half a day on this and I can guarantee that the code in this test pollutes the global state. It is not being restored (at least with node 22.17.0, I can try with other node versions in a couple of weeks).

// tests of functionalities that rely on intervals
it.skip("should start and stop interval correctly", () => {
const setIntervalSpy = sinon.spy(global, "setInterval");
const clearIntervalSpy = sinon.spy(global, "clearInterval");

Expand Down
6 changes: 6 additions & 0 deletions packages/sdk/src/query_on_connect/query_on_connect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import { delay } from "@waku/utils";
import { utf8ToBytes } from "@waku/utils/bytes";
import { expect } from "chai";
import { afterEach } from "mocha";
import sinon from "sinon";

import {
Expand Down Expand Up @@ -91,6 +92,10 @@ describe("QueryOnConnect", () => {
};
});

afterEach(() => {
sinon.restore();
});

describe("constructor", () => {
it("should create QueryOnConnect instance with all required parameters", () => {
queryOnConnect = new QueryOnConnect(
Expand Down Expand Up @@ -337,6 +342,7 @@ describe("QueryOnConnect", () => {
});

afterEach(() => {
sinon.restore();
mockClock.restore();
});

Expand Down
6 changes: 6 additions & 0 deletions packages/sdk/src/reliable_channel/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
export { ReliableChannel, ReliableChannelOptions } from "./reliable_channel.js";
export { ReliableChannelEvents, ReliableChannelEvent } from "./events.js";
export {
StatusEvent,
StatusEvents,
StatusDetail,
ISyncStatusEvents
} from "./sync_status.js";
67 changes: 67 additions & 0 deletions packages/sdk/src/reliable_channel/random_timeout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { Logger } from "@waku/utils";

const log = new Logger("sdk:random-timeout");

/**
* Enables waiting a random time before doing an action (using `setTimeout`),
* with possibility to apply a multiplier to manipulate said time.
*/
export class RandomTimeout {
private timeout: ReturnType<typeof setTimeout> | undefined;

public constructor(
/**
* The maximum interval one would wait before the call is made, in milliseconds.
*/
private maxIntervalMs: number,
/**
* When not zero: Anytime a call is made, then a new call will be rescheduled
* using this multiplier
*/
private multiplierOnCall: number,
/**
* The function to call when the timer is reached
*/
private callback: () => void | Promise<void>
) {
if (!Number.isFinite(maxIntervalMs) || maxIntervalMs < 0) {
throw new Error(
`maxIntervalMs must be a non-negative finite number, got: ${maxIntervalMs}`
);
}
if (!Number.isFinite(multiplierOnCall)) {
throw new Error(
`multiplierOnCall must be a finite number, got: ${multiplierOnCall}`
);
}
}

/**
* Use to start the timer. If a timer was already set, it deletes it and
* schedule a new one.
* @param multiplier applied to [[maxIntervalMs]]
*/
public restart(multiplier: number = 1): void {
this.stop();

if (this.maxIntervalMs) {
const timeoutMs = Math.random() * this.maxIntervalMs * multiplier;

this.timeout = setTimeout(() => {
try {
void this.callback();
} catch (error) {
log.error("Error in RandomTimeout callback:", error);
}
void this.restart(this.multiplierOnCall);
}, timeoutMs);
}
}

public stop(): void {
if (this.timeout) {
clearTimeout(this.timeout);
this.timeout = undefined;
}
}
}
Loading
Loading