Skip to content

Commit 56acff5

Browse files
committed
Refactoring of the threaded TaskRunner approach
1 parent 65119ba commit 56acff5

File tree

3 files changed

+114
-70
lines changed

3 files changed

+114
-70
lines changed

src/main/java/org/utplsql/cli/RunCommand.java

Lines changed: 47 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.beust.jcommander.Parameter;
44
import com.beust.jcommander.Parameters;
55
import com.zaxxer.hikari.HikariDataSource;
6-
import com.zaxxer.hikari.pool.HikariProxyConnection;
76
import org.slf4j.Logger;
87
import org.slf4j.LoggerFactory;
98
import org.utplsql.api.*;
@@ -26,13 +25,12 @@
2625
import java.util.ArrayList;
2726
import java.util.Arrays;
2827
import java.util.List;
29-
import java.util.concurrent.ExecutorService;
30-
import java.util.concurrent.Executors;
31-
import java.util.concurrent.Future;
32-
import java.util.concurrent.TimeUnit;
28+
import java.util.concurrent.*;
3329

3430
/**
35-
* Created by vinicius.moreira on 19/04/2017.
31+
* Issues a Run-Command with all the options
32+
*
33+
* Uses an executor to start a RunTestRunnerTask and one ReporterGatheringTask per Reporter requested.
3634
*
3735
* @author vinicious moreira
3836
* @author pesse
@@ -143,18 +141,17 @@ else if ( logDebug ) {
143141
LoggerConfiguration.configure(level);
144142
}
145143

146-
public int doRun() {
144+
public int doRun() throws OracleCreateStatmenetStuckException {
147145
init();
148146
outputMainInformation();
149147

148+
HikariDataSource dataSource = null;
149+
int returnCode = 0;
150150
try {
151151

152152
final List<Reporter> reporterList;
153153

154-
final File baseDir = new File("").getAbsoluteFile();
155-
final int[] returnCode = {0};
156-
157-
final DataSource dataSource = DataSourceProvider.getDataSource(getConnectionInfo(), getReporterManager().getNumberOfReporters() + 2);
154+
dataSource = (HikariDataSource) DataSourceProvider.getDataSource(getConnectionInfo(), getReporterManager().getNumberOfReporters() + 2);
158155

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

170167
// Run tests.
171-
Future<Integer> future = executorService.submit(() -> {
172-
Connection conn = null;
173-
try {
174-
conn = dataSource.getConnection();
175-
TestRunner testRunner = newTestRunner(reporterList);
176-
177-
logger.info("Running tests now.");
178-
logger.info("--------------------------------------");
179-
testRunner.run(conn);
180-
} catch (SomeTestsFailedException e) {
181-
returnCode[0] = this.failureExitCode;
182-
}
183-
catch (OracleCreateStatmenetStuckException e ) {
184-
try {
185-
conn.abort(Executors.newSingleThreadExecutor());
186-
conn = null;
187-
} catch (SQLException e1) {
188-
logger.error(e1.getMessage(), e1);
189-
}
190-
executorService.shutdownNow();
191-
return 3;
192-
}
193-
catch (SQLException e) {
194-
System.out.println(e.getMessage());
195-
//returnCode[0] = Cli.DEFAULT_ERROR_CODE;
196-
executorService.shutdownNow();
197-
return Cli.DEFAULT_ERROR_CODE;
198-
}
199-
finally {
200-
if ( conn != null ) {
201-
try {
202-
conn.close();
203-
} catch (SQLException e) {
204-
e.printStackTrace();
205-
}
206-
}
207-
}
208-
return 0;
209-
});
168+
Future<Boolean> future = executorService.submit(new RunTestRunnerTask(dataSource, newTestRunner(reporterList)));
210169

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

214-
Integer mainTestResult = future.get();
215-
216-
executorService.shutdown();
217-
if ( !executorService.awaitTermination(timeoutInMinutes, TimeUnit.MINUTES) ) {
173+
try {
174+
future.get(timeoutInMinutes, TimeUnit.MINUTES);
175+
} catch (TimeoutException e) {
176+
executorService.shutdownNow();
218177
throw new ReporterTimeoutException(timeoutInMinutes);
178+
} catch (ExecutionException e) {
179+
if (e.getCause() instanceof SomeTestsFailedException) {
180+
returnCode = failureExitCode;
181+
} else {
182+
executorService.shutdownNow();
183+
throw e.getCause();
184+
}
185+
} catch (InterruptedException e) {
186+
executorService.shutdownNow();
187+
throw e;
188+
}
189+
finally {
190+
executorService.shutdown();
191+
if (!executorService.awaitTermination(timeoutInMinutes, TimeUnit.MINUTES)) {
192+
throw new ReporterTimeoutException(timeoutInMinutes);
193+
}
219194
}
220195

221196
logger.info("--------------------------------------");
222197
logger.info("All tests done.");
223-
224-
((HikariDataSource)dataSource).close();
225-
226-
return mainTestResult;
227-
}
228-
catch ( DatabaseNotCompatibleException | UtPLSQLNotInstalledException | DatabaseConnectionFailed | ReporterTimeoutException e ) {
198+
} catch ( OracleCreateStatmenetStuckException e ) {
199+
throw e;
200+
} catch ( DatabaseNotCompatibleException | UtPLSQLNotInstalledException | DatabaseConnectionFailed | ReporterTimeoutException e ) {
229201
System.out.println(e.getMessage());
230-
} catch (Exception e) {
202+
returnCode = Cli.DEFAULT_ERROR_CODE;
203+
} catch (Throwable e) {
231204
e.printStackTrace();
205+
returnCode = Cli.DEFAULT_ERROR_CODE;
206+
} finally {
207+
if ( dataSource != null )
208+
dataSource.close();
232209
}
233-
return Cli.DEFAULT_ERROR_CODE;
210+
return returnCode;
234211
}
235212

236213
public int run() {
237-
int i = 1;
238-
int exitCode = doRun();
239-
// Retry
240-
while ( exitCode == 3 && i<10 ) {
241-
logger.warn("Retry");
242-
exitCode = doRun();
243-
i++;
214+
for ( int i = 1; i<5; i++ ) {
215+
try {
216+
return doRun();
217+
} catch (OracleCreateStatmenetStuckException e) {
218+
logger.warn("WARNING: Caught Oracle stuck during creation of Runner-Statement. Retrying ({})", i);
219+
}
244220
}
245-
return exitCode;
221+
222+
return Cli.DEFAULT_ERROR_CODE;
246223
}
247224

248225
private TestRunner newTestRunner( List<Reporter> reporterList) {
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package org.utplsql.cli;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.utplsql.api.TestRunner;
6+
import org.utplsql.api.exception.OracleCreateStatmenetStuckException;
7+
import org.utplsql.api.exception.SomeTestsFailedException;
8+
9+
import javax.sql.DataSource;
10+
import java.sql.Connection;
11+
import java.sql.SQLException;
12+
import java.util.concurrent.Callable;
13+
import java.util.concurrent.Executors;
14+
15+
/** Runs the utPLSQL Test-Runner
16+
*
17+
* Takes care of its connection.
18+
* In case of an OracleCreateStatementStuckException it will abort the connection, otherwise close it.
19+
*
20+
* @author pesse
21+
*/
22+
public class RunTestRunnerTask implements Callable<Boolean> {
23+
24+
private static final Logger logger = LoggerFactory.getLogger(RunTestRunnerTask.class);
25+
private DataSource dataSource;
26+
private TestRunner testRunner;
27+
28+
RunTestRunnerTask(DataSource dataSource, TestRunner testRunner) {
29+
this.dataSource = dataSource;
30+
this.testRunner = testRunner;
31+
}
32+
33+
@Override
34+
public Boolean call() throws Exception {
35+
Connection conn = null;
36+
try {
37+
conn = dataSource.getConnection();
38+
logger.info("Running tests now.");
39+
logger.info("--------------------------------------");
40+
testRunner.run(conn);
41+
} catch (SomeTestsFailedException e) {
42+
throw e;
43+
} catch (OracleCreateStatmenetStuckException e ) {
44+
try {
45+
conn.abort(Executors.newSingleThreadExecutor());
46+
conn = null;
47+
} catch (SQLException e1) {
48+
logger.error(e1.getMessage(), e1);
49+
}
50+
throw e;
51+
} catch (SQLException e) {
52+
System.out.println(e.getMessage());
53+
throw e;
54+
} finally {
55+
if ( conn != null ) {
56+
try {
57+
conn.close();
58+
} catch (SQLException e) {
59+
logger.error(e.getMessage(), e);
60+
}
61+
}
62+
}
63+
return true;
64+
}
65+
}

src/test/java/org/utplsql/cli/RunCommandIssue20Test.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
/**
1414
* Unit test for run command.
15+
*
16+
* @author philipp salivsberg
1517
*/
1618
class RunCommandIssue20Test {
1719

0 commit comments

Comments
 (0)