|
5 | 5 | import org.utplsql.api.compatibility.CompatibilityProxy; |
6 | 6 | import org.utplsql.api.db.DatabaseInformation; |
7 | 7 | import org.utplsql.api.db.DefaultDatabaseInformation; |
| 8 | +import org.utplsql.api.exception.OracleCreateStatmenetStuckException; |
8 | 9 | import org.utplsql.api.exception.SomeTestsFailedException; |
9 | 10 | import org.utplsql.api.exception.UtPLSQLNotInstalledException; |
10 | 11 | import org.utplsql.api.reporter.DocumentationReporter; |
|
16 | 17 | import java.sql.SQLException; |
17 | 18 | import java.util.ArrayList; |
18 | 19 | import java.util.List; |
| 20 | +import java.util.concurrent.*; |
19 | 21 |
|
20 | 22 | /** |
21 | 23 | * Created by Vinicius Avellar on 12/04/2017. |
@@ -124,6 +126,21 @@ private void delayedAddReporters() { |
124 | 126 | } |
125 | 127 | } |
126 | 128 |
|
| 129 | + private void handleException(Throwable e) throws SQLException { |
| 130 | + if (e instanceof SQLException) { |
| 131 | + SQLException sqlException = (SQLException) e; |
| 132 | + if (sqlException.getErrorCode() == SomeTestsFailedException.ERROR_CODE) { |
| 133 | + throw new SomeTestsFailedException(sqlException.getMessage(), e); |
| 134 | + } else if (((SQLException) e).getErrorCode() == UtPLSQLNotInstalledException.ERROR_CODE) { |
| 135 | + throw new UtPLSQLNotInstalledException(sqlException); |
| 136 | + } else { |
| 137 | + throw sqlException; |
| 138 | + } |
| 139 | + } else { |
| 140 | + throw new SQLException("Unknown exception, wrapping: " + e.getMessage(), e); |
| 141 | + } |
| 142 | + } |
| 143 | + |
127 | 144 | public void run(Connection conn) throws SQLException { |
128 | 145 |
|
129 | 146 | logger.info("TestRunner initialized"); |
@@ -156,19 +173,29 @@ public void run(Connection conn) throws SQLException { |
156 | 173 | options.reporterList.add(new DocumentationReporter().init(conn)); |
157 | 174 | } |
158 | 175 |
|
159 | | - try (TestRunnerStatement testRunnerStatement = compatibilityProxy.getTestRunnerStatement(options, conn)) { |
| 176 | + ExecutorService executor = Executors.newSingleThreadExecutor(); |
| 177 | + Callable<TestRunnerStatement> callable = () -> compatibilityProxy.getTestRunnerStatement(options, conn); |
| 178 | + Future<TestRunnerStatement> future = executor.submit(callable); |
| 179 | + |
| 180 | + // We want to leave the statement open in case of stuck scenario |
| 181 | + TestRunnerStatement testRunnerStatement = null; |
| 182 | + try { |
| 183 | + testRunnerStatement = future.get(2, TimeUnit.SECONDS); |
160 | 184 | logger.info("Running tests"); |
161 | 185 | testRunnerStatement.execute(); |
162 | 186 | logger.info("Running tests finished."); |
163 | | - } catch (SQLException e) { |
164 | | - if (e.getErrorCode() == SomeTestsFailedException.ERROR_CODE) { |
165 | | - throw new SomeTestsFailedException(e.getMessage(), e); |
166 | | - } else if (e.getErrorCode() == UtPLSQLNotInstalledException.ERROR_CODE) { |
167 | | - throw new UtPLSQLNotInstalledException(e); |
168 | | - } else { |
169 | | - throw e; |
170 | | - } |
| 187 | + testRunnerStatement.close(); |
| 188 | + } catch (TimeoutException e) { |
| 189 | + executor.shutdownNow(); |
| 190 | + throw new OracleCreateStatmenetStuckException(e); |
| 191 | + } catch (ExecutionException e) { |
| 192 | + handleException(e.getCause()); |
| 193 | + } catch (Exception e) { |
| 194 | + if (testRunnerStatement != null) testRunnerStatement.close(); |
| 195 | + handleException(e); |
171 | 196 | } |
| 197 | + |
| 198 | + executor.shutdown(); |
172 | 199 | } |
173 | 200 |
|
174 | 201 | /** |
|
0 commit comments