-
Notifications
You must be signed in to change notification settings - Fork 456
Use asynchronous childProcess commands in Network tests - Closes #2334 #2382
Changes from 7 commits
425b2b4
ed88a37
8e82426
55fc17d
8e93e43
b5dbb4a
fd40d9b
b4220f8
bf16d9e
b835a01
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,9 @@ | |
const childProcess = require('child_process'); | ||
const utils = require('../utils'); | ||
|
||
const NODE_FINISHED_SYNC_REGEX = /Finished sync/; | ||
const NODE_FINISHED_SYNC_TIMEOUT = 40000; | ||
|
||
const getPeersStatus = peers => { | ||
return Promise.all( | ||
peers.map(peer => { | ||
|
@@ -52,7 +55,7 @@ const getAllPeers = sockets => { | |
); | ||
}; | ||
|
||
module.exports = { | ||
const common = { | ||
setMonitoringSocketsConnections: (params, configurations) => { | ||
// eslint-disable-next-line mocha/no-top-level-hooks | ||
before(done => { | ||
|
@@ -78,13 +81,57 @@ module.exports = { | |
getAllPeers, | ||
|
||
stopNode: nodeName => { | ||
return childProcess.execSync(`pm2 stop ${nodeName}`); | ||
return new Promise((resolve, reject) => { | ||
childProcess.exec(`pm2 stop ${nodeName}`, err => { | ||
if (err) { | ||
return reject( | ||
new Error(`Failed to stop node ${nodeName}: ${err.message}`) | ||
); | ||
} | ||
resolve(); | ||
}); | ||
}); | ||
}, | ||
startNode: nodeName => { | ||
childProcess.execSync(`pm2 start ${nodeName}`); | ||
return new Promise((resolve, reject) => { | ||
childProcess.exec(`pm2 start ${nodeName}`, err => { | ||
if (err) { | ||
return reject( | ||
new Error(`Failed to start node ${nodeName}: ${err.message}`) | ||
); | ||
} | ||
|
||
const pm2LogProcess = childProcess.spawn('pm2', ['logs', nodeName]); | ||
|
||
const nodeReadyTimeout = setTimeout(() => { | ||
pm2LogProcess.stdout.removeAllListeners('data'); | ||
pm2LogProcess.removeAllListeners('error'); | ||
reject(new Error(`Failed to start node ${nodeName} before timeout`)); | ||
}, NODE_FINISHED_SYNC_TIMEOUT); | ||
|
||
pm2LogProcess.once('error', err => { | ||
clearTimeout(nodeReadyTimeout); | ||
pm2LogProcess.stdout.removeAllListeners('data'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not remove error listener here as well? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It uses the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, based on your earlier feedback, I moved up the |
||
reject(new Error(`Failed to start node ${nodeName}: ${err.message}`)); | ||
}); | ||
pm2LogProcess.stdout.on('data', data => { | ||
const dataString = data.toString(); | ||
// Make sure that all nodes have fully synced before we | ||
// run the test cases. | ||
if (NODE_FINISHED_SYNC_REGEX.test(dataString)) { | ||
clearTimeout(nodeReadyTimeout); | ||
pm2LogProcess.stdout.removeAllListeners('error'); | ||
pm2LogProcess.stdout.removeAllListeners('data'); | ||
resolve(); | ||
} | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better practice to handle this use case would be to use This would be the best way to in the core and then either PM2 or some other process manager can also use it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't know about this feature. I'll give it a try. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That feature would be perfect but I can't get it to work for some reason. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May be we can sit together to check it out. |
||
}); | ||
}); | ||
}, | ||
restartNode: nodeName => { | ||
return childProcess.execSync(`pm2 restart ${nodeName}`); | ||
return common.stopNode(nodeName).then(() => { | ||
return common.startNode(nodeName); | ||
}); | ||
}, | ||
getNodesStatus: (sockets, cb) => { | ||
getAllPeers(sockets) | ||
|
@@ -109,3 +156,5 @@ module.exports = { | |
}); | ||
}, | ||
}; | ||
|
||
module.exports = common; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,10 +54,10 @@ module.exports = function( | |
|
||
describe('when a node is stopped', () => { | ||
before(done => { | ||
common.stopNode('node_1'); | ||
setTimeout(() => { | ||
done(); | ||
}, 2000); | ||
common | ||
.stopNode('node_1') | ||
.then(done) | ||
.catch(done); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please check if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We were running the network tests with the mocha This is an open issue with mocha: mochajs/mocha#3398 Removing the |
||
}); | ||
|
||
it(`peer manager should remove peer from the list and there should be ${EXPECTED_TOTAL_CONNECTIONS_AFTER_REMOVING_PEER} established connections from 500[0-9] ports`, done => { | ||
|
@@ -85,10 +85,10 @@ module.exports = function( | |
|
||
describe('when a stopped node is started', () => { | ||
before(done => { | ||
common.startNode('node_1'); | ||
setTimeout(() => { | ||
done(); | ||
}, 2000); | ||
common | ||
.startNode('node_1') | ||
.then(done) | ||
.catch(done); | ||
}); | ||
|
||
it(`there should be ${EXPECTED_TOTAL_CONNECTIONS} established connections from 500[0-9] ports`, done => { | ||
|
@@ -115,24 +115,22 @@ module.exports = function( | |
// To validate peers holding socket connection | ||
// Need to keep one peer so that we can validate | ||
// Duplicate socket connection exists or not | ||
it('stop all the nodes in the network except node_0', done => { | ||
it('stop all the nodes in the network except node_0', () => { | ||
const peersPromises = []; | ||
for (let i = 1; i < TOTAL_PEERS; i++) { | ||
common.stopNode(`node_${i}`); | ||
peersPromises.push(common.stopNode(`node_${i}`)); | ||
} | ||
setTimeout(() => { | ||
console.info('Wait for nodes to be stopped'); | ||
done(); | ||
}, 10000); | ||
console.info('Wait for nodes to be stopped'); | ||
return Promise.all(peersPromises); | ||
}); | ||
|
||
it('start all nodes that were stopped', done => { | ||
it('start all nodes that were stopped', () => { | ||
const peersPromises = []; | ||
for (let i = 1; i < TOTAL_PEERS; i++) { | ||
common.startNode(`node_${i}`); | ||
peersPromises.push(common.startNode(`node_${i}`)); | ||
} | ||
setTimeout(() => { | ||
console.info('Wait for nodes to be started'); | ||
done(); | ||
}, 10000); | ||
console.info('Wait for nodes to be started'); | ||
return Promise.all(peersPromises); | ||
}); | ||
|
||
describe('after all the node restarts', () => { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are we using
Finished sync
as a regular expression?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like doing
regex.test(...)
instead ofstring.indexOf(...) !== ...
it really doesn't matter for me though.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a comment in the code.