Skip to content

Commit

Permalink
feat: generate health check test for daily.
Browse files Browse the repository at this point in the history
  • Loading branch information
woog2roid committed Apr 28, 2024
1 parent 3c48742 commit aa8099f
Show file tree
Hide file tree
Showing 4 changed files with 6,062 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.env
node_modules
77 changes: 77 additions & 0 deletions __tests__/health-check.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
const mysql = require("mysql2/promise");
const redis = require("ioredis");

const dotenv = require("dotenv");
dotenv.config();

describe("Health check", () => {
/* ---------------------- Gateway Server Health Check. ---------------------- */
it("should return OK if gateways server is healthy", async () => {
try {
const response = await fetch(process.env.GATEWAY_SERVER_URL + "/");
const data = await response.json();
expect(data.message).toEqual("ok");
} catch (error) {
console.error("Gateway server health check failed:", error);
throw error;
}
});

/* ------------------------- Ai Server Health Check. ------------------------ */
// it("should return OK from gateway and ai server both, if ai server is healthy", async () => {
// try {
// // from gateway
// const gatewayResponse = await fetch(process.env.GATEWAY_SERVER_URL + "/ai");
// const gatewayData = await gatewayResponse.json();
// expect(gatewayData).toEqual("ok");
// // from AI
// const aiResponse = await fetch(process.env.AI_SERVER_URL + "/");
// const aiData = await aiResponse.json();
// expect(aiData.status).toEqual(200);
// } catch (error) {
// console.error("AI server health check failed:", error);
// throw error;
// }
// });

/* --------------------- MySQL Dev-Server Health Check. --------------------- */
it("should return OK if MySQL dev-server is healthy", async () => {
try {
const connection = await mysql.createConnection({
host: process.env.MYSQL_HOST,
user: process.env.MYSQL_USER,
password: process.env.MYSQL_PASSWORD,
database: process.env.MYSQL_DATABASE,
});

const [rows] = await connection.query("SELECT 1");
expect(rows).toEqual([{ 1: 1 }]);
await connection.end();
} catch (error) {
console.error("MySQL health check failed:", error);
throw error;
}
});

/* --------------------- Redis Dev-Server Health Check. --------------------- */
it("should return OK if Redis dev-server is healthy", async () => {
const client = redis.createClient({
host: process.env.REDIS_HOST,
password: process.env.REDIS_PASSWORD,
port: process.env.REDIS_PORT,
});

try {
await new Promise((resolve, reject) => {
client.on("connect", resolve);
client.on("error", reject);
});
expect(client.status).toEqual("connect");
} catch (error) {
console.error("Redis health check failed:", error);
throw error;
} finally {
await client.quit();
}
});
});
Loading

0 comments on commit aa8099f

Please sign in to comment.