33import com .beust .jcommander .Parameter ;
44import com .beust .jcommander .Parameters ;
55import com .zaxxer .hikari .HikariDataSource ;
6- import com .zaxxer .hikari .pool .HikariProxyConnection ;
76import org .slf4j .Logger ;
87import org .slf4j .LoggerFactory ;
98import org .utplsql .api .*;
2625import java .util .ArrayList ;
2726import java .util .Arrays ;
2827import 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 ) {
0 commit comments