Skip to content

Commit

Permalink
fix(client): Shouldn’t send the Complete message if socket is not open
Browse files Browse the repository at this point in the history
  • Loading branch information
enisdenjo committed Feb 8, 2021
1 parent c667d70 commit cd12024
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/client.ts
Expand Up @@ -518,8 +518,8 @@ export function createClient(options: ClientOptions): Client {
);

releaserRef.current = () => {
if (!completed) {
// if not completed already, send complete message to server on release
if (!completed && socket.readyState === WebSocketImpl.OPEN) {
// if not completed already and socket is open, send complete message to server on release
socket.send(
stringifyMessage<MessageType.Complete>({
id: id,
Expand Down
41 changes: 41 additions & 0 deletions src/tests/client.ts
Expand Up @@ -284,6 +284,47 @@ it('should close the socket if the `connectionParams` rejects or throws', async
});
});

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

class MockWebSocket extends WebSocket {
constructor(...args: unknown[]) {
// @ts-expect-error Args will fit
super(...args);
}

public send(data: unknown) {
if (this.readyState !== WebSocket.OPEN)
fail("Shouldn't send anything through a non-OPEN socket");
super.send(data);
}
}

const client = createClient({
webSocketImpl: MockWebSocket,
url,
retryAttempts: 0,
onNonLazyError: noop,
});
const sub = tsubscribe(client, { query: 'subscription { ping }' });
await waitForOperation();

// kick the client off
for (const client of clients) {
client.close();
await waitForClientClose();
}

// dispose of the subscription which should complete the connection
sub.dispose();
await sub.waitForComplete();
});

describe('query operation', () => {
it('should execute the query, "next" the result and then complete', async () => {
const { url } = await startTServer();
Expand Down

0 comments on commit cd12024

Please sign in to comment.