Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactoring of the threaded TaskRunner approach
  • Loading branch information
pesse committed Mar 14, 2019
commit 56acff59d58a691200e4e742c83440bd5d20b337
117 changes: 47 additions & 70 deletions src/main/java/org/utplsql/cli/RunCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.zaxxer.hikari.HikariDataSource;
import com.zaxxer.hikari.pool.HikariProxyConnection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.utplsql.api.*;
Expand All @@ -26,13 +25,12 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.*;

/**
* Created by vinicius.moreira on 19/04/2017.
* Issues a Run-Command with all the options
*
* Uses an executor to start a RunTestRunnerTask and one ReporterGatheringTask per Reporter requested.
*
* @author vinicious moreira
* @author pesse
Expand Down Expand Up @@ -143,18 +141,17 @@ else if ( logDebug ) {
LoggerConfiguration.configure(level);
}

public int doRun() {
public int doRun() throws OracleCreateStatmenetStuckException {
init();
outputMainInformation();

HikariDataSource dataSource = null;
int returnCode = 0;
try {

final List<Reporter> reporterList;

final File baseDir = new File("").getAbsoluteFile();
final int[] returnCode = {0};

final DataSource dataSource = DataSourceProvider.getDataSource(getConnectionInfo(), getReporterManager().getNumberOfReporters() + 2);
dataSource = (HikariDataSource) DataSourceProvider.getDataSource(getConnectionInfo(), getReporterManager().getNumberOfReporters() + 2);

initDatabase(dataSource);
reporterList = initReporters(dataSource);
Expand All @@ -168,81 +165,61 @@ public int doRun() {
ExecutorService executorService = Executors.newFixedThreadPool(1 + reporterList.size());

// Run tests.
Future<Integer> future = executorService.submit(() -> {
Connection conn = null;
try {
conn = dataSource.getConnection();
TestRunner testRunner = newTestRunner(reporterList);

logger.info("Running tests now.");
logger.info("--------------------------------------");
testRunner.run(conn);
} catch (SomeTestsFailedException e) {
returnCode[0] = this.failureExitCode;
}
catch (OracleCreateStatmenetStuckException e ) {
try {
conn.abort(Executors.newSingleThreadExecutor());
conn = null;
} catch (SQLException e1) {
logger.error(e1.getMessage(), e1);
}
executorService.shutdownNow();
return 3;
}
catch (SQLException e) {
System.out.println(e.getMessage());
//returnCode[0] = Cli.DEFAULT_ERROR_CODE;
executorService.shutdownNow();
return Cli.DEFAULT_ERROR_CODE;
}
finally {
if ( conn != null ) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return 0;
});
Future<Boolean> future = executorService.submit(new RunTestRunnerTask(dataSource, newTestRunner(reporterList)));

// Gather each reporter results on a separate thread.
getReporterManager().startReporterGatherers(executorService, dataSource);

Integer mainTestResult = future.get();

executorService.shutdown();
if ( !executorService.awaitTermination(timeoutInMinutes, TimeUnit.MINUTES) ) {
try {
future.get(timeoutInMinutes, TimeUnit.MINUTES);
} catch (TimeoutException e) {
executorService.shutdownNow();
throw new ReporterTimeoutException(timeoutInMinutes);
} catch (ExecutionException e) {
if (e.getCause() instanceof SomeTestsFailedException) {
returnCode = failureExitCode;
} else {
executorService.shutdownNow();
throw e.getCause();
}
} catch (InterruptedException e) {
executorService.shutdownNow();
throw e;
}
finally {
executorService.shutdown();
if (!executorService.awaitTermination(timeoutInMinutes, TimeUnit.MINUTES)) {
throw new ReporterTimeoutException(timeoutInMinutes);
}
}

logger.info("--------------------------------------");
logger.info("All tests done.");

((HikariDataSource)dataSource).close();

return mainTestResult;
}
catch ( DatabaseNotCompatibleException | UtPLSQLNotInstalledException | DatabaseConnectionFailed | ReporterTimeoutException e ) {
} catch ( OracleCreateStatmenetStuckException e ) {
throw e;
} catch ( DatabaseNotCompatibleException | UtPLSQLNotInstalledException | DatabaseConnectionFailed | ReporterTimeoutException e ) {
System.out.println(e.getMessage());
} catch (Exception e) {
returnCode = Cli.DEFAULT_ERROR_CODE;
} catch (Throwable e) {
e.printStackTrace();
returnCode = Cli.DEFAULT_ERROR_CODE;
} finally {
if ( dataSource != null )
dataSource.close();
}
return Cli.DEFAULT_ERROR_CODE;
return returnCode;
}

public int run() {
int i = 1;
int exitCode = doRun();
// Retry
while ( exitCode == 3 && i<10 ) {
logger.warn("Retry");
exitCode = doRun();
i++;
for ( int i = 1; i<5; i++ ) {
try {
return doRun();
} catch (OracleCreateStatmenetStuckException e) {
logger.warn("WARNING: Caught Oracle stuck during creation of Runner-Statement. Retrying ({})", i);
}
}
return exitCode;

return Cli.DEFAULT_ERROR_CODE;
}

private TestRunner newTestRunner( List<Reporter> reporterList) {
Expand Down
65 changes: 65 additions & 0 deletions src/main/java/org/utplsql/cli/RunTestRunnerTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package org.utplsql.cli;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.utplsql.api.TestRunner;
import org.utplsql.api.exception.OracleCreateStatmenetStuckException;
import org.utplsql.api.exception.SomeTestsFailedException;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;

/** Runs the utPLSQL Test-Runner
*
* Takes care of its connection.
* In case of an OracleCreateStatementStuckException it will abort the connection, otherwise close it.
*
* @author pesse
*/
public class RunTestRunnerTask implements Callable<Boolean> {

private static final Logger logger = LoggerFactory.getLogger(RunTestRunnerTask.class);
private DataSource dataSource;
private TestRunner testRunner;

RunTestRunnerTask(DataSource dataSource, TestRunner testRunner) {
this.dataSource = dataSource;
this.testRunner = testRunner;
}

@Override
public Boolean call() throws Exception {
Connection conn = null;
try {
conn = dataSource.getConnection();
logger.info("Running tests now.");
logger.info("--------------------------------------");
testRunner.run(conn);
} catch (SomeTestsFailedException e) {
throw e;
} catch (OracleCreateStatmenetStuckException e ) {
try {
conn.abort(Executors.newSingleThreadExecutor());
conn = null;
} catch (SQLException e1) {
logger.error(e1.getMessage(), e1);
}
throw e;
} catch (SQLException e) {
System.out.println(e.getMessage());
throw e;
} finally {
if ( conn != null ) {
try {
conn.close();
} catch (SQLException e) {
logger.error(e.getMessage(), e);
}
}
}
return true;
}
}
2 changes: 2 additions & 0 deletions src/test/java/org/utplsql/cli/RunCommandIssue20Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

/**
* Unit test for run command.
*
* @author philipp salivsberg
*/
class RunCommandIssue20Test {

Expand Down