Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
refactor: move 'start' and 'end' events to the transaction runner
  • Loading branch information
honzajavorek committed Apr 4, 2019
1 parent d87f34f commit 2e93c2f
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 201 deletions.
56 changes: 9 additions & 47 deletions lib/Dredd.js
Expand Up @@ -11,7 +11,7 @@ const configureReporters = require('./configureReporters');
const handleRuntimeProblems = require('./handleRuntimeProblems');
const resolveLocations = require('./resolveLocations');
const logger = require('./logger');
const Runner = require('./TransactionRunner');
const TransactionRunner = require('./TransactionRunner');
const { applyConfiguration } = require('./configuration');


Expand All @@ -33,13 +33,13 @@ class Dredd {
};
this.files = [];
this.transactions = [];
this.runner = new Runner(this.configuration);
this.transactionRunner = new TransactionRunner(this.configuration);
this.logger = logger;
}

run(callback) {
this.logger.debug('Configuring reporters');
configureReporters(this.configuration, this.stats, this.runner);
configureReporters(this.configuration, this.stats, this.transactionRunner);

this.logger.debug('Resolving --require');
if (this.configuration.options.require) {
Expand All @@ -66,7 +66,6 @@ class Dredd {
return;
}

// Spin that merry-go-round
this.logger.debug('Reading API description files.');
this.loadFiles((loadErr) => {
if (loadErr) { return callback(loadErr, this.stats); }
Expand All @@ -75,17 +74,9 @@ class Dredd {
this.compileTransactions((compileErr) => {
if (compileErr) { return callback(compileErr, this.stats); }

this.logger.debug('Starting reporters and waiting until all of them are ready.');
this.emitStart((emitStartErr) => {
if (emitStartErr) { return callback(emitStartErr, this.stats); }

this.logger.debug('Starting transaction runner.');
this.startRunner((runnerErr) => {
if (runnerErr) { return callback(runnerErr, this.stats); }

this.logger.debug('Wrapping up testing.');
this.transactionsComplete(callback);
});
this.logger.debug('Starting transaction runner');
this.startTransactionRunner((runnerErr) => {
callback(runnerErr, this.stats);
});
});
});
Expand Down Expand Up @@ -187,38 +178,9 @@ Is the provided path correct?
});
}

// Start the runner
emitStart(callback) {
// More than one reporter is supported
let reporterCount = this.configuration.emitter.listeners('start').length;

// When event 'start' is emitted, function in callback is executed for each
// reporter registered by listeners
this.configuration.emitter.emit('start', this.configuration.apiDescriptions, (reporterError) => {
if (reporterError) { this.logger.error(reporterError.message); }

// Last called reporter callback function starts the runner
reporterCount--;
if (reporterCount === 0) {
callback(null, this.stats);
}
});
}

startRunner(callback) {
// Run all transactions
this.runner.config(this.configuration);
this.runner.run(this.transactions, callback);
}

transactionsComplete(callback) {
let reporterCount = this.configuration.emitter.listeners('end').length;
this.configuration.emitter.emit('end', () => {
reporterCount--;
if (reporterCount === 0) {
callback(null, this.stats);
}
});
startTransactionRunner(callback) {
this.transactionRunner.config(this.configuration);
this.transactionRunner.run(this.transactions, callback);
}
}

Expand Down
52 changes: 42 additions & 10 deletions lib/TransactionRunner.js
Expand Up @@ -42,19 +42,43 @@ class TransactionRunner {
}

run(transactions, callback) {
logger.debug('Sorting HTTP transactions');
transactions = this.configuration.options.sorted ? sortTransactions(transactions) : transactions;
logger.debug('Starting reporters and waiting until all of them are ready');
this.emitStart((emitStartErr) => {
if (emitStartErr) { return callback(emitStartErr); }

logger.debug('Configuring HTTP transactions');
transactions = transactions.map(this.configureTransaction.bind(this));
logger.debug('Sorting HTTP transactions');
transactions = this.configuration.options.sorted ? sortTransactions(transactions) : transactions;

// Remainings of functional approach, probs to be eradicated
logger.debug('Reading hook files and registering hooks');
addHooks(this, transactions, (addHooksError) => {
if (addHooksError) { return callback(addHooksError); }
logger.debug('Configuring HTTP transactions');
transactions = transactions.map(this.configureTransaction.bind(this));

logger.debug('Executing HTTP transactions');
this.executeAllTransactions(transactions, this.hooks, callback);
logger.debug('Reading hook files and registering hooks');
addHooks(this, transactions, (addHooksError) => {
if (addHooksError) { return callback(addHooksError); }

logger.debug('Executing HTTP transactions');
this.executeAllTransactions(transactions, this.hooks, (execAllTransErr) => {
if (execAllTransErr) { return callback(execAllTransErr); }

logger.debug('Wrapping up testing and waiting until all reporters are done');
this.emitEnd(callback);
});
});
});
}

emitStart(callback) {
// More than one reporter is supported
let reporterCount = this.configuration.emitter.listeners('start').length;

// When event 'start' is emitted, function in callback is executed for each
// reporter registered by listeners
this.configuration.emitter.emit('start', this.configuration.apiDescriptions, (reporterError) => {
if (reporterError) { logger.error(reporterError.message); }

// Last called reporter callback function starts the runner
reporterCount--;
if (reporterCount === 0) { callback(); }
});
}

Expand Down Expand Up @@ -599,6 +623,14 @@ include a message body: https://tools.ietf.org/html/rfc7231#section-6.3\
});
});
}

emitEnd(callback) {
let reporterCount = this.configuration.emitter.listeners('end').length;
this.configuration.emitter.emit('end', () => {
reporterCount--;
if (reporterCount === 0) { callback(); }
});
}
}

module.exports = TransactionRunner;

0 comments on commit 2e93c2f

Please sign in to comment.