Skip to content

Commit

Permalink
test: add utility to wait for values
Browse files Browse the repository at this point in the history
  • Loading branch information
braydonf committed Apr 16, 2019
1 parent 43f4df4 commit 4db8d99
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
5 changes: 5 additions & 0 deletions test/indexer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const FullNode = require('../lib/node/fullnode');
const Network = require('../lib/protocol/network');
const network = Network.get('regtest');
const {NodeClient, WalletClient} = require('bclient');
const {forValue} = require('./util/common');

const workers = new WorkerPool({
enabled: true
Expand Down Expand Up @@ -298,6 +299,8 @@ describe('Indexer', function() {
assert.equal(blocks.length, 1);
}

await forValue(node.chain, 'height', 160);

// Send unconfirmed to the vector addresses.
for (let i = 0; i < 3; i++) {
for (const v of vectors) {
Expand All @@ -307,6 +310,8 @@ describe('Indexer', function() {
unconfirmed.push(txid);
}
}

await forValue(node.mempool.map, 'size', 6);
});

after(async () => {
Expand Down
21 changes: 21 additions & 0 deletions test/util/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,27 @@ common.rimraf = async function(p) {
return await fs.rimraf(p);
};

common.forValue = async function(obj, key, val, timeout = 60000) {
assert(typeof obj === 'object');
assert(typeof key === 'string');

const ms = 10;
let interval = null;
let count = 0;
return new Promise((resolve, reject) => {
interval = setInterval(() => {
if (obj[key] === val) {
clearInterval(interval);
resolve();
} else if (count * ms >= timeout) {
clearInterval(interval);
reject(new Error('Timeout waiting for value.'));
}
count += 1;
}, ms);
});
};

function parseUndo(data) {
const br = bio.read(data);
const items = [];
Expand Down

0 comments on commit 4db8d99

Please sign in to comment.