From 47231fce1020ac28965a18a213486e0125452c8f Mon Sep 17 00:00:00 2001 From: Joe Clark Date: Fri, 23 Aug 2024 10:38:43 +0100 Subject: [PATCH] mock: allow a delay on socket messages --- packages/lightning-mock/src/api-sockets.ts | 4 ++- packages/lightning-mock/src/server.ts | 5 ++- packages/lightning-mock/src/socket-server.ts | 8 +++-- .../lightning-mock/test/socket-server.test.ts | 36 +++++++++++++++++++ 4 files changed, 49 insertions(+), 4 deletions(-) diff --git a/packages/lightning-mock/src/api-sockets.ts b/packages/lightning-mock/src/api-sockets.ts index f56d5c51d..6a4725c7d 100644 --- a/packages/lightning-mock/src/api-sockets.ts +++ b/packages/lightning-mock/src/api-sockets.ts @@ -83,7 +83,8 @@ const createSocketAPI = ( path: string, httpServer: Server, logger?: Logger, - logLevel?: LogLevel + logLevel?: LogLevel, + socketDelay = 1 ) => { // set up a websocket server to listen to connections const server = new WebSocketServer({ @@ -99,6 +100,7 @@ const createSocketAPI = ( server, state, logger: logger && createLogger('PHX', { level: logLevel }), + socketDelay: socketDelay, }); wss.registerEvents('worker:queue', { diff --git a/packages/lightning-mock/src/server.ts b/packages/lightning-mock/src/server.ts index a035d6da4..f85c34625 100644 --- a/packages/lightning-mock/src/server.ts +++ b/packages/lightning-mock/src/server.ts @@ -55,6 +55,8 @@ export type LightningOptions = { // if passed, a JWT will be included in all claim responses runPrivateKey?: string; + + socketDelay?: number; // add a delay to all web socket replies }; export type RunId = string; @@ -107,7 +109,8 @@ const createLightningServer = (options: LightningOptions = {}) => { '/worker', // TODO I should option drive this server, options.logger, - options.logLevel + options.logLevel, + options.socketDelay ); app.use(createDevAPI(app as any, state, logger, api)); diff --git a/packages/lightning-mock/src/socket-server.ts b/packages/lightning-mock/src/socket-server.ts index 86df987f3..4e21de883 100644 --- a/packages/lightning-mock/src/socket-server.ts +++ b/packages/lightning-mock/src/socket-server.ts @@ -66,6 +66,7 @@ type CreateServerOptions = { state: ServerState; logger?: Logger; onMessage?: (evt: PhoenixEvent) => void; + socketDelay?: number; }; type MockSocketServer = typeof WebSocketServer & { @@ -87,6 +88,7 @@ function createServer({ state, logger, onMessage = () => {}, + socketDelay = 1, }: CreateServerOptions) { const channels: Record> = { // create a stub listener for pheonix to prevent errors @@ -171,8 +173,10 @@ function createServer({ topic, payload, }); - // @ts-ignore - ws.send(evt); + setTimeout(() => { + // @ts-ignore + ws.send(evt); + }, socketDelay); }; ws.sendJSON = async ({ event, ref, topic, payload }: PhoenixEvent) => { diff --git a/packages/lightning-mock/test/socket-server.test.ts b/packages/lightning-mock/test/socket-server.test.ts index c21dd6a9f..c37538c12 100644 --- a/packages/lightning-mock/test/socket-server.test.ts +++ b/packages/lightning-mock/test/socket-server.test.ts @@ -155,3 +155,39 @@ test.serial('onMessage', (t) => { }); }); }); + +test.serial('defer message', (t) => { + return new Promise(async (done) => { + // Bit annoying but we need to rebuild the server to + // get a delay on it + server.close(); + + server = createSocketServer({ + // @ts-ignore + state: { + events: new EventEmitter(), + }, + onMessage: (evt) => { + messages.push(evt); + }, + socketDelay: 500, + }); + + socket = new Socket('ws://localhost:8080', { + transport: WebSocket, + params: { token: 'x.y.z' }, + }); + + socket.connect(); + + await wait(500); + + const channel = socket.channel('x', {}); + const start = Date.now(); + channel.join().receive('ok', async () => { + const duration = Date.now() - start; + t.assert(duration >= 500); + done(); + }); + }); +});