feat: open child process for each mock machine

This commit is contained in:
Sérgio Salgado 2021-01-13 14:15:47 +00:00 committed by Josh Harvey
parent 20727a60cd
commit 86e437a23a
3 changed files with 70 additions and 55 deletions

59
test/stress/child.js Normal file
View file

@ -0,0 +1,59 @@
const https = require('https')
const path = require('path')
const fs = require('fs')
const uuid = require('uuid')
function getCert (machineIndex) {
try {
return {
key: fs.readFileSync(path.resolve(__dirname, 'machines', `${machineIndex}`, 'client.key')),
cert: fs.readFileSync(path.resolve(__dirname, 'machines', `${machineIndex}`, 'client.pem'))
}
} catch (e) {
return null
}
}
function connectionInfo (machineIndex) {
try {
return JSON.parse(fs.readFileSync(path.resolve(__dirname, 'machines', `${machineIndex}`, 'connection_info.json')))
} catch (e) {
return null
}
}
let counter = 0
function request (machineIndex, pid) {
https.get({
hostname: 'localhost',
port: 3000,
path: '/poll?state=chooseCoin&model=unknown&version=7.5.0-beta.0&idle=true&pid=' + pid + '&sn=' + counter,
method: 'GET',
key: getCert(machineIndex).key,
cert: getCert(machineIndex).cert,
ca: connectionInfo(machineIndex).ca,
headers: {
date: new Date().toISOString(),
'request-id': uuid.v4()
}
}, res => {
res.on('data', (d) => {
process.send({ message: Buffer.from(d).toString() })
})
})
counter++
}
process.on('message', (msg) => {
console.log('Message from parent:', msg)
const pid = uuid.v4()
request(msg.machineIndex, pid)
setInterval(() => {
const pid = uuid.v4()
request(msg.machineIndex, pid)
}, 5000)
})