2024-12-12 15:42:09 +01:00
|
|
|
import { killProcess, sleep } from "./utils/helper";
|
|
|
|
import { Livekit } from "./utils/livekit";
|
|
|
|
import { LocalDeployment } from "./utils/local-deployment";
|
2024-12-05 21:04:41 +01:00
|
|
|
import { MongoService } from "./utils/mongodb";
|
|
|
|
import { EntityType } from "./utils/types";
|
|
|
|
|
|
|
|
describe("OpenVidu active entities fixer", () => {
|
|
|
|
const mongoService = MongoService.getInstance();
|
|
|
|
|
|
|
|
beforeEach(async () => {
|
2024-12-12 15:42:09 +01:00
|
|
|
await LocalDeployment.start();
|
2024-12-09 19:04:05 +01:00
|
|
|
}, 60000); // 1 minute
|
2024-12-05 21:04:41 +01:00
|
|
|
|
|
|
|
afterEach(() => {
|
2024-12-12 15:42:09 +01:00
|
|
|
LocalDeployment.stop();
|
2024-12-05 21:04:41 +01:00
|
|
|
}, 60000); // 1 minute
|
|
|
|
|
2024-12-12 15:42:09 +01:00
|
|
|
afterAll(async () => {
|
|
|
|
await mongoService.disconnect();
|
|
|
|
});
|
|
|
|
|
2024-12-05 21:04:41 +01:00
|
|
|
it("should fix fake active entities in MongoDB", async () => {
|
|
|
|
console.log("Joining participant to room...");
|
|
|
|
const roomName = "TestRoom";
|
|
|
|
const participantIdentity = "TestParticipant1";
|
2024-12-12 15:42:09 +01:00
|
|
|
const pid = Livekit.joinParticipantToRoom(participantIdentity, roomName);
|
2024-12-05 21:04:41 +01:00
|
|
|
await sleep(5);
|
|
|
|
|
|
|
|
const roomStartedEvent = await mongoService.findStartEvent(EntityType.ROOM, roomName);
|
|
|
|
if (!roomStartedEvent) {
|
|
|
|
throw new Error("ROOM_CREATED event not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
const participantActiveEvent = await mongoService.findStartEvent(EntityType.PARTICIPANT, participantIdentity);
|
|
|
|
if (!participantActiveEvent) {
|
|
|
|
throw new Error("PARTICIPANT_ACTIVE event not found");
|
|
|
|
}
|
|
|
|
|
|
|
|
const roomId = roomStartedEvent.room.sid;
|
|
|
|
const participantId = participantActiveEvent.participant_id;
|
|
|
|
|
2024-12-12 15:42:09 +01:00
|
|
|
LocalDeployment.stopContainer("openvidu");
|
2024-12-05 21:04:41 +01:00
|
|
|
killProcess(pid);
|
|
|
|
await sleep(5);
|
2024-12-12 15:42:09 +01:00
|
|
|
LocalDeployment.startContainer("openvidu");
|
2024-12-05 21:04:41 +01:00
|
|
|
|
|
|
|
// Check if there is a fake close event for room and participant in MongoDB
|
|
|
|
// and the active entities are removed
|
|
|
|
let roomEndedEvent;
|
|
|
|
let participantLeftEvent;
|
|
|
|
let isRoomActive: boolean;
|
|
|
|
let isParticipantActive: boolean;
|
|
|
|
|
|
|
|
do {
|
|
|
|
console.log("Waiting for fake close events in MongoDB...");
|
|
|
|
await sleep(10);
|
|
|
|
roomEndedEvent = await mongoService.findCloseEvent(EntityType.ROOM, roomId);
|
|
|
|
participantLeftEvent = await mongoService.findCloseEvent(EntityType.PARTICIPANT, participantId);
|
|
|
|
isRoomActive = await mongoService.isActiveEntity(roomId);
|
|
|
|
isParticipantActive = await mongoService.isActiveEntity(participantId);
|
|
|
|
} while (!roomEndedEvent || !participantLeftEvent || isRoomActive || isParticipantActive);
|
|
|
|
|
|
|
|
// Check properties of fake ROOM_ENDED event
|
|
|
|
expect(isRoomActive).toBeFalsy();
|
|
|
|
expect(roomEndedEvent).not.toBeNull();
|
|
|
|
expect(roomEndedEvent).toMatchObject({
|
|
|
|
room_id: roomId,
|
|
|
|
room: {
|
|
|
|
sid: roomId,
|
|
|
|
name: roomName,
|
|
|
|
creation_time: roomStartedEvent.room.creation_time
|
|
|
|
}
|
|
|
|
});
|
|
|
|
expect(roomEndedEvent!.timestamp.seconds).toBeGreaterThan(roomStartedEvent.room.creation_time);
|
|
|
|
expect(roomEndedEvent!.openvidu_expire_at).toBeDefined();
|
|
|
|
|
|
|
|
// Check properties of fake PARTICIPANT_LEFT event
|
|
|
|
expect(isParticipantActive).toBeFalsy();
|
|
|
|
expect(participantLeftEvent).not.toBeNull();
|
|
|
|
expect(participantLeftEvent).toMatchObject({
|
|
|
|
room_id: roomId,
|
|
|
|
room: {
|
|
|
|
sid: roomId
|
|
|
|
},
|
|
|
|
participant_id: participantId,
|
|
|
|
participant: {
|
|
|
|
sid: participantId,
|
|
|
|
identity: participantIdentity,
|
|
|
|
joined_at: participantActiveEvent.participant.joined_at
|
|
|
|
}
|
|
|
|
});
|
|
|
|
expect(participantLeftEvent!.timestamp.seconds).toBeGreaterThan(participantActiveEvent.participant.joined_at);
|
|
|
|
expect(participantLeftEvent!.openvidu_expire_at).toBeDefined();
|
|
|
|
}, 300000); // 5 minutes
|
|
|
|
});
|