Skip to content

Commit 107f5c0

Browse files
committed
adjusted code rabbit comments
1 parent a1eff3a commit 107f5c0

1 file changed

Lines changed: 16 additions & 12 deletions

File tree

test/cluster/cluster.cjs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const { node } = require('@elysiajs/node')
55

66
const workersAmount = 5
77
const port = 3000
8-
// Use args to differentiate paramater
8+
// Use args to differentiate parameter
99
// So code is more compact
1010
const arg2 = process.argv[2]
1111
let parameter
@@ -27,47 +27,51 @@ if (arg2 === 'port') {
2727
}
2828
}
2929

30+
/**
31+
* Method to stop workers and then exit the current program
32+
* @param {Worker[]} workers list of workers
33+
* @param {number} code exit code
34+
*/
3035
function shutdown(workers, code) {
3136
workers.forEach((it) => {
3237
it.kill()
3338
})
3439
exit(code)
3540
}
3641

37-
42+
/**
43+
* Start primary node
44+
* This will create workers and then send request to primary which will then
45+
* spread the requests to the workers. After that it will check for the result.
46+
*/
3847
async function startPrimary() {
3948
let workers = []
4049
for (let i = 0; i < workersAmount; i++) {
4150
workers.push(cluster.fork())
4251
}
4352
// we need some delay to allow Elysia to initialize
44-
const delayPromise = new Promise((resolve, reject) => {
45-
setTimeout(() => {
46-
resolve()
47-
}, 2000)
48-
})
49-
await delayPromise
53+
await new Promise((resolve) => setTimeout(resolve, 2000))
5054

5155
// Make n API calls, we should receive n different PIDs back
5256
// Checking if a server is run can only be done this way at the moment
5357
// because error is really deep in srvx and async
5458
// Even callback in Elysia.listen will be still be run even on error
5559
const promises = workers.map(async (it) => {
5660
const result = await fetch(`http://localhost:${port}`)
57-
const pid = await result.text()
58-
return pid
61+
const workerPid = await result.text()
62+
return workerPid
5963
})
6064
const result = await Promise.all(promises)
6165
const pidsCount = new Set(result).size;
6266
if (arg2 === 'false') {
63-
if (pidsCount != 1) {
67+
if (pidsCount !== 1) {
6468
console.error('❌ Server should return 1 pid.')
6569
shutdown(workers, 1)
6670
}
6771
console.log('✅ Test exclusive mode succeed!')
6872
shutdown(workers, 0)
6973
}
70-
if (pidsCount != workersAmount) {
74+
if (pidsCount !== workersAmount) {
7175
console.error("❌ Clustering error, number of pids doesn't match.")
7276
shutdown(workers, 1)
7377
}

0 commit comments

Comments
 (0)