22
33import com .beust .jcommander .Parameter ;
44import com .beust .jcommander .Parameters ;
5+ import com .zaxxer .hikari .HikariDataSource ;
56import org .slf4j .Logger ;
67import org .slf4j .LoggerFactory ;
78import org .utplsql .api .*;
89import org .utplsql .api .compatibility .CompatibilityProxy ;
910import org .utplsql .api .db .DefaultDatabaseInformation ;
1011import org .utplsql .api .exception .DatabaseNotCompatibleException ;
12+ import org .utplsql .api .exception .OracleCreateStatmenetStuckException ;
1113import org .utplsql .api .exception .SomeTestsFailedException ;
1214import org .utplsql .api .exception .UtPLSQLNotInstalledException ;
1315import org .utplsql .api .reporter .Reporter ;
1416import org .utplsql .api .reporter .ReporterFactory ;
1517import org .utplsql .cli .exception .DatabaseConnectionFailed ;
18+ import org .utplsql .cli .exception .ReporterTimeoutException ;
1619import org .utplsql .cli .log .StringBlockFormatter ;
1720
1821import javax .sql .DataSource ;
2225import java .util .ArrayList ;
2326import java .util .Arrays ;
2427import java .util .List ;
25- import java .util .concurrent .ExecutorService ;
26- import java .util .concurrent .Executors ;
27- import java .util .concurrent .TimeUnit ;
28+ import java .util .concurrent .*;
2829
2930/**
30- * 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.
3134 *
3235 * @author vinicious moreira
3336 * @author pesse
@@ -138,27 +141,17 @@ else if ( logDebug ) {
138141 LoggerConfiguration .configure (level );
139142 }
140143
141- public int run () {
144+ public int doRun () throws OracleCreateStatmenetStuckException {
142145 init ();
143146 outputMainInformation ();
144147
148+ HikariDataSource dataSource = null ;
149+ int returnCode = 0 ;
145150 try {
146151
147152 final List <Reporter > reporterList ;
148153
149- final File baseDir = new File ("" ).getAbsoluteFile ();
150- final FileMapperOptions [] sourceMappingOptions = {null };
151- final FileMapperOptions [] testMappingOptions = {null };
152-
153- final int [] returnCode = {0 };
154-
155- sourceMappingOptions [0 ] = getFileMapperOptionsByParamListItem (this .sourcePathParams , baseDir );
156- testMappingOptions [0 ] = getFileMapperOptionsByParamListItem (this .testPathParams , baseDir );
157-
158- final List <String > finalIncludeObjectsList = getObjectList (includeObjects );
159- final List <String > finalExcludeObjectsList = getObjectList (excludeObjects );
160-
161- final DataSource dataSource = DataSourceProvider .getDataSource (getConnectionInfo (), getReporterManager ().getNumberOfReporters () + 1 );
154+ dataSource = (HikariDataSource ) DataSourceProvider .getDataSource (getConnectionInfo (), getReporterManager ().getNumberOfReporters () + 2 );
162155
163156 initDatabase (dataSource );
164157 reporterList = initReporters (dataSource );
@@ -172,48 +165,77 @@ public int run() {
172165 ExecutorService executorService = Executors .newFixedThreadPool (1 + reporterList .size ());
173166
174167 // Run tests.
175- executorService .submit (() -> {
176- try (Connection conn = dataSource .getConnection ()) {
177- TestRunner testRunner = new TestRunner ()
178- .addPathList (testPaths )
179- .addReporterList (reporterList )
180- .sourceMappingOptions (sourceMappingOptions [0 ])
181- .testMappingOptions (testMappingOptions [0 ])
182- .colorConsole (this .colorConsole )
183- .failOnErrors (true )
184- .skipCompatibilityCheck (skipCompatibilityCheck )
185- .includeObjects (finalIncludeObjectsList )
186- .excludeObjects (finalExcludeObjectsList );
187-
188- logger .info ("Running tests now." );
189- logger .info ("--------------------------------------" );
190- testRunner .run (conn );
191- } catch (SomeTestsFailedException e ) {
192- returnCode [0 ] = this .failureExitCode ;
193- } catch (SQLException e ) {
194- System .out .println (e .getMessage ());
195- returnCode [0 ] = Cli .DEFAULT_ERROR_CODE ;
196- executorService .shutdownNow ();
197- }
198- });
168+ Future <Boolean > future = executorService .submit (new RunTestRunnerTask (dataSource , newTestRunner (reporterList )));
199169
200170 // Gather each reporter results on a separate thread.
201- getReporterManager ().startReporterGatherers (executorService , dataSource , returnCode );
202-
203- executorService .shutdown ();
204- executorService .awaitTermination (timeoutInMinutes , TimeUnit .MINUTES );
171+ getReporterManager ().startReporterGatherers (executorService , dataSource );
172+
173+ try {
174+ future .get (timeoutInMinutes , TimeUnit .MINUTES );
175+ } catch (TimeoutException e ) {
176+ executorService .shutdownNow ();
177+ 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+ }
194+ }
205195
206196 logger .info ("--------------------------------------" );
207197 logger .info ("All tests done." );
208-
209- return returnCode [0 ];
210- }
211- catch ( DatabaseNotCompatibleException | UtPLSQLNotInstalledException | DatabaseConnectionFailed e ) {
198+ } catch ( OracleCreateStatmenetStuckException e ) {
199+ throw e ;
200+ } catch ( DatabaseNotCompatibleException | UtPLSQLNotInstalledException | DatabaseConnectionFailed | ReporterTimeoutException e ) {
212201 System .out .println (e .getMessage ());
213- } catch (Exception e ) {
202+ returnCode = Cli .DEFAULT_ERROR_CODE ;
203+ } catch (Throwable e ) {
214204 e .printStackTrace ();
205+ returnCode = Cli .DEFAULT_ERROR_CODE ;
206+ } finally {
207+ if ( dataSource != null )
208+ dataSource .close ();
215209 }
216- return 1 ;
210+ return returnCode ;
211+ }
212+
213+ public int run () {
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+ }
220+ }
221+
222+ return Cli .DEFAULT_ERROR_CODE ;
223+ }
224+
225+ private TestRunner newTestRunner ( List <Reporter > reporterList ) {
226+
227+ final File baseDir = new File ("" ).getAbsoluteFile ();
228+
229+ return new TestRunner ()
230+ .addPathList (testPaths )
231+ .addReporterList (reporterList )
232+ .sourceMappingOptions (getFileMapperOptionsByParamListItem (this .sourcePathParams , baseDir ))
233+ .testMappingOptions (getFileMapperOptionsByParamListItem (this .testPathParams , baseDir ))
234+ .colorConsole (this .colorConsole )
235+ .failOnErrors (true )
236+ .skipCompatibilityCheck (skipCompatibilityCheck )
237+ .includeObjects (getObjectList (includeObjects ))
238+ .excludeObjects (getObjectList (excludeObjects ));
217239 }
218240
219241 private ArrayList <String > getObjectList (String includeObjects ) {
0 commit comments