Skip to content

Commit

Permalink
mock: allow a delay on socket messages
Browse files Browse the repository at this point in the history
  • Loading branch information
josephjclark committed Aug 23, 2024
1 parent 9c45cb0 commit 47231fc
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
4 changes: 3 additions & 1 deletion packages/lightning-mock/src/api-sockets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -99,6 +100,7 @@ const createSocketAPI = (
server,
state,
logger: logger && createLogger('PHX', { level: logLevel }),
socketDelay: socketDelay,
});

wss.registerEvents('worker:queue', {
Expand Down
5 changes: 4 additions & 1 deletion packages/lightning-mock/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down
8 changes: 6 additions & 2 deletions packages/lightning-mock/src/socket-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type CreateServerOptions = {
state: ServerState;
logger?: Logger;
onMessage?: (evt: PhoenixEvent) => void;
socketDelay?: number;
};

type MockSocketServer = typeof WebSocketServer & {
Expand All @@ -87,6 +88,7 @@ function createServer({
state,
logger,
onMessage = () => {},
socketDelay = 1,
}: CreateServerOptions) {
const channels: Record<Topic, Set<EventHandler>> = {
// create a stub listener for pheonix to prevent errors
Expand Down Expand Up @@ -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) => {
Expand Down
36 changes: 36 additions & 0 deletions packages/lightning-mock/test/socket-server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});

0 comments on commit 47231fc

Please sign in to comment.