Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always pass a callback as the last function parameter - Closes #2449 #2597

Merged
5 changes: 4 additions & 1 deletion app.js
Expand Up @@ -103,7 +103,10 @@ var workersControllerPath = path.join(__dirname, 'workers_controller');
process.stdin.resume();

// Read build version from file
var versionBuild = fs.readFileSync(path.join(__dirname, '.build'), 'utf8').toString().trim();
var versionBuild = fs
.readFileSync(path.join(__dirname, '.build'), 'utf8')
.toString()
.trim();

/**
* Hash of the last git commit.
Expand Down
33 changes: 16 additions & 17 deletions test/common/utils/wait_for.js
Expand Up @@ -23,12 +23,12 @@ var apiHelpers = require('../helpers/api');
const { ACTIVE_DELEGATES } = global.constants;

/**
* @param {function} cb
* @param {number} [retries=10] retries
* @param {number} [timeout=200] timeout
* @param {string} [baseUrl='http://localhost:5000'] timeout
* @param {function} cb
*/
function blockchainReady(cb, retries, timeout, baseUrl, doNotLogRetries) {
function blockchainReady(retries, timeout, baseUrl, doNotLogRetries, cb) {
if (!retries) {
retries = 10;
}
Expand Down Expand Up @@ -82,7 +82,7 @@ function blockchainReady(cb, retries, timeout, baseUrl, doNotLogRetries) {
})();
}

function nodeStatus(cb, baseUrl) {
function nodeStatus(baseUrl, cb) {
var request = popsicle.get(
`${baseUrl || __testContext.baseUrl}/api/node/status`
);
Expand All @@ -104,39 +104,38 @@ function nodeStatus(cb, baseUrl) {
});
}
// Returns current block height
function getHeight(cb, baseUrl) {
nodeStatus((err, res) => {
function getHeight(baseUrl, cb) {
nodeStatus(baseUrl, (err, res) => {
if (err) {
return setImmediate(cb, err);
}
return setImmediate(cb, null, res.height);
}, baseUrl);
});
}

// Run callback on new round
function newRound(cb, baseUrl) {
getHeight((err, height) => {
function newRound(baseUrl, cb) {
getHeight(baseUrl, (err, height) => {
if (err) {
return cb(err);
}
var nextRound = slots.calcRound(height);
var blocksToWait = nextRound * ACTIVE_DELEGATES - height;
__testContext.debug('blocks to wait: '.grey, blocksToWait);
return newBlock(height, blocksToWait, cb);
}, baseUrl);
return newBlock(height, blocksToWait, null, cb);
});
}

// Waits for (n) blocks to be created
function blocks(blocksToWait, cb, baseUrl) {
getHeight((err, height) => {
function blocks(blocksToWait, baseUrl, cb) {
getHeight(baseUrl, (err, height) => {
if (err) {
return cb(err);
}
return newBlock(height, blocksToWait, cb, baseUrl);
}, baseUrl);
return newBlock(height, blocksToWait, baseUrl, cb);
});
}

function newBlock(height, blocksToWait, cb, baseUrl) {
function newBlock(height, blocksToWait, baseUrl, cb) {
if (blocksToWait === 0) {
return setImmediate(cb, null, height);
}
Expand Down Expand Up @@ -210,7 +209,7 @@ function confirmations(transactions, limitHeight) {
}
limit -= 1;

return blocksPromise(1)
return blocksPromise(1, null)
.then(() => {
return checkConfirmations(transactions);
})
Expand Down
8 changes: 5 additions & 3 deletions test/functional/functional.js
Expand Up @@ -18,11 +18,13 @@
before(done => {
// Retry 20 times with 3 second gap
require('../common/utils/wait_for').blockchainReady(
20,
3000,
null,
null,
reason => {
console.info(`Blockchain ready status: ${reason}`);
done();
},
20,
3000
}
);
});
2 changes: 1 addition & 1 deletion test/functional/http/get/blocks.js
Expand Up @@ -47,7 +47,7 @@ describe('GET /blocks', () => {
}

before(() => {
return waitFor.blocksPromise(2);
return waitFor.blocksPromise(2, null);
});

describe('?', () => {
Expand Down
4 changes: 2 additions & 2 deletions test/functional/http/get/cache.js
Expand Up @@ -154,7 +154,7 @@ describe('cached endpoints', () => {
expect(responses).to.deep.include(initialResponse.body);
})
.then(() => {
return waitForBlocksPromise(1);
return waitForBlocksPromise(1, null);
})
.then(() => {
return getJsonForKeyPromise(initialResponse.req.path);
Expand Down Expand Up @@ -217,7 +217,7 @@ describe('cached endpoints', () => {
})
).then(responses => {
expect(responses).to.deep.include(res.body);
return onNewRoundPromise().then(() => {
return onNewRoundPromise(null).then(() => {
return getJsonForKeyPromise(urlPath).then(result => {
expect(result).to.not.exist;
});
Expand Down
14 changes: 7 additions & 7 deletions test/network/network.js
Expand Up @@ -242,6 +242,10 @@ class Network {

return new Promise((resolve, reject) => {
waitFor.blockchainReady(
retries,
timeout,
`http://${configuration.ip}:${configuration.httpPort}`,
!logRetries,
err => {
if (err) {
return reject(
Expand All @@ -252,11 +256,7 @@ class Network {
);
}
return resolve();
},
retries,
timeout,
`http://${configuration.ip}:${configuration.httpPort}`,
!logRetries
}
);
});
}
Expand Down Expand Up @@ -293,6 +293,7 @@ class Network {
return new Promise((resolve, reject) => {
waitFor.blocks(
blocksToWait,
`http://${configuration.ip}:${configuration.httpPort}`,
err => {
if (err) {
return reject(
Expand All @@ -303,8 +304,7 @@ class Network {
);
}
return resolve();
},
`http://${configuration.ip}:${configuration.httpPort}`
}
);
});
}
Expand Down