Move more logic into sync-client
This commit is contained in:
parent
3f089bd37e
commit
9177984ff6
20 changed files with 68 additions and 143 deletions
|
|
@ -2,12 +2,10 @@ import { choose } from "../utils/choose";
|
|||
import { v4 as uuidv4 } from "uuid";
|
||||
import { assert } from "../utils/assert";
|
||||
import type { RelativePath, SyncSettings } from "sync-client";
|
||||
import { Logger, LogLevel } from "sync-client";
|
||||
import { debugging, Logger, LogLevel } from "sync-client";
|
||||
import { MockClient } from "./mock-client";
|
||||
import { sleep } from "../utils/sleep";
|
||||
import type { LogLine } from "sync-client/dist/types/tracing/logger";
|
||||
import { flakyFetchFactory } from "../utils/flaky-fetch";
|
||||
import { flakyWebSocketFactory } from "../utils/flaky-websocket-factory";
|
||||
|
||||
export class MockAgent extends MockClient {
|
||||
private readonly writtenContents: string[] = [];
|
||||
|
|
@ -28,8 +26,8 @@ export class MockAgent extends MockClient {
|
|||
|
||||
public async init(): Promise<void> {
|
||||
await super.init(
|
||||
flakyFetchFactory(this.jitterScaleInSeconds),
|
||||
flakyWebSocketFactory(
|
||||
debugging.slowFetchFactory(this.jitterScaleInSeconds),
|
||||
debugging.slowWebSocketFactory(
|
||||
this.jitterScaleInSeconds,
|
||||
new Logger() // this logger isn't wired anywhere, so messages to it will be ignored
|
||||
)
|
||||
|
|
|
|||
|
|
@ -1,20 +0,0 @@
|
|||
import { sleep } from "./sleep";
|
||||
|
||||
export const flakyFetchFactory =
|
||||
(jitterScaleInSeconds: number) =>
|
||||
async (
|
||||
input: string | URL | globalThis.Request,
|
||||
init?: RequestInit
|
||||
): Promise<Response> => {
|
||||
if (jitterScaleInSeconds > 0) {
|
||||
await sleep(Math.random() * jitterScaleInSeconds * 1000);
|
||||
}
|
||||
|
||||
const response = await fetch(input, init);
|
||||
|
||||
if (jitterScaleInSeconds > 0) {
|
||||
await sleep(Math.random() * jitterScaleInSeconds * 1000);
|
||||
}
|
||||
|
||||
return response;
|
||||
};
|
||||
|
|
@ -1,82 +0,0 @@
|
|||
import type { Logger } from "sync-client";
|
||||
import { helpers } from "sync-client";
|
||||
import { sleep } from "./sleep";
|
||||
|
||||
export function flakyWebSocketFactory(
|
||||
jitterScaleInSeconds: number,
|
||||
logger: Logger
|
||||
): typeof WebSocket {
|
||||
// eslint-disable-next-line
|
||||
return class FlakyWebSocket extends WebSocket {
|
||||
private static readonly RECEIVE_KEY = "websocket-receive";
|
||||
private static readonly SEND_KEY = "websocket-send";
|
||||
|
||||
private readonly locks = new helpers.Locks(logger);
|
||||
|
||||
public set onopen(callback: (event: Event) => void) {
|
||||
super.onopen = async (event: Event): Promise<void> => {
|
||||
if (jitterScaleInSeconds > 0) {
|
||||
await sleep(Math.random() * jitterScaleInSeconds * 1000);
|
||||
}
|
||||
|
||||
callback(event);
|
||||
};
|
||||
}
|
||||
|
||||
public set onmessage(callback: (event: MessageEvent) => void) {
|
||||
super.onmessage = async (event: MessageEvent): Promise<void> => {
|
||||
return this.locks.withLock(
|
||||
FlakyWebSocket.RECEIVE_KEY,
|
||||
async () => {
|
||||
if (jitterScaleInSeconds > 0) {
|
||||
await sleep(
|
||||
Math.random() * jitterScaleInSeconds * 1000
|
||||
);
|
||||
}
|
||||
|
||||
callback(event);
|
||||
}
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
public set onclose(callback: (event: CloseEvent) => void) {
|
||||
super.onclose = async (event: CloseEvent): Promise<void> => {
|
||||
if (jitterScaleInSeconds > 0) {
|
||||
await sleep(Math.random() * jitterScaleInSeconds * 1000);
|
||||
}
|
||||
callback(event);
|
||||
};
|
||||
}
|
||||
|
||||
public set onerror(callback: (event: Event) => void) {
|
||||
super.onerror = async (event: Event): Promise<void> => {
|
||||
if (jitterScaleInSeconds > 0) {
|
||||
await sleep(Math.random() * jitterScaleInSeconds * 1000);
|
||||
}
|
||||
callback(event);
|
||||
};
|
||||
}
|
||||
|
||||
public send(
|
||||
data: string | ArrayBufferLike | Blob | ArrayBufferView
|
||||
): void {
|
||||
this.waitingSend(data).catch((error: unknown) => {
|
||||
logger.error(`Error sending WebSocket message: ${error}`);
|
||||
});
|
||||
}
|
||||
|
||||
private async waitingSend(
|
||||
data: string | ArrayBufferLike | Blob | ArrayBufferView
|
||||
): Promise<void> {
|
||||
// maintain message order
|
||||
return this.locks.withLock(FlakyWebSocket.SEND_KEY, async () => {
|
||||
if (jitterScaleInSeconds > 0) {
|
||||
await sleep(Math.random() * jitterScaleInSeconds * 1000);
|
||||
}
|
||||
|
||||
super.send(data);
|
||||
});
|
||||
}
|
||||
} as unknown as typeof WebSocket;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue