-
Notifications
You must be signed in to change notification settings - Fork 340
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Limit the number of idle connections and catch errors in aux calls
- Loading branch information
1 parent
e606c37
commit 893e824
Showing
6 changed files
with
118 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
cdap-data-fabric/src/test/java/io/cdap/cdap/spi/data/sql/SqlTransactionRunnerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Copyright © 2023 Cask Data, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
|
||
package io.cdap.cdap.spi.data.sql; | ||
|
||
import io.cdap.cdap.common.metrics.NoOpMetricsCollectionService; | ||
import io.cdap.cdap.spi.data.StructuredTableAdmin; | ||
import io.cdap.cdap.spi.data.transaction.TransactionException; | ||
import java.io.IOException; | ||
import java.sql.Connection; | ||
import java.sql.SQLException; | ||
import javax.sql.DataSource; | ||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
|
||
/** | ||
* Unit Tests for SQL Transaction Runner. | ||
*/ | ||
public class SqlTransactionRunnerTest { | ||
|
||
@Test(expected = SqlTransactionException.class) | ||
public void testSqlExceptionPropagation() throws SQLException, TransactionException { | ||
StructuredTableAdmin mockTableAdmin = Mockito.mock(StructuredTableAdmin.class); | ||
DataSource mockDataSource = Mockito.mock(DataSource.class); | ||
Connection mockConnection = Mockito.mock(Connection.class); | ||
|
||
SqlTransactionRunner sqlTransactionRunner = new SqlTransactionRunner(mockTableAdmin, | ||
mockDataSource, new NoOpMetricsCollectionService(), false, 0); | ||
|
||
Mockito.when(mockDataSource.getConnection()).thenReturn(mockConnection); | ||
// throw an exception from setTransactionIsolation method | ||
Mockito.doThrow(new RuntimeException("RuntimeException", new IOException("IOException", | ||
new SQLException("SQLConnectionException", "08*")))) | ||
.when(mockConnection).setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); | ||
|
||
// run the test | ||
sqlTransactionRunner.run(context -> {}); | ||
} | ||
|
||
@Test(expected = SqlTransactionException.class) | ||
public void testSqlException() throws SQLException, TransactionException { | ||
StructuredTableAdmin mockTableAdmin = Mockito.mock(StructuredTableAdmin.class); | ||
DataSource mockDataSource = Mockito.mock(DataSource.class); | ||
Connection mockConnection = Mockito.mock(Connection.class); | ||
|
||
SqlTransactionRunner sqlTransactionRunner = new SqlTransactionRunner(mockTableAdmin, | ||
mockDataSource, new NoOpMetricsCollectionService(), false, 0); | ||
|
||
Mockito.when(mockDataSource.getConnection()).thenReturn(mockConnection); | ||
// throw an exception from setTransactionIsolation method | ||
Mockito.doThrow(new SQLException("SQLConnectionException", "08*")) | ||
.when(mockConnection).setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ); | ||
|
||
// run the test | ||
sqlTransactionRunner.run(context -> {}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters