Skip to content

Commit dd91645

Browse files
committed
Added tests
1 parent f3745c5 commit dd91645

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

jdbc/src/main/java/tech/ydb/jdbc/context/TableTxExecutor.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.sql.SQLException;
44
import java.util.concurrent.locks.ReentrantLock;
5+
import java.util.logging.Level;
6+
import java.util.logging.Logger;
57

68
import com.google.common.cache.Cache;
79
import com.google.common.hash.Hashing;
@@ -31,6 +33,7 @@
3133
* @author mzinal
3234
*/
3335
public class TableTxExecutor extends QueryServiceExecutor {
36+
private static final Logger LOGGER = Logger.getLogger(TableTxExecutor.class.getName());
3437
private static final ReentrantLock VALIDATE_LOCK = new ReentrantLock();
3538

3639
private static final String CREATE_SQL = ""
@@ -147,6 +150,7 @@ public static void validate(YdbContext ctx, String tablePath, Cache<String, Tabl
147150

148151
VALIDATE_LOCK.lock();
149152
try {
153+
LOGGER.log(Level.INFO, "Validate TxTableExecutor {0}", tablePath);
150154
if (cache.getIfPresent(tablePath) != null) {
151155
return;
152156
}
@@ -158,6 +162,7 @@ public static void validate(YdbContext ctx, String tablePath, Cache<String, Tabl
158162

159163
// validate table name
160164
Result<TableDescription> res = retryCtx.supplyResult(s -> s.describeTable(tablePath)).join();
165+
LOGGER.log(Level.INFO, "Describe TxTableExecutor {0} -> {1}", new Object[] {tablePath, res.getStatus()});
161166
if (res.isSuccess()) {
162167
cache.put(tablePath, res.getValue());
163168
return;
@@ -171,12 +176,14 @@ public static void validate(YdbContext ctx, String tablePath, Cache<String, Tabl
171176
// Try to create a table
172177
String query = String.format(CREATE_SQL, tablePath);
173178
Status status = retryCtx.supplyStatus(session -> session.executeSchemeQuery(query)).join();
179+
LOGGER.log(Level.INFO, "Create TxTableExecutor {0} -> {1}", new Object[] {tablePath, status});
174180
if (!status.isSuccess()) {
175181
throw ExceptionFactory.createException("Cannot initialize TableTxExecutor with tx table " + tablePath,
176182
new UnexpectedResultException("Cannot create table", status));
177183
}
178184

179185
Result<TableDescription> res2 = retryCtx.supplyResult(s -> s.describeTable(tablePath)).join();
186+
LOGGER.log(Level.INFO, "Validate TxTableExecutor {0} -> {1}", new Object[] {tablePath, res2.getStatus()});
180187
if (!res2.isSuccess()) {
181188
throw ExceptionFactory.createException("Cannot initialize TableTxExecutor with tx table " + tablePath,
182189
new UnexpectedResultException("Cannot describe after creating", res2.getStatus()));

jdbc/src/test/java/tech/ydb/jdbc/YdbDriverTxValidateTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import java.sql.ResultSet;
66
import java.sql.SQLException;
77
import java.sql.Statement;
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
import java.util.concurrent.CompletableFuture;
811

912
import org.junit.Assert;
1013
import org.junit.jupiter.api.Assertions;
@@ -14,6 +17,7 @@
1417
import tech.ydb.core.Status;
1518
import tech.ydb.core.StatusCode;
1619
import tech.ydb.core.UnexpectedResultException;
20+
import tech.ydb.jdbc.context.YdbContext;
1721
import tech.ydb.jdbc.impl.YdbTracerImpl;
1822
import tech.ydb.jdbc.impl.helper.ExceptionAssert;
1923
import tech.ydb.jdbc.impl.helper.JdbcConnectionExtention;
@@ -94,6 +98,33 @@ public void basicUsageTest() throws SQLException {
9498
}
9599
}
96100

101+
@Test
102+
public void testContextCacheConncurrent() throws SQLException {
103+
String url = jdbcURL.withArg("withTxValidationTable", "tx2_store").build();
104+
List<CompletableFuture<YdbConnection>> list = new ArrayList<>();
105+
106+
for (int idx = 0; idx < 20; idx++) {
107+
list.add(CompletableFuture.supplyAsync(() -> {
108+
try {
109+
Connection connection = DriverManager.getConnection(url);
110+
return connection.unwrap(YdbConnection.class);
111+
} catch (SQLException ex) {
112+
throw new RuntimeException("Cannot connect", ex);
113+
}
114+
}));
115+
}
116+
117+
YdbContext first = list.get(0).join().getCtx();
118+
119+
for (CompletableFuture<YdbConnection> future: list) {
120+
Assertions.assertEquals(first, future.join().getCtx());
121+
}
122+
123+
for (CompletableFuture<YdbConnection> future: list) {
124+
future.join().close();
125+
}
126+
}
127+
97128
@Test
98129
public void commitedTxTest() throws SQLException {
99130
String url = jdbcURL.withArg("withTxValidationTable", "tx1_store").build();

0 commit comments

Comments
 (0)