You've already forked matrix-js-sdk
mirror of
https://github.com/matrix-org/matrix-js-sdk.git
synced 2025-08-09 10:22:46 +03:00
Fix and add a test for toDevice ordering
This commit is contained in:
83
spec/unit/webrtc/callEventHandler.spec.ts
Normal file
83
spec/unit/webrtc/callEventHandler.spec.ts
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
import { TestClient } from '../../TestClient';
|
||||||
|
import { CallEventHandler } from '../../../src/webrtc/callEventHandler';
|
||||||
|
import { MatrixEvent } from '../../../src/models/event';
|
||||||
|
import { EventType } from '../../../src/@types/event';
|
||||||
|
|
||||||
|
describe('CallEventHandler', function() {
|
||||||
|
let client;
|
||||||
|
|
||||||
|
beforeEach(function() {
|
||||||
|
client = new TestClient("@alice:foo", "somedevice", "token", undefined, {});
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(function() {
|
||||||
|
client.stop();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should enforce inbound toDevice message ordering', async function() {
|
||||||
|
const callEventHandler = new CallEventHandler(client);
|
||||||
|
|
||||||
|
const event1 = new MatrixEvent({
|
||||||
|
type: EventType.CallInvite,
|
||||||
|
content: {
|
||||||
|
call_id: "123",
|
||||||
|
seq: 0,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
callEventHandler["onToDeviceEvent"](event1);
|
||||||
|
|
||||||
|
expect(callEventHandler.callEventBuffer.length).toBe(1);
|
||||||
|
expect(callEventHandler.callEventBuffer[0]).toBe(event1);
|
||||||
|
|
||||||
|
const event2 = new MatrixEvent({
|
||||||
|
type: EventType.CallCandidates,
|
||||||
|
content: {
|
||||||
|
call_id: "123",
|
||||||
|
seq: 1,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
callEventHandler["onToDeviceEvent"](event2);
|
||||||
|
|
||||||
|
expect(callEventHandler.callEventBuffer.length).toBe(2);
|
||||||
|
expect(callEventHandler.callEventBuffer[1]).toBe(event2);
|
||||||
|
|
||||||
|
const event3 = new MatrixEvent({
|
||||||
|
type: EventType.CallCandidates,
|
||||||
|
content: {
|
||||||
|
call_id: "123",
|
||||||
|
seq: 3,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
callEventHandler["onToDeviceEvent"](event3);
|
||||||
|
|
||||||
|
expect(callEventHandler.callEventBuffer.length).toBe(2);
|
||||||
|
expect(callEventHandler.nextSeqByCall.get("123")).toBe(2);
|
||||||
|
expect(callEventHandler.toDeviceEventBuffers.get("123").length).toBe(1);
|
||||||
|
|
||||||
|
const event4 = new MatrixEvent({
|
||||||
|
type: EventType.CallCandidates,
|
||||||
|
content: {
|
||||||
|
call_id: "123",
|
||||||
|
seq: 4,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
callEventHandler["onToDeviceEvent"](event4);
|
||||||
|
|
||||||
|
expect(callEventHandler.callEventBuffer.length).toBe(2);
|
||||||
|
expect(callEventHandler.nextSeqByCall.get("123")).toBe(2);
|
||||||
|
expect(callEventHandler.toDeviceEventBuffers.get("123").length).toBe(2);
|
||||||
|
|
||||||
|
const event5 = new MatrixEvent({
|
||||||
|
type: EventType.CallCandidates,
|
||||||
|
content: {
|
||||||
|
call_id: "123",
|
||||||
|
seq: 2,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
callEventHandler["onToDeviceEvent"](event5);
|
||||||
|
|
||||||
|
expect(callEventHandler.callEventBuffer.length).toBe(5);
|
||||||
|
expect(callEventHandler.nextSeqByCall.get("123")).toBe(5);
|
||||||
|
expect(callEventHandler.toDeviceEventBuffers.get("123").length).toBe(0);
|
||||||
|
});
|
||||||
|
});
|
@@ -107,7 +107,12 @@ export class CallEventHandler {
|
|||||||
|
|
||||||
const buffer = this.toDeviceEventBuffers.get(content.call_id);
|
const buffer = this.toDeviceEventBuffers.get(content.call_id);
|
||||||
const index = buffer.findIndex((e) => e.getContent().seq > content.seq);
|
const index = buffer.findIndex((e) => e.getContent().seq > content.seq);
|
||||||
buffer.splice(index, 0, event);
|
|
||||||
|
if (index === -1) {
|
||||||
|
buffer.push(event);
|
||||||
|
} else {
|
||||||
|
buffer.splice(index, 0, event);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
const callId = content.call_id;
|
const callId = content.call_id;
|
||||||
this.callEventBuffer.push(event);
|
this.callEventBuffer.push(event);
|
||||||
@@ -115,10 +120,12 @@ export class CallEventHandler {
|
|||||||
|
|
||||||
const buffer = this.toDeviceEventBuffers.get(callId);
|
const buffer = this.toDeviceEventBuffers.get(callId);
|
||||||
|
|
||||||
while (buffer.length > 0 && buffer[0].getContent().seq === content.seq + 1) {
|
let nextEvent = buffer && buffer.shift();
|
||||||
const nextEvent = buffer.pop();
|
|
||||||
|
while (nextEvent && nextEvent.getContent().seq === this.nextSeqByCall.get(callId)) {
|
||||||
this.callEventBuffer.push(nextEvent);
|
this.callEventBuffer.push(nextEvent);
|
||||||
this.nextSeqByCall.set(callId, nextEvent.getContent().seq + 1);
|
this.nextSeqByCall.set(callId, nextEvent.getContent().seq + 1);
|
||||||
|
nextEvent = buffer.shift();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user