Skip to content
Merged
Show file tree
Hide file tree
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
70 changes: 1 addition & 69 deletions app/lib/services/__tests__/socketHealth.integration.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import sdk from '../sdk';
import { recoverSocket } from '../socketHealth';
import {
addMediaSubs,
backdateLastPing,
buildConnectedDriver,
framesOn,
stopAnsweringFrames
} from '~/lib/testUtils/sdkIntegration';
import { backdateLastPing, buildConnectedDriver, framesOn, stopAnsweringFrames } from '~/lib/testUtils/sdkIntegration';
import type { IMockSdk, MockConnection, IMockSdkDriver } from '~/lib/testUtils/sdkIntegration';
import type * as SdkIntegration from '~/lib/testUtils/sdkIntegration';

Expand Down Expand Up @@ -140,27 +134,6 @@ describe('recoverSocket against the real SDK socket', () => {
await expect(recovery).resolves.toBe('reopened');
});

it('re-sends the media subscriptions on the new socket reusing their ids', async () => {
backdateLastPing(driver, PING_INTERVAL * 3);
addMediaSubs(driver, USER_ID);

const recovery = recoverSocket();
await jest.advanceTimersByTimeAsync(0);
mockConnections[1].onopen();
await jest.advanceTimersByTimeAsync(0);
await expect(recovery).resolves.toBe('reopened');

const resubscribed = driver.waitForNotifyUserMediaSubs();
await jest.advanceTimersByTimeAsync(200);
await expect(resubscribed).resolves.toBe(true);

expect(framesOn(mockConnections[0], 'sub')).toHaveLength(0);
expect(framesOn(mockConnections[1], 'sub')).toEqual([
expect.objectContaining({ id: 'sub-0', name: 'stream-notify-user', params: [`${USER_ID}/media-signal`] }),
expect.objectContaining({ id: 'sub-1', name: 'stream-notify-user', params: [`${USER_ID}/media-calls`] })
]);
});

it('reopens a closed transport without a round trip even when lastPing is fresh', async () => {
mockConnections[0].readyState = CLOSED;

Expand All @@ -176,47 +149,6 @@ describe('recoverSocket against the real SDK socket', () => {
await expect(recovery).resolves.toBe('reopened');
});

it('waits for media subs to appear after reopen, then re-acks them', async () => {
backdateLastPing(driver, PING_INTERVAL * 3);

const recovery = recoverSocket();
await jest.advanceTimersByTimeAsync(0);
mockConnections[1].onopen();
await jest.advanceTimersByTimeAsync(0);
await expect(recovery).resolves.toBe('reopened');

const resubscribed = driver.waitForNotifyUserMediaSubs(1000);
await jest.advanceTimersByTimeAsync(100);
expect(framesOn(mockConnections[1], 'sub')).toHaveLength(0);

addMediaSubs(driver, USER_ID);
await jest.advanceTimersByTimeAsync(200);

await expect(resubscribed).resolves.toBe(true);
expect(framesOn(mockConnections[1], 'sub')).toEqual([
expect.objectContaining({ id: 'sub-0', name: 'stream-notify-user', params: [`${USER_ID}/media-signal`] }),
expect.objectContaining({ id: 'sub-1', name: 'stream-notify-user', params: [`${USER_ID}/media-calls`] })
]);
});

it('resolves false when the reopened socket never acks the re-sub', async () => {
backdateLastPing(driver, PING_INTERVAL * 3);
addMediaSubs(driver, USER_ID);

const recovery = recoverSocket();
await jest.advanceTimersByTimeAsync(0);
mockConnections[1].onopen();
await jest.advanceTimersByTimeAsync(0);
await expect(recovery).resolves.toBe('reopened');

stopAnsweringFrames(mockConnections[1]);

const resubscribed = driver.waitForNotifyUserMediaSubs(500);
await jest.advanceTimersByTimeAsync(500);

await expect(resubscribed).resolves.toBe(false);
});

it('shares one reopen between two concurrent recoverSocket calls', async () => {
backdateLastPing(driver, PING_INTERVAL * 3);

Expand Down
10 changes: 7 additions & 3 deletions app/lib/services/voip/acceptNativeCall.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ describe('acceptNativeCallWithReadiness against real login readiness', () => {
mockGetCallState.mockReturnValue({ call: null, resetNativeCallId: jest.fn() });
mockRecoverSocket.mockResolvedValue('reopened');
driver = await buildConnectedDriver(mockConnections, USER_ID);
addMediaSubs(driver, USER_ID);
(sdk as unknown as IMockSdk).setClient({ driver });
});

Expand All @@ -124,6 +123,7 @@ describe('acceptNativeCallWithReadiness against real login readiness', () => {
await jest.advanceTimersByTimeAsync(0);
expect(mediaSession.answerCall).not.toHaveBeenCalled();
redux.setLoginReady();
await addMediaSubs(driver, USER_ID);

await jest.advanceTimersByTimeAsync(200);
await gate;
Expand All @@ -137,12 +137,18 @@ describe('acceptNativeCallWithReadiness against real login readiness', () => {
it('releases its store listener and readiness polling as soon as readiness lands', async () => {
redux.setLoginReady();
const mediaSession = makeMediaSession();
const timersBefore = jest.getTimerCount();

const gate = acceptNativeCallWithReadiness(CALL_ID, mediaSession);
await jest.advanceTimersByTimeAsync(0);
expect(jest.getTimerCount()).toBeGreaterThan(timersBefore);

await addMediaSubs(driver, USER_ID);
await jest.advanceTimersByTimeAsync(200);
await gate;

expect(redux.listenerCount()).toBe(0);
expect(jest.getTimerCount()).toBe(timersBefore);

// Nothing is left scheduled: no late failure ladder.
await jest.advanceTimersByTimeAsync(60000);
Expand All @@ -151,14 +157,12 @@ describe('acceptNativeCallWithReadiness against real login readiness', () => {
});

it('runs the failure ladder once and leaves nothing behind when readiness never lands', async () => {
driver.socket.subscriptions = {};
const resetNativeCallId = jest.fn();
mockGetCallState.mockReturnValue({ call: null, resetNativeCallId });
const mediaSession = makeMediaSession();

const gate = acceptNativeCallWithReadiness(CALL_ID, mediaSession);

// Login never authenticates and the media subs never ack.
await jest.advanceTimersByTimeAsync(READINESS_TIMEOUT);
await gate;

Expand Down
50 changes: 41 additions & 9 deletions app/lib/services/voip/acceptNativeCall.sdk.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,14 @@ import { acceptNativeCallWithReadiness } from './acceptNativeCall';
import { useCallStore } from './useCallStore';
import { terminateNativeCall } from './terminateNativeCall';
import { waitForLoginReady } from '../waitForLoginReady';
import { addMediaSubs, backdateLastPing, buildConnectedDriver, stopAnsweringFrames } from '~/lib/testUtils/sdkIntegration';
import {
addMediaSubs,
backdateLastPing,
buildConnectedDriver,
framesOn,
resumeLogin,
stopAnsweringFrames
} from '~/lib/testUtils/sdkIntegration';
import type { IMockSdk, MockConnection, IMockSdkDriver } from '~/lib/testUtils/sdkIntegration';
import type * as SdkIntegration from '~/lib/testUtils/sdkIntegration';

Expand Down Expand Up @@ -82,43 +89,66 @@ afterEach(() => {
});

describe('acceptNativeCallWithReadiness against the real SDK socket', () => {
it('answers the call once media subs re-ack on the reopened socket', async () => {
it('answers on a live socket whose media subs were acked, without sending a sub', async () => {
const mediaSession = makeMediaSession();
await addMediaSubs(driver, USER_ID);
const subsBefore = framesOn(mockConnections[0], 'sub').length;

const accept = acceptNativeCallWithReadiness(CALL_ID, mediaSession);
await jest.advanceTimersByTimeAsync(0);
await accept;

expect(mockConnections).toHaveLength(1);
expect(framesOn(mockConnections[0], 'sub')).toHaveLength(subsBefore);
expect(mediaSession.answerCall).toHaveBeenCalledWith(CALL_ID);
expect(mockTerminateNativeCall).not.toHaveBeenCalled();
});

it('answers the call once the resume login resubscribes the media subs on the reopened socket', async () => {
const mediaSession = makeMediaSession();

await addMediaSubs(driver, USER_ID);
const mediaSubIds = framesOn(mockConnections[0], 'sub').map(frame => frame.id);
backdateLastPing(driver, PING_INTERVAL * 3);
addMediaSubs(driver, USER_ID);

const accept = acceptNativeCallWithReadiness(CALL_ID, mediaSession);
await jest.advanceTimersByTimeAsync(0);
mockConnections[1].onopen();
await jest.advanceTimersByTimeAsync(0);
await jest.advanceTimersByTimeAsync(200);
expect(mediaSession.answerCall).not.toHaveBeenCalled();
expect(framesOn(mockConnections[1], 'sub')).toHaveLength(0);

await resumeLogin(driver);
await jest.advanceTimersByTimeAsync(200);
await accept;

expect(framesOn(mockConnections[1], 'sub').map(frame => frame.id)).toEqual(mediaSubIds);
expect(mockWaitForLoginReady).toHaveBeenCalledTimes(1);
expect(mediaSession.applyRestStateSignals).toHaveBeenCalledTimes(1);
expect(mediaSession.answerCall).toHaveBeenCalledWith(CALL_ID);
expect(mockTerminateNativeCall).not.toHaveBeenCalled();
expect(mediaSession.endCall).not.toHaveBeenCalled();
});

it('fails the call without answering when the reopened socket never acks the re-sub', async () => {
it('fails the call without answering when the reopened socket never acks the resubscribe', async () => {
const mediaSession = makeMediaSession();
const resetNativeCallId = jest.fn();
mockGetState.mockReturnValue({ call: null, resetNativeCallId });

await addMediaSubs(driver, USER_ID);
backdateLastPing(driver, PING_INTERVAL * 3);
addMediaSubs(driver, USER_ID);

const accept = acceptNativeCallWithReadiness(CALL_ID, mediaSession);
await jest.advanceTimersByTimeAsync(0);
mockConnections[1].onopen();

stopAnsweringFrames(mockConnections[1]);
stopAnsweringFrames(mockConnections[1], 'sub');
await jest.advanceTimersByTimeAsync(0);

await resumeLogin(driver);
await jest.advanceTimersByTimeAsync(8000);
await accept;

expect(framesOn(mockConnections[1], 'sub')).toHaveLength(2);
expect(mockTerminateNativeCall).toHaveBeenCalledWith(CALL_ID);
expect(resetNativeCallId).toHaveBeenCalled();
expect(mediaSession.endCall).toHaveBeenCalledWith(CALL_ID);
Expand All @@ -138,7 +168,9 @@ describe('acceptNativeCallWithReadiness against the real SDK socket', () => {

await jest.advanceTimersByTimeAsync(100);

addMediaSubs(driver, USER_ID);
await resumeLogin(driver);
expect(framesOn(mockConnections[1], 'sub')).toHaveLength(0);
await addMediaSubs(driver, USER_ID);
await jest.advanceTimersByTimeAsync(200);
await accept;

Expand Down
34 changes: 21 additions & 13 deletions app/lib/testUtils/sdkIntegration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ export interface IMockSdkDriver extends ISocketDriver {
openTimeout?: ReturnType<typeof setTimeout>;
open(): Promise<void>;
send(message: Record<string, unknown>): Promise<unknown>;
subscriptions: Record<string, { id: string; name: string; params: string[]; unsubscribe: jest.Mock }>;
subscribe(name: string, params: unknown[]): Promise<unknown>;
login(credentials: { resume: string }): Promise<unknown>;
};
}

Expand Down Expand Up @@ -116,24 +117,31 @@ export async function buildConnectedDriver(connections: MockConnection[], userId
return driver;
}

export function addMediaSubs(driver: IMockSdkDriver, userId: string): void {
['media-signal', 'media-calls'].forEach((name, index) => {
const id = `sub-${index}`;
driver.socket.subscriptions[id] = {
id,
name: 'stream-notify-user',
params: [`${userId}/${name}`],
unsubscribe: jest.fn()
};
});
export async function addMediaSubs(driver: IMockSdkDriver, userId: string): Promise<void> {
const subscribing = Promise.all(
['media-signal', 'media-calls'].map(name => driver.socket.subscribe('stream-notify-user', [`${userId}/${name}`]))
);
await jest.advanceTimersByTimeAsync(0);
await subscribing;
}

export async function resumeLogin(driver: IMockSdkDriver): Promise<void> {
const loggingIn = driver.socket.login({ resume: 'auth-token' });
await jest.advanceTimersByTimeAsync(0);
await loggingIn;
}

export function backdateLastPing(driver: IMockSdkDriver, ageMs: number): void {
driver.socket.lastPing = Date.now() - ageMs;
}

export function stopAnsweringFrames(connection: MockConnection): void {
connection.send.mockImplementation(() => undefined);
export function stopAnsweringFrames(connection: MockConnection, msg?: string): void {
const answer = connection.send.getMockImplementation();
connection.send.mockImplementation((frame: string) => {
if (msg && (JSON.parse(frame) as IDdpMessage).msg !== msg) {
answer?.(frame);
}
});
}

export interface IMockCollection {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"@rocket.chat/media-signaling": "1.0.0-rc.1",
"@rocket.chat/message-parser": "0.31.36",
"@rocket.chat/mobile-crypto": "RocketChat/rocket.chat-mobile-crypto#main",
"@rocket.chat/sdk": "RocketChat/Rocket.Chat.js.SDK#ecf4fedcf8ab6121e62a6b95bb48823afd805afd",
"@rocket.chat/sdk": "RocketChat/Rocket.Chat.js.SDK#59708d7b48aace43ef65b16de101fc12a96b69aa",
"@rocket.chat/ui-kit": "^0.39.0",
"@zoontek/react-native-navigation-bar": "^1.1.1",
"axios": "0.30.3",
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading