Skip to content

Commit c086958

Browse files
committed
1 parent 28288a5 commit c086958

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

src/ic-websocket.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { CallRequest, Cbor } from "@dfinity/agent";
55
import { IDL } from "@dfinity/candid";
66
import { Principal } from "@dfinity/principal";
77

8-
import IcWebSocket, { MAX_ALLOWED_NETWORK_LATENCY_MS, createWsConfig } from "./ic-websocket";
8+
import IcWebSocket, { COMMUNICATION_LATENCY_BOUND_MS, createWsConfig } from "./ic-websocket";
99
import { generateRandomIdentity } from "./identity";
1010
import {
1111
CanisterWsMessageArguments,
@@ -241,7 +241,7 @@ describe("IcWebsocket class", () => {
241241
mockWsServer.send(encodeHandshakeMessage(VALID_HANDSHAKE_MESSAGE_FROM_GATEWAY));
242242

243243
// advance the open timeout
244-
await jest.advanceTimersByTimeAsync(2 * MAX_ALLOWED_NETWORK_LATENCY_MS);
244+
await jest.advanceTimersByTimeAsync(2 * COMMUNICATION_LATENCY_BOUND_MS);
245245

246246
expect(icWs["_isConnectionEstablished"]).toEqual(false);
247247
expect(onOpen).not.toHaveBeenCalled();
@@ -313,15 +313,15 @@ describe("IcWebsocket class", () => {
313313
await sleep(100);
314314

315315
// wait for the open timeout so that it expires
316-
await sleep(2 * MAX_ALLOWED_NETWORK_LATENCY_MS);
316+
await sleep(2 * COMMUNICATION_LATENCY_BOUND_MS);
317317

318318
expect(onOpen).toHaveBeenCalled();
319319
expect(icWs["_isConnectionEstablished"]).toEqual(true);
320320
expect(onError).not.toHaveBeenCalled();
321321
expect(icWs.readyState).toEqual(WebSocket.OPEN);
322322
// make sure onmessage callback is not called when receiving the first message
323323
expect(onMessage).not.toHaveBeenCalled();
324-
}, 3 * MAX_ALLOWED_NETWORK_LATENCY_MS);
324+
}, 3 * COMMUNICATION_LATENCY_BOUND_MS);
325325

326326
it("onmessage is called when a valid message is received", async () => {
327327
const onMessage = jest.fn();
@@ -563,7 +563,7 @@ describe("Messages acknowledgement", () => {
563563
await mockWsServer.nextMessage;
564564

565565
// make the ack timeout expire
566-
await jest.advanceTimersByTimeAsync(ackMessageIntervalMs + MAX_ALLOWED_NETWORK_LATENCY_MS);
566+
await jest.advanceTimersByTimeAsync(ackMessageIntervalMs + COMMUNICATION_LATENCY_BOUND_MS);
567567

568568
const ackTimeoutError = new Error(`Ack message timeout. Not received ack for sequence numbers: ${[BigInt(1)]}`);
569569
expect(onError).toHaveBeenCalledWith(new ErrorEvent("error", { error: ackTimeoutError }));
@@ -610,7 +610,7 @@ describe("Messages acknowledgement", () => {
610610
await mockWsServer.nextMessage;
611611

612612
// make the ack timeout expire
613-
await jest.advanceTimersByTimeAsync(ackMessageIntervalMs + MAX_ALLOWED_NETWORK_LATENCY_MS);
613+
await jest.advanceTimersByTimeAsync(ackMessageIntervalMs + COMMUNICATION_LATENCY_BOUND_MS);
614614

615615
// send the ack message from the canister
616616
// when the ack timeout is already expired
@@ -667,7 +667,7 @@ describe("Messages acknowledgement", () => {
667667
console.log("sent ack message from canister");
668668

669669
// make the ack timeout expire
670-
await jest.advanceTimersByTimeAsync(ackMessageIntervalMs + MAX_ALLOWED_NETWORK_LATENCY_MS);
670+
await jest.advanceTimersByTimeAsync(ackMessageIntervalMs + COMMUNICATION_LATENCY_BOUND_MS);
671671

672672
// first message has been acknowledged correctly,
673673
// as the error only reports the missing ack for the keep alive response

src/ic-websocket.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ import { WsAgent } from "./agent";
4141
*/
4242
const DEFAULT_ACK_MESSAGE_INTERVAL_MS = 300_000;
4343
/**
44-
* The maximum latency allowed between the client and the canister.
44+
* The maximum communication latency allowed between the client and the canister (same as in the canister).
4545
*
4646
* Used to determine the ack message timeout.
4747
*/
48-
export const MAX_ALLOWED_NETWORK_LATENCY_MS = 30_000;
48+
export const COMMUNICATION_LATENCY_BOUND_MS = 30_000;
4949

5050
/**
5151
* Interface to create a new IcWebSocketConfig. For a simple configuration, use {@link createWsConfig}.
@@ -181,7 +181,7 @@ export default class IcWebSocket<
181181
});
182182

183183
this._ackMessagesQueue = new AckMessagesQueue({
184-
expirationMs: (config.ackMessageIntervalMs || DEFAULT_ACK_MESSAGE_INTERVAL_MS) + MAX_ALLOWED_NETWORK_LATENCY_MS,
184+
expirationMs: (config.ackMessageIntervalMs || DEFAULT_ACK_MESSAGE_INTERVAL_MS) + COMMUNICATION_LATENCY_BOUND_MS,
185185
timeoutExpiredCallback: this._onAckMessageTimeout.bind(this),
186186
});
187187

@@ -244,7 +244,7 @@ export default class IcWebSocket<
244244
}
245245

246246
this._openTimeout = null;
247-
}, 2 * MAX_ALLOWED_NETWORK_LATENCY_MS);
247+
}, 2 * COMMUNICATION_LATENCY_BOUND_MS);
248248
}
249249

250250
private _cancelOpenTimeout() {

0 commit comments

Comments
 (0)