Skip to content

Commit

Permalink
VBLOCKS-1299 | Making all integration tests green (twilio#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
charliesantos authored Jun 27, 2023
1 parent 4e3addc commit 576d353
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 61 deletions.
11 changes: 8 additions & 3 deletions karma.conf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ if (fs.existsSync(__dirname + '/config.yaml')) {
process.env.AUTH_TOKEN = process.env.AUTH_TOKEN || creds.auth_token;
}

const testFiles = process.env.INTEGRATION_TEST_FILES ?
process.env.INTEGRATION_TEST_FILES.split(',') : ['tests/integration/*.ts'];

console.log('Test Files:', testFiles);

module.exports = function(config: any) {
const supportedBrowsers: Record<string, string[]> = {
chrome: ['ChromeWebRTC'],
Expand Down Expand Up @@ -41,7 +46,7 @@ module.exports = function(config: any) {
'--autoplay-policy=no-user-gesture-required',
];

if (isDocker) {
if (process.env.HEADLESS === 'true' || isDocker) {
firefoxFlags.push('-headless');
chromeFlags.push(
'--headless',
Expand Down Expand Up @@ -75,7 +80,7 @@ module.exports = function(config: any) {
files: [
'lib/twilio.ts',
'lib/twilio/**/*.ts',
'tests/integration/*.ts',
...testFiles,
],
frameworks: ['mocha', 'karma-typescript'],
karmaTypescriptConfig: {
Expand All @@ -95,7 +100,7 @@ module.exports = function(config: any) {
},
include: [
'lib/**/*',
'tests/integration/**/*.ts',
...testFiles,
],
tsconfig: './tsconfig.json',
},
Expand Down
1 change: 1 addition & 0 deletions lib/twilio/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,7 @@ class Device extends EventEmitter {
customSounds: this._options.sounds,
dialtonePlayer: Device._dialtonePlayer,
dscp: this._options.dscp,
// TODO(csantos): Remove forceAggressiveIceNomination option in 3.x
forceAggressiveIceNomination: this._options.forceAggressiveIceNomination,
getInputStream: (): MediaStream | null => this._options.fileInputStream || this._callInputStream,
getSinkIds: (): string[] => this._callSinkIds,
Expand Down
117 changes: 61 additions & 56 deletions tests/integration/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,67 @@ describe('Device', function() {
}
});

describe('tokenWillExpire event', () => {
const setupDevice = (tokenTtl: number, tokenRefreshMs: number) => {
const accessToken = generateAccessToken(`device-tokenWillExpire-${Date.now()}`, tokenTtl);
const device = new Device(accessToken, { tokenRefreshMs });
device.on(Device.EventName.Error, () => { /* no-op */ });
return device;
};

it('should emit a "tokenWillExpire" event', async () => {
const device = setupDevice(10, 1000);
await new Promise(async (resolve, reject) => {
let failureTimeout: any = null;
device.on(Device.EventName.TokenWillExpire, () => {
if (failureTimeout) {
clearTimeout(failureTimeout);
}
resolve();
});
await device.register();
failureTimeout = setTimeout(reject, 9500);
});
});

it('should not emit a "tokenWillExpire" event early', async () => {
const device = setupDevice(10, 1000);
await new Promise(async (resolve, reject) => {
let successTimeout: any = null;
device.on(Device.EventName.TokenWillExpire, () => {
if (successTimeout) {
clearTimeout(successTimeout);
}
reject();
});
await device.register();
// NOTE(csantos): Expected time before tokenWillExpire event is raised is about 9s
// for a ttl: 10s and tokenRefreshMS: 1000, meaning we should resolve the test at less than 9s.
// However, the ttl returned from signaling, which we use to calculate the timeout,
// sometimes returns 1s less from the original ttl we provided. So let's account for that.
// Instead of resolving the test before 9s, we should do it before 8s instead.
successTimeout = setTimeout(resolve, 7500);
});
});

it('should emit a "tokenWillExpire" event as soon as possible if the option is smaller than the ttl', async () => {
const device = setupDevice(5, 10000);

const eventPromises = Promise.all([
Device.EventName.TokenWillExpire,
Device.EventName.Registering,
].map((eventName: string) => new Promise<number>(res => {
device.on(eventName, () => res(Date.now()));
})));

device.register();

const [expireTime, registeringTime] = await eventPromises;
const diff = Math.abs(expireTime - registeringTime);
assert(diff < 500, `event time occurred too late; diff: ${diff}`);
});
});

describe('device 1 calls device 2', () => {
let call1: Call;
let call2: Call;
Expand Down Expand Up @@ -164,60 +225,4 @@ describe('Device', function() {
});
});
});

describe('tokenWillExpire event', () => {
const setupDevice = (tokenTtl: number, tokenRefreshMs: number) => {
const accessToken = generateAccessToken(`device-tokenWillExpire-${Date.now()}`, tokenTtl);
const device = new Device(accessToken, { tokenRefreshMs });
device.on(Device.EventName.Error, () => { /* no-op */ });
return device;
};

it('should emit a "tokenWillExpire" event', async () => {
const device = setupDevice(10, 1000);
await new Promise(async (resolve, reject) => {
let failureTimeout: any = null;
device.on(Device.EventName.TokenWillExpire, () => {
if (failureTimeout) {
clearTimeout(failureTimeout);
}
resolve();
});
await device.register();
failureTimeout = setTimeout(reject, 9500);
});
});

it('should not emit a "tokenWillExpire" event early', async () => {
const device = setupDevice(10, 1000);
await new Promise(async (resolve, reject) => {
let successTimeout: any = null;
device.on(Device.EventName.TokenWillExpire, () => {
if (successTimeout) {
clearTimeout(successTimeout);
}
reject();
});
await device.register();
successTimeout = setTimeout(resolve, 8500);
});
});

it('should emit a "tokenWillExpire" event as soon as possible if the option is smaller than the ttl', async () => {
const device = setupDevice(5, 10000);

const eventPromises = Promise.all([
Device.EventName.TokenWillExpire,
Device.EventName.Registering,
].map((eventName: string) => new Promise<number>(res => {
device.on(eventName, () => res(Date.now()));
})));

device.register();

const [expireTime, registeringTime] = await eventPromises;
const diff = Math.abs(expireTime - registeringTime);
assert(diff < 500, `event time occurred too late; diff: ${diff}`);
});
});
});
4 changes: 2 additions & 2 deletions tests/integration/preflight.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const DURATION_PADDING = 3000;
const EVENT_TIMEOUT = 30000;
const MAX_TIMEOUT = 300000;

describe.skip('Preflight Test', function() {
describe('Preflight Test', function() {
this.timeout(MAX_TIMEOUT);

let callerIdentity: string;
Expand Down Expand Up @@ -269,7 +269,7 @@ describe.skip('Preflight Test', function() {

(callerDevice as any).connectOverride = callerDevice.connect;
callerDevice.connect = () => {
return (callerDevice as any).connectOverride({ To: receiverIdentity });
return (callerDevice as any).connectOverride({ params: { To: receiverIdentity } });
};
});

Expand Down

0 comments on commit 576d353

Please sign in to comment.