Skip to content

Commit

Permalink
fix(calling): detectNetwork change (#3142)
Browse files Browse the repository at this point in the history
Co-authored-by: Priya Kesari <[email protected]>
Co-authored-by: Kesari3008 <[email protected]>
  • Loading branch information
3 people authored Nov 8, 2023
1 parent 92c13b6 commit 8b1b756
Show file tree
Hide file tree
Showing 16 changed files with 174 additions and 168 deletions.
75 changes: 37 additions & 38 deletions packages/calling/src/CallingClient/CallingClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
getMockDeviceInfo,
getMobiusDiscoveryResponse,
} from '../common/testUtil';
import {CallType, MobiusStatus, ServiceIndicator, WebexRequestPayload} from '../common/types';
import {CallType, RegistrationStatus, ServiceIndicator, WebexRequestPayload} from '../common/types';
/* eslint-disable dot-notation */
import {CALLING_CLIENT_EVENT_KEYS, CallSessionEvent, MOBIUS_EVENT_KEYS} from '../Events/types';
import log from '../Logger';
Expand All @@ -23,7 +23,7 @@ import {
SPARK_USER_AGENT,
} from './constants';
import {MOCK_MULTIPLE_SESSIONS_EVENT, MOCK_SESSION_EVENT} from './callRecordFixtures';
import {ILine, LineStatus} from './line/types';
import {ILine} from './line/types';
import {
ipPayload,
regionBody,
Expand Down Expand Up @@ -370,8 +370,6 @@ describe('CallingClient Tests', () => {
callingClient = await createClient(webex, {logger: {level: LOGGER.INFO}});
line = Object.values(callingClient.lineDict)[0] as ILine;
reg = line.registration;

expect(line.getRegistrationStatus()).toEqual(MobiusStatus.DEFAULT);
await line.register();

deRegSpy = jest.spyOn(line.registration, 'deregister');
Expand All @@ -390,15 +388,15 @@ describe('CallingClient Tests', () => {
});

it('detect a network flap in mercury connection', async () => {
expect(line.getRegistrationStatus()).toEqual(MobiusStatus.ACTIVE);
expect(line.getStatus()).toEqual(RegistrationStatus.ACTIVE);

/* Set mercury connection to be down and execute a delay of 2.5 seconds */
webex.internal.mercury.connected = false;

jest.advanceTimersByTime(NETWORK_FLAP_TIMEOUT + 500);

/* We should be detecting the network flap */
expect(warnSpy).toBeCalledWith(
expect(warnSpy).toBeCalledOnceWith(
'Network has flapped, waiting for mercury connection to be up',
{file: CALLING_CLIENT_FILE, method: NETWORK_CHANGE_DETECTION_UTIL}
);
Expand All @@ -411,13 +409,14 @@ describe('CallingClient Tests', () => {
await flushPromises();

/* We should be detecting the network recovery */
expect(logSpy).nthCalledWith(
7,
'Mercury connection is up again, Re-registering with Mobius',
{file: REGISTRATION_FILE, method: 'handleConnectionRestoration'}
expect(logSpy).toBeCalledWith(
'Mercury connection is up again, re-registering with Webex Calling if needed',
{
file: REGISTRATION_FILE,
method: 'handleConnectionRestoration',
}
);

expect(deRegSpy).toBeCalledWith();
expect(restoreSpy).toBeCalledWith('handleConnectionRestoration');
expect(restartRegisterSpy).toBeCalledWith('handleConnectionRestoration');
expect(webex.request).toBeCalledTimes(6);
Expand All @@ -426,7 +425,7 @@ describe('CallingClient Tests', () => {
});

it('Simulate a network flap with no active calls and re-verify registration: Restore Failure', async () => {
expect(line.getRegistrationStatus()).toEqual(MobiusStatus.ACTIVE);
expect(line.getStatus()).toEqual(RegistrationStatus.ACTIVE);

const failurePayload = <WebexRequestPayload>(<unknown>{
statusCode: 500,
Expand All @@ -453,37 +452,37 @@ describe('CallingClient Tests', () => {

/* Set mercury connection to be up and execute a delay of 2.5 seconds */
webex.internal.mercury.connected = true;

jest.advanceTimersByTime(NETWORK_FLAP_TIMEOUT + 500);

await flushPromises();

/* We should be detecting the network recovery */
expect(logSpy).toBeCalledWith('Mercury connection is up again, Re-registering with Mobius', {
file: REGISTRATION_FILE,
method: 'handleConnectionRestoration',
});
expect(logSpy).toBeCalledWith(
'Mercury connection is up again, re-registering with Webex Calling if needed',
{
file: REGISTRATION_FILE,
method: 'handleConnectionRestoration',
}
);

expect(deRegSpy).toBeCalledWith();
expect(restoreSpy).toBeCalledWith('handleConnectionRestoration');
expect(restartRegisterSpy).toBeCalledWith('handleConnectionRestoration');
expect(restoreSpy).toBeCalledOnceWith('handleConnectionRestoration');
expect(restartRegisterSpy).toBeCalledOnceWith('handleConnectionRestoration');
expect(webex.request).toBeCalledTimes(6);
expect(registerSpy).toBeCalledWith('handleConnectionRestoration', [reg.getActiveMobiusUrl()]);
expect(registerSpy).lastCalledWith('handleConnectionRestoration', [primaryUrl]);
});

it('Simulate a network flap before initial registration is done', async () => {
expect(line.getRegistrationStatus()).toEqual(MobiusStatus.ACTIVE);

reg.deregister();
reg.setActiveMobiusUrl(undefined);

jest.clearAllMocks();
const handleConnectionRestoreSpy = jest.spyOn(reg, 'handleConnectionRestoration');
reg.setStatus(RegistrationStatus.IDLE);

/* Set mercury connection to be down and execute a delay of 2.5 seconds */
webex.internal.mercury.connected = false;
jest.advanceTimersByTime(NETWORK_FLAP_TIMEOUT + 500);

/* We should be detecting the network flap */
expect(warnSpy).toBeCalledWith(
expect(warnSpy).toBeCalledOnceWith(
'Network has flapped, waiting for mercury connection to be up',
{file: CALLING_CLIENT_FILE, method: NETWORK_CHANGE_DETECTION_UTIL}
);
Expand All @@ -495,22 +494,23 @@ describe('CallingClient Tests', () => {
await flushPromises();

/* We should be detecting the network recovery */
expect(logSpy).toBeCalledWith('Mercury connection is up again, Re-registering with Mobius', {
file: REGISTRATION_FILE,
method: 'handleConnectionRestoration',
});
expect(logSpy).not.toBeCalledWith(
'Mercury connection is up again, re-registering with Webex Calling if needed',
{
file: REGISTRATION_FILE,
method: 'handleConnectionRestoration',
}
);

/*
* When initial registration is not done, network flap
* will not trigger de-registration/registration
*/
expect(webex.request).not.toBeCalled();
expect(restoreSpy).not.toBeCalled();
expect(registerSpy).not.toBeCalled();
expect(handleConnectionRestoreSpy).not.toBeCalledOnceWith();
});

it('Simulate a network flap with 1 active call', async () => {
expect(line.getRegistrationStatus()).toEqual(MobiusStatus.ACTIVE);
expect(line.getStatus()).toEqual(RegistrationStatus.ACTIVE);

/** create a new call */
reg.callManager.createCall();
Expand All @@ -522,7 +522,7 @@ describe('CallingClient Tests', () => {
await flushPromises();

/* We should be detecting the network flap */
expect(warnSpy).not.toBeCalledWith(
expect(warnSpy).not.toBeCalledOnceWith(
'Network has flapped, waiting for mercury connection to be up',
{file: CALLING_CLIENT_FILE, method: 'handleConnectionRestoration'}
);
Expand All @@ -534,8 +534,8 @@ describe('CallingClient Tests', () => {
await flushPromises();

/* We should be detecting the network recovery */
expect(logSpy).not.toBeCalledWith(
'Mercury connection is up again, Re-registering with Mobius',
expect(logSpy).not.toBeCalledOnceWith(
'Mercury connection is up again, re-registering with Webex Calling if needed',
{file: REGISTRATION_FILE, method: 'handleConnectionRestoration'}
);

Expand Down Expand Up @@ -590,7 +590,6 @@ describe('CallingClient Tests', () => {
line = new Line(
userId,
clientDeviceUri,
LineStatus.ACTIVE,
mutex,
primaryMobiusUris(),
backupMobiusUris(),
Expand Down
11 changes: 7 additions & 4 deletions packages/calling/src/CallingClient/CallingClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
IpInfo,
MobiusServers,
WebexRequestPayload,
RegistrationStatus,
} from '../common/types';
import {ICallingClient, CallingClientConfig} from './types';
import {ICall, ICallManager} from './calling/types';
Expand All @@ -41,7 +42,7 @@ import {
NETWORK_FLAP_TIMEOUT,
} from './constants';
import Line from './line';
import {ILine, LINE_EVENTS, LineStatus} from './line/types';
import {ILine, LINE_EVENTS} from './line/types';
import {METRIC_EVENT, REG_ACTION, METRIC_TYPE, IMetricManager} from '../Metrics/types';
import {getMetricManager} from '../Metrics';

Expand Down Expand Up @@ -156,14 +157,17 @@ export class CallingClient extends Eventing<CallingClientEventTypes> implements
method: this.detectNetworkChange.name,
});

line.lineEmitter(LINE_EVENTS.UNREGISTERED);
line.registration.clearKeepaliveTimer();

retry = true;
}

if (retry && this.webex.internal.mercury.connected) {
retry = await line.registration.handleConnectionRestoration(retry);
if (line.getStatus() !== RegistrationStatus.IDLE) {
retry = await line.registration.handleConnectionRestoration(retry);
} else {
retry = false;
}
}
}, NETWORK_FLAP_TIMEOUT);
}
Expand Down Expand Up @@ -411,7 +415,6 @@ export class CallingClient extends Eventing<CallingClientEventTypes> implements
const line = new Line(
this.webex.internal.device.userId,
this.webex.internal.device.url,
LineStatus.INACTIVE,
this.mutex,
this.primaryMobiusUris,
this.backupMobiusUris,
Expand Down
1 change: 1 addition & 0 deletions packages/calling/src/CallingClient/calling/call.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ describe('Call Tests', () => {
outputStream: {
getAudioTracks: jest.fn().mockReturnValue([mockTrack]),
},
on: jest.fn(),
};

const localAudioStream = mockStream as unknown as MediaSDK.LocalMicrophoneStream;
Expand Down
17 changes: 6 additions & 11 deletions packages/calling/src/CallingClient/line/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import {
CorrelationId,
IDeviceInfo,
MobiusDeviceId,
MobiusStatus,
RegistrationStatus,
ServiceIndicator,
} from '../../common/types';
import {ILine, LINE_EVENTS, LineEventTypes, LineStatus} from './types';
import {ILine, LINE_EVENTS, LineEventTypes} from './types';
import {LINE_FILE, VALID_PHONE} from '../constants';
import log from '../../Logger';
import {IRegistration} from '../registration/types';
Expand Down Expand Up @@ -49,8 +49,6 @@ export default class Line extends Eventing<LineEventTypes> implements ILine {

public extension?: string;

public status: LineStatus;

public sipAddresses?: string[];

public voicemail?: string;
Expand Down Expand Up @@ -78,7 +76,6 @@ export default class Line extends Eventing<LineEventTypes> implements ILine {
constructor(
userId: string,
clientDeviceUri: string,
status: LineStatus,
mutex: Mutex,
primaryMobiusUris: string[],
backupMobiusUris: string[],
Expand All @@ -92,7 +89,6 @@ export default class Line extends Eventing<LineEventTypes> implements ILine {
this.lineId = uuid();
this.userId = userId;
this.clientDeviceUri = clientDeviceUri;
this.status = status;
this.phoneNumber = phoneNumber;
this.extension = extension;
this.voicemail = voicemail;
Expand All @@ -118,7 +114,6 @@ export default class Line extends Eventing<LineEventTypes> implements ILine {
logLevel
);

this.registration.setStatus(MobiusStatus.DEFAULT);
log.setLogger(logLevel, LINE_FILE);

this.callManager = getCallManager(this.#webex, serviceData.indicator);
Expand All @@ -131,7 +126,6 @@ export default class Line extends Eventing<LineEventTypes> implements ILine {
*/
public async register() {
await this.#mutex.runExclusive(async () => {
this.registration.setStatus(MobiusStatus.DEFAULT);
this.emit(LINE_EVENTS.CONNECTING);

this.registration.setMobiusServers(this.#primaryMobiusUris, this.#backupMobiusUris);
Expand All @@ -146,7 +140,8 @@ export default class Line extends Eventing<LineEventTypes> implements ILine {
* Wrapper to for device deregister.
*/
public async deregister() {
this.registration.deregister();
await this.registration.deregister();
this.registration.setStatus(RegistrationStatus.IDLE);
}

/**
Expand Down Expand Up @@ -218,7 +213,7 @@ export default class Line extends Eventing<LineEventTypes> implements ILine {
/**
* Gets registration status
*/
public getRegistrationStatus = (): MobiusStatus => this.registration.getStatus();
public getStatus = (): RegistrationStatus => this.registration.getStatus();

/**
* Gets device id
Expand Down Expand Up @@ -260,7 +255,7 @@ export default class Line extends Eventing<LineEventTypes> implements ILine {
'An invalid phone number was detected. Check the number and try again.',
{},
ERROR_TYPE.CALL_ERROR,
LineStatus.ACTIVE
RegistrationStatus.ACTIVE
);

this.emit(LINE_EVENTS.ERROR, err);
Expand Down
Loading

0 comments on commit 8b1b756

Please sign in to comment.