From 267b2e326bff3693e8155ce0e8c30e07ac0dd7a6 Mon Sep 17 00:00:00 2001 From: Richard Brady Date: Mon, 10 Feb 2020 17:10:44 +0000 Subject: [PATCH] Add steps to reproduce issue #173 showing redundant connections. --- .../countConnectionsBitSocket.js | 57 +++++++++++++++++++ .../countConnectionsSocketIO.js | 55 ++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 test/e2e/count-connections-issue-173/countConnectionsBitSocket.js create mode 100644 test/e2e/count-connections-issue-173/countConnectionsSocketIO.js diff --git a/test/e2e/count-connections-issue-173/countConnectionsBitSocket.js b/test/e2e/count-connections-issue-173/countConnectionsBitSocket.js new file mode 100644 index 00000000..604ad097 --- /dev/null +++ b/test/e2e/count-connections-issue-173/countConnectionsBitSocket.js @@ -0,0 +1,57 @@ +const BITBOX = require("../../../lib/BITBOX").BITBOX; +const { exec } = require('child_process'); + +const bitbox = new BITBOX(); +const socket = new bitbox.Socket(); + +function countSockets(stage) { + return new Promise((resolve, reject) => { + // Call the lsof system command for outgoing internet connections. + exec(`lsof -i -n -P | grep ${process.pid}`, (err, stdout, stderr) => { + // Print list of open connections allowing a visual count to be done. + console.log(`Outbound connections from this node process ${stage}:\n${stdout}`); + resolve(); + }); + }); +} + +function sleep(ms) { + return new Promise(resolve => setTimeout(resolve, ms)); +} + +(async () => { + + await countSockets("before calling listen"); + + //First call to listen() which should create new connection. + socket.listen({"v": 3, "q": {"find": {}}}, + (message) => { + console.log("Callback from first query invoked."); + }); + + //Second call to listen() which should share connection with first call. + socket.listen({"v": 3, "q": {"find": {}}}, + (message) => { + console.log("Callback from first query invoked."); + }); + + // listen doesn't return a promise so wait 100ms for connections to establish. + await sleep(100); + + await countSockets("after calling listen twice"); + + // now close the socket + socket.close(); + + // callback from close() is short-circuited so give it 100ms to clean up. + await sleep(100); + + // check if any zombie connections remaining + await countSockets("after calling close (zombie connections)"); + + // exit process + process.exit(); + +})(); + + diff --git a/test/e2e/count-connections-issue-173/countConnectionsSocketIO.js b/test/e2e/count-connections-issue-173/countConnectionsSocketIO.js new file mode 100644 index 00000000..2cdbb88b --- /dev/null +++ b/test/e2e/count-connections-issue-173/countConnectionsSocketIO.js @@ -0,0 +1,55 @@ +const BITBOX = require("../../../lib/BITBOX").BITBOX; +const { exec } = require('child_process'); + +const bitbox = new BITBOX(); +const socket = new bitbox.Socket(); + +function countSockets(stage) { + return new Promise((resolve, reject) => { + // Call the lsof system command for outgoing internet connections. + exec(`lsof -i -n -P | grep ${process.pid}`, (err, stdout, stderr) => { + // Print list of open connections allowing a visual count to be done. + console.log(`Outbound connections from this node process ${stage}:\n${stdout}`); + resolve(); + }); + }); +} + +function sleep(ms) { + return new Promise(resolve => setTimeout(resolve, ms)); +} + +(async () => { + + await countSockets("before calling listen"); + + //First call to listen() which should create new connection. + socket.listen("transactions", (message) => { + console.log("Received a transaction."); + }); + + //Second call to listen() which should share connection with first call. + socket.listen("blocks", (message) => { + console.log("Received a block."); + }); + + // listen doesn't return a promise so wait 100ms for connections to establish. + await sleep(100); + + await countSockets("after calling listen twice"); + + // now close the socket + socket.close(); + + // callback from close() is short-circuited so give it 100ms to clean up. + await sleep(100); + + // check if any zombie connections remaining + await countSockets("after calling close (zombie connections)"); + + // exit process + process.exit(); + +})(); + +