Skip to content

Commit

Permalink
add test to improve coverage report of caliper worker test coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Babatunde Sanusi <swisskid95@gmail.com>
  • Loading branch information
tunedev committed Oct 16, 2024
1 parent 3c6b739 commit e678fb2
Show file tree
Hide file tree
Showing 2 changed files with 326 additions and 18 deletions.
19 changes: 12 additions & 7 deletions packages/caliper-core/lib/worker/caliper-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,10 @@ class CaliperWorker {
* @param {Object} rateController rate controller object
* @async
*/
async runFixedNumber(workloadModule, number, rateController) {
async _runFixedNumber(workloadModule, number, rateController) {
const stats = this.internalTxObserver.getCurrentStatistics();
let error = undefined;

while (stats.getTotalSubmittedTx() < number && !error) {
await rateController.applyRateControl();

Expand All @@ -107,26 +108,31 @@ class CaliperWorker {
await CaliperWorker._waitForTxsToFinish(stats);
}


/**
* Perform test with specified test duration
* @param {object} workloadModule The user test module.
* @param {Object} duration duration to run for
* @param {Object} rateController rate controller object
* @async
*/
async runDuration(workloadModule, duration, rateController) {
async _runDuration(workloadModule, duration, rateController) {
const stats = this.internalTxObserver.getCurrentStatistics();
let startTime = stats.getRoundStartTime();
let error = undefined;
while ((Date.now() - startTime) < (duration * 1000) && !error) {
while ((Date.now() - startTime) < (duration * 1000) && !error) {
// eslint-disable-next-line no-unused-vars
const now = Date.now();
await rateController.applyRateControl();


// If this function calls this.workloadModule.submitTransaction() too quickly, micro task queue will be filled with unexecuted promises,
// and I/O task(s) will get no chance to be execute and fall into starvation, for more detail info please visit:
// https://snyk.io/blog/nodejs-how-even-quick-async-functions-can-block-the-event-loop-starve-io/
await this.setImmediatePromise(() => {
workloadModule.submitTransaction()
.catch(err => { error = err; });

});
}

Expand Down Expand Up @@ -161,7 +167,7 @@ class CaliperWorker {
await this.workloadModule.initializeWorkloadModule(this.workerIndex, prepareTestMessage.getWorkersNumber(), roundIndex, prepareTestMessage.getWorkloadSpec().arguments, this.connector, context);
await CaliperUtils.sleep(this.txUpdateTime);
} catch (err) {
Logger.info(`Worker [${this.workerIndex}] encountered an error during prepare test phase for round ${roundIndex}: ${(err.stack ? err.stack : err)}`);
Logger.warn(`Worker [${this.workerIndex}] encountered an error during prepare test phase for round ${roundIndex}: ${(err.stack ? err.stack : err)}`);
throw err;
} finally {
await this.connector.releaseContext(context);
Expand Down Expand Up @@ -201,12 +207,11 @@ class CaliperWorker {

if (testMessage.getRoundDuration()) {
const duration = testMessage.getRoundDuration(); // duration in seconds
await this.runDuration(this.workloadModule, duration, rateController);
await this._runDuration(this.workloadModule, duration, rateController);
} else {
const number = testMessage.getNumberOfTxs();
await this.runFixedNumber(this.workloadModule, number, rateController);
await this._runFixedNumber(this.workloadModule, number, rateController);
}

Logger.debug(`Worker #${this.workerIndex} finished round #${roundIndex}`, this.internalTxObserver.getCurrentStatistics().getCumulativeTxStatistics());
return this.internalTxObserver.getCurrentStatistics();
} catch (err) {
Expand Down
Loading

0 comments on commit e678fb2

Please sign in to comment.