2
2
3
3
import com .beust .jcommander .Parameter ;
4
4
import com .beust .jcommander .Parameters ;
5
+ import com .zaxxer .hikari .HikariDataSource ;
5
6
import org .slf4j .Logger ;
6
7
import org .slf4j .LoggerFactory ;
7
8
import org .utplsql .api .*;
8
9
import org .utplsql .api .compatibility .CompatibilityProxy ;
9
10
import org .utplsql .api .db .DefaultDatabaseInformation ;
10
11
import org .utplsql .api .exception .DatabaseNotCompatibleException ;
12
+ import org .utplsql .api .exception .OracleCreateStatmenetStuckException ;
11
13
import org .utplsql .api .exception .SomeTestsFailedException ;
12
14
import org .utplsql .api .exception .UtPLSQLNotInstalledException ;
13
15
import org .utplsql .api .reporter .Reporter ;
14
16
import org .utplsql .api .reporter .ReporterFactory ;
15
17
import org .utplsql .cli .exception .DatabaseConnectionFailed ;
18
+ import org .utplsql .cli .exception .ReporterTimeoutException ;
16
19
import org .utplsql .cli .log .StringBlockFormatter ;
17
20
18
21
import javax .sql .DataSource ;
22
25
import java .util .ArrayList ;
23
26
import java .util .Arrays ;
24
27
import 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 .*;
28
29
29
30
/**
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.
31
34
*
32
35
* @author vinicious moreira
33
36
* @author pesse
@@ -138,27 +141,17 @@ else if ( logDebug ) {
138
141
LoggerConfiguration .configure (level );
139
142
}
140
143
141
- public int run () {
144
+ public int doRun () throws OracleCreateStatmenetStuckException {
142
145
init ();
143
146
outputMainInformation ();
144
147
148
+ HikariDataSource dataSource = null ;
149
+ int returnCode = 0 ;
145
150
try {
146
151
147
152
final List <Reporter > reporterList ;
148
153
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 );
162
155
163
156
initDatabase (dataSource );
164
157
reporterList = initReporters (dataSource );
@@ -172,48 +165,77 @@ public int run() {
172
165
ExecutorService executorService = Executors .newFixedThreadPool (1 + reporterList .size ());
173
166
174
167
// 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 )));
199
169
200
170
// 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
+ }
205
195
206
196
logger .info ("--------------------------------------" );
207
197
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 ) {
212
201
System .out .println (e .getMessage ());
213
- } catch (Exception e ) {
202
+ returnCode = Cli .DEFAULT_ERROR_CODE ;
203
+ } catch (Throwable e ) {
214
204
e .printStackTrace ();
205
+ returnCode = Cli .DEFAULT_ERROR_CODE ;
206
+ } finally {
207
+ if ( dataSource != null )
208
+ dataSource .close ();
215
209
}
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 ));
217
239
}
218
240
219
241
private ArrayList <String > getObjectList (String includeObjects ) {
0 commit comments