Skip to content

Commit

Permalink
fix(client): Stop execution if connectionParams took too long and t…
Browse files Browse the repository at this point in the history
…he server kicked the client off

Closes enisdenjo#331
  • Loading branch information
enisdenjo committed Mar 13, 2022
1 parent 126a19a commit 1e94e45
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/__tests__/client.ts
Expand Up @@ -330,6 +330,45 @@ it('should close the socket if the `connectionParams` rejects or throws', async
});
});

it('should report close events when `connectionParams` takes too long', async (done) => {
const server = await startTServer({
connectionInitWaitTimeout: 10,
});

// lazy
const client = createClient({
url: server.url,
retryAttempts: 0,
connectionParams: () =>
// takes longer than the connectionInitWaitTimeout
new Promise<undefined>((resolve) => setTimeout(resolve, 20)),
});

const sub = tsubscribe(client, { query: '{ getValue }' });

await sub.waitForError((event) => {
expect((event as CloseEvent).code).toBe(
CloseCode.ConnectionInitialisationTimeout,
);
});

// non-lazy
createClient({
url: server.url,
retryAttempts: 0,
lazy: false,
onNonLazyError: (event) => {
expect((event as CloseEvent).code).toBe(
CloseCode.ConnectionInitialisationTimeout,
);
done();
},
connectionParams: () =>
// takes longer than the connectionInitWaitTimeout
new Promise<undefined>((resolve) => setTimeout(resolve, 20)),
});
});

it('should not send the complete message if the socket is not open', async () => {
const { url, waitForOperation } = await startTServer();

Expand Down
5 changes: 5 additions & 0 deletions src/client.ts
Expand Up @@ -628,6 +628,11 @@ export function createClient<
typeof connectionParams === 'function'
? await connectionParams()
: connectionParams;

// connectionParams might take too long causing the server to kick off the client
// the necessary error/close event is already reported - simply stop execution
if (socket.readyState !== WebSocketImpl.OPEN) return;

socket.send(
stringifyMessage<MessageType.ConnectionInit>(
payload
Expand Down

0 comments on commit 1e94e45

Please sign in to comment.