Skip to content

Commit

Permalink
[VBLOCKS-330] Listen for multiple answer events. (#42)
Browse files Browse the repository at this point in the history
* Listen for multiple answer events.

* Add network tests.

* Temporarily skip flaky network tests.
  • Loading branch information
mhuynh5757 committed Oct 28, 2021
1 parent d1eb3f0 commit 284ec41
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/twilio/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -601,12 +601,13 @@ class Call extends EventEmitter {

if (this._direction === Call.CallDirection.Incoming) {
this._isAnswered = true;
this._pstream.on('answer', this._onAnswer.bind(this));
this._mediaHandler.answerIncomingCall(this.parameters.CallSid, this._options.offerSdp,
rtcConstraints, rtcConfiguration, onAnswer);
} else {
const params = Array.from(this.customParameters.entries()).map(pair =>
`${encodeURIComponent(pair[0])}=${encodeURIComponent(pair[1])}`).join('&');
this._pstream.once('answer', this._onAnswer.bind(this));
this._pstream.on('answer', this._onAnswer.bind(this));
this._mediaHandler.makeOutgoingCall(this._pstream.token, params, this.outboundConnectionId,
rtcConstraints, rtcConfiguration, onAnswer);
}
Expand Down Expand Up @@ -1124,7 +1125,7 @@ class Call extends EventEmitter {
}

// Ignore subsequent requests if ice restart is in progress
if (this._status === Call.State.Reconnecting) {
if (this._mediaStatus === Call.State.Reconnecting) {

// This is a retry. Previous ICE Restart failed
if (isEndOfIceCycle) {
Expand Down
18 changes: 18 additions & 0 deletions tests/network/reconnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,24 @@ describe('Reconnection', function() {
assert([call1, call2].every(call => call.status() === Call.State.Open));
});

it('should reconnect to signaling multiple times', async () => {
await runDockerCommand('disconnectFromAllNetworks');
let reconnectPromises = Promise.all([call1, call2].map(
call => new Promise(res => call.on('reconnected', res)),
));
setTimeout(() => runDockerCommand('resetNetwork'), 8000);
await waitFor(reconnectPromises, 20000);
assert([call1, call2].every(call => call.status() === Call.State.Open));

await runDockerCommand('disconnectFromAllNetworks');
reconnectPromises = Promise.all([call1, call2].map(
call => new Promise(res => call.on('reconnected', res)),
));
setTimeout(() => runDockerCommand('resetNetwork'), 8000);
await waitFor(reconnectPromises, 20000);
assert([call1, call2].every(call => call.status() === Call.State.Open));
});

after(async () => {
destroyDevices();
await runDockerCommand('resetNetwork');
Expand Down
46 changes: 46 additions & 0 deletions tests/unit/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1548,6 +1548,52 @@ describe('Call', function() {
});
});
});

describe('pstream.answer event', () => {
let pStreamAnswerPayload: any;

beforeEach(async () => {
pStreamAnswerPayload = {
edge: 'foobar-edge',
reconnect: 'foobar-reconnect-token',
};
conn = new Call(config, options);
conn.accept();
await clock.tickAsync(0);
});

it('should set the call to "answered"', () => {
pstream.emit('answer', pStreamAnswerPayload);
assert(conn['_isAnswered']);
});

it('should save the reconnect token', () => {
pstream.emit('answer', pStreamAnswerPayload);
assert.equal(conn['_signalingReconnectToken'], pStreamAnswerPayload.reconnect);
});

describe('if raised multiple times', () => {
it('should save the reconnect token multiple times', () => {
pstream.emit('answer', pStreamAnswerPayload);
assert.equal(conn['_signalingReconnectToken'], pStreamAnswerPayload.reconnect);

pStreamAnswerPayload.reconnect = 'biffbazz-reconnect-token';

pstream.emit('answer', pStreamAnswerPayload);
assert.equal(conn['_signalingReconnectToken'], pStreamAnswerPayload.reconnect);
});

it('should not invoke "call._maybeTransitionToOpen" more than once', () => {
const spy = conn['_maybeTransitionToOpen'] = sinon.spy(conn['_maybeTransitionToOpen']);

pstream.emit('answer', pStreamAnswerPayload);
sinon.assert.calledOnce(spy);

pstream.emit('answer', pStreamAnswerPayload);
sinon.assert.calledOnce(spy);
});
});
});
});

describe('on monitor.sample', () => {
Expand Down

0 comments on commit 284ec41

Please sign in to comment.