Skip to content

Commit dead485

Browse files
committed
integration-tests: Refactor utility code. Exec MongoDB disconnect method after running all tests
1 parent d320cb2 commit dead485

File tree

7 files changed

+168
-132
lines changed

7 files changed

+168
-132
lines changed

openvidu-test-integration/package-lock.json

+50-50
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

openvidu-test-integration/package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
"name": "openvidu-test-integration",
33
"version": "1.0.0",
44
"scripts": {
5-
"test": "jest --forceExit",
6-
"test:ci": "jest --ci --json --outputFile=test-results.json --forceExit"
5+
"test": "jest",
6+
"test:ci": "jest --ci --json --outputFile=test-results.json"
77
},
88
"devDependencies": {
99
"@types/jest": "29.5.14",
10-
"@types/node": "22.10.1",
10+
"@types/node": "22.10.2",
1111
"jest": "29.7.0",
12-
"mongodb": "6.11.0",
12+
"mongodb": "6.12.0",
1313
"ts-jest": "29.2.5",
1414
"ts-node": "10.9.2",
1515
"typescript": "5.7.2"

openvidu-test-integration/tests/openvidu-active-entities-fixer.test.ts

+12-14
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,29 @@
1-
import {
2-
joinParticipantToRoom,
3-
killProcess,
4-
sleep,
5-
startComposeContainer,
6-
startLocalDeployment,
7-
stopComposeContainer,
8-
stopLocalDeployment
9-
} from "./utils/helper";
1+
import { killProcess, sleep } from "./utils/helper";
2+
import { Livekit } from "./utils/livekit";
3+
import { LocalDeployment } from "./utils/local-deployment";
104
import { MongoService } from "./utils/mongodb";
115
import { EntityType } from "./utils/types";
126

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

1610
beforeEach(async () => {
17-
await startLocalDeployment();
11+
await LocalDeployment.start();
1812
}, 60000); // 1 minute
1913

2014
afterEach(() => {
21-
stopLocalDeployment();
15+
LocalDeployment.stop();
2216
}, 60000); // 1 minute
2317

18+
afterAll(async () => {
19+
await mongoService.disconnect();
20+
});
21+
2422
it("should fix fake active entities in MongoDB", async () => {
2523
console.log("Joining participant to room...");
2624
const roomName = "TestRoom";
2725
const participantIdentity = "TestParticipant1";
28-
const pid = joinParticipantToRoom(participantIdentity, roomName);
26+
const pid = Livekit.joinParticipantToRoom(participantIdentity, roomName);
2927
await sleep(5);
3028

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

44-
stopComposeContainer("openvidu");
42+
LocalDeployment.stopContainer("openvidu");
4543
killProcess(pid);
4644
await sleep(5);
47-
startComposeContainer("openvidu");
45+
LocalDeployment.startContainer("openvidu");
4846

4947
// Check if there is a fake close event for room and participant in MongoDB
5048
// and the active entities are removed
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
import { execSync, spawn } from "child_process";
22

3-
const LOCAL_DEPLOYMENT_PATH = "../../openvidu-local-deployment/community/docker-compose.yaml";
4-
const LIVEKIT_URL = "ws://localhost:7880";
5-
const LIVEKIT_API_KEY = "devkey";
6-
const LIVEKIT_API_SECRET = "secret";
7-
8-
const execCommand = (command: string): string => {
3+
export const execCommand = (command: string): string => {
94
try {
105
return execSync(command).toString().trim();
116
} catch (error) {
@@ -15,72 +10,32 @@ const execCommand = (command: string): string => {
1510
}
1611
};
1712

18-
const execCommandInBackground = (command: string, args: string[]): number | undefined => {
19-
const child = spawn(command, args, { detached: true, stdio: "ignore" });
20-
// child.unref();
13+
export const execCommandInBackground = (command: string, args: string[]): number | undefined => {
14+
const child = spawn(command, args, { detached: true });
15+
16+
child.stdout.on("data", (data) => {
17+
console.log(`stdout (${command}): ${data}`);
18+
});
19+
child.stderr.on("data", (data) => {
20+
console.log(`stderr (${command}): ${data}`);
21+
});
22+
child.on("close", (code) => {
23+
console.log(`child process (${command}) exited with code ${code}`);
24+
});
25+
child.on("error", (error) => {
26+
console.error(`child process (${command}) error: ${error}`);
27+
throw error;
28+
});
29+
2130
return child.pid;
2231
};
2332

2433
export const killProcess = (pid: number) => {
25-
process.kill(-pid);
34+
process.kill(pid);
2635
};
2736

2837
export const sleep = async (seconds: number) => {
2938
return new Promise((resolve) => {
3039
setTimeout(resolve, seconds * 1000);
3140
});
3241
};
33-
34-
export const startLocalDeployment = async () => {
35-
console.log("Starting local deployment...");
36-
execCommand(`docker compose -f ${LOCAL_DEPLOYMENT_PATH} up -d`);
37-
let statusCode: string;
38-
39-
// Check that container "ready-check" exited with code 0
40-
do {
41-
await sleep(1);
42-
statusCode = execCommand("docker inspect ready-check -f {{.State.Status}}:{{.State.ExitCode}}");
43-
} while (statusCode !== "exited:0");
44-
45-
console.log("Local deployment started");
46-
};
47-
48-
export const stopLocalDeployment = () => {
49-
console.log("Stopping local deployment...");
50-
execCommand(`docker compose -f ${LOCAL_DEPLOYMENT_PATH} down -v`);
51-
};
52-
53-
export const startComposeContainer = (containerName: string) => {
54-
console.log(`Starting container ${containerName}...`);
55-
execCommand(`docker compose -f ${LOCAL_DEPLOYMENT_PATH} start ${containerName}`);
56-
};
57-
58-
export const stopComposeContainer = (containerName: string) => {
59-
console.log(`Stopping container ${containerName}...`);
60-
execCommand(`docker compose -f ${LOCAL_DEPLOYMENT_PATH} stop ${containerName}`);
61-
};
62-
63-
export const joinParticipantToRoom = (participantIdentity: string, roomName: string): number => {
64-
const command = "lk";
65-
const args = [
66-
"room",
67-
"join",
68-
"--url",
69-
LIVEKIT_URL,
70-
"--api-key",
71-
LIVEKIT_API_KEY,
72-
"--api-secret",
73-
LIVEKIT_API_SECRET,
74-
"--publish-demo",
75-
"--identity",
76-
participantIdentity,
77-
roomName
78-
];
79-
const pid = execCommandInBackground(command, args);
80-
81-
if (!pid) {
82-
throw new Error("Error starting participant");
83-
}
84-
85-
return pid;
86-
};

0 commit comments

Comments
 (0)