Skip to content

Commit

Permalink
test: add test case for the issue in the bsock#11.
Browse files Browse the repository at this point in the history
If the client closes before the socket.call, next call would never resolve nor reject and locker would never unlock.
  • Loading branch information
nodech committed Oct 31, 2023
1 parent 15b572a commit c71aee1
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 8 deletions.
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -36,7 +36,7 @@
"bmutex": "~0.1.6",
"bns": "~0.15.0",
"bsert": "~0.0.12",
"bsock": "~0.1.9",
"bsock": "~0.1.11",
"bsocks": "~0.2.6",
"btcp": "~0.1.5",
"buffer-map": "~0.0.7",
Expand Down
37 changes: 36 additions & 1 deletion test/node-rescan-test.js
Expand Up @@ -7,7 +7,7 @@ const TX = require('../lib/primitives/tx');
const nodeCommon = require('../lib/blockchain/common');
const {scanActions} = nodeCommon;
const NodeContext = require('./util/node');
const {forEvent} = require('./util/common');
const {forEvent, sleep} = require('./util/common');
const MemWallet = require('./util/memwallet');
const rules = require('../lib/covenants/rules');

Expand Down Expand Up @@ -771,6 +771,41 @@ describe('Node Rescan Interactive API', function() {
i++;
}
});

// Make sure the client closing does not cause the chain locker to get
// indefinitely locked. (https://github.com/bcoin-org/bsock/pull/11)
it('should stop rescan when client closes', async () => {
const client2 = nodeCtx.nodeClient();

const addr = funderWallet.getAddress().toString(nodeCtx.network);

// Client does not need rescan hooks, because we make
// sure that the rescan hooks are never actually called.
// Client closes before they are called.
// We simulate this by acquiring chain lock before we
// call rescan and then closing the client.
const unlock = await nodeCtx.chain.locker.lock();
const rescan = client.rescanInteractive(0);
let err = null;
rescan.catch(e => err = e);

// make sure call reaches the server.
await sleep(50);
await client.close();
try {
await rescan;
} catch (e) {
err = e;
}

assert(err);
assert(err.message, 'Job timed out.');
unlock();

// Make sure lock was unlocked.
// w/o bsock update this will fail with timeout.
await client2.execute('generatetoaddress', [1, addr]);
});
});
});

Expand Down
21 changes: 20 additions & 1 deletion test/util/node.js
Expand Up @@ -8,13 +8,19 @@ const FullNode = require('../../lib/node/fullnode');
const plugin = require('../../lib/wallet/plugin');
const Network = require('../../lib/protocol/network');
const {NodeClient, WalletClient} = require('../../lib/client');
const Logger = require('blgr');

class NodeContext {
constructor(options = {}) {
this.name = 'node-test';
this.options = {};
this.node = null;
this.opened = false;
this.logger = new Logger({
console: true,
filename: null,
level: 'none'
});

this.nclient = null;
this.wclient = null;
Expand All @@ -32,7 +38,8 @@ class NodeContext {
network: 'regtest',
listen: false,
wallet: false,
spv: false
spv: false,
logger: this.logger
};

if (options.network != null)
Expand Down Expand Up @@ -196,6 +203,18 @@ class NodeContext {
await fs.rimraf(this.prefix);
}

/*
* Helpers
*/

enableLogging() {
this.logger.setLevel('debug');
}

disableLogging() {
this.logger.setLevel('none');
}

/**
* Execute an RPC using the node client.
* @param {String} method - RPC method
Expand Down

0 comments on commit c71aee1

Please sign in to comment.