Skip to content

Commit

Permalink
fix(client): Lazy connects after successful reconnects are not retries
Browse files Browse the repository at this point in the history
  • Loading branch information
enisdenjo committed Apr 22, 2021
1 parent 2826c10 commit 99b85a3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/client.ts
Expand Up @@ -410,6 +410,7 @@ export function createClient(options: ClientOptions): Client {
);
acknowledged = true;
emitter.emit('connected', socket, message.payload); // connected = socket opened + acknowledged
retrying = false; // future lazy connects are not retries
retries = 0; // reset the retries on connect
connected([
socket,
Expand Down
42 changes: 42 additions & 0 deletions src/tests/client.ts
Expand Up @@ -1142,6 +1142,48 @@ describe('reconnecting', () => {
fail("Client shouldn't have reconnected");
}, 20);
});

it('should not count lazy connect after succesful reconnect as another retry', async () => {
const { url, ...server } = await startTServer();

const retry = jest.fn();
const client = createClient({
url,
retryAttempts: 1,
retryWait: () => {
retry();
return Promise.resolve();
},
});

let sub = tsubscribe(client, {
query: 'subscription { ping }',
});

// closed connection, retried and successfully subscribed
await server.waitForClient((client) => {
client.close();
});
await server.waitForClientClose();
await server.waitForClient();

// complete subscription and close connection (because lazy)
sub.dispose();
await server.waitForClientClose();

// new subscription, connect again
sub = tsubscribe(client, {
query: 'subscription { ping }',
});
await server.waitForClient();

// complete subscription and close connection (because lazy)
sub.dispose();
await server.waitForClientClose();

// only one retry had happened (for first subscription)
expect(retry).toBeCalledTimes(1);
});
});

describe('events', () => {
Expand Down

0 comments on commit 99b85a3

Please sign in to comment.