Skip to content

Commit

Permalink
integration-tests: Refactor utility code. Exec MongoDB disconnect met…
Browse files Browse the repository at this point in the history
…hod after running all tests
  • Loading branch information
juancarmore committed Dec 12, 2024
1 parent d320cb2 commit dead485
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 132 deletions.
100 changes: 50 additions & 50 deletions openvidu-test-integration/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions openvidu-test-integration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
"name": "openvidu-test-integration",
"version": "1.0.0",
"scripts": {
"test": "jest --forceExit",
"test:ci": "jest --ci --json --outputFile=test-results.json --forceExit"
"test": "jest",
"test:ci": "jest --ci --json --outputFile=test-results.json"
},
"devDependencies": {
"@types/jest": "29.5.14",
"@types/node": "22.10.1",
"@types/node": "22.10.2",
"jest": "29.7.0",
"mongodb": "6.11.0",
"mongodb": "6.12.0",
"ts-jest": "29.2.5",
"ts-node": "10.9.2",
"typescript": "5.7.2"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
import {
joinParticipantToRoom,
killProcess,
sleep,
startComposeContainer,
startLocalDeployment,
stopComposeContainer,
stopLocalDeployment
} from "./utils/helper";
import { killProcess, sleep } from "./utils/helper";
import { Livekit } from "./utils/livekit";
import { LocalDeployment } from "./utils/local-deployment";
import { MongoService } from "./utils/mongodb";
import { EntityType } from "./utils/types";

describe("OpenVidu active entities fixer", () => {
const mongoService = MongoService.getInstance();

beforeEach(async () => {
await startLocalDeployment();
await LocalDeployment.start();
}, 60000); // 1 minute

afterEach(() => {
stopLocalDeployment();
LocalDeployment.stop();
}, 60000); // 1 minute

afterAll(async () => {
await mongoService.disconnect();
});

it("should fix fake active entities in MongoDB", async () => {
console.log("Joining participant to room...");
const roomName = "TestRoom";
const participantIdentity = "TestParticipant1";
const pid = joinParticipantToRoom(participantIdentity, roomName);
const pid = Livekit.joinParticipantToRoom(participantIdentity, roomName);
await sleep(5);

const roomStartedEvent = await mongoService.findStartEvent(EntityType.ROOM, roomName);
Expand All @@ -41,10 +39,10 @@ describe("OpenVidu active entities fixer", () => {
const roomId = roomStartedEvent.room.sid;
const participantId = participantActiveEvent.participant_id;

stopComposeContainer("openvidu");
LocalDeployment.stopContainer("openvidu");
killProcess(pid);
await sleep(5);
startComposeContainer("openvidu");
LocalDeployment.startContainer("openvidu");

// Check if there is a fake close event for room and participant in MongoDB
// and the active entities are removed
Expand Down
83 changes: 19 additions & 64 deletions openvidu-test-integration/tests/utils/helper.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import { execSync, spawn } from "child_process";

const LOCAL_DEPLOYMENT_PATH = "../../openvidu-local-deployment/community/docker-compose.yaml";
const LIVEKIT_URL = "ws://localhost:7880";
const LIVEKIT_API_KEY = "devkey";
const LIVEKIT_API_SECRET = "secret";

const execCommand = (command: string): string => {
export const execCommand = (command: string): string => {
try {
return execSync(command).toString().trim();
} catch (error) {
Expand All @@ -15,72 +10,32 @@ const execCommand = (command: string): string => {
}
};

const execCommandInBackground = (command: string, args: string[]): number | undefined => {
const child = spawn(command, args, { detached: true, stdio: "ignore" });
// child.unref();
export const execCommandInBackground = (command: string, args: string[]): number | undefined => {
const child = spawn(command, args, { detached: true });

child.stdout.on("data", (data) => {
console.log(`stdout (${command}): ${data}`);
});
child.stderr.on("data", (data) => {
console.log(`stderr (${command}): ${data}`);
});
child.on("close", (code) => {
console.log(`child process (${command}) exited with code ${code}`);
});
child.on("error", (error) => {
console.error(`child process (${command}) error: ${error}`);
throw error;
});

return child.pid;
};

export const killProcess = (pid: number) => {
process.kill(-pid);
process.kill(pid);
};

export const sleep = async (seconds: number) => {
return new Promise((resolve) => {
setTimeout(resolve, seconds * 1000);
});
};

export const startLocalDeployment = async () => {
console.log("Starting local deployment...");
execCommand(`docker compose -f ${LOCAL_DEPLOYMENT_PATH} up -d`);
let statusCode: string;

// Check that container "ready-check" exited with code 0
do {
await sleep(1);
statusCode = execCommand("docker inspect ready-check -f {{.State.Status}}:{{.State.ExitCode}}");
} while (statusCode !== "exited:0");

console.log("Local deployment started");
};

export const stopLocalDeployment = () => {
console.log("Stopping local deployment...");
execCommand(`docker compose -f ${LOCAL_DEPLOYMENT_PATH} down -v`);
};

export const startComposeContainer = (containerName: string) => {
console.log(`Starting container ${containerName}...`);
execCommand(`docker compose -f ${LOCAL_DEPLOYMENT_PATH} start ${containerName}`);
};

export const stopComposeContainer = (containerName: string) => {
console.log(`Stopping container ${containerName}...`);
execCommand(`docker compose -f ${LOCAL_DEPLOYMENT_PATH} stop ${containerName}`);
};

export const joinParticipantToRoom = (participantIdentity: string, roomName: string): number => {
const command = "lk";
const args = [
"room",
"join",
"--url",
LIVEKIT_URL,
"--api-key",
LIVEKIT_API_KEY,
"--api-secret",
LIVEKIT_API_SECRET,
"--publish-demo",
"--identity",
participantIdentity,
roomName
];
const pid = execCommandInBackground(command, args);

if (!pid) {
throw new Error("Error starting participant");
}

return pid;
};
Loading

0 comments on commit dead485

Please sign in to comment.