Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fire health.check event after successful connection when using long poll transport #900

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/connection_fallback.ts
Original file line number Diff line number Diff line change
@@ -171,6 +171,8 @@ export class WSConnectionFallback<

this._setState(ConnectionState.Connected);
this.connectionID = event.connection_id;
// @ts-expect-error
this.client.dispatchEvent(event);
this._poll();
if (reconnect) {
this.client.recoverState();
24 changes: 18 additions & 6 deletions test/unit/client.js
Original file line number Diff line number Diff line change
@@ -352,15 +352,16 @@ describe('Client WSFallback', () => {
});

it('should try wsFallback if WebSocket fails', async () => {
const eventDate = new Date(Date.UTC(2009, 1, 3, 23, 3, 3));
const stub = sinon
.stub()
.onCall(0)
.resolves({ event: { connection_id: 'new_id' } })
.resolves({});
.resolves({ event: { connection_id: 'new_id', received_at: eventDate } });

client.doAxiosRequest = stub;
client.wsBaseURL = 'ws://getstream.io';
const health = await client.connectUser({ id: 'amin' }, userToken);
expect(health).to.be.eql({ connection_id: 'new_id' });
expect(health).to.be.eql({ connection_id: 'new_id', received_at: eventDate });
expect(client.wsFallback.state).to.be.eql(ConnectionState.Connected);
expect(client.wsFallback.connectionID).to.be.eql('new_id');
expect(client.wsFallback.consecutiveFailures).to.be.eql(0);
@@ -371,16 +372,27 @@ describe('Client WSFallback', () => {
expect(client.wsConnection.totalFailures).to.be.greaterThan(1);
await client.disconnectUser();
expect(client.wsFallback.state).to.be.eql(ConnectionState.Disconnected);
stub.reset();
});

it('should fire transport.changed event', async () => {
it('should fire transport.changed and health.check event', async () => {
const eventDate = new Date(Date.UTC(2009, 1, 3, 23, 3, 3));
sinon.spy(client, 'dispatchEvent');
client.doAxiosRequest = () => ({ event: { connection_id: 'new_id' } });
client.doAxiosRequest = () => ({
event: { type: 'health.check', connection_id: 'new_id', received_at: eventDate },
});
client.wsBaseURL = 'ws://getstream.io';
const health = await client.connectUser({ id: 'amin' }, userToken);
await client.disconnectUser();
expect(health).to.be.eql({ connection_id: 'new_id' });
expect(health).to.be.eql({ type: 'health.check', connection_id: 'new_id', received_at: eventDate });
expect(client.dispatchEvent.calledWithMatch({ type: 'transport.changed', mode: 'longpoll' })).to.be.true;
expect(
client.dispatchEvent.calledWithMatch({
type: 'health.check',
connection_id: 'new_id',
received_at: eventDate,
}),
).to.be.true;
});

it('should ignore fallback if flag is false', async () => {