From 8a313d335fcfbe35154e6acc73485bd4d1c7d909 Mon Sep 17 00:00:00 2001 From: Hishida Masato Date: Mon, 2 Oct 2023 14:24:34 +0900 Subject: [PATCH] remove iceaxe-dbtest --- .github/workflows/ci-build.yml | 2 +- modules/iceaxe-dbtest/.gitignore | 1 - modules/iceaxe-dbtest/build.gradle | 13 - .../test/connector/DbConnectErrorTest.java | 146 ---- .../test/connector/DbConnectorTest.java | 23 - .../test/delete/DbDeleteExplainTest.java | 67 -- .../test/delete/DbDeleteIrregularTest.java | 57 -- .../iceaxe/test/delete/DbDeleteNoPkTest.java | 102 --- .../iceaxe/test/delete/DbDeleteTest.java | 314 --------- .../test/error/DbErrorInactiveTxTest.java | 205 ------ .../error/DbErrorMultiplexInsertTest.java | 126 ---- .../error/DbErrorMultiplexSelectTest.java | 138 ---- .../test/error/DbErrorTableNotExistsTest.java | 180 ----- .../test/error/DbGeneratedRowidTest.java | 219 ------ .../test/error/DbSystemReservedWordTest.java | 74 --- .../insert/DbInsertBoundaryValueTest.java | 147 ---- .../iceaxe/test/insert/DbInsertCharTest.java | 168 ----- .../test/insert/DbInsertConstraintTest.java | 201 ------ .../test/insert/DbInsertDecimalTest.java | 112 ---- .../test/insert/DbInsertDuplicate2Test.java | 199 ------ .../test/insert/DbInsertDuplicateTest.java | 177 ----- .../iceaxe/test/insert/DbInsertErrorTest.java | 161 ----- .../test/insert/DbInsertExplainTest.java | 62 -- .../test/insert/DbInsertIrregularTest.java | 41 -- .../test/insert/DbInsertMultiThread2Test.java | 248 ------- .../test/insert/DbInsertMultiThreadTest.java | 188 ------ .../iceaxe/test/insert/DbInsertNoPkTest.java | 113 ---- .../iceaxe/test/insert/DbInsertTest.java | 244 ------- .../test/insert/DbInsertVarcharTest.java | 45 -- .../iceaxe/test/select/DbQueryResultTest.java | 147 ---- .../test/select/DbResultRecordTest.java | 67 -- .../iceaxe/test/select/DbSelect2LoopTest.java | 315 --------- .../test/select/DbSelectAggregateTest.java | 206 ------ .../iceaxe/test/select/DbSelectAliasTest.java | 117 ---- .../iceaxe/test/select/DbSelectAllTest.java | 123 ---- .../select/DbSelectBoundaryValueTest.java | 212 ------ .../iceaxe/test/select/DbSelectCountTest.java | 162 ----- .../iceaxe/test/select/DbSelectDateTest.java | 95 --- .../iceaxe/test/select/DbSelectEmptyTest.java | 137 ---- .../iceaxe/test/select/DbSelectErrorTest.java | 224 ------- .../test/select/DbSelectExplainTest.java | 94 --- .../iceaxe/test/select/DbSelectFetchTest.java | 68 -- .../iceaxe/test/select/DbSelectFewTest.java | 86 --- .../test/select/DbSelectIrregularTest.java | 46 -- .../iceaxe/test/select/DbSelectJoinTest.java | 270 -------- .../test/select/DbSelectLiteralTest.java | 109 --- .../test/select/DbSelectOtherTxTest.java | 134 ---- .../iceaxe/test/select/DbSelectTest.java | 157 ----- .../select/DbSelectWhereExpressionTest.java | 145 ---- .../test/select/DbSelectWhileInsertTest.java | 173 ----- .../test/session/DbMultiSessionTest.java | 203 ------ .../test/session/DbSessionErrorTest.java | 92 --- .../iceaxe/test/session/DbSessionTest.java | 76 --- .../session/DbSessionTxFileLoggerTest.java | 201 ------ .../iceaxe/test/sql/DbBindVariableTest.java | 184 ------ .../iceaxe/test/sql/DbPsExecuteErrorTest.java | 112 ---- .../iceaxe/test/sql/DbQueryCommentTest.java | 87 --- .../test/sql/DbStatementCommentTest.java | 64 -- .../test/table/DbCreateTableErrorTest.java | 110 --- .../test/table/DbCreateTableExplainTest.java | 66 -- .../iceaxe/test/table/DbCreateTableTest.java | 196 ------ .../test/table/DbDropTableExplainTest.java | 60 -- .../iceaxe/test/table/DbDropTableTest.java | 73 -- .../iceaxe/test/table/DbTableListTest.java | 56 -- .../test/table/DbTableMetadataTest.java | 92 --- .../test/timeout/DbServerStopSessionTest.java | 56 -- .../timeout/DbServerStopTransactionTest.java | 61 -- .../iceaxe/test/timeout/DbSlotLimitTest.java | 98 --- .../timeout/DbTimeoutExplainCloseTest.java | 127 ---- .../timeout/DbTimeoutExplainConnectTest.java | 104 --- .../test/timeout/DbTimeoutPsCloseTest.java | 90 --- .../test/timeout/DbTimeoutPsConnectTest.java | 90 --- .../timeout/DbTimeoutResultCheckTest.java | 94 --- .../timeout/DbTimeoutResultCloseTest.java | 87 --- .../test/timeout/DbTimeoutRsCloseTest.java | 88 --- .../test/timeout/DbTimeoutRsConnectTest.java | 91 --- .../timeout/DbTimeoutSessionCloseTest.java | 71 -- .../timeout/DbTimeoutSessionConnectTest.java | 73 -- .../timeout/DbTimeoutTableListCloseTest.java | 90 --- .../DbTimeoutTableListConnectTest.java | 67 -- .../DbTimeoutTableMetadataCloseTest.java | 90 --- .../DbTimeoutTableMetadataConnectTest.java | 67 -- .../DbTimeoutTransactionBeginTest.java | 75 --- .../DbTimeoutTransactionCloseTest.java | 74 --- .../DbTimeoutTransactionCommitTest.java | 76 --- .../DbTimeoutTransactionRollbackTest.java | 75 --- .../timeout/DbTimeoutTxStatusCloseTest.java | 93 --- .../timeout/DbTimeoutTxStatusConnectTest.java | 70 -- .../iceaxe/test/timeout/DbTimetoutTest.java | 112 ---- .../test/timeout/PipeServerThtread.java | 117 ---- .../test/timeout/TimeoutServerThread.java | 54 -- .../transaction/DbManagerCounterTest.java | 242 ------- .../transaction/DbManagerExceptionTest.java | 90 --- .../transaction/DbManagerExecuteDdlTest.java | 60 -- .../DbManagerExecuteQueryTest.java | 625 ------------------ .../DbManagerExecuteStatementTest.java | 148 ----- .../DbTransactionConflictLtxTest.java | 324 --------- .../DbTransactionConflictOccTest.java | 332 ---------- .../DbTransactionConflictRtxTest.java | 208 ------ .../transaction/DbTransactionErrorTest.java | 223 ------- .../DbTransactionExceptionTest.java | 124 ---- .../DbTransactionExecuteQueryTest.java | 205 ------ .../DbTransactionExecuteStatementTest.java | 150 ----- .../DbTransactionParallelTest.java | 171 ----- .../DbTransactionReadArea3Test.java | 268 -------- .../DbTransactionReadAreaTest.java | 280 -------- .../test/transaction/DbTransactionTest.java | 337 ---------- .../test/update/DbSelectUpdateTest.java | 233 ------- .../iceaxe/test/update/DbUpdate2Test.java | 133 ---- .../test/update/DbUpdateDecimalTest.java | 130 ---- .../iceaxe/test/update/DbUpdateErrorTest.java | 132 ---- .../test/update/DbUpdateExplainTest.java | 67 -- .../test/update/DbUpdateIrregularTest.java | 62 -- .../iceaxe/test/update/DbUpdateLongTest.java | 101 --- .../iceaxe/test/update/DbUpdateTest.java | 443 ------------- .../iceaxe/test/util/DbTestConnector.java | 139 ---- .../iceaxe/test/util/DbTestSessions.java | 48 -- .../iceaxe/test/util/DbTestTableTester.java | 492 -------------- .../iceaxe/test/util/TestEntity.java | 77 --- .../src/test/resources/logback-test.xml | 50 -- settings.gradle | 1 - 121 files changed, 1 insertion(+), 16616 deletions(-) delete mode 100644 modules/iceaxe-dbtest/.gitignore delete mode 100755 modules/iceaxe-dbtest/build.gradle delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/connector/DbConnectErrorTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/connector/DbConnectorTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/delete/DbDeleteExplainTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/delete/DbDeleteIrregularTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/delete/DbDeleteNoPkTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/delete/DbDeleteTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/error/DbErrorInactiveTxTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/error/DbErrorMultiplexInsertTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/error/DbErrorMultiplexSelectTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/error/DbErrorTableNotExistsTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/error/DbGeneratedRowidTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/error/DbSystemReservedWordTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertBoundaryValueTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertCharTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertConstraintTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertDecimalTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertDuplicate2Test.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertDuplicateTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertErrorTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertExplainTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertIrregularTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertMultiThread2Test.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertMultiThreadTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertNoPkTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertVarcharTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbQueryResultTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbResultRecordTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelect2LoopTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectAggregateTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectAliasTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectAllTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectBoundaryValueTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectCountTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectDateTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectEmptyTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectErrorTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectExplainTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectFetchTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectFewTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectIrregularTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectJoinTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectLiteralTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectOtherTxTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectWhereExpressionTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectWhileInsertTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/session/DbMultiSessionTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/session/DbSessionErrorTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/session/DbSessionTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/session/DbSessionTxFileLoggerTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/sql/DbBindVariableTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/sql/DbPsExecuteErrorTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/sql/DbQueryCommentTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/sql/DbStatementCommentTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/table/DbCreateTableErrorTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/table/DbCreateTableExplainTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/table/DbCreateTableTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/table/DbDropTableExplainTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/table/DbDropTableTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/table/DbTableListTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/table/DbTableMetadataTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbServerStopSessionTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbServerStopTransactionTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbSlotLimitTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutExplainCloseTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutExplainConnectTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutPsCloseTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutPsConnectTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutResultCheckTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutResultCloseTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutRsCloseTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutRsConnectTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutSessionCloseTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutSessionConnectTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutTableListCloseTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutTableListConnectTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutTableMetadataCloseTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutTableMetadataConnectTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutTransactionBeginTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutTransactionCloseTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutTransactionCommitTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutTransactionRollbackTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutTxStatusCloseTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutTxStatusConnectTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimetoutTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/PipeServerThtread.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/TimeoutServerThread.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbManagerCounterTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbManagerExceptionTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbManagerExecuteDdlTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbManagerExecuteQueryTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbManagerExecuteStatementTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbTransactionConflictLtxTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbTransactionConflictOccTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbTransactionConflictRtxTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbTransactionErrorTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbTransactionExceptionTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbTransactionExecuteQueryTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbTransactionExecuteStatementTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbTransactionParallelTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbTransactionReadArea3Test.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbTransactionReadAreaTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbTransactionTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/update/DbSelectUpdateTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/update/DbUpdate2Test.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/update/DbUpdateDecimalTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/update/DbUpdateErrorTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/update/DbUpdateExplainTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/update/DbUpdateIrregularTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/update/DbUpdateLongTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/update/DbUpdateTest.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/util/DbTestConnector.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/util/DbTestSessions.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/util/DbTestTableTester.java delete mode 100755 modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/util/TestEntity.java delete mode 100755 modules/iceaxe-dbtest/src/test/resources/logback-test.xml diff --git a/.github/workflows/ci-build.yml b/.github/workflows/ci-build.yml index 468bbb33..587ce51b 100755 --- a/.github/workflows/ci-build.yml +++ b/.github/workflows/ci-build.yml @@ -32,7 +32,7 @@ jobs: - name: Check run: | - ./gradlew -i check -x :iceaxe-dbtest:check --continue + ./gradlew -i check --continue - name: Verify uses: project-tsurugi/tsurugi-annotations-action@v1 diff --git a/modules/iceaxe-dbtest/.gitignore b/modules/iceaxe-dbtest/.gitignore deleted file mode 100644 index 333c1e91..00000000 --- a/modules/iceaxe-dbtest/.gitignore +++ /dev/null @@ -1 +0,0 @@ -logs/ diff --git a/modules/iceaxe-dbtest/build.gradle b/modules/iceaxe-dbtest/build.gradle deleted file mode 100755 index ea582d0a..00000000 --- a/modules/iceaxe-dbtest/build.gradle +++ /dev/null @@ -1,13 +0,0 @@ -plugins { - id 'iceaxe.java-conventions' -} - -dependencies { - implementation project(':iceaxe-core') - testImplementation "ch.qos.logback:logback-classic:1.2.3" -} - -tasks.named('test') { - systemProperty 'tsurugi.dbtest.endpoint', findProperty('dbtest.endpoint') ?: 'tcp://localhost:12345' - systemProperty 'com.tsurugidb.tsubakuro.jniverify', 'false' -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/connector/DbConnectErrorTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/connector/DbConnectErrorTest.java deleted file mode 100755 index 7989c735..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/connector/DbConnectErrorTest.java +++ /dev/null @@ -1,146 +0,0 @@ -package com.tsurugidb.iceaxe.test.connector; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrowsExactly; - -import java.io.IOException; -import java.io.UncheckedIOException; -import java.net.ServerSocket; -import java.net.SocketException; -import java.net.URI; -import java.util.Arrays; -import java.util.concurrent.TimeUnit; - -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; -import org.opentest4j.AssertionFailedError; - -import com.tsurugidb.iceaxe.TsurugiConnector; -import com.tsurugidb.iceaxe.session.TgSessionOption; -import com.tsurugidb.iceaxe.session.TgSessionOption.TgTimeoutKey; -import com.tsurugidb.iceaxe.test.util.DbTestConnector; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.tsubakuro.channel.stream.StreamLink; - -/** - * connect error test - */ -public class DbConnectErrorTest extends DbTestTableTester { - - @Test - void connectError() throws Exception { - try (var server = new ServerSocket(0)) { - int port = server.getLocalPort(); - - var thread = new ServerThtread(server); - thread.start(); - - var e = assertThrowsExactly(IOException.class, () -> connect(port)); - try { - assertEquals("Server crashed", e.getMessage()); - } catch (AssertionFailedError t) { - throw e; - } - } - } - - private static class ServerThtread extends Thread { - - private final ServerSocket server; - - public ServerThtread(ServerSocket server) { - this.server = server; - } - - @Override - public void run() { - // クライアントから接続された後、何も返さずにクローズする - try (var socket = server.accept()) { - TimeUnit.SECONDS.sleep(1); - // don't respond - } catch (InterruptedException e) { - throw new RuntimeException(e); - } catch (IOException e) { - throw new UncheckedIOException(e); - } - } - } - - @Test - @Disabled // TODO remove Disabled - void recvIllegalMessage() throws Exception { - try (var server = new ServerSocket(0)) { - int port = server.getLocalPort(); - - var thread = new IllegalMessageServerThtread(server); - thread.start(); - - var e = assertThrowsExactly(IOException.class, () -> connect(port)); - try { - assertEquals("Server crashed", e.getMessage()); - } catch (AssertionFailedError t) { - throw e; - } - } - } - - private static class IllegalMessageServerThtread extends Thread { - - private final ServerSocket server; - - public IllegalMessageServerThtread(ServerSocket server) { - this.server = server; - } - - @Override - public void run() { - try (var socket = server.accept()) { - try (var os = socket.getOutputStream()) { - // 不正なメッセージを返す - var buf = new byte[128]; - Arrays.fill(buf, (byte) 0xff); - buf[0] = StreamLink.RESPONSE_RESULT_SET_PAYLOAD; - os.write(buf); - os.flush(); - TimeUnit.SECONDS.sleep(1); - } - } catch (InterruptedException e) { - throw new RuntimeException(e); - } catch (IOException e) { - throw new UncheckedIOException(e); - } - } - } - - private void connect(int port) throws Exception { - var endpoint = URI.create("tcp://localhost:" + port); - var connector = TsurugiConnector.of(endpoint); - - var sessionOption = TgSessionOption.of(); - sessionOption.setTimeout(TgTimeoutKey.DEFAULT, 3, TimeUnit.SECONDS); - try (var session = connector.createSession(sessionOption)) { - session.getLowSession(); - } - } - - @Test - void sendIllegalMessage() throws Exception { - try (var socket = DbTestConnector.createSocket()) { - try (var os = socket.getOutputStream();) { - // 不正なメッセージを送信する - var buf = new byte[128]; - Arrays.fill(buf, (byte) 0xff); - os.write(buf); - os.flush(); - try (var is = socket.getInputStream()) { - try { - int len = is.read(buf); - assertEquals(-1, len); - } catch (SocketException e) { - assertEquals("Connection reset", e.getMessage()); - } - } - } - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/connector/DbConnectorTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/connector/DbConnectorTest.java deleted file mode 100755 index 779923ff..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/connector/DbConnectorTest.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.tsurugidb.iceaxe.test.connector; - -import org.junit.jupiter.api.Test; - -import com.tsurugidb.iceaxe.test.util.DbTestConnector; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; - -/** - * connector test - */ -class DbConnectorTest extends DbTestTableTester { - - @Test - void doNothing() throws Exception { - try (var socket = DbTestConnector.createSocket()) { - // do nothing - } - - // tateyama-serverが落ちていなければ、正常に動作する - var session = getSession(); - session.findTableMetadata(TEST); - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/delete/DbDeleteExplainTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/delete/DbDeleteExplainTest.java deleted file mode 100755 index d2256ff2..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/delete/DbDeleteExplainTest.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.tsurugidb.iceaxe.test.delete; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertThrowsExactly; - -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.slf4j.LoggerFactory; - -import com.tsurugidb.iceaxe.sql.explain.TgStatementMetadata; -import com.tsurugidb.iceaxe.sql.parameter.TgBindParameters; -import com.tsurugidb.iceaxe.sql.parameter.TgBindVariable; -import com.tsurugidb.iceaxe.sql.parameter.TgParameterMapping; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; - -/** - * explain delete test - */ -class DbDeleteExplainTest extends DbTestTableTester { - - @BeforeAll - static void beforeAll(TestInfo info) throws Exception { - var LOG = LoggerFactory.getLogger(DbDeleteExplainTest.class); - logInitStart(LOG, info); - - dropTestTable(); - createTestTable(); - - logInitEnd(LOG, info); - } - - @Test - void pareparedStatement() throws Exception { - var sql = "delete from " + TEST; - - var session = getSession(); - try (var ps = session.createStatement(sql)) { - assertThrowsExactly(UnsupportedOperationException.class, () -> { - var result = ps.explain(); - assertExplain(result); - }); // TODO explain実装待ち - } - } - - @Test - void psParameter() throws Exception { - var bar = TgBindVariable.ofLong("bar"); - var sql = "delete from " + TEST + " where bar=" + bar; - var parameterMapping = TgParameterMapping.of(bar); - - var session = getSession(); - try (var ps = session.createStatement(sql, parameterMapping)) { - var parameter = TgBindParameters.of(bar.bind(1)); - var result = ps.explain(parameter); - assertExplain(result); - } - } - - private static void assertExplain(TgStatementMetadata actual) throws Exception { - assertNotNull(actual.getLowPlanGraph()); - - var list = actual.getLowColumnList(); - assertEquals(0, list.size()); - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/delete/DbDeleteIrregularTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/delete/DbDeleteIrregularTest.java deleted file mode 100755 index 0d8b7667..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/delete/DbDeleteIrregularTest.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.tsurugidb.iceaxe.test.delete; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotEquals; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; - -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; - -/** - * irregular delete test - */ -class DbDeleteIrregularTest extends DbTestTableTester { - - private static final int SIZE = 10; - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTestTable(); - insertTestTable(SIZE); - - logInitEnd(info); - } - - @Test - void closePsBeforeCloseRc() throws Exception { - int number = SIZE / 2; - var sql = "delete from " + TEST // - + " where foo = " + number; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - tm.execute(transaction -> { - var ps = session.createStatement(sql); - var result = ps.execute(transaction); - ps.close(); - int count = result.getUpdateCount(); - assertUpdateCount(1, count); - result.close(); - }); - - var list = selectAllFromTest(); - assertEquals(SIZE - 1, list.size()); - for (var entity : list) { - assertNotEquals(number, entity.getFoo()); - - int i = entity.getFoo(); - var expected = createTestEntity(i); - assertEquals(expected, entity); - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/delete/DbDeleteNoPkTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/delete/DbDeleteNoPkTest.java deleted file mode 100755 index a92d73ff..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/delete/DbDeleteNoPkTest.java +++ /dev/null @@ -1,102 +0,0 @@ -package com.tsurugidb.iceaxe.test.delete; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.RepeatedTest; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; - -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.transaction.manager.exception.TsurugiTmRetryOverIOException; - -/** - * delete (table without primary key) test - */ -class DbDeleteNoPkTest extends DbTestTableTester { - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTable(); - insertTestTable(DbDeleteTest.SIZE); - - logInitEnd(info); - } - - private static void createTable() throws Exception { - // no primary key - var sql = "create table " + TEST // - + "(" // - + " foo int," // - + " bar bigint," // - + " zzz varchar(10)" // - + ")"; - executeDdl(getSession(), sql); - } - - @Test - void deleteAll() throws Exception { - new DbDeleteTest().deleteAll(); - } - - @Test - void deleteConstant() throws Exception { - new DbDeleteTest().deleteConstant(); - } - - @Test - void deleteByBind() throws Exception { - new DbDeleteTest().deleteByBind(); - } - - @RepeatedTest(15) - void delete2SeqTx() throws Exception { - try { - new DbDeleteTest().delete2SeqTx(); - } catch (TsurugiTmRetryOverIOException e) { - // TODO ERR_PHANTOMが解消したら、catchを削除する - var c = e.getCause(); - if (c.getMessage().contains("ERR_PHANTOM")) { - LOG.warn("delete2SeqTx fail. {}", c.getMessage()); - return; - } - throw e; - } - } - - @Test - void delete2SameTx() throws Exception { - new DbDeleteTest().delete2SameTx(); - } - - @Test - void delete2Range() throws Exception { - new DbDeleteTest().delete2Range(); - } - - @Test - void deleteInsert() throws Exception { - new DbDeleteTest().deleteInsert(); - } - - @Test - void deleteInsertDeleteExists() throws Exception { - new DbDeleteTest().deleteInsertDeleteExists(); - } - - @Test - void deleteInsertDeleteNotExists() throws Exception { - new DbDeleteTest().deleteInsertDeleteNotExists(); - } - - @Test - void insertDelete() throws Exception { - new DbDeleteTest().insertDelete(); - } - - @Test - void insertDeleteInsert() throws Exception { - new DbDeleteTest().insertDeleteInsert(); - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/delete/DbDeleteTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/delete/DbDeleteTest.java deleted file mode 100755 index 548a3aab..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/delete/DbDeleteTest.java +++ /dev/null @@ -1,314 +0,0 @@ -package com.tsurugidb.iceaxe.test.delete; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.stream.Collectors; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; - -import com.tsurugidb.iceaxe.session.TsurugiSession; -import com.tsurugidb.iceaxe.sql.parameter.TgBindParameters; -import com.tsurugidb.iceaxe.sql.parameter.TgBindVariable; -import com.tsurugidb.iceaxe.sql.parameter.TgParameterMapping; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.test.util.TestEntity; -import com.tsurugidb.iceaxe.transaction.TsurugiTransaction; -import com.tsurugidb.iceaxe.transaction.exception.TsurugiTransactionException; - -/** - * delete test - */ -class DbDeleteTest extends DbTestTableTester { - - static final int SIZE = 4; - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTestTable(); - insertTestTable(SIZE); - - logInitEnd(info); - } - - @Test - void deleteAll() throws Exception { - var sql = "delete from " + TEST; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(sql)) { - int count = tm.executeAndGetCount(ps); - assertUpdateCount(SIZE, count); - } - - assertEqualsTestTable(); - } - - @Test - void deleteConstant() throws Exception { - int number = 2; - var sql = "delete from " + TEST // - + " where foo = " + number; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(sql)) { - int count = tm.executeAndGetCount(ps); - assertUpdateCount(1, count); - } - - assertEqualsDelete(number); - } - - @Test - void deleteByBind() throws Exception { - int number = 2; - - var foo = TgBindVariable.ofInt("foo"); - var sql = "delete from " + TEST // - + " where foo = " + foo; - var parameterMapping = TgParameterMapping.of(foo); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(sql, parameterMapping)) { - var parameter = TgBindParameters.of(foo.bind(number)); - int count = tm.executeAndGetCount(ps, parameter); - assertUpdateCount(1, count); - } - - assertEqualsDelete(number); - } - - @Test - void delete2SeqTx() throws Exception { - int number = 2; - var sql = "delete from " + TEST // - + " where foo = " + number; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(sql)) { - int count1 = tm.executeAndGetCount(ps); - assertUpdateCount(1, count1); - - int count2 = tm.executeAndGetCount(ps); - assertUpdateCount(0, count2); - } - - assertEqualsDelete(number); - } - - @Test - void delete2SameTx() throws Exception { - int number = 2; - var sql = "delete from " + TEST // - + " where foo = " + number; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(sql)) { - tm.execute(transaction -> { - int count1 = transaction.executeAndGetCount(ps); - assertUpdateCount(1, count1); - assertNothingInTx(session, transaction, number); - - int count2 = transaction.executeAndGetCount(ps); - assertUpdateCount(0, count2); - assertNothingInTx(session, transaction, number); - }); - } - - assertEqualsDelete(number); - } - - @Test - void delete2Range() throws Exception { - var sql1 = "delete from " + TEST + " where 1 <= foo and foo <= 2"; - var sql2 = "delete from " + TEST + " where 2 <= foo and foo <= 3"; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps1 = session.createStatement(sql1); // - var ps2 = session.createStatement(sql2)) { - tm.execute(transaction -> { - int count1 = transaction.executeAndGetCount(ps1); - assertUpdateCount(2, count1); - assertNothingInTx(session, transaction, 1); - assertNothingInTx(session, transaction, 2); - - int count2 = transaction.executeAndGetCount(ps2); - assertUpdateCount(1, count2); - assertNothingInTx(session, transaction, 2); - assertNothingInTx(session, transaction, 3); - }); - } - - assertEqualsDelete(1, 2, 3); - } - - @Test - void deleteInsert() throws Exception { - int number = 2; - var sql = "delete from " + TEST // - + " where foo = " + number; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var deletePs = session.createStatement(sql)) { - tm.execute(transaction -> { - int count1 = transaction.executeAndGetCount(deletePs); - assertUpdateCount(1, count1); - assertNothingInTx(session, transaction, number); - - var entity = createTestEntity(number); - try (var insertPs = session.createStatement(INSERT_SQL, INSERT_MAPPING)) { - int count2 = transaction.executeAndGetCount(insertPs, entity); - assertUpdateCount(1, count2); - } - assertEqualsInTx(session, transaction, entity); - }); - } - - assertEqualsTestTable(SIZE); - } - - @Test - void deleteInsertDeleteExists() throws Exception { - int number = 2; - assert number < SIZE; - deleteInsertDelete(number, 1); - } - - @Test - void deleteInsertDeleteNotExists() throws Exception { - int number = 123; - assert number >= SIZE; - deleteInsertDelete(number, 0); - } - - private void deleteInsertDelete(int number, int expected1) throws IOException, InterruptedException { - var sql = "delete from " + TEST // - + " where foo = " + number; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var deletePs = session.createStatement(sql)) { - tm.execute(transaction -> { - int count1 = transaction.executeAndGetCount(deletePs); - assertUpdateCount(expected1, count1); - assertNothingInTx(session, transaction, number); - - var entity = createTestEntity(number); - try (var insertPs = session.createStatement(INSERT_SQL, INSERT_MAPPING)) { - int count2 = transaction.executeAndGetCount(insertPs, entity); - assertUpdateCount(1, count2); - } - assertEqualsInTx(session, transaction, entity); - - int count3 = transaction.executeAndGetCount(deletePs); - assertUpdateCount(1, count3); - assertNothingInTx(session, transaction, number); - }); - } - - assertEqualsDelete(number); - } - - @Test - void insertDelete() throws Exception { - var entity = new TestEntity(123, 456, "abc"); - var sql = "delete from " + TEST + " where foo = " + entity.getFoo(); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var insertPs = session.createStatement(INSERT_SQL, INSERT_MAPPING)) { - tm.execute(transaction -> { - int count1 = transaction.executeAndGetCount(insertPs, entity); - assertUpdateCount(1, count1); - assertEqualsInTx(session, transaction, entity); - - try (var deletePs = session.createStatement(sql)) { - int count2 = transaction.executeAndGetCount(deletePs); - assertUpdateCount(1, count2); - } - assertNothingInTx(session, transaction, entity.getFoo()); - }); - } - - assertEqualsTestTable(SIZE); - } - - @Test - void insertDeleteInsert() throws Exception { - var entity1 = new TestEntity(123, 456, "abc"); - var entity2 = new TestEntity(entity1.getFoo(), 999, "zzz"); - var sql = "delete from " + TEST + " where foo = " + entity1.getFoo(); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var insertPs = session.createStatement(INSERT_SQL, INSERT_MAPPING)) { - tm.execute(transaction -> { - int count1 = transaction.executeAndGetCount(insertPs, entity1); - assertUpdateCount(1, count1); - assertEqualsInTx(session, transaction, entity1); - - try (var deletePs = session.createStatement(sql)) { - int count2 = transaction.executeAndGetCount(deletePs); - assertUpdateCount(1, count2); - } - assertNothingInTx(session, transaction, entity1.getFoo()); - - int count3 = transaction.executeAndGetCount(insertPs, entity2); - assertUpdateCount(1, count3); - assertEqualsInTx(session, transaction, entity2); - }); - } - - var expectedList = new ArrayList(SIZE - 1); - for (int i = 0; i < SIZE; i++) { - var expected = createTestEntity(i); - expectedList.add(expected); - } - expectedList.add(entity2); - assertEqualsTestTable(expectedList); - } - - private void assertNothingInTx(TsurugiSession session, TsurugiTransaction transaction, int foo) throws IOException, InterruptedException, TsurugiTransactionException { - var sql = SELECT_SQL + " where foo = " + foo; - try (var ps = session.createQuery(sql)) { - var actual = transaction.executeAndFindRecord(ps); - assertTrue(actual.isEmpty()); - } - } - - private void assertEqualsInTx(TsurugiSession session, TsurugiTransaction transaction, TestEntity expected) throws IOException, InterruptedException, TsurugiTransactionException { - var sql = SELECT_SQL + " where foo = " + expected.getFoo(); - try (var ps = session.createQuery(sql, SELECT_MAPPING)) { - var actual = transaction.executeAndFindRecord(ps).get(); - assertEquals(expected, actual); - } - } - - private void assertEqualsDelete(int... numbers) throws IOException, InterruptedException { - var deleteSet = Arrays.stream(numbers).boxed().collect(Collectors.toSet()); - var expectedList = new ArrayList(SIZE - 1); - for (int i = 0; i < SIZE; i++) { - if (deleteSet.contains(i)) { - continue; - } - var expected = createTestEntity(i); - expectedList.add(expected); - } - assertEqualsTestTable(expectedList); - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/error/DbErrorInactiveTxTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/error/DbErrorInactiveTxTest.java deleted file mode 100755 index 1912cca7..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/error/DbErrorInactiveTxTest.java +++ /dev/null @@ -1,205 +0,0 @@ -package com.tsurugidb.iceaxe.test.error; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.concurrent.Callable; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; -import java.util.stream.Collectors; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.tsurugidb.iceaxe.session.TsurugiSession; -import com.tsurugidb.iceaxe.sql.TsurugiSqlPreparedQuery; -import com.tsurugidb.iceaxe.sql.parameter.TgBindParameters; -import com.tsurugidb.iceaxe.sql.parameter.TgBindVariable; -import com.tsurugidb.iceaxe.sql.parameter.TgBindVariable.TgBindVariableInteger; -import com.tsurugidb.iceaxe.sql.parameter.TgBindVariables; -import com.tsurugidb.iceaxe.sql.parameter.TgParameterMapping; -import com.tsurugidb.iceaxe.sql.result.TsurugiResultEntity; -import com.tsurugidb.iceaxe.test.util.DbTestSessions; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.transaction.TsurugiTransaction; -import com.tsurugidb.iceaxe.transaction.exception.TsurugiTransactionException; -import com.tsurugidb.iceaxe.transaction.manager.TgTmSetting; -import com.tsurugidb.iceaxe.transaction.manager.event.TsurugiTmEventListener; -import com.tsurugidb.iceaxe.transaction.manager.exception.TsurugiTmIOException; -import com.tsurugidb.iceaxe.transaction.manager.option.TgTmTxOption; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; - -/** - * ERR_INACTIVE_TRANSACTION bug test - */ -class DbErrorInactiveTxTest extends DbTestTableTester { - - private static final int TEST_SIZE = 10; - private static final String TEST2 = "test2"; - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTestTable(); - insertTestTable(TEST_SIZE); - - dropTable(TEST2); - createTest2Table(); - - logInitEnd(info); - } - - private static void createTest2Table() throws IOException, InterruptedException { - var sql = "create table " + TEST2 // - + "(" // - + " group_id int," // - + " id int," // - + " fk_foo int," // - + " value1 decimal(10,2)," // - + " value2 decimal(10,2)," // - + " primary key(group_id, id)" // - + ")"; - executeDdl(getSession(), sql); - } - - @Test - void test() throws Exception { - int batchSize = 10; - int onlineSize = 20; - - try (var sessions = new DbTestSessions()) { - var batchList = new ArrayList(batchSize); - for (int i = 0; i < batchSize; i++) { - var task = new BatchTask(sessions.createSession(), i); - batchList.add(task); - } - var onlineList = new ArrayList(onlineSize); - for (int i = 0; i < onlineSize; i++) { - var task = new OnlineTask(sessions.createSession(), i); - onlineList.add(task); - } - - var service = Executors.newCachedThreadPool(); - var futureList = new ArrayList>(batchSize + onlineSize); - onlineList.forEach(task -> futureList.add(service.submit(task))); - batchList.forEach(task -> futureList.add(service.submit(task))); - - var exceptionList = new ArrayList(); - for (var future : futureList) { - try { - future.get(); - } catch (Exception e) { - exceptionList.add(e); - } - } - if (!exceptionList.isEmpty()) { - if (exceptionList.size() == 1) { - throw exceptionList.get(0); - } - var e = new Exception(exceptionList.stream().map(Exception::getMessage).collect(Collectors.joining("\n"))); - exceptionList.stream().forEach(e::addSuppressed); - throw e; - } - } - } - - private static class BatchTask implements Callable { - private final TsurugiSession session; - private final int targetGroupId; - - public BatchTask(TsurugiSession session, int groupId) { - this.session = session; - this.targetGroupId = groupId; - } - - @Override - public Void call() throws Exception { - var setting = TgTmSetting.ofAlways(TgTxOption.ofLTX(TEST2)); - var tm = session.createTransactionManager(setting); - tm.execute(transaction -> { - execute(transaction); - }); - return null; - } - - private void execute(TsurugiTransaction transaction) throws IOException, InterruptedException, TsurugiTransactionException { - var groupId = TgBindVariable.ofInt("groupId"); - var id = TgBindVariable.ofInt("id"); - var fkFoo = TgBindVariable.ofInt("fkFoo"); - var value1 = TgBindVariable.ofDecimal("value1"); - var value2 = TgBindVariable.ofDecimal("value2"); - var vlist = TgBindVariables.of(groupId, id, fkFoo, value1, value2); - var sql = "insert into " + TEST2 // - + "(group_id, id, fk_foo, value1, value2)" // - + "values(" + vlist.getSqlNames() + ")"; - var parameterMapping = TgParameterMapping.of(vlist); - try (var ps = session.createStatement(sql, parameterMapping)) { - for (int i = 0; i < 50; i++) { - var parameter = TgBindParameters.of( // - groupId.bind(targetGroupId), // - id.bind(i + 1), // - fkFoo.bind(i % TEST_SIZE), // - value1.bind(i + 100), // - value2.bind(i + 200) // - ); - transaction.executeAndGetCount(ps, parameter); - } - } - } - } - - private static class OnlineTask implements Callable { - private static final Logger LOG = LoggerFactory.getLogger(OnlineTask.class); - - private final TsurugiSession session; - private final int maxGroupId; - private final TgBindVariableInteger vGruopId = TgBindVariable.ofInt("groupId"); - - public OnlineTask(TsurugiSession session, int groupId) { - this.session = session; - this.maxGroupId = groupId; - } - - @Override - public Void call() throws Exception { - var sql = "select group_id, id, fk_foo, value1, value2\n" // - + "from " + TEST2 + "\n" // - + "inner join " + TEST + " t1 on t1.foo=fk_foo\n" // - + "where group_id=" + vGruopId; - var parameterMapping = TgParameterMapping.of(vGruopId); - try (var ps = session.createQuery(sql, parameterMapping)) { - var setting = TgTmSetting.ofAlways(TgTxOption.ofOCC()); - var tm = session.createTransactionManager(setting); - tm.addEventListener(new TsurugiTmEventListener() { - @Override - public void transactionRetry(TsurugiTransaction transaction, Exception cause, TgTmTxOption nextTmOption) { -// LOG.info("OCC retry: " + cause.getMessage()); - } - }); - - for (int i = 0; i < maxGroupId; i++) { - int groupId = i; - try { - tm.execute(transaction -> { - execute(transaction, ps, groupId); - }); - } catch (TsurugiTmIOException e) { - LOG.error("online task error: {}", e.getMessage()); - throw e; - } - } - } - return null; - } - - private void execute(TsurugiTransaction transaction, TsurugiSqlPreparedQuery ps, int groupId) - throws IOException, InterruptedException, TsurugiTransactionException { - var parameter = TgBindParameters.of(vGruopId.bind(groupId)); - transaction.executeAndGetList(ps, parameter); - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/error/DbErrorMultiplexInsertTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/error/DbErrorMultiplexInsertTest.java deleted file mode 100755 index 085c3aae..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/error/DbErrorMultiplexInsertTest.java +++ /dev/null @@ -1,126 +0,0 @@ -package com.tsurugidb.iceaxe.test.error; - -import java.io.IOException; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; - -import com.tsurugidb.iceaxe.exception.TsurugiIOException; -import com.tsurugidb.iceaxe.test.util.DbTestConnector; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.test.util.TestEntity; -import com.tsurugidb.iceaxe.transaction.exception.TsurugiTransactionException; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; -import com.tsurugidb.tsubakuro.channel.common.connection.wire.impl.ResponseBox; -import com.tsurugidb.tsubakuro.sql.SqlServiceCode; - -/** - * multiplex insert error test - */ -class DbErrorMultiplexInsertTest extends DbTestTableTester { - - private static final int ATTEMPT_SIZE = ResponseBox.responseBoxSize() + 100; - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTestTable(); - var entity = new TestEntity(1024, 456, "abc"); - insertTestTable(entity); - - logInitEnd(info); - } - - @Test - void insertMultiCheck1() throws Exception { - for (int i = 0; i < ATTEMPT_SIZE; i++) { - insertMulti(1, ExecuteType.RESULT_CHECK); - } - } - - @ParameterizedTest - @ValueSource(ints = { 15, 16, 255, 256 }) - void insertMultiCheckN(int size) throws Exception { - insertMulti(size, ExecuteType.RESULT_CHECK); - } - - @Test - void insertMultiClose1() throws Exception { - for (int i = 0; i < ATTEMPT_SIZE; i++) { - insertMulti(1, ExecuteType.CLOSE_ONLY); - } - } - - @ParameterizedTest - @ValueSource(ints = { 15, 16, 255, 256 }) - void insertMultiCloseN(int size) throws Exception { - insertMulti(size, ExecuteType.CLOSE_ONLY); - } - - @Test - void insertMulti1() throws Exception { - for (int i = 0; i < ATTEMPT_SIZE; i++) { - insertMulti(1, ExecuteType.EXEUTE_ONLY); - } - } - - @ParameterizedTest - @ValueSource(ints = { 15, 16, 255, 256 }) - void insertMultiN(int size) throws Exception { - insertMulti(size, ExecuteType.EXEUTE_ONLY); - } - - private enum ExecuteType { - RESULT_CHECK, CLOSE_ONLY, EXEUTE_ONLY - } - - private void insertMulti(int size, ExecuteType type) throws IOException, InterruptedException, TsurugiTransactionException { - int threshold = 255; - try { - insertMultiMain(size, type); - } catch (TsurugiIOException e) { - if (size < threshold) { - throw e; - } - assertEqualsCode(SqlServiceCode.SQL_SERVICE_EXCEPTION, e); // TODO ERR_RESOURCE_LIMIT_REACHED - assertContains("creating transaction failed with error:err_resource_limit_reached", e.getMessage()); - LOG.warn("err_resource_limit_reached occur. size={}, type={}", size, type); - } - } - - private void insertMultiMain(int size, ExecuteType type) throws IOException, InterruptedException, TsurugiTransactionException { - try (var session = DbTestConnector.createSession()) { - for (int i = 0; i < size; i++) { - var transaction = session.createTransaction(TgTxOption.ofOCC()); - // transaction.close is called on session.close - - var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING); - // ps.close is called on session.close - - var entity = createTestEntity(i); - switch (type) { - case RESULT_CHECK: - default: - transaction.executeAndGetCount(ps, entity); - break; - case CLOSE_ONLY: - try (var result = ps.execute(transaction, entity)) { - } - break; - case EXEUTE_ONLY: - @SuppressWarnings("unused") - var result = transaction.executeStatement(ps, entity); - // result.close is called on transaction.close - break; - } - - // do not commit/rollback - } - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/error/DbErrorMultiplexSelectTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/error/DbErrorMultiplexSelectTest.java deleted file mode 100755 index d547eb37..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/error/DbErrorMultiplexSelectTest.java +++ /dev/null @@ -1,138 +0,0 @@ -package com.tsurugidb.iceaxe.test.error; - -import java.io.IOException; - -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; -import org.slf4j.LoggerFactory; - -import com.tsurugidb.iceaxe.exception.TsurugiIOException; -import com.tsurugidb.iceaxe.test.util.DbTestConnector; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.transaction.exception.TsurugiTransactionException; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; -import com.tsurugidb.tsubakuro.channel.common.connection.wire.impl.ResponseBox; -import com.tsurugidb.tsubakuro.exception.CoreServiceCode; -import com.tsurugidb.tsubakuro.sql.SqlServiceCode; - -/** - * multiplex select error test - */ -class DbErrorMultiplexSelectTest extends DbTestTableTester { - - private static final int ATTEMPT_SIZE = ResponseBox.responseBoxSize() + 100; - - @BeforeAll - static void beforeAll(TestInfo info) throws Exception { - var LOG = LoggerFactory.getLogger(DbErrorMultiplexSelectTest.class); - logInitStart(LOG, info); - - dropTestTable(); - createTestTable(); - insertTestTable(1); - - logInitEnd(LOG, info); - } - - @Test - public void selectMultiRead1() throws Exception { - for (int i = 0; i < ATTEMPT_SIZE; i++) { - selectMulti(1, ExecuteType.READ_ALL); - } - } - - @ParameterizedTest - @ValueSource(ints = { 240, 241, 255, 256, 300 }) - public void selectMultiReadN(int size) throws Exception { - selectMulti(size, ExecuteType.READ_ALL); - } - - @Test - public void selectMultiClose1() throws Exception { - for (int i = 0; i < ATTEMPT_SIZE; i++) { - selectMulti(1, ExecuteType.CLOSE_ONLY); - } - } - - @ParameterizedTest - @ValueSource(ints = { 240, 241, 255, 256, 300 }) - public void selectMultiCloseN(int size) throws Exception { - selectMulti(size, ExecuteType.CLOSE_ONLY); - } - - @Test - void selectMulti1() throws Exception { - for (int i = 0; i < ATTEMPT_SIZE; i++) { - selectMulti(1, ExecuteType.EXEUTE_ONLY); - } - } - - @ParameterizedTest - @ValueSource(ints = { 240, 241, 255, 256, 300 }) - void selectMultiN(int size) throws Exception { - selectMulti(size, ExecuteType.EXEUTE_ONLY); - } - - private enum ExecuteType { - READ_ALL, CLOSE_ONLY, EXEUTE_ONLY - } - - private void selectMulti(int size, ExecuteType type) throws IOException, InterruptedException, TsurugiTransactionException { - try { - selectMultiMain(size, type); - } catch (TsurugiIOException e) { - if (DbTestConnector.isTcp()) { - if (size > 280) { - assertEqualsCode(SqlServiceCode.TRANSACTION_EXCEEDED_LIMIT_EXCEPTION, e); - LOG.info("(TCP)TRANSACTION_EXCEEDED_LIMIT_EXCEPTION occur. size={}, type={}", size, type); - return; - } - } - throw e; - } catch (IOException e) { - if (DbTestConnector.isIpc()) { - if (size > 240) { - assertEqualsCode(CoreServiceCode.RESOURCE_LIMIT_REACHED, e); - LOG.info("(IPC)RESOURCE_LIMIT_REACHED occur. size={}, type={}", size, type); - return; - } - } - throw e; - } - } - - private void selectMultiMain(int size, ExecuteType type) throws IOException, InterruptedException, TsurugiTransactionException { - var sql = "select * from " + TEST; - - try (var session = DbTestConnector.createSession()) { - for (int i = 0; i < size; i++) { - var transaction = session.createTransaction(TgTxOption.ofOCC()); - // transaction.close is called on session.close - - var ps = session.createQuery(sql); - // ps.close is called on session.close - - switch (type) { - default: - case READ_ALL: - transaction.executeAndGetList(ps); - break; - case CLOSE_ONLY: - try (var result = transaction.executeQuery(ps)) { - } - break; - case EXEUTE_ONLY: - @SuppressWarnings("unused") - var result = transaction.executeQuery(ps); - // result.close is called on transaction.close - break; - } - - // do not commit/rollback - } - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/error/DbErrorTableNotExistsTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/error/DbErrorTableNotExistsTest.java deleted file mode 100755 index 31452fa3..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/error/DbErrorTableNotExistsTest.java +++ /dev/null @@ -1,180 +0,0 @@ -package com.tsurugidb.iceaxe.test.error; - -import static org.junit.jupiter.api.Assertions.assertThrowsExactly; - -import java.io.IOException; - -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.slf4j.LoggerFactory; - -import com.tsurugidb.iceaxe.exception.IceaxeErrorCode; -import com.tsurugidb.iceaxe.exception.TsurugiIOException; -import com.tsurugidb.iceaxe.sql.parameter.TgBindParameters; -import com.tsurugidb.iceaxe.sql.parameter.TgBindVariable; -import com.tsurugidb.iceaxe.sql.parameter.TgParameterMapping; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.test.util.TestEntity; -import com.tsurugidb.iceaxe.transaction.exception.TsurugiTransactionException; -import com.tsurugidb.iceaxe.transaction.function.TsurugiTransactionAction; -import com.tsurugidb.iceaxe.transaction.manager.exception.TsurugiTmIOException; -import com.tsurugidb.tsubakuro.channel.common.connection.wire.impl.ResponseBox; -import com.tsurugidb.tsubakuro.sql.SqlServiceCode; - -/** - * table not exists test - */ -class DbErrorTableNotExistsTest extends DbTestTableTester { - - private static final int ATTEMPT_SIZE = ResponseBox.responseBoxSize() + 200; - - @BeforeAll - static void beforeAll(TestInfo info) throws Exception { - var LOG = LoggerFactory.getLogger(DbErrorTableNotExistsTest.class); - logInitStart(LOG, info); - - dropTestTable(); - - logInitEnd(LOG, info); - } - - @Test - void select() throws Exception { - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(SELECT_SQL)) { - for (int i = 0; i < ATTEMPT_SIZE; i++) { - LOG.trace("i={}", i); - var e0 = assertThrowsExactly(TsurugiTmIOException.class, () -> { - tm.execute((TsurugiTransactionAction) transaction -> { - var e = assertThrowsExactly(TsurugiTransactionException.class, () -> { - transaction.executeAndGetList(ps); - }); - assertEqualsCode(SqlServiceCode.SYMBOL_ANALYZE_EXCEPTION, e); - throw e; - }); - }); - assertEqualsCode(SqlServiceCode.SYMBOL_ANALYZE_EXCEPTION, e0); - assertContains("compile failed with error:table_not_found message:\"" + TEST + "\" location:(unknown)", e0.getMessage()); - } - } - } - - @Test - void insert() throws Exception { - var sql = "insert into " + TEST + "(" + TEST_COLUMNS + ") values(123, 456, 'abc')"; - var expected = "compile failed with error:table_not_found message:\"table `" + TEST + "' is not found\" location:(unknown)"; - statement(sql, expected); - } - - @Test - void update() throws Exception { - var sql = "update " + TEST + " set bar = 0"; - var expected = "compile failed with error:table_not_found message:\"" + TEST + "\" location:(unknown)"; - statement(sql, expected); - } - - @Test - void delete() throws Exception { - var sql = "delete from " + TEST; - var expected = "compile failed with error:table_not_found message:\"" + TEST + "\" location:(unknown)"; - statement(sql, expected); - } - - private void statement(String sql, String expected) throws IOException, InterruptedException { - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(sql)) { - for (int i = 0; i < ATTEMPT_SIZE; i++) { - var e0 = assertThrowsExactly(TsurugiTmIOException.class, () -> { - tm.execute((TsurugiTransactionAction) transaction -> { - var e = assertThrowsExactly(TsurugiTransactionException.class, () -> { - transaction.executeAndGetCount(ps); - }); - assertEqualsCode(SqlServiceCode.SYMBOL_ANALYZE_EXCEPTION, e); - throw e; - }); - }); - assertEqualsCode(SqlServiceCode.SYMBOL_ANALYZE_EXCEPTION, e0); - assertContains(expected, e0.getMessage()); - } - } - } - - @Test - void selectBind() throws Exception { - var foo = TgBindVariable.ofInt("foo"); - var sql = SELECT_SQL + " where foo=" + foo; - var parameterMapping = TgParameterMapping.of(foo); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql, parameterMapping)) { - tm.execute(transaction -> { - var parameter = TgBindParameters.of(foo.bind(1)); - var e = assertThrowsExactly(TsurugiIOException.class, () -> { - transaction.executeAndGetList(ps, parameter); - }); - assertEqualsCode(SqlServiceCode.SYMBOL_ANALYZE_EXCEPTION, e); - assertContains("compile failed with error:table_not_found message:\"" + TEST + "\" location:(unknown)", e.getMessage()); - }); - tm.execute(transaction -> { - var parameter = TgBindParameters.of(foo.bind(1)); - var e = assertThrowsExactly(TsurugiIOException.class, () -> { - transaction.executeAndGetList(ps, parameter); - }); - assertEqualsCode(IceaxeErrorCode.PS_LOW_ERROR, e); - assertEqualsCode(SqlServiceCode.SYMBOL_ANALYZE_EXCEPTION, e.getCause()); - }); - } - } - - @Test - void insertBind() throws Exception { - var entity = new TestEntity(123, 456, "abc"); - var expected = "compile failed with error:table_not_found message:\"table `" + TEST + "' is not found\" location:(unknown)"; - preparedStatement(INSERT_SQL, INSERT_MAPPING, entity, expected); - } - - @Test - void updateBind() throws Exception { - var bar = TgBindVariable.ofLong("bar"); - var sql = "update " + TEST + " set bar = " + bar; - var mapping = TgParameterMapping.of(bar); - var parameter = TgBindParameters.of(bar.bind(0)); - var expected = "compile failed with error:table_not_found message:\"" + TEST + "\" location:(unknown)"; - preparedStatement(sql, mapping, parameter, expected); - } - - @Test - void deleteBind() throws Exception { - var foo = TgBindVariable.ofInt("foo"); - var sql = "delete from " + TEST + " where foo=" + foo; - var mapping = TgParameterMapping.of(foo); - var parameter = TgBindParameters.of(foo.bind(1)); - var expected = "compile failed with error:table_not_found message:\"" + TEST + "\" location:(unknown)"; - preparedStatement(sql, mapping, parameter, expected); - } - - private

void preparedStatement(String sql, TgParameterMapping

parameterMapping, P parameter, String expected) throws IOException, InterruptedException { - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(sql, parameterMapping)) { - tm.execute(transaction -> { - var e = assertThrowsExactly(TsurugiIOException.class, () -> { - transaction.executeAndGetCount(ps, parameter); - }); - assertEqualsCode(SqlServiceCode.SYMBOL_ANALYZE_EXCEPTION, e); - assertContains(expected, e.getMessage()); - }); - tm.execute(transaction -> { - var e = assertThrowsExactly(TsurugiIOException.class, () -> { - transaction.executeAndGetCount(ps, parameter); - }); - assertEqualsCode(IceaxeErrorCode.PS_LOW_ERROR, e); - assertEqualsCode(SqlServiceCode.SYMBOL_ANALYZE_EXCEPTION, e.getCause()); - }); - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/error/DbGeneratedRowidTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/error/DbGeneratedRowidTest.java deleted file mode 100755 index 1b1ceb79..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/error/DbGeneratedRowidTest.java +++ /dev/null @@ -1,219 +0,0 @@ -package com.tsurugidb.iceaxe.test.error; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrowsExactly; - -import java.io.IOException; -import java.util.Set; -import java.util.stream.Collectors; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; - -import com.tsurugidb.iceaxe.sql.parameter.TgBindParameters; -import com.tsurugidb.iceaxe.sql.parameter.TgParameterMapping; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.transaction.manager.exception.TsurugiTmIOException; -import com.tsurugidb.tsubakuro.sql.SqlServiceCode; - -/** - * generated rowid test - * - *

    - *
  • プライマリキーが無いテーブルでは、暗黙に「__generated_rowid___テーブル名」というカラムが作られるが、このカラムはユーザーからは見えない。
  • - *
- */ -class DbGeneratedRowidTest extends DbTestTableTester { - - private static final String GENERATED_KEY = "__generated_rowid___" + TEST; - private static final int SIZE = 4; - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTable(); - insertTestTable(SIZE); - - logInitEnd(info); - } - - private static void createTable() throws IOException, InterruptedException { - // no primary key - var sql = "create table " + TEST // - + "(" // - + " foo int," // - + " bar bigint," // - + " zzz varchar(10)" // - + ")"; - executeDdl(getSession(), sql); - } - - @Test - void metadata() throws Exception { - var session = getSession(); - var metadata = session.findTableMetadata(TEST).get(); - var actualSet = metadata.getLowColumnList().stream().map(c -> c.getName()).collect(Collectors.toSet()); - var expectedSet = Set.of("foo", "bar", "zzz"); - assertEquals(expectedSet, actualSet); - } - - @Test - void explain() throws Exception { - var sql = "select * from " + TEST; - - var session = getSession(); - try (var ps = session.createQuery(sql, TgParameterMapping.of())) { - var result = ps.explain(TgBindParameters.of()); - var actualSet = result.getLowColumnList().stream().map(c -> c.getName()).collect(Collectors.toSet()); - var expectedSet = Set.of("foo", "bar", "zzz"); // 「select *」ではgenerated_rowidは出てこない - assertEquals(expectedSet, actualSet); - } - } - - @Test - void selectKey() throws Exception { - var sql = "select " + GENERATED_KEY + " from " + TEST; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql)) { - var e = assertThrowsExactly(TsurugiTmIOException.class, () -> { - tm.executeAndGetList(ps); - }); - assertEqualsCode(SqlServiceCode.SYMBOL_ANALYZE_EXCEPTION, e); - assertContains("compile failed with error:variable_not_found message:\"" + GENERATED_KEY + "\" location:(unknown)", e.getMessage()); - } - - assertEqualsTestTable(SIZE); - } - - @Test - void selectKeyAs() throws Exception { - var sql = "select " + GENERATED_KEY + " as k from " + TEST; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql)) { - var e = assertThrowsExactly(TsurugiTmIOException.class, () -> { - tm.executeAndGetList(ps); - }); - assertEqualsCode(SqlServiceCode.SYMBOL_ANALYZE_EXCEPTION, e); - assertContains("compile failed with error:variable_not_found message:\"" + GENERATED_KEY + "\" location:(unknown)", e.getMessage()); - } - - assertEqualsTestTable(SIZE); - } - - @Test - void selectKeyExpression() throws Exception { - var sql = "select " + GENERATED_KEY + "+0 from " + TEST + " order by " + GENERATED_KEY; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql)) { - var e = assertThrowsExactly(TsurugiTmIOException.class, () -> { - tm.executeAndGetList(ps); - }); - assertEqualsCode(SqlServiceCode.SYMBOL_ANALYZE_EXCEPTION, e); - assertContains("compile failed with error:variable_not_found message:\"" + GENERATED_KEY + "\" location:(unknown)", e.getMessage()); - } - - assertEqualsTestTable(SIZE); - } - - @Test - void selectGroupBy0() throws Exception { - var sql = "select " + GENERATED_KEY + "+0, count(*) as c from " + TEST + " group by " + GENERATED_KEY; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql)) { - var e = assertThrowsExactly(TsurugiTmIOException.class, () -> { - tm.executeAndGetList(ps); - }); - assertEqualsCode(SqlServiceCode.SYMBOL_ANALYZE_EXCEPTION, e); - assertContains("compile failed with error:variable_not_found message:\"" + GENERATED_KEY + "\" location:(unknown)", e.getMessage()); - } - - assertEqualsTestTable(SIZE); - } - - @Test - void selectGroupBy() throws Exception { - var sql = "select " + GENERATED_KEY + ", count(*) as c from " + TEST + " group by " + GENERATED_KEY; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql)) { - var e = assertThrowsExactly(TsurugiTmIOException.class, () -> { - tm.executeAndGetList(ps); - }); - assertEqualsCode(SqlServiceCode.SYMBOL_ANALYZE_EXCEPTION, e); - assertContains("compile failed with error:variable_not_found message:\"" + GENERATED_KEY + "\" location:(unknown)", e.getMessage()); - } - - assertEqualsTestTable(SIZE); - } - - @Test - void update() throws Exception { - int key = 2; - var sql = "update " + TEST + " set bar=11 where " + GENERATED_KEY + "=" + key; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(sql)) { - var e = assertThrowsExactly(TsurugiTmIOException.class, () -> { - tm.executeAndGetCount(ps); - }); - assertEqualsCode(SqlServiceCode.SYMBOL_ANALYZE_EXCEPTION, e); - assertContains("compile failed with error:variable_not_found message:\"" + GENERATED_KEY + "\" location:(unknown)", e.getMessage()); - } - - assertEqualsTestTable(SIZE); - } - - @Test - void updateKey() throws Exception { - int key = 2; - var sql = "update " + TEST + " set " + GENERATED_KEY + "=1, bar=11 where " + GENERATED_KEY + "=" + key; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(sql)) { - var e = assertThrowsExactly(TsurugiTmIOException.class, () -> { - tm.executeAndGetCount(ps); - }); - assertEqualsCode(SqlServiceCode.SYMBOL_ANALYZE_EXCEPTION, e); - assertContains("compile failed with error:variable_not_found message:\"" + GENERATED_KEY + "\" location:(unknown)", e.getMessage()); - } - - assertEqualsTestTable(SIZE); - } - - @Test - void selectAsGeneratedRowid() throws Exception { - dropTestTable(); - createTestTable(); - insertTestTable(SIZE); - - // TODO アンダースコア2個で始まるカラム名はシステム予約でありユーザーが使用できないので、使ったらエラーになるべき - var sql = "select foo as " + GENERATED_KEY + " from " + TEST + " order by foo"; - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql)) { - var list = tm.executeAndGetList(ps); - assertEquals(SIZE, list.size()); - for (var entity : list) { - // 現状、generated_rowidで始まる別名は取得できない - var e = assertThrowsExactly(IllegalArgumentException.class, () -> { - entity.getInt(GENERATED_KEY); - }); - assertEquals("not found column. name=__generated_rowid___test", e.getMessage()); - } - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/error/DbSystemReservedWordTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/error/DbSystemReservedWordTest.java deleted file mode 100755 index 04bb4054..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/error/DbSystemReservedWordTest.java +++ /dev/null @@ -1,74 +0,0 @@ -package com.tsurugidb.iceaxe.test.error; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; - -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; - -/** - * system reserved word test - * - *
    - *
  • アンダースコア2個で始まるテーブル名やカラム名はシステムで予約されており、ユーザーは使用することが出来ない。
  • - *
- */ -class DbSystemReservedWordTest extends DbTestTableTester { - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - - logInitEnd(info); - } - - @Test - void createTable() throws Exception { - // TODO アンダースコア2個で始まるテーブル名はシステム予約でありユーザーが使用できないので、使ったらエラーになるべき - String tableName = "__test"; - dropTable(tableName); - var sql = "create table " + tableName // - + "(" // - + " foo int," // - + " bar bigint," // - + " zzz varchar(10)" // - + ")"; - executeDdl(getSession(), sql, tableName); - } - - @Test - void createTableColumn() throws Exception { - // TODO アンダースコア2個で始まるカラム名はシステム予約でありユーザーが使用できないので、使ったらエラーになるべき - var sql = "create table " + TEST // - + "(" // - + " __foo int," // - + " __bar bigint," // - + " __zzz varchar(10)" // - + ")"; - executeDdl(getSession(), sql); - } - - @Test - void selectAs() throws Exception { - int size = 4; - createTestTable(); - insertTestTable(size); - - // TODO アンダースコア2個で始まるカラム名はシステム予約でありユーザーが使用できないので、使ったらエラーになるべき - var sql = "select foo as __foo from " + TEST + " order by foo"; - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql)) { - var list = tm.executeAndGetList(ps); - assertEquals(size, list.size()); - int i = 0; - for (var entity : list) { - assertEquals(i++, entity.getInt("__foo")); - } - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertBoundaryValueTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertBoundaryValueTest.java deleted file mode 100755 index 7ac26c43..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertBoundaryValueTest.java +++ /dev/null @@ -1,147 +0,0 @@ -package com.tsurugidb.iceaxe.test.insert; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import java.io.IOException; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.TestInfo; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; - -import com.tsurugidb.iceaxe.sql.parameter.TgBindParameters; -import com.tsurugidb.iceaxe.sql.parameter.TgBindVariable; -import com.tsurugidb.iceaxe.sql.parameter.TgParameterMapping; -import com.tsurugidb.iceaxe.sql.result.TgResultMapping; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; - -/** - * insert boundary value test - */ -class DbInsertBoundaryValueTest extends DbTestTableTester { - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTable(); - - logInitEnd(info); - } - - private static void createTable() throws IOException, InterruptedException { - var sql = "create table " + TEST // - + "(" // - + " int4 int," // - + " int8 bigint," // - + " float4 float," // - + " float8 double" // - + ")"; - executeDdl(getSession(), sql); - } - - @ParameterizedTest - @ValueSource(ints = { Integer.MIN_VALUE, Integer.MAX_VALUE }) - void insertInt4(int value) throws Exception { - String name = "int4"; - var variable = TgBindVariable.ofInt("value"); - - var insertSql = "insert into " + TEST // - + "(" + name + ")" // - + " values(" + variable + ")"; - var insertMapping = TgParameterMapping.of(variable); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(insertSql, insertMapping)) { - var parameter = TgBindParameters.of(variable.bind(value)); - tm.executeAndGetCount(ps, parameter); - } - - var selectSql = "select " + name + " from " + TEST; - var selectMapping = TgResultMapping.of(record -> record.nextInt()); - try (var ps = session.createQuery(selectSql, selectMapping)) { - var actual = tm.executeAndFindRecord(ps).get(); - assertEquals(value, actual); - } - } - - @ParameterizedTest - @ValueSource(longs = { Long.MIN_VALUE, Long.MAX_VALUE }) - void insertInt8(long value) throws Exception { - String name = "int8"; - var variable = TgBindVariable.ofLong("value"); - - var insertSql = "insert into " + TEST // - + "(" + name + ")" // - + " values(" + variable + ")"; - var insertMapping = TgParameterMapping.of(variable); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(insertSql, insertMapping)) { - var parameter = TgBindParameters.of(variable.bind(value)); - tm.executeAndGetCount(ps, parameter); - } - - var selectSql = "select " + name + " from " + TEST; - var selectMapping = TgResultMapping.of(record -> record.nextLong()); - try (var ps = session.createQuery(selectSql, selectMapping)) { - var actual = tm.executeAndFindRecord(ps).get(); - assertEquals(value, actual); - } - } - - @ParameterizedTest - @ValueSource(floats = { Float.MIN_VALUE, Float.MAX_VALUE, Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY, Float.NaN }) - void insertFloat4(float value) throws Exception { - String name = "float4"; - var variable = TgBindVariable.ofFloat("value"); - - var insertSql = "insert into " + TEST // - + "(" + name + ")" // - + " values(" + variable + ")"; - var insertMapping = TgParameterMapping.of(variable); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(insertSql, insertMapping)) { - var parameter = TgBindParameters.of(variable.bind(value)); - tm.executeAndGetCount(ps, parameter); - } - - var selectSql = "select " + name + " from " + TEST; - var selectMapping = TgResultMapping.of(record -> record.nextFloat()); - try (var ps = session.createQuery(selectSql, selectMapping)) { - var actual = tm.executeAndFindRecord(ps).get(); - assertEquals(value, actual); - } - } - - @ParameterizedTest - @ValueSource(doubles = { Double.MIN_VALUE, Double.MAX_VALUE, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, Double.NaN }) - void insertFloat8(double value) throws Exception { - String name = "float8"; - var variable = TgBindVariable.ofDouble("value"); - - var insertSql = "insert into " + TEST // - + "(" + name + ")" // - + " values(" + variable + ")"; - var insertMapping = TgParameterMapping.of(variable); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(insertSql, insertMapping)) { - var parameter = TgBindParameters.of(variable.bind(value)); - tm.executeAndGetCount(ps, parameter); - } - - var selectSql = "select " + name + " from " + TEST; - var selectMapping = TgResultMapping.of(record -> record.nextDouble()); - try (var ps = session.createQuery(selectSql, selectMapping)) { - var actual = tm.executeAndFindRecord(ps).get(); - assertEquals(value, actual); - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertCharTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertCharTest.java deleted file mode 100755 index 72a29e66..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertCharTest.java +++ /dev/null @@ -1,168 +0,0 @@ -package com.tsurugidb.iceaxe.test.insert; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrowsExactly; - -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.util.List; -import java.util.function.Function; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; - -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.test.util.TestEntity; -import com.tsurugidb.iceaxe.transaction.manager.exception.TsurugiTmIOException; -import com.tsurugidb.tsubakuro.sql.SqlServiceCode; - -/** - * insert char test - */ -class DbInsertCharTest extends DbTestTableTester { - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTable(); - - logInitEnd(info); - } - - private static void createTable() throws IOException, InterruptedException { - var sql = "create table " + TEST // - + "(" // - + " foo int primary key," // - + " bar bigint," // - + " zzz char(" + ZZZ_SIZE + ")" // - + ")"; - executeDdl(getSession(), sql); - } - - @Test - void insertNull() throws Exception { - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING)) { - var entity = new TestEntity(1, 1, null); - int count = tm.executeAndGetCount(ps, entity); - assertUpdateCount(1, count); - - var actual = selectFromTest(entity.getFoo()); - assertEquals(entity, actual); - } - } - - @Test - void insertOK() throws Exception { - insertOK(DbInsertCharTest::padTail); - } - - void insertOK(Function pad) throws Exception { - var list = List.of("", "0123456789", "あいう", "あいう0", "\0\1\uffff"); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING)) { - int i = 0; - for (var zzz : list) { - assert zzz.getBytes(StandardCharsets.UTF_8).length <= ZZZ_SIZE; - - var entity = new TestEntity(++i, i, zzz); - int count = tm.executeAndGetCount(ps, entity); - assertUpdateCount(1, count); - - var actual = selectFromTest(entity.getFoo()); - assertEquals(entity.getFoo(), actual.getFoo()); - assertEquals(entity.getBar(), actual.getBar()); - assertEquals(pad.apply(entity.getZzz()), actual.getZzz()); - } - } - } - - @Test - void insertError() throws Exception { - // UTF-8でのバイト数がcharのサイズを超えるとエラー - var list = List.of("0123456789A", "あいうえ", "あいう01"); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING)) { - int i = 0; - for (var zzz : list) { - assert zzz.getBytes(StandardCharsets.UTF_8).length > ZZZ_SIZE; - - var entity = new TestEntity(++i, i, zzz); - var e = assertThrowsExactly(TsurugiTmIOException.class, () -> { - tm.executeAndGetCount(ps, entity); - }); - assertEqualsCode(SqlServiceCode.SQL_LIMIT_REACHED_EXCEPTION, e); - assertContains("Insufficient storage to store field data", e.getMessage()); // TODO エラー詳細情報(カラム名や桁数) - } - } - } - - @Test - void insertNulChar() throws Exception { - insertNulChar(DbInsertCharTest::padTail); - } - - void insertNulChar(Function pad) throws Exception { - int size = 6; - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING)) { - for (int i = 1; i <= size; i++) { - var entity = new TestEntity(i, i, nulCharText(i)); - - int count = tm.executeAndGetCount(ps, entity); - assertUpdateCount(1, count); - } - } - - try (var ps = session.createQuery(SELECT_SQL + " order by zzz", SELECT_MAPPING)) { - var list = tm.executeAndGetList(ps); - assertEquals(size, list.size()); - // int i = size; - for (var entity : list) { - // TODO assertEquals(i--, entity.getFoo()); - var expected = pad.apply(nulCharText(entity.getFoo())); - switch (entity.getFoo()) { // TODO remove 補正 - case 4: - case 5: - case 6: - expected = "ab"; // 補正 - break; - default: - break; - } - assertEquals(expected, entity.getZzz()); - } - } - } - - private static String nulCharText(int size) { - var sb = new StringBuilder(); - sb.append("ab"); - for (int i = 0; i < size; i++) { - sb.append('\0'); - } - sb.append("c"); - return sb.toString(); - } - - private static String padTail(String s) { - int length = s.getBytes(StandardCharsets.UTF_8).length; - if (length >= ZZZ_SIZE) { - return s; - } - var sb = new StringBuilder(s); - for (int i = 0; i < ZZZ_SIZE - length; i++) { - sb.append(" "); - } - return sb.toString(); - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertConstraintTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertConstraintTest.java deleted file mode 100755 index 7826ed2a..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertConstraintTest.java +++ /dev/null @@ -1,201 +0,0 @@ -package com.tsurugidb.iceaxe.test.insert; - -import static org.junit.jupiter.api.Assertions.assertThrowsExactly; - -import java.io.IOException; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; - -import com.tsurugidb.iceaxe.sql.TsurugiSqlPreparedStatement; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.test.util.TestEntity; -import com.tsurugidb.iceaxe.transaction.TgCommitType; -import com.tsurugidb.iceaxe.transaction.exception.TsurugiTransactionException; -import com.tsurugidb.iceaxe.transaction.function.TsurugiTransactionAction; -import com.tsurugidb.iceaxe.transaction.manager.TsurugiTransactionManager; -import com.tsurugidb.iceaxe.transaction.manager.exception.TsurugiTmIOException; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; -import com.tsurugidb.tsubakuro.sql.SqlServiceCode; - -/** - * insert constraint violation test - */ -public class DbInsertConstraintTest extends DbTestTableTester { - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTestTable(); - - logInitEnd(info); - } - - @Test - void insertSequentialTx() throws Exception { - var entity = new TestEntity(123, 456, "abc"); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING)) { - int count1 = tm.executeAndGetCount(ps, entity); - assertUpdateCount(1, count1); - - var e = assertThrowsExactly(TsurugiTmIOException.class, () -> { - tm.executeAndGetCount(ps, entity); - }); - assertEqualsCode(SqlServiceCode.UNIQUE_CONSTRAINT_VIOLATION_EXCEPTION, e); - } - - assertEqualsTestTable(entity); - } - - @Test - void insertSameTx() throws Exception { - var entity = new TestEntity(123, 456, "abc"); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING)) { - var e0 = assertThrowsExactly(TsurugiTmIOException.class, () -> { - tm.execute((TsurugiTransactionAction) transaction -> { - int count1 = transaction.executeAndGetCount(ps, entity); - assertUpdateCount(1, count1); - - var e = assertThrowsExactly(TsurugiTransactionException.class, () -> { - transaction.executeAndGetCount(ps, entity); - }); - assertEqualsCode(SqlServiceCode.UNIQUE_CONSTRAINT_VIOLATION_EXCEPTION, e); - throw e; - }); - }); - - assertEqualsCode(SqlServiceCode.UNIQUE_CONSTRAINT_VIOLATION_EXCEPTION, e0); - } - - assertEqualsTestTable(); - } - - @Test - void insertSameTxIgnoreEx() throws Exception { - var entity = new TestEntity(123, 456, "abc"); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING)) { - var e0 = assertThrowsExactly(TsurugiTmIOException.class, () -> { - tm.execute(transaction -> { - int count1 = transaction.executeAndGetCount(ps, entity); - assertUpdateCount(1, count1); - - var e = assertThrowsExactly(TsurugiTransactionException.class, () -> { - transaction.executeAndGetCount(ps, entity); - }); - assertEqualsCode(SqlServiceCode.UNIQUE_CONSTRAINT_VIOLATION_EXCEPTION, e); - // throw e; // ignore exception - }); - }); - - assertEqualsCode(SqlServiceCode.INACTIVE_TRANSACTION_EXCEPTION, e0); - } - - assertEqualsTestTable(); - } - - @Test - void insertSameTxLazyCheck() throws Exception { - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING)) { - for (int i = 0; i < 10; i++) { - insertSameTxLazyCheck(tm, ps); - } - } - } - - private void insertSameTxLazyCheck(TsurugiTransactionManager tm, TsurugiSqlPreparedStatement ps) throws IOException, InterruptedException { - var e0 = assertThrowsExactly(TsurugiTmIOException.class, () -> { - tm.execute((TsurugiTransactionAction) transaction -> { - var entity = createTestEntity(1); - var r1 = ps.execute(transaction, entity); - var r2 = ps.execute(transaction, entity); - try (r1; r2) { - int count1 = r1.getUpdateCount(); - assertUpdateCount(1, count1); - - var e = assertThrowsExactly(TsurugiTransactionException.class, () -> { - r2.getUpdateCount(); - }); - assertEqualsCode(SqlServiceCode.UNIQUE_CONSTRAINT_VIOLATION_EXCEPTION, e); - throw e; - } - }); - }); - assertEqualsCode(SqlServiceCode.UNIQUE_CONSTRAINT_VIOLATION_EXCEPTION, e0); -// assertContains("TODO", e0.getMessage()); // TODO エラー詳細情報の確認 - - assertEqualsTestTable(0); - } - - @Test - void insertParallelTx() throws Exception { - var entity = new TestEntity(123, 456, "abc"); - - var session = getSession(); - try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING)) { - var e0 = assertThrowsExactly(TsurugiTransactionException.class, () -> { - try (var tx1 = session.createTransaction(TgTxOption.ofOCC()); // - var tx2 = session.createTransaction(TgTxOption.ofOCC())) { - int count1 = tx1.executeAndGetCount(ps, entity); - assertUpdateCount(1, count1); - int count2 = tx2.executeAndGetCount(ps, entity); - assertUpdateCount(1, count2); - - tx1.commit(TgCommitType.DEFAULT); - var e = assertThrowsExactly(TsurugiTransactionException.class, () -> { - tx2.commit(TgCommitType.DEFAULT); - }); - assertEqualsCode(SqlServiceCode.CC_EXCEPTION, e); - - try (var tx3 = session.createTransaction(TgTxOption.ofOCC())) { - int count3 = tx3.executeAndGetCount(ps, entity); - assertUpdateCount(1, count3); - var e3 = assertThrowsExactly(TsurugiTransactionException.class, () -> { - tx3.commit(TgCommitType.DEFAULT); - }); - assertEqualsCode(SqlServiceCode.UNIQUE_CONSTRAINT_VIOLATION_EXCEPTION, e3); - throw e3; - } - } - }); - - assertEqualsCode(SqlServiceCode.UNIQUE_CONSTRAINT_VIOLATION_EXCEPTION, e0); - } - - assertEqualsTestTable(entity); - } - - @Test - void insertParallelTxRollback() throws Exception { - var entity = new TestEntity(123, 456, "abc"); - - var session = getSession(); - try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING)) { - try (var tx1 = session.createTransaction(TgTxOption.ofOCC()); // - var tx2 = session.createTransaction(TgTxOption.ofOCC())) { - int count1 = tx1.executeAndGetCount(ps, entity); - assertUpdateCount(1, count1); - int count2 = tx2.executeAndGetCount(ps, entity); - assertUpdateCount(1, count2); - - tx1.rollback(); - tx2.commit(TgCommitType.DEFAULT); - } - } - - assertEqualsTestTable(entity); - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertDecimalTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertDecimalTest.java deleted file mode 100755 index f130f23a..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertDecimalTest.java +++ /dev/null @@ -1,112 +0,0 @@ -package com.tsurugidb.iceaxe.test.insert; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrowsExactly; - -import java.io.IOException; -import java.math.BigDecimal; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.TestInfo; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; - -import com.tsurugidb.iceaxe.sql.parameter.TgBindParameters; -import com.tsurugidb.iceaxe.sql.parameter.TgBindVariable; -import com.tsurugidb.iceaxe.sql.parameter.TgBindVariable.TgBindVariableBigDecimal; -import com.tsurugidb.iceaxe.sql.parameter.TgParameterMapping; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.transaction.manager.exception.TsurugiTmIOException; -import com.tsurugidb.tsubakuro.sql.SqlServiceCode; - -/** - * insert decimal test - */ -class DbInsertDecimalTest extends DbTestTableTester { - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTable(); - - logInitEnd(info); - } - - private static void createTable() throws IOException, InterruptedException { - var sql = "create table " + TEST // - + "(" // - + " value decimal(5,2)" // - + ")"; - executeDdl(getSession(), sql); - } - - private static final String VNAME = "value"; - private static final String SQL = "insert into " + TEST + "(value) values(:" + VNAME + ")"; - - @ParameterizedTest - @ValueSource(strings = { "123", "1.01" }) - void normal(String s) throws Exception { - var value = new BigDecimal(s); - var variable = TgBindVariable.ofDecimal(VNAME); - var mapping = TgParameterMapping.of(variable); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(SQL, mapping)) { - var parameter = TgBindParameters.of(variable.bind(value)); - int count = tm.executeAndGetCount(ps, parameter); - assertUpdateCount(1, count); - } - - var actual = selectValue(); - assertEquals(value.setScale(2), actual); - } - - @ParameterizedTest - @ValueSource(strings = { "1234", "1.001" }) - void outOfRange(String s) throws Exception { - var value = new BigDecimal(s); - var variable = TgBindVariable.ofDecimal(VNAME); - var mapping = TgParameterMapping.of(variable); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(SQL, mapping)) { - var parameter = TgBindParameters.of(variable.bind(value)); - var e = assertThrowsExactly(TsurugiTmIOException.class, () -> tm.executeAndGetCount(ps, parameter)); - assertEqualsCode(SqlServiceCode.VALUE_EVALUATION_EXCEPTION, e); - assertContains("An error occurred in evaluating values. Encoding failed", e.getMessage()); // TODO エラー詳細情報の確認 - } - } - - @ParameterizedTest - @ValueSource(strings = { "1.1", "1.01", "1.001" }) - void scale(String s) throws Exception { - var value = new BigDecimal(s); - var variable = TgBindVariable.ofDecimal(VNAME, 2); // TgVariable with scale - var mapping = TgParameterMapping.of(variable); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(SQL, mapping)) { - var parameter = TgBindParameters.of(variable.bind(value)); - int count = tm.executeAndGetCount(ps, parameter); - assertUpdateCount(1, count); - } - - var actual = selectValue(); - var expected = value.setScale(2, TgBindVariableBigDecimal.DEFAULT_ROUNDING_MODE); - assertEquals(expected, actual); - } - - private BigDecimal selectValue() throws IOException, InterruptedException { - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery("select value from " + TEST)) { - var entity = tm.executeAndFindRecord(ps).get(); - return entity.getDecimal(VNAME); - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertDuplicate2Test.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertDuplicate2Test.java deleted file mode 100755 index fd5075b7..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertDuplicate2Test.java +++ /dev/null @@ -1,199 +0,0 @@ -package com.tsurugidb.iceaxe.test.insert; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.concurrent.Callable; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicInteger; -import java.util.stream.Collectors; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.tsurugidb.iceaxe.exception.TsurugiExceptionUtil; -import com.tsurugidb.iceaxe.session.TsurugiSession; -import com.tsurugidb.iceaxe.sql.TsurugiSqlPreparedStatement; -import com.tsurugidb.iceaxe.sql.TsurugiSqlQuery; -import com.tsurugidb.iceaxe.sql.parameter.TgBindParameters; -import com.tsurugidb.iceaxe.sql.parameter.TgBindVariable; -import com.tsurugidb.iceaxe.sql.parameter.TgBindVariable.TgBindVariableInteger; -import com.tsurugidb.iceaxe.sql.parameter.TgBindVariable.TgBindVariableString; -import com.tsurugidb.iceaxe.sql.parameter.TgBindVariables; -import com.tsurugidb.iceaxe.sql.parameter.TgParameterMapping; -import com.tsurugidb.iceaxe.sql.result.TgResultMapping; -import com.tsurugidb.iceaxe.test.util.DbTestSessions; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.test.util.TestEntity; -import com.tsurugidb.iceaxe.transaction.TsurugiTransaction; -import com.tsurugidb.iceaxe.transaction.exception.TsurugiTransactionException; -import com.tsurugidb.iceaxe.transaction.manager.TgTmSetting; -import com.tsurugidb.iceaxe.transaction.manager.exception.TsurugiTmIOException; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; - -/** - * insert duplicate bug test - */ -class DbInsertDuplicate2Test extends DbTestTableTester { - - private static final String TEST2 = "test2"; - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTestTable(); - insertTestTable(10); - - dropTable(TEST2); - createTest2Table(); - - logInitEnd(info); - } - - private static void createTest2Table() throws IOException, InterruptedException { - var sql = "create table " + TEST2 // - + "(" // - + " key1 int," // - + " key2 int," // - + " zzz2 varchar(10)," // - + " primary key(key1, key2)" // - + ")"; - executeDdl(getSession(), sql); - } - - @Test - void occ() throws Exception { - var setting = TgTmSetting.ofAlways(TgTxOption.ofOCC()); - test(setting, 30, 500); - } - - @Test - void ltx() throws Exception { - var setting = TgTmSetting.ofAlways(TgTxOption.ofLTX(TEST, TEST2)); - test(setting, 30, 500); - } - - @Test - void mix() throws Exception { - var setting = TgTmSetting.of(TgTxOption.ofOCC(), 3, TgTxOption.ofLTX(TEST, TEST2), 10); - test(setting, 10, 500); - } - - private void test(TgTmSetting setting, int threadSize, int attemptSize) throws Exception { - try (var sessions = new DbTestSessions(Long.MAX_VALUE, TimeUnit.NANOSECONDS)) { - var counter = new AtomicInteger(0); - - var onlineList = new ArrayList(threadSize); - for (int i = 0; i < threadSize; i++) { - var task = new OnlineTask(sessions.createSession(), setting, attemptSize, counter); - onlineList.add(task); - } - - var service = Executors.newCachedThreadPool(); - var futureList = new ArrayList>(threadSize); - onlineList.forEach(task -> futureList.add(service.submit(task))); - - var exceptionList = new ArrayList(); - for (var future : futureList) { - try { - future.get(); - } catch (Exception e) { - exceptionList.add(e); - } - } - if (!exceptionList.isEmpty()) { - if (exceptionList.size() == 1) { - throw exceptionList.get(0); - } - var e = new Exception(exceptionList.stream().map(Exception::getMessage).collect(Collectors.joining("\n"))); - exceptionList.stream().forEach(e::addSuppressed); - throw e; - } - } - } - - private static class OnlineTask implements Callable { - private static final Logger LOG = LoggerFactory.getLogger(OnlineTask.class); - - private static final TgBindVariableInteger vKey1 = TgBindVariable.ofInt("key1"); - private static final TgBindVariableInteger vKey2 = TgBindVariable.ofInt("key2"); - private static final TgBindVariableString vZzz2 = TgBindVariable.ofString("zzz2"); - - private final TsurugiSession session; - private final TgTmSetting setting; - private final int attemptSize; - private final AtomicInteger counter; - - public OnlineTask(TsurugiSession session, TgTmSetting setting, int attemptSize, AtomicInteger counter) { - this.session = session; - this.setting = setting; - this.attemptSize = attemptSize; - this.counter = counter; - } - - @Override - public Void call() throws Exception { - var maxSql = "select max(foo) + 1 as foo from " + TEST; - var maxMapping = TgResultMapping.of(record -> record.nextInt()); // x ofSingle(), nextIntOrNull() - - var insert2List = TgBindVariables.of(vKey1, vKey2, vZzz2); - var insert2Sql = "insert into " + TEST2 // - + "(key1, key2, zzz2)" // - + "values(" + insert2List.getSqlNames() + ")"; - var insert2Mapping = TgParameterMapping.of(insert2List); - - try (var maxPs = session.createQuery(maxSql, maxMapping); // - var insertPs = session.createStatement(INSERT_SQL, INSERT_MAPPING); // - var insert2Ps = session.createStatement(insert2Sql, insert2Mapping)) { - var tm = session.createTransactionManager(setting); - - for (;;) { - if (counter.get() >= attemptSize) { - break; - } - try { - tm.execute(transaction -> { - execute(transaction, maxPs, insertPs, insert2Ps); - }); - } catch (TsurugiTmIOException e) { - var exceptionUtil = TsurugiExceptionUtil.getInstance(); - if (exceptionUtil.isUniqueConstraintViolation(e)) { -// LOG.info("UNIQUE_CONSTRAINT_VIOLATION: {}", e.getMessage()); - continue; - } - if (exceptionUtil.isSerializationFailure(e)) { - String message = e.getMessage(); - if (message.contains("reason_code:KVS_INSERT")) { -// LOG.info("CC_EXCEPTION:KVS_INSERT: {}", message); - continue; - } - } - LOG.error("online task error: {}", e.getMessage()); - throw e; - } - counter.incrementAndGet(); - } - } - return null; - } - - private void execute(TsurugiTransaction transaction, TsurugiSqlQuery maxPs, TsurugiSqlPreparedStatement insertPs, TsurugiSqlPreparedStatement insert2Ps) - throws IOException, InterruptedException, TsurugiTransactionException { - int foo = transaction.executeAndFindRecord(maxPs).get(); - - var entity = new TestEntity(foo, foo, Integer.toString(foo)); - transaction.executeAndGetCount(insertPs, entity); - - for (int i = 0; i < 10; i++) { - var parameter = TgBindParameters.of(vKey1.bind(foo), vKey2.bind(i + 1), vZzz2.bind(Integer.toString(foo))); - transaction.executeAndGetCount(insert2Ps, parameter); - } - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertDuplicateTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertDuplicateTest.java deleted file mode 100755 index aaea8cf7..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertDuplicateTest.java +++ /dev/null @@ -1,177 +0,0 @@ -package com.tsurugidb.iceaxe.test.insert; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.concurrent.Callable; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; -import java.util.stream.Collectors; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.tsurugidb.iceaxe.exception.TsurugiExceptionUtil; -import com.tsurugidb.iceaxe.session.TsurugiSession; -import com.tsurugidb.iceaxe.sql.TsurugiSqlPreparedStatement; -import com.tsurugidb.iceaxe.sql.TsurugiSqlQuery; -import com.tsurugidb.iceaxe.sql.parameter.TgBindParameters; -import com.tsurugidb.iceaxe.sql.parameter.TgBindVariable; -import com.tsurugidb.iceaxe.sql.parameter.TgBindVariable.TgBindVariableInteger; -import com.tsurugidb.iceaxe.sql.parameter.TgBindVariable.TgBindVariableString; -import com.tsurugidb.iceaxe.sql.parameter.TgBindVariables; -import com.tsurugidb.iceaxe.sql.parameter.TgParameterMapping; -import com.tsurugidb.iceaxe.sql.result.TsurugiResultEntity; -import com.tsurugidb.iceaxe.test.util.DbTestSessions; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.test.util.TestEntity; -import com.tsurugidb.iceaxe.transaction.TsurugiTransaction; -import com.tsurugidb.iceaxe.transaction.exception.TsurugiTransactionException; -import com.tsurugidb.iceaxe.transaction.manager.TgTmSetting; -import com.tsurugidb.iceaxe.transaction.manager.exception.TsurugiTmIOException; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; - -/** - * insert duplicate bug test - */ -class DbInsertDuplicateTest extends DbTestTableTester { - - private static final String TEST2 = "test2"; - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTestTable(); - insertTestTable(10); - - dropTable(TEST2); - createTest2Table(); - - logInitEnd(info); - } - - private static void createTest2Table() throws IOException, InterruptedException { - var sql = "create table " + TEST2 // - + "(" // - + " key1 int," // - + " key2 int," // - + " zzz2 varchar(10)," // - + " primary key(key1, key2)" // - + ")"; - executeDdl(getSession(), sql); - } - - @Test - void occ() throws Exception { - test(TgTxOption.ofOCC()); - } - - @Test - void ltx() throws Exception { - test(TgTxOption.ofLTX(TEST, TEST2)); - } - - private void test(TgTxOption txOption) throws Exception { - test(txOption, new DbTestSessions()); - } - - private void test(TgTxOption txOption, DbTestSessions sessions) throws Exception { - int onlineSize = 40; - - try (sessions) { - var onlineList = new ArrayList(onlineSize); - for (int i = 0; i < onlineSize; i++) { - var task = new OnlineTask(sessions.createSession(), txOption); - onlineList.add(task); - } - - var service = Executors.newCachedThreadPool(); - var futureList = new ArrayList>(onlineSize); - onlineList.forEach(task -> futureList.add(service.submit(task))); - - var exceptionList = new ArrayList(); - for (var future : futureList) { - try { - future.get(); - } catch (Exception e) { - exceptionList.add(e); - } - } - if (!exceptionList.isEmpty()) { - if (exceptionList.size() == 1) { - throw exceptionList.get(0); - } - var e = new Exception(exceptionList.stream().map(Exception::getMessage).collect(Collectors.joining("\n"))); - exceptionList.stream().forEach(e::addSuppressed); - throw e; - } - } - } - - private static class OnlineTask implements Callable { - private static final Logger LOG = LoggerFactory.getLogger(OnlineTask.class); - - private static final TgBindVariableInteger vKey1 = TgBindVariable.ofInt("key1"); - private static final TgBindVariableInteger vKey2 = TgBindVariable.ofInt("key2"); - private static final TgBindVariableString vZzz2 = TgBindVariable.ofString("zzz2"); - - private final TsurugiSession session; - private final TgTxOption txOption; - - public OnlineTask(TsurugiSession session, TgTxOption txOption) { - this.session = session; - this.txOption = txOption; - } - - @Override - public Void call() throws Exception { - var maxSql = "select max(foo) + 1 as foo from " + TEST; - - var insert2List = TgBindVariables.of(vKey1, vKey2, vZzz2); - var insert2Sql = "insert into " + TEST2 // - + "(key1, key2, zzz2)" // - + "values(" + insert2List.getSqlNames() + ")"; - var insert2Mapping = TgParameterMapping.of(insert2List); - - try (var maxPs = session.createQuery(maxSql); // - var insertPs = session.createStatement(INSERT_SQL, INSERT_MAPPING); // - var insert2Ps = session.createStatement(insert2Sql, insert2Mapping)) { - var setting = TgTmSetting.ofAlways(txOption); - var tm = session.createTransactionManager(setting); - - for (int i = 0; i < 40; i++) { - try { - tm.execute(transaction -> { - execute(transaction, maxPs, insertPs, insert2Ps); - }); - } catch (TsurugiTmIOException e) { - if (TsurugiExceptionUtil.getInstance().isUniqueConstraintViolation(e)) { -// LOG.info("UNIQUE_CONSTRAINT_VIOLATION {}", i); - i--; - continue; - } - LOG.error("online task error: {}", e.getMessage()); - throw e; - } - } - } - return null; - } - - private void execute(TsurugiTransaction transaction, TsurugiSqlQuery maxPs, TsurugiSqlPreparedStatement insertPs, - TsurugiSqlPreparedStatement insert2Ps) throws IOException, InterruptedException, TsurugiTransactionException { - var max = transaction.executeAndFindRecord(maxPs).get(); - int foo = max.getInt("foo"); - - var entity = new TestEntity(foo, foo, Integer.toString(foo)); - transaction.executeAndGetCount(insertPs, entity); - - var parameter = TgBindParameters.of(vKey1.bind(foo), vKey2.bind(foo / 2), vZzz2.bind(Integer.toString(foo))); - transaction.executeAndGetCount(insert2Ps, parameter); - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertErrorTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertErrorTest.java deleted file mode 100755 index f296d81b..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertErrorTest.java +++ /dev/null @@ -1,161 +0,0 @@ -package com.tsurugidb.iceaxe.test.insert; - -import static org.junit.jupiter.api.Assertions.assertThrowsExactly; - -import java.io.IOException; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; - -import com.tsurugidb.iceaxe.exception.IceaxeErrorCode; -import com.tsurugidb.iceaxe.exception.TsurugiIOException; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.test.util.TestEntity; -import com.tsurugidb.iceaxe.transaction.exception.TsurugiTransactionException; -import com.tsurugidb.iceaxe.transaction.function.TsurugiTransactionAction; -import com.tsurugidb.iceaxe.transaction.manager.exception.TsurugiTmIOException; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; -import com.tsurugidb.tsubakuro.sql.SqlServiceCode; - -/** - * insert error test - */ -class DbInsertErrorTest extends DbTestTableTester { - - private static final int SIZE = 4; - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - if (!info.getDisplayName().equals("insertNullToNotNull()")) { - createTestTable(); - insertTestTable(SIZE); - } - - logInitEnd(info); - } - - @Test - void insertNullToPK() throws Exception { - var sql = "insert into " + TEST // - + "(" + TEST_COLUMNS + ")" // - + "values(null, 456, 'abc')"; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(sql)) { - var e0 = assertThrowsExactly(TsurugiTmIOException.class, () -> { - tm.execute((TsurugiTransactionAction) transaction -> { - var e = assertThrowsExactly(TsurugiTransactionException.class, () -> { - transaction.executeAndGetCount(ps); - }); - assertEqualsCode(SqlServiceCode.NOT_NULL_CONSTRAINT_VIOLATION_EXCEPTION, e); - assertContains("Null assigned for non-nullable field", e.getMessage()); // TODO エラー詳細情報(カラム名等)の確認 - throw e; - }); - }); - assertEqualsCode(SqlServiceCode.NOT_NULL_CONSTRAINT_VIOLATION_EXCEPTION, e0); - } - - assertEqualsTestTable(SIZE); - } - - @Test - void insertNullToNotNull() throws Exception { - var session = getSession(); - - var createSql = "create table " + TEST // - + "(" // - + " foo int not null," // - + " bar bigint not null," // - + " zzz varchar(10) not null," // - + " primary key(foo)" // - + ")"; - executeDdl(session, createSql); - - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING)) { - var e0 = assertThrowsExactly(TsurugiTmIOException.class, () -> { - tm.execute((TsurugiTransactionAction) transaction -> { - var e = assertThrowsExactly(TsurugiTransactionException.class, () -> { - var entity = new TestEntity(123, null, null); - transaction.executeAndGetCount(ps, entity); - }); - assertEqualsCode(SqlServiceCode.NOT_NULL_CONSTRAINT_VIOLATION_EXCEPTION, e); - assertContains("Null assigned for non-nullable field", e.getMessage()); // TODO エラー詳細情報(カラム名等)の確認 - throw e; - }); - }); - assertEqualsCode(SqlServiceCode.NOT_NULL_CONSTRAINT_VIOLATION_EXCEPTION, e0); - } - - assertEqualsTestTable(0); - } - - @Test - void ps0ExecuteAfterClose() throws Exception { - var sql = "insert into " + TEST // - + "(" + TEST_COLUMNS + ")" // - + "values(1, 1, '1')"; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - var ps = session.createStatement(sql); - ps.close(); - var e = assertThrowsExactly(TsurugiIOException.class, () -> { - tm.executeAndGetCount(ps); - }); - assertEqualsCode(IceaxeErrorCode.PS_ALREADY_CLOSED, e); - - assertEqualsTestTable(SIZE); - } - - @Test - void ps1ExecuteAfterClose() throws Exception { - var session = getSession(); - var tm = createTransactionManagerOcc(session); - var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING); - ps.close(); - var entity = createTestEntity(1); - var e = assertThrowsExactly(TsurugiIOException.class, () -> { - tm.executeAndGetCount(ps, entity); - }); - assertEqualsCode(IceaxeErrorCode.PS_ALREADY_CLOSED, e); - - assertEqualsTestTable(SIZE); - } - - @Test - void ps0ExecuteAfterTxFutureClose() throws Exception { - ps0ExecuteAfterTxClose(false, "Future is already closed"); - } - - @Test - void ps0ExecuteAfterTxClose() throws Exception { - ps0ExecuteAfterTxClose(true, "already closed"); - } - - private void ps0ExecuteAfterTxClose(boolean getLow, String expected) throws IOException, InterruptedException { - var sql = "insert into " + TEST // - + "(" + TEST_COLUMNS + ")" // - + "values(1, 1, '1')"; - - var session = getSession(); - try (var ps = session.createStatement(sql)) { - var transaction = session.createTransaction(TgTxOption.ofOCC()); - if (getLow) { - transaction.getLowTransaction(); - } - transaction.close(); - var e = assertThrowsExactly(TsurugiIOException.class, () -> { - transaction.executeAndGetCount(ps); - }); - assertEqualsCode(IceaxeErrorCode.TX_ALREADY_CLOSED, e); - } - - assertEqualsTestTable(SIZE); - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertExplainTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertExplainTest.java deleted file mode 100755 index 57f2ff0a..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertExplainTest.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.tsurugidb.iceaxe.test.insert; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertThrowsExactly; - -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.slf4j.LoggerFactory; - -import com.tsurugidb.iceaxe.sql.explain.TgStatementMetadata; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; - -/** - * explain insert test - */ -class DbInsertExplainTest extends DbTestTableTester { - - @BeforeAll - static void beforeAll(TestInfo info) throws Exception { - var LOG = LoggerFactory.getLogger(DbInsertExplainTest.class); - logInitStart(LOG, info); - - dropTestTable(); - createTestTable(); - - logInitEnd(LOG, info); - } - - @Test - void pareparedStatement() throws Exception { - var sql = "insert into " + TEST // - + "(" + TEST_COLUMNS + ")" // - + "values(1, 2, '3')"; - - var session = getSession(); - try (var ps = session.createStatement(sql)) { - assertThrowsExactly(UnsupportedOperationException.class, () -> { - var result = ps.explain(); - assertExplain(result); - }); // TODO explain実装待ち - } - } - - @Test - void psParameter() throws Exception { - var session = getSession(); - try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING)) { - var entity = createTestEntity(1); - var result = ps.explain(entity); - assertExplain(result); - } - } - - private static void assertExplain(TgStatementMetadata actual) throws Exception { - assertNotNull(actual.getLowPlanGraph()); - - var list = actual.getLowColumnList(); - assertEquals(0, list.size()); - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertIrregularTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertIrregularTest.java deleted file mode 100755 index 13d6f4a9..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertIrregularTest.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.tsurugidb.iceaxe.test.insert; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; - -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; - -/** - * irregular insert test - */ -class DbInsertIrregularTest extends DbTestTableTester { - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTestTable(); - - logInitEnd(info); - } - - @Test - void closePsBeforeCloseRc() throws Exception { - var sql = "insert into " + TEST // - + "(" + TEST_COLUMNS + ")" // - + "values(123, 456, 'abc')"; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - tm.execute(transaction -> { - var ps = session.createStatement(sql); - var result = ps.execute(transaction); - ps.close(); - int count = result.getUpdateCount(); - assertUpdateCount(1, count); - result.close(); - }); - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertMultiThread2Test.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertMultiThread2Test.java deleted file mode 100755 index 7557e326..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertMultiThread2Test.java +++ /dev/null @@ -1,248 +0,0 @@ -package com.tsurugidb.iceaxe.test.insert; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import java.io.IOException; -import java.io.UncheckedIOException; -import java.time.LocalDate; -import java.util.ArrayList; -import java.util.stream.Collectors; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.RepeatedTest; -import org.junit.jupiter.api.TestInfo; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; - -import com.tsurugidb.iceaxe.sql.TsurugiSqlPreparedStatement; -import com.tsurugidb.iceaxe.sql.TsurugiSqlQuery; -import com.tsurugidb.iceaxe.sql.parameter.TgBindParameter; -import com.tsurugidb.iceaxe.sql.parameter.TgBindParameters; -import com.tsurugidb.iceaxe.sql.parameter.TgBindVariable; -import com.tsurugidb.iceaxe.sql.parameter.TgParameterMapping; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.test.util.TestEntity; -import com.tsurugidb.iceaxe.transaction.TsurugiTransaction; -import com.tsurugidb.iceaxe.transaction.exception.TsurugiTransactionException; -import com.tsurugidb.iceaxe.transaction.manager.TgTmSetting; -import com.tsurugidb.iceaxe.transaction.manager.TsurugiTransactionManager; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; - -/** - * multi thread insert test - */ -class DbInsertMultiThread2Test extends DbTestTableTester { - private static final String TEST2 = "test2"; - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTestTable(); - insertTestTable(4); - - createTest2Table(); - - logInitEnd(info); - } - - private static void createTest2Table() throws IOException, InterruptedException { - dropTable(TEST2); - var sql = "create table " + TEST2 // - + "(" // - + " key1 int," // - + " key2 bigint," // - + " key3 date," // - + " value1 varchar(10)," // - + " primary key(key1, key2, key3)" // - + ")"; - executeDdl(getSession(), sql); - } - - private static class Test2Entity { - private int key1; - private long key2; - private LocalDate key3; - private String value1; - - public Test2Entity(int key1, long key2) { - this.key1 = key1; - this.key2 = key2; - this.key3 = LocalDate.of(2022, 11, 2); - this.value1 = Long.toString(key2); - } - - public int getKey1() { - return this.key1; - } - - public long getKey2() { - return this.key2; - } - - public LocalDate getKey3() { - return this.key3; - } - - public String getValue1() { - return this.value1; - } - } - - @ParameterizedTest - @ValueSource(booleans = { false, true }) - void insertMultiTxOcc1(boolean prepare) throws Exception { -// insertMultiTx(100, 1, TgTmSetting.of(TgTxOption.ofOCC()), prepare); - insertMultiTxOcc(1, prepare); - } - - @ParameterizedTest - @ValueSource(booleans = { false, true }) - @Disabled // TODO remove Disabled. CC_OCC_PHANTOM_AVOIDANCE retry-over - void insertMultiTxOcc30(boolean prepare) throws Exception { - insertMultiTxOcc(30, prepare); - } - - @RepeatedTest(4) - @Disabled // TODO remove Disabled. CC_OCC_PHANTOM_AVOIDANCE retry-over - void insertMultiTxOcc30False() throws Exception { - insertMultiTxOcc(30, false); - } - - private void insertMultiTxOcc(int threadSize, boolean prepare) throws IOException, InterruptedException { - var setting = TgTmSetting.ofAlways(TgTxOption.ofOCC(), 20); // TODO リトライ無しにしたい - setting.getTransactionOptionSupplier().setTmOptionListener((attempt, exception, tmOption) -> { - if (attempt > 0) { - LOG.info("insertMultiTxOcc({}, {}) OCC retry {}", threadSize, prepare, attempt); - } - }); - insertMultiTx(100, threadSize, setting, prepare); - } - - @ParameterizedTest - @ValueSource(booleans = { false, true }) - void insertMultiTxLtx(boolean prepare) throws Exception { - insertMultiTx(100, 30, TgTmSetting.of(TgTxOption.ofLTX(TEST2)), prepare); - } - - private void insertMultiTx(int recordSize, int threadSize, TgTmSetting setting, boolean prepare) throws IOException, InterruptedException { - var key1 = TgBindVariable.ofInt("key1"); - var deleteSql = "delete from " + TEST2 + " where key1=" + key1; - var deleteMapping = TgParameterMapping.of(key1); - var insertSql = "insert into " + TEST2 + "(key1,key2,key3,value1) values(:key1,:key2,:key3,:value1)"; - var insertMapping = TgParameterMapping.of(Test2Entity.class) // - .addInt("key1", Test2Entity::getKey1) // - .addLong("key2", Test2Entity::getKey2) // - .addDate("key3", Test2Entity::getKey3) // - .addString("value1", Test2Entity::getValue1); - - var excptionList = new ArrayList(); - var session = getSession(); - try (var selectPs = session.createQuery(SELECT_SQL, SELECT_MAPPING); // - var deletePs = session.createStatement(deleteSql, deleteMapping); // - var insertPs = session.createStatement(insertSql, insertMapping)) { - if (prepare) { - var tm = session.createTransactionManager(TgTmSetting.of(TgTxOption.ofLTX(TEST2))); - tm.execute(transaction -> { - for (int i = 0; i < threadSize; i++) { - for (int j = 0; j < 2; j++) { - var entity = new Test2Entity(i, 1 + j); - transaction.executeAndGetCount(insertPs, entity); - } - } - return; - }); - } - - var tm = session.createTransactionManager(setting); - var list = new ArrayList(threadSize); - for (int i = 0; i < threadSize; i++) { - var thread = new InsertMultiTxThread(selectPs, deletePs, insertPs, i, recordSize, tm); - list.add(thread); - thread.start(); - } - for (var thread : list) { - try { - thread.join(); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } - var exception = thread.getException(); - if (exception != null) { - excptionList.add(exception); - } - } - } - - if (!excptionList.isEmpty()) { - String message = excptionList.stream().map(e -> e.getMessage()).collect(Collectors.joining("\n")); - var re = new RuntimeException(message); - for (var e : excptionList) { - re.addSuppressed(e); - } - throw re; - } - - var actual = selectCountFrom(TEST2); - try { - assertEquals(recordSize * threadSize, actual); - } catch (Throwable t) { - for (var e : excptionList) { - t.addSuppressed(e); - } - throw t; - } - } - - private static class InsertMultiTxThread extends Thread { - private final TsurugiSqlQuery selectPs; - private final TsurugiSqlPreparedStatement deletePs; - private final TsurugiSqlPreparedStatement insertPs; - private final int number; - private final int recordSize; - private final TsurugiTransactionManager tm; - private Exception exception; - - public InsertMultiTxThread(TsurugiSqlQuery selectPs, TsurugiSqlPreparedStatement deletePs, TsurugiSqlPreparedStatement insertPs, int number, - int recordSize, TsurugiTransactionManager tm) { - this.selectPs = selectPs; - this.deletePs = deletePs; - this.insertPs = insertPs; - this.number = number; - this.recordSize = recordSize; - this.tm = tm; - } - - @Override - public void run() { - try { - tm.execute(transaction -> { - runInTransaction(transaction); - }); - } catch (IOException e) { - this.exception = e; - throw new UncheckedIOException(e.getMessage(), e); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } - } - - private void runInTransaction(TsurugiTransaction transaction) throws IOException, InterruptedException, TsurugiTransactionException { - var parameter = TgBindParameters.of(TgBindParameter.of("key1", number)); - transaction.executeAndGetCount(deletePs, parameter); - - for (int i = 0; i < recordSize; i++) { - transaction.executeAndGetList(selectPs); - - var entity = new Test2Entity(number, i); - transaction.executeAndGetCount(insertPs, entity); - } - } - - public Exception getException() { - return this.exception; - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertMultiThreadTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertMultiThreadTest.java deleted file mode 100755 index f4836fb7..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertMultiThreadTest.java +++ /dev/null @@ -1,188 +0,0 @@ -package com.tsurugidb.iceaxe.test.insert; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import java.io.IOException; -import java.io.UncheckedIOException; -import java.util.ArrayList; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; - -import com.tsurugidb.iceaxe.sql.TsurugiSqlPreparedStatement; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.test.util.TestEntity; -import com.tsurugidb.iceaxe.transaction.TsurugiTransaction; -import com.tsurugidb.iceaxe.transaction.exception.TsurugiTransactionException; -import com.tsurugidb.iceaxe.transaction.manager.TsurugiTransactionManager; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; - -/** - * multi thread insert test - */ -class DbInsertMultiThreadTest extends DbTestTableTester { - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTestTable(); - - logInitEnd(info); - } - - @Test - void insertSingleTxOcc() throws Exception { - insertSingleTx(1000, 30, TgTxOption.ofOCC()); - } - - @Test - void insertSingleTxLtx() throws Exception { - insertSingleTx(1000, 30, TgTxOption.ofLTX(TEST)); - } - - @Test - void insertMultiTxOcc() throws Exception { - insertMultiTx(1000, 30, TgTxOption.ofOCC()); - } - - @ParameterizedTest - @ValueSource(ints = { 2, 3, 8, 30 }) - void insertMultiTxLtx(int threadSize) throws Exception { - insertMultiTx(1000, threadSize, TgTxOption.ofLTX(TEST)); - } - - /** - * single transaction, parallel insert - */ - private void insertSingleTx(int recordSize, int threadSize, TgTxOption tx) throws IOException, InterruptedException { - var session = getSession(); - var tm = session.createTransactionManager(tx); - try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING)) { - tm.execute(transaction -> { - var list = new ArrayList(threadSize); - for (int i = 0; i < threadSize; i++) { - var thread = new InsertSingleTxThread(ps, i, threadSize, recordSize, transaction); - list.add(thread); - thread.start(); - } - for (var thread : list) { - try { - thread.join(); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } - } - }); - } - - var actual = selectCountFromTest(); - assertEquals(recordSize, actual); - } - - private static class InsertSingleTxThread extends Thread { - - private final TsurugiSqlPreparedStatement ps; - private final int number; - private final int threadSize; - private final int recordSize; - private final TsurugiTransaction transaction; - - public InsertSingleTxThread(TsurugiSqlPreparedStatement ps, int number, int threadSize, int recordSize, TsurugiTransaction transaction) { - this.ps = ps; - this.number = number; - this.threadSize = threadSize; - this.recordSize = recordSize; - this.transaction = transaction; - } - - @Override - public void run() { - try { - runInTransaction(); - } catch (IOException e) { - throw new UncheckedIOException(e); - } catch (InterruptedException | TsurugiTransactionException e) { - throw new RuntimeException(e); - } - } - - private void runInTransaction() throws IOException, InterruptedException, TsurugiTransactionException { - for (int i = 0; i < recordSize; i++) { - if (i % threadSize == number) { - var entity = createTestEntity(i); - transaction.executeAndGetCount(ps, entity); - } - } - } - } - - /** - * transaction per thread, parallel insert - */ - private void insertMultiTx(int recordSize, int threadSize, TgTxOption tx) throws IOException, InterruptedException { - var session = getSession(); - var tm = session.createTransactionManager(tx); - try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING)) { - var list = new ArrayList(threadSize); - for (int i = 0; i < threadSize; i++) { - var thread = new InsertMultiTxThread(ps, i, threadSize, recordSize, tm); - list.add(thread); - thread.start(); - } - for (var thread : list) { - try { - thread.join(); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } - } - } - - var actual = selectCountFromTest(); - assertEquals(recordSize, actual); - } - - private static class InsertMultiTxThread extends Thread { - - private final TsurugiSqlPreparedStatement ps; - private final int number; - private final int threadSize; - private final int recordSize; - private final TsurugiTransactionManager tm; - - public InsertMultiTxThread(TsurugiSqlPreparedStatement ps, int number, int threadSize, int recordSize, TsurugiTransactionManager tm) { - this.ps = ps; - this.number = number; - this.threadSize = threadSize; - this.recordSize = recordSize; - this.tm = tm; - } - - @Override - public void run() { - try { - tm.execute(transaction -> { - runInTransaction(transaction); - }); - } catch (IOException e) { - throw new UncheckedIOException(e); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } - } - - private void runInTransaction(TsurugiTransaction transaction) throws IOException, InterruptedException, TsurugiTransactionException { - for (int i = 0; i < recordSize; i++) { - if (i % threadSize == number) { - var entity = createTestEntity(i); - transaction.executeAndGetCount(ps, entity); - } - } - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertNoPkTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertNoPkTest.java deleted file mode 100755 index 1092e8e5..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertNoPkTest.java +++ /dev/null @@ -1,113 +0,0 @@ -package com.tsurugidb.iceaxe.test.insert; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import java.io.IOException; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; - -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.test.util.TestEntity; - -/** - * insert (table without primary key) test - */ -class DbInsertNoPkTest extends DbTestTableTester { - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTable(); - - logInitEnd(info); - } - - private static void createTable() throws IOException, InterruptedException { - // no primary key - var sql = "create table " + TEST // - + "(" // - + " foo int," // - + " bar bigint," // - + " zzz varchar(10)" // - + ")"; - executeDdl(getSession(), sql); - } - - @ParameterizedTest - @ValueSource(booleans = { true, false }) - void insertConstant(boolean columns) throws Exception { - new DbInsertTest().insertConstant(columns); - } - - @ParameterizedTest - @ValueSource(booleans = { true, false }) - void insertByBindVariables(boolean columns) throws Exception { - new DbInsertTest().insertByBindVariables(columns); - } - - @ParameterizedTest - @ValueSource(booleans = { true, false }) - void insertByBind(boolean columns) throws Exception { - new DbInsertTest().insertByBind(columns); - } - - @ParameterizedTest - @ValueSource(booleans = { true, false }) - void insertByEntityMapping(boolean columns) throws Exception { - new DbInsertTest().insertByEntityMapping(columns); - } - - @ParameterizedTest - @ValueSource(booleans = { true, false }) - void insertByEntityConverter(boolean columns) throws Exception { - new DbInsertTest().insertByEntityConverter(columns); - } - - @Test - void insertMany() throws Exception { - new DbInsertTest().insertMany(); - } - - @Test - void insertResultCheck() throws Exception { - new DbInsertTest().insertResultCheck(); - } - - @Test - void insertResultNoCheck() throws Exception { - new DbInsertTest().insertResultNoCheck(); - } - - @Test - void insertDuplicate() throws Exception { - var entity = new TestEntity(123, 456, "abc"); - int size = 4; - - var sql = "insert into " + TEST // - + " (" + TEST_COLUMNS + ")" // - + " values(" + entity.getFoo() + ", " + entity.getBar() + ", '" + entity.getZzz() + "')"; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(sql)) { - for (int i = 0; i < size; i++) { - tm.execute(transaction -> { - int count = transaction.executeAndGetCount(ps); - assertUpdateCount(1, count); - }); - } - } - - var actualList = selectAllFromTest(); - assertEquals(size, actualList.size()); - for (var actual : actualList) { - assertEquals(entity, actual); - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertTest.java deleted file mode 100755 index 6081c9ba..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertTest.java +++ /dev/null @@ -1,244 +0,0 @@ -package com.tsurugidb.iceaxe.test.insert; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; - -import java.io.IOException; -import java.util.List; -import java.util.function.Function; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; - -import com.tsurugidb.iceaxe.sql.parameter.TgBindParameters; -import com.tsurugidb.iceaxe.sql.parameter.TgBindVariable; -import com.tsurugidb.iceaxe.sql.parameter.TgBindVariables; -import com.tsurugidb.iceaxe.sql.parameter.TgParameterMapping; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.test.util.TestEntity; - -/** - * insert test - */ -class DbInsertTest extends DbTestTableTester { - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTestTable(); - - logInitEnd(info); - } - - @ParameterizedTest - @ValueSource(booleans = { true, false }) - void insertConstant(boolean columns) throws Exception { - var entity = new TestEntity(123, 456, "abc"); - - var sql = "insert into " + TEST // - + (columns ? " (" + TEST_COLUMNS + ")" : "") // - + " values(" + entity.getFoo() + ", " + entity.getBar() + ", '" + entity.getZzz() + "')"; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(sql)) { - int count = tm.executeAndGetCount(ps); - assertUpdateCount(1, count); - } - - assertEqualsTestTable(entity); - } - - @ParameterizedTest - @ValueSource(booleans = { true, false }) - void insertByBindVariables(boolean columns) throws Exception { - var entity = new TestEntity(123, 456, "abc"); - - var sql = "insert into " + TEST // - + (columns ? " (" + TEST_COLUMNS + ")" : "") // - + " values(:foo, :bar, :zzz)"; - var variables = TgBindVariables.of() // - .addInt("foo") // - .addLong("bar") // - .addString("zzz"); - var parameterMapping = TgParameterMapping.of(variables); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(sql, parameterMapping)) { - var parameter = TgBindParameters.of() // - .addInt("foo", entity.getFoo()) // - .addLong("bar", entity.getBar()) // - .addString("zzz", entity.getZzz()); - int count = tm.executeAndGetCount(ps, parameter); - assertUpdateCount(1, count); - } - - assertEqualsTestTable(entity); - } - - @ParameterizedTest - @ValueSource(booleans = { true, false }) - void insertByBind(boolean columns) throws Exception { - var entity = new TestEntity(123, 456, "abc"); - - var foo = TgBindVariable.ofInt("foo"); - var bar = TgBindVariable.ofLong("bar"); - var zzz = TgBindVariable.ofString("zzz"); - - var sql = "insert into " + TEST // - + (columns ? " (" + TEST_COLUMNS + ")" : "") // - + " values(" + foo + ", " + bar + ", " + zzz + ")"; - var parameterMapping = TgParameterMapping.of(foo, bar, zzz); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(sql, parameterMapping)) { - var parameter = TgBindParameters.of( // - foo.bind(entity.getFoo()), // - bar.bind(entity.getBar()), // - zzz.bind(entity.getZzz())); - int count = tm.executeAndGetCount(ps, parameter); - assertUpdateCount(1, count); - } - - assertEqualsTestTable(entity); - } - - @ParameterizedTest - @ValueSource(booleans = { true, false }) - void insertByEntityMapping(boolean columns) throws Exception { - var entity = new TestEntity(123, 456, "abc"); - - var sql = "insert into " + TEST // - + (columns ? " (" + TEST_COLUMNS + ")" : "") // - + " values(:foo, :bar, :zzz)"; - var parameterMapping = TgParameterMapping.of(TestEntity.class) // - .addInt("foo", TestEntity::getFoo) // - .addLong("bar", TestEntity::getBar) // - .addString("zzz", TestEntity::getZzz); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(sql, parameterMapping)) { - int count = tm.executeAndGetCount(ps, entity); - assertUpdateCount(1, count); - } - - assertEqualsTestTable(entity); - } - - @ParameterizedTest - @ValueSource(booleans = { true, false }) - void insertByEntityConverter(boolean columns) throws Exception { - var variables = TgBindVariables.of().addInt("foo").addLong("bar").addString("zzz"); - var sql = "insert into " + TEST // - + (columns ? " (" + TEST_COLUMNS + ")" : "") // - + " values(" + variables.getSqlNames() + ")"; - Function parameterConverter = entity -> TgBindParameters.of().add("foo", entity.getFoo()).add("bar", entity.getBar()).add("zzz", entity.getZzz()); - var parameterMapping = TgParameterMapping.of(variables, parameterConverter); - - var entity = new TestEntity(123, 456, "abc"); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(sql, parameterMapping)) { - int count = tm.executeAndGetCount(ps, entity); - assertUpdateCount(1, count); - } - - assertEqualsTestTable(entity); - } - - @Test - void insertMany() throws Exception { - var entityList = List.of( // - new TestEntity(123, 456, "abc"), // - new TestEntity(234, 789, "def")); - - var sql = "insert into " + TEST // - + "(" + TEST_COLUMNS + ")" // - + "values(:foo, :bar, :zzz)"; - var parameterMapping = TgParameterMapping.of(TestEntity.class) // - .addInt("foo", TestEntity::getFoo) // - .addLong("bar", TestEntity::getBar) // - .addString("zzz", TestEntity::getZzz); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(sql, parameterMapping)) { - tm.execute(transaction -> { - for (var entity : entityList) { - int count = transaction.executeAndGetCount(ps, entity); - assertUpdateCount(1, count); - } - }); - } - - assertEqualsTestTable(entityList); - } - - @Test - void insertResultCheck() throws Exception { - insertResultCheck(true); - } - - @Test - void insertResultNoCheck() throws Exception { - insertResultCheck(false); - } - - private void insertResultCheck(boolean resultCheck) throws IOException, InterruptedException { - int size = 100; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING)) { - tm.execute(transaction -> { - for (int i = 0; i < size; i++) { - var entity = createTestEntity(i); - - if (resultCheck) { - try (var result = ps.execute(transaction, entity)) { - var count = result.getUpdateCount(); - assertUpdateCount(1, count); - } - } else { - @SuppressWarnings("unused") - var result = ps.execute(transaction, entity); - // result.close is called on transaction.close - } - } - return; - }); - } - - assertEqualsTestTable(size); - } - - @Test - void insertPart() throws Exception { - int key = 1; - var foo = TgBindVariable.ofInt("foo"); - var sql = "insert into " + TEST + "(foo) values(" + foo + ")"; - var parameterMapping = TgParameterMapping.of(foo); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(sql, parameterMapping)) { - var parameter = TgBindParameters.of(foo.bind(key)); - int count = tm.executeAndGetCount(ps, parameter); - assertUpdateCount(1, count); - } - - var actual = selectFromTest(key); - assertEquals(key, actual.getFoo()); - assertNull(actual.getBar()); - assertNull(actual.getZzz()); - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertVarcharTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertVarcharTest.java deleted file mode 100755 index 90d6f3fa..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/insert/DbInsertVarcharTest.java +++ /dev/null @@ -1,45 +0,0 @@ -package com.tsurugidb.iceaxe.test.insert; - -import java.util.function.Function; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; - -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; - -/** - * insert varchar test - */ -class DbInsertVarcharTest extends DbTestTableTester { - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTestTable(); - - logInitEnd(info); - } - - @Test - void insertNull() throws Exception { - new DbInsertCharTest().insertNull(); - } - - @Test - void insertOK() throws Exception { - new DbInsertCharTest().insertOK(Function.identity()); - } - - @Test - void insertError() throws Exception { - new DbInsertCharTest().insertError(); - } - - @Test - void insertNulChar() throws Exception { - new DbInsertCharTest().insertNulChar(Function.identity()); - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbQueryResultTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbQueryResultTest.java deleted file mode 100755 index 3d005f6c..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbQueryResultTest.java +++ /dev/null @@ -1,147 +0,0 @@ -package com.tsurugidb.iceaxe.test.select; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.util.Iterator; -import java.util.List; -import java.util.Optional; - -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.slf4j.LoggerFactory; - -import com.tsurugidb.iceaxe.sql.result.TsurugiQueryResult; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.test.util.TestEntity; - -/** - * {@link TsurugiQueryResult} test - */ -class DbQueryResultTest extends DbTestTableTester { - - private static final int SIZE = 4; - - @BeforeAll - static void beforeAll(TestInfo info) throws Exception { - var LOG = LoggerFactory.getLogger(DbQueryResultTest.class); - logInitStart(LOG, info); - - dropTestTable(); - createTestTable(); - insertTestTable(SIZE); - - logInitEnd(LOG, info); - } - - @Test - void whileEach() throws Exception { - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(SELECT_SQL, SELECT_MAPPING)) { - tm.execute(transaction -> { - try (var result = transaction.executeQuery(ps)) { - assertEquals(Optional.empty(), result.getHasNextRow()); - - int[] count = { 0 }; - result.whileEach(entity -> { - var expected = createTestEntity(count[0]++); - assertEquals(expected, entity); - }); - assertEquals(SIZE, count[0]); - - assertEquals(Optional.of(false), result.getHasNextRow()); - assertEquals(SIZE, result.getReadCount()); - } - }); - } - } - - @Test - void getRecordList() throws Exception { - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(SELECT_SQL, SELECT_MAPPING)) { - tm.execute(transaction -> { - try (var result = transaction.executeQuery(ps)) { - assertEquals(Optional.empty(), result.getHasNextRow()); - - List list = result.getRecordList(); - assertEqualsTestTable(SIZE, list); - - assertEquals(Optional.of(false), result.getHasNextRow()); - assertEquals(SIZE, result.getReadCount()); - } - }); - } - } - - @Test - void findRecord() throws Exception { - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(SELECT_SQL + " order by foo", SELECT_MAPPING)) { - tm.execute(transaction -> { - try (var result = transaction.executeQuery(ps)) { - assertEquals(Optional.empty(), result.getHasNextRow()); - - Optional recordOpt = result.findRecord(); - assertTrue(recordOpt.isPresent()); - var expected = createTestEntity(0); - assertEquals(expected, recordOpt.get()); - - assertEquals(Optional.of(true), result.getHasNextRow()); - assertEquals(1, result.getReadCount()); - } - }); - } - } - - @Test - void iterator() throws Exception { - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(SELECT_SQL, SELECT_MAPPING)) { - tm.execute(transaction -> { - try (var result = transaction.executeQuery(ps)) { - assertEquals(Optional.empty(), result.getHasNextRow()); - - int[] count = { 0 }; - for (Iterator i = result.iterator(); i.hasNext();) { - var entity = i.next(); - var expected = createTestEntity(count[0]++); - assertEquals(expected, entity); - } - assertEquals(SIZE, count[0]); - - assertEquals(Optional.of(false), result.getHasNextRow()); - assertEquals(SIZE, result.getReadCount()); - } - }); - } - } - - @Test - void forEach() throws Exception { - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(SELECT_SQL, SELECT_MAPPING)) { - tm.execute(transaction -> { - try (var result = transaction.executeQuery(ps)) { - assertEquals(Optional.empty(), result.getHasNextRow()); - - int[] count = { 0 }; - result.forEach(entity -> { - var expected = createTestEntity(count[0]++); - assertEquals(expected, entity); - }); - assertEquals(SIZE, count[0]); - - assertEquals(Optional.of(false), result.getHasNextRow()); - assertEquals(SIZE, result.getReadCount()); - } - }); - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbResultRecordTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbResultRecordTest.java deleted file mode 100755 index 75ebbda8..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbResultRecordTest.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.tsurugidb.iceaxe.test.select; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrowsExactly; - -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.slf4j.LoggerFactory; - -import com.tsurugidb.iceaxe.sql.result.TgResultMapping; -import com.tsurugidb.iceaxe.sql.result.TsurugiResultRecord; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; - -/** - * {@link TsurugiResultRecord} test - */ -class DbResultRecordTest extends DbTestTableTester { - - private static final int SIZE = 4; - - @BeforeAll - static void beforeAll(TestInfo info) throws Exception { - var LOG = LoggerFactory.getLogger(DbResultRecordTest.class); - logInitStart(LOG, info); - - dropTestTable(); - createTestTable(); - insertTestTable(SIZE); - - logInitEnd(LOG, info); - } - - @Test - void getInt_null() throws Exception { - var sql = "select max(foo) as foo from " + TEST + " where foo < 0"; - var resultMapping = TgResultMapping.of(record -> record.getInt("foo")); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql, resultMapping)) { - var e = assertThrowsExactly(NullPointerException.class, () -> { - tm.execute(transaction -> { - transaction.executeAndFindRecord(ps); - }); - }); - assertEquals("TsurugiResultRecord.getInt(foo) is null", e.getMessage()); - } - } - - @Test - void nextInt_null() throws Exception { - var sql = "select max(foo) from " + TEST + " where foo < 0"; - var resultMapping = TgResultMapping.of(record -> record.nextInt()); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql, resultMapping)) { - var e = assertThrowsExactly(NullPointerException.class, () -> { - tm.execute(transaction -> { - transaction.executeAndFindRecord(ps); - }); - }); - assertEquals("TsurugiResultRecord.nextInt(0) is null", e.getMessage()); - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelect2LoopTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelect2LoopTest.java deleted file mode 100755 index 1fc83a8d..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelect2LoopTest.java +++ /dev/null @@ -1,315 +0,0 @@ -package com.tsurugidb.iceaxe.test.select; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.concurrent.Callable; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicInteger; - -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.RepeatedTest; -import org.junit.jupiter.api.TestInfo; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.tsurugidb.iceaxe.exception.TsurugiExceptionUtil; -import com.tsurugidb.iceaxe.session.TsurugiSession; -import com.tsurugidb.iceaxe.sql.TsurugiSqlPreparedQuery; -import com.tsurugidb.iceaxe.sql.parameter.TgBindParameters; -import com.tsurugidb.iceaxe.sql.parameter.TgBindVariable; -import com.tsurugidb.iceaxe.sql.parameter.TgBindVariable.TgBindVariableInteger; -import com.tsurugidb.iceaxe.sql.parameter.TgParameterMapping; -import com.tsurugidb.iceaxe.sql.result.TgResultMapping; -import com.tsurugidb.iceaxe.sql.result.TsurugiResultEntity; -import com.tsurugidb.iceaxe.sql.result.TsurugiStatementResult; -import com.tsurugidb.iceaxe.test.util.DbTestConnector; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.transaction.TsurugiTransaction; -import com.tsurugidb.iceaxe.transaction.exception.TsurugiTransactionException; -import com.tsurugidb.iceaxe.transaction.manager.TgTmSetting; -import com.tsurugidb.iceaxe.transaction.manager.event.TsurugiTmEventListener; -import com.tsurugidb.iceaxe.transaction.manager.option.TgTmTxOption; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; - -/** - * select 2-loop test - */ -class DbSelect2LoopTest extends DbTestTableTester { - private static final Logger LOG = LoggerFactory.getLogger(DbSelect2LoopTest.class); - - private static final int SIZE = 100; - private static final String TEST2 = "test2"; - - private static final int OCC_THREAD_SIZE = 8; - private static final int LTX_THREAD_SIZE = 2; - - private static final int LTX_ATTEMPT_SIZE = 5; - - @BeforeAll - static void beforeAll(TestInfo info) throws Exception { - var LOG = LoggerFactory.getLogger(DbSelect2LoopTest.class); - logInitStart(LOG, info); - - dropTestTable(); - createTestTable(); - insertTestTable(SIZE); - - dropTable(TEST2); - createTest2Table(); - - logInitEnd(LOG, info); - } - - private static void createTest2Table() throws IOException, InterruptedException { - var sql = "create table " + TEST2 + "(key1 int , key2 int, foo int, primary key(key1, key2))"; - executeDdl(getSession(), sql); - } - - private static class Test2Entity { - private int key1; - private int key2; - private int foo; - - public Test2Entity() { - } - - public Test2Entity(int key1, int key2, int foo) { - this.key1 = key1; - this.key2 = key2; - this.foo = foo; - } - - public int getKey1() { - return key1; - } - - public void setKey1(int key1) { - this.key1 = key1; - } - - public int getKey2() { - return key2; - } - - public void setKey2(int key2) { - this.key2 = key2; - } - - public int getFoo() { - return foo; - } - - public void setFoo(int foo) { - this.foo = foo; - } - } - - @RepeatedTest(4) - void test() throws Throwable { - test(false); - } - - @RepeatedTest(4) - void testTm() throws Throwable { - test(true); - } - - private void test(boolean useTm) throws Throwable { - var service = Executors.newFixedThreadPool(OCC_THREAD_SIZE + LTX_THREAD_SIZE); - var sessionList = new ArrayList(OCC_THREAD_SIZE + LTX_THREAD_SIZE); - var futureList = new ArrayList>(OCC_THREAD_SIZE + LTX_THREAD_SIZE); - Throwable re = null; - try { - var ltxThreadCounter = new AtomicInteger(LTX_THREAD_SIZE); - for (int i = 0; i < LTX_THREAD_SIZE; i++) { - var session = DbTestConnector.createSession(); - sessionList.add(session); - var future = service.submit(new LtxThread(session, i, ltxThreadCounter)); - futureList.add(future); - } - for (int i = 0; i < OCC_THREAD_SIZE; i++) { - var session = DbTestConnector.createSession(); - sessionList.add(session); - var future = service.submit(new OccThread(session, 0, ltxThreadCounter, useTm)); - futureList.add(future); - } - } catch (Throwable e) { - re = e; - throw e; - } finally { - for (var future : futureList) { - try { - future.get(); - // test success if no error - } catch (Exception e) { - if (re == null) { - re = e; - } else { - re.addSuppressed(e); - } - } - } - for (var session : sessionList) { - try { - session.close(); - } catch (Exception e) { - if (re == null) { - re = e; - } else { - re.addSuppressed(e); - } - } - } - if (re != null) { - throw re; - } - } - } - - private static class LtxThread implements Callable { - private final TsurugiSession session; - private final int key1; - private final AtomicInteger threadCounter; - - public LtxThread(TsurugiSession session, int key1, AtomicInteger threadCounter) { - this.session = session; - this.key1 = key1; - this.threadCounter = threadCounter; - } - - @Override - public Void call() throws Exception { - try { - execute(); - } finally { - threadCounter.decrementAndGet(); - } - return null; - } - - private void execute() throws Exception { - var key = TgBindVariable.ofInt("key1"); - var deleteSql = "delete from " + TEST2 + " where key1=" + key; - var deleteMapping = TgParameterMapping.of(key); - var insertSql = "insert into " + TEST2 + "(key1, key2, foo) values(:key1, :key2, :foo)"; - var insertMapping = TgParameterMapping.of(Test2Entity.class) // - .addInt("key1", Test2Entity::getKey1) // - .addInt("key2", Test2Entity::getKey2) // - .addInt("foo", Test2Entity::getFoo); - try (var deletePs = session.createStatement(deleteSql, deleteMapping); // - var insertPs = session.createStatement(insertSql, insertMapping)) { - var setting = TgTmSetting.ofAlways(TgTxOption.ofLTX(TEST2)); - var tm = session.createTransactionManager(setting); - - for (int n = 0; n < LTX_ATTEMPT_SIZE; n++) { - tm.execute(transaction -> { - var parameter = TgBindParameters.of(key.bind(this.key1)); - transaction.executeAndGetCount(deletePs, parameter); - - var rcList = new ArrayList(); - for (int i = 0; i < 100; i++) { - var test2 = new Test2Entity(this.key1, i, i); - var rc = transaction.executeStatement(insertPs, test2); - rcList.add(rc); - } - for (var rc : rcList) { - rc.close(); - } - }); - - TimeUnit.MILLISECONDS.sleep(100); - } - } - } - } - - private static class OccThread implements Callable { - private final TsurugiSession session; - private final int key1; - private final AtomicInteger threadCounter; - private final boolean useTm; - - public OccThread(TsurugiSession session, int key1, AtomicInteger threadCounter, boolean useTm) { - this.session = session; - this.key1 = key1; - this.threadCounter = threadCounter; - this.useTm = useTm; - } - - @Override - public Void call() throws Exception { - execute(); - return null; - } - - private void execute() throws Exception { - var key = TgBindVariable.ofInt("key1"); - var selectSql = "select * from " + TEST2 + " where key1=" + key; - var selectMapping = TgParameterMapping.of(key); - var selectResultMapping = TgResultMapping.of(Test2Entity::new) // - .addInt("key1", Test2Entity::setKey1) // - .addInt("key2", Test2Entity::setKey2) // - .addInt("foo", Test2Entity::setFoo); - var foo = TgBindVariable.ofInt("foo"); - var select1Sql = "select * from " + TEST + " where foo=" + foo; - var select1Mapping = TgParameterMapping.of(foo); - try (var selectPs = session.createQuery(selectSql, selectMapping, selectResultMapping); // - var select1Ps = session.createQuery(select1Sql, select1Mapping)) { - while (threadCounter.get() > 0) { - if (useTm) { - execute1Tm(key, foo, selectPs, select1Ps); - } else { - execute1(key, foo, selectPs, select1Ps); - } - } - } - } - - private void execute1(TgBindVariableInteger key, TgBindVariableInteger foo, TsurugiSqlPreparedQuery selectPs, - TsurugiSqlPreparedQuery select1Ps) throws IOException, InterruptedException, TsurugiTransactionException { - try (var transaction = session.createTransaction(TgTxOption.ofOCC())) { - var parameter = TgBindParameters.of(key.bind(this.key1)); - try { - transaction.executeAndForEach(selectPs, parameter, entity -> { - var parameter1 = TgBindParameters.of(foo.bind(entity.getFoo())); - transaction.executeAndGetList(select1Ps, parameter1); - }); - } catch (TsurugiTransactionException e) { - var exceptionUtil = TsurugiExceptionUtil.getInstance(); - if (exceptionUtil.isSerializationFailure(e)) { - return; - } - if (exceptionUtil.isInactiveTransaction(e)) { - var status = transaction.getTransactionStatus(); - if (exceptionUtil.isSerializationFailure(status.getTransactionException())) { - LOG.debug("ERR_INACTIVE_TRANSACTION with ERR_SERIALIZATION_FAILURE"); - return; - } - } - throw e; - } - } - } - - private void execute1Tm(TgBindVariableInteger key, TgBindVariableInteger foo, TsurugiSqlPreparedQuery selectPs, - TsurugiSqlPreparedQuery select1Ps) throws IOException, InterruptedException { - var setting = TgTmSetting.ofAlways(TgTxOption.ofOCC()); - var tm = session.createTransactionManager(setting); - tm.addEventListener(new TsurugiTmEventListener() { - @Override - public void transactionRetry(TsurugiTransaction transaction, Exception cause, TgTmTxOption nextTmOption) { - LOG.debug("tm.retry {}", nextTmOption.getRetryInstruction()); - } - }); - tm.execute(transaction -> { - var parameter = TgBindParameters.of(key.bind(this.key1)); - transaction.executeAndForEach(selectPs, parameter, entity -> { - var parameter1 = TgBindParameters.of(foo.bind(entity.getFoo())); - transaction.executeAndGetList(select1Ps, parameter1); - }); - }); - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectAggregateTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectAggregateTest.java deleted file mode 100755 index 7fd683f9..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectAggregateTest.java +++ /dev/null @@ -1,206 +0,0 @@ -package com.tsurugidb.iceaxe.test.select; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrowsExactly; - -import java.util.stream.LongStream; - -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; -import org.slf4j.LoggerFactory; - -import com.tsurugidb.iceaxe.exception.TsurugiIOException; -import com.tsurugidb.iceaxe.sql.parameter.TgBindParameters; -import com.tsurugidb.iceaxe.sql.parameter.TgParameterMapping; -import com.tsurugidb.iceaxe.sql.result.TgResultMapping; -import com.tsurugidb.iceaxe.sql.result.TsurugiResultEntity; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.transaction.manager.exception.TsurugiTmIOException; -import com.tsurugidb.tsubakuro.sql.SqlServiceCode; - -/** - * select aggregate test - */ -class DbSelectAggregateTest extends DbTestTableTester { - - private static final int SIZE = 4; - - @BeforeAll - static void beforeAll(TestInfo info) throws Exception { - var LOG = LoggerFactory.getLogger(DbSelectAggregateTest.class); - logInitStart(LOG, info); - - dropTestTable(); - createTestTable(); - insertTestTable(SIZE); - - logInitEnd(LOG, info); - } - - @ParameterizedTest - @ValueSource(strings = { "", " order by count(*)" }) - void selectCount(String order) throws Exception { - var sql = "select count(*) from " + TEST + order; - var resultMapping = TgResultMapping.of(record -> record.nextInt()); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql, resultMapping)) { - if (order.contains("order by")) { // TODO 集約のorder by実装待ち - var e = assertThrowsExactly(TsurugiTmIOException.class, () -> tm.executeAndFindRecord(ps)); - assertEqualsCode(SqlServiceCode.UNSUPPORTED_COMPILER_FEATURE_EXCEPTION, e); - return; - } - int count = tm.executeAndFindRecord(ps).get(); - assertEquals(SIZE, count); - } - } - - @ParameterizedTest - @ValueSource(strings = { "", " order by sum(bar)" }) - void selectSum(String order) throws Exception { - var sql = "select sum(bar) as sum, min(zzz) as zzz from " + TEST + order; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql)) { - if (order.contains("order by")) { // TODO 集約のorder by実装待ち - var e = assertThrowsExactly(TsurugiTmIOException.class, () -> tm.executeAndFindRecord(ps)); - assertEqualsCode(SqlServiceCode.UNSUPPORTED_COMPILER_FEATURE_EXCEPTION, e); - return; - } - TsurugiResultEntity entity = tm.executeAndFindRecord(ps).get(); - assertEquals(LongStream.range(0, SIZE).sum(), entity.getLongOrNull("sum")); - assertEquals("0", entity.getStringOrNull("zzz")); - } - } - - @ParameterizedTest - @ValueSource(strings = { "", " order by foo" }) - void selectKeyCount(String order) throws Exception { - var sql = "select foo, count(*) as cnt from " + TEST + " group by foo" + order; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql)) { - if (order.contains("order by")) { // TODO 集約のorder by実装待ち - var e = assertThrowsExactly(TsurugiTmIOException.class, () -> tm.executeAndGetList(ps)); - assertEqualsCode(SqlServiceCode.UNSUPPORTED_COMPILER_FEATURE_EXCEPTION, e); - return; - } - var list = tm.executeAndGetList(ps); - assertEquals(SIZE, list.size()); - - int i = 0; - for (var actual : list) { - if (!order.isEmpty()) { - assertEquals(i, actual.getInt("foo")); - } - assertEquals(1, actual.getInt("cnt")); - i++; - } - } - } - - @ParameterizedTest - @ValueSource(strings = { "", " order by foo" }) - void selectKeySum(String order) throws Exception { - var sql = "select foo, sum(bar) as sum, min(zzz) as zzz from " + TEST + " group by foo" + order; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql)) { - if (order.contains("order by")) { // TODO 集約のorder by実装待ち - var e = assertThrowsExactly(TsurugiTmIOException.class, () -> tm.executeAndGetList(ps)); - assertEqualsCode(SqlServiceCode.UNSUPPORTED_COMPILER_FEATURE_EXCEPTION, e); - return; - } - var list = tm.executeAndGetList(ps); - assertEquals(SIZE, list.size()); - - int i = 0; - for (var actual : list) { - int foo = actual.getInt("foo"); - if (!order.isEmpty()) { - assertEquals(i, foo); - } - assertEquals(Long.valueOf(foo), actual.getLong("sum")); - assertEquals(Integer.toString(foo), actual.getString("zzz")); - i++; - } - } - } - - @Test - void selectCountWhereEmpty() throws Exception { - new DbSelectEmptyTest().selectCount(" where foo<0"); - } - - @Test - void selectSumWhereEmpty() throws Exception { - new DbSelectEmptyTest().selectSum(" where foo<0"); - } - - @Test - void selectKeyCountWhereEmpty() throws Exception { - new DbSelectEmptyTest().selectKeyCount(" where foo<0"); - } - - @Test - void selectKeySumWhereEmpty() throws Exception { - new DbSelectEmptyTest().selectKeySum(" where foo<0"); - } - - @ParameterizedTest - @ValueSource(strings = { "foo,foo", "foo,foo,foo" }) - @Disabled // TODO remove Disabled. group byに同カラムが複数あるとtateyama-serverがクラッシュする - void selectGroupBySameKey(String groupKey) throws Exception { - var sql = "select foo, count(*) as cnt from " + TEST + " group by " + groupKey; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql)) { - var list = tm.executeAndGetList(ps); - assertEquals(SIZE, list.size()); - - for (var actual : list) { - assertEquals(1, actual.getInt("cnt")); - } - } - } - - @ParameterizedTest - @ValueSource(strings = { "foo", "foo,foo" }) - void selectKeyWithoutGroup(String key) throws Exception { - var sql = "select " + key + ", count(*) from " + TEST; // without 'group by' - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql, TgParameterMapping.of())) { - var e = assertThrowsExactly(TsurugiIOException.class, () -> { - tm.executeAndGetList(ps, TgBindParameters.of()); - }); - assertEqualsCode(SqlServiceCode.COMPILE_EXCEPTION, e); - assertContains("compile failed with error:invalid_aggregation_column message:\"target column must be aggregated\" location:(unknown)", e.getMessage()); // TODO エラー情報詳細 - } - } - - @Test - void errorGroupNameNotFound() throws Exception { - var sql = "select foo as k, count(*) as cnt from " + TEST + " group by k"; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql)) { - var e = assertThrowsExactly(TsurugiTmIOException.class, () -> { - tm.executeAndGetList(ps); - }); - assertEqualsCode(SqlServiceCode.SYMBOL_ANALYZE_EXCEPTION, e); - assertContains("compile failed with error:variable_not_found message:\"k\" location:(unknown))", e.getMessage()); - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectAliasTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectAliasTest.java deleted file mode 100755 index 6c1081bf..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectAliasTest.java +++ /dev/null @@ -1,117 +0,0 @@ -package com.tsurugidb.iceaxe.test.select; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrowsExactly; - -import java.util.List; - -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; -import org.slf4j.LoggerFactory; - -import com.tsurugidb.iceaxe.sql.result.TgResultMapping; -import com.tsurugidb.iceaxe.sql.result.TsurugiResultEntity; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.transaction.manager.exception.TsurugiTmIOException; -import com.tsurugidb.tsubakuro.sql.SqlServiceCode; - -/** - * select alias test - */ -class DbSelectAliasTest extends DbTestTableTester { - - private static final int SIZE = 4; - - @BeforeAll - static void beforeAll(TestInfo info) throws Exception { - var LOG = LoggerFactory.getLogger(DbSelectAliasTest.class); - logInitStart(LOG, info); - - dropTestTable(); - createTestTable(); - insertTestTable(SIZE); - - logInitEnd(LOG, info); - } - - @ParameterizedTest - @ValueSource(strings = { " as", "" }) - void selectTableAlias(String as) throws Exception { - var sql = "select t.foo, t.bar, t.zzz from " + TEST + as + " t order by t.foo"; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql, SELECT_MAPPING)) { - var list = tm.executeAndGetList(ps); - assertEquals(SIZE, list.size()); - for (int i = 0; i < SIZE; i++) { - var expected = createTestEntity(i); - var actual = list.get(i); - assertEquals(expected, actual); - } - } - } - - @Test - void selectAsName() throws Exception { - var sql = "select count(*) as count from " + TEST; - selectCount(sql, "count"); - } - - @Test - void selectName() throws Exception { - var sql = "select count(*) cnt from " + TEST; - var e = assertThrowsExactly(TsurugiTmIOException.class, () -> { - selectCount(sql, "cnt"); - }); - assertEqualsCode(SqlServiceCode.SYNTAX_EXCEPTION, e); // TODO as無し別名実装待ち - } - - @Test - void selectNoName() throws Exception { - var sql = "select count(*) from " + TEST; - selectCount(sql, "@#0"); - } - - private void selectCount(String sql, String name) throws Exception { - var resultMapping = TgResultMapping.of(record -> { - List nameList = record.getNameList(); - assertEquals(List.of(name), nameList); - return record.getInt(name); - }); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql, resultMapping)) { - int count = tm.executeAndFindRecord(ps).get(); - assertEquals(SIZE, count); - } - } - - @Test - void selectNoName2() throws Exception { - var sql = "select count(*), count(foo) from " + TEST; - var resultMapping = TgResultMapping.of(record -> { - List nameList = record.getNameList(); - assertEquals(List.of("@#0", "@#1"), nameList); - return TsurugiResultEntity.of(record); - }); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql, resultMapping)) { - tm.execute(transaction -> { - try (var result = ps.execute(transaction)) { - List nameList = result.getNameList(); - assertEquals(List.of("@#0", "@#1"), nameList); - TsurugiResultEntity entity = result.findRecord().get(); - assertEquals(SIZE, entity.getInt("@#0")); - assertEquals(SIZE, entity.getInt("@#1")); - } - }); - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectAllTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectAllTest.java deleted file mode 100755 index 682c5069..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectAllTest.java +++ /dev/null @@ -1,123 +0,0 @@ -package com.tsurugidb.iceaxe.test.select; - -import static org.junit.jupiter.api.Assertions.fail; - -import java.text.MessageFormat; -import java.util.concurrent.atomic.AtomicInteger; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; - -import com.tsurugidb.iceaxe.test.util.DbTestConnector; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.transaction.function.TsurugiTransactionAction; -import com.tsurugidb.iceaxe.transaction.manager.TgTmSetting; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; - -/** - * select all test - */ -class DbSelectAllTest extends DbTestTableTester { - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTestTable(); - - logInitEnd(info); - } - - @Test - @Disabled // TODO remove Disabled. countがinsert/commit件数と一致しないことがある - void selectWhileInsertOcc() throws Exception { - selectWhileInsert(TgTxOption.ofOCC()); - } - - @Test - void selectWhileInserLtx() throws Exception { - selectWhileInsert(TgTxOption.ofLTX()); - } - - @Test - void selectWhileInserRtx() throws Exception { - selectWhileInsert(TgTxOption.ofRTX()); - } - - private static final int COMMIT_SIZE = 100; - - private void selectWhileInsert(TgTxOption txOption) throws Exception { - var session = getSession(); - var tm = session.createTransactionManager(TgTmSetting.ofAlways(txOption)); - try (var ps = session.createQuery(SELECT_SQL, SELECT_MAPPING)) { - var insertThread = new InsertThread(); - insertThread.start(); - - Throwable occurred = null; - try { - while (!insertThread.end) { - tm.execute(transaction -> { - var list = transaction.executeAndGetList(ps); - int count = list.size(); - if (count % COMMIT_SIZE != 0) { - fail(MessageFormat.format("count={0} (COMMIT_SIZE={1})", count, COMMIT_SIZE)); - } - }); - } - } catch (Throwable e) { - occurred = e; - throw e; - } finally { - insertThread.join(); - - var e = insertThread.exception; - if (e != null) { - if (occurred != null) { - occurred.addSuppressed(e); - } else { - if (e instanceof Exception) { - throw (Exception) e; - } - throw new Exception(e); - } - } - } - } - } - - private static class InsertThread extends Thread { - private volatile boolean end = false; - private Throwable exception; - - @Override - public void run() { - try { - execute(); - } catch (Throwable e) { - this.exception = e; - } finally { - this.end = true; - } - } - - private void execute() throws Exception { - try (var session = DbTestConnector.createSession()) { - var tm = session.createTransactionManager(TgTxOption.ofOCC()); - try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING)) { - var foo = new AtomicInteger(0); - for (int i = 0; i < 20; i++) { - tm.execute((TsurugiTransactionAction) transaction -> { - for (int j = 0; j < COMMIT_SIZE; j++) { - var entity = createTestEntity(foo.getAndIncrement()); - transaction.executeAndGetCount(ps, entity); - } - }); - } - } - } - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectBoundaryValueTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectBoundaryValueTest.java deleted file mode 100755 index 7d88e933..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectBoundaryValueTest.java +++ /dev/null @@ -1,212 +0,0 @@ -package com.tsurugidb.iceaxe.test.select; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import java.io.IOException; -import java.util.List; -import java.util.Objects; -import java.util.function.Predicate; -import java.util.stream.Collectors; - -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.TestInfo; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; -import org.slf4j.LoggerFactory; - -import com.tsurugidb.iceaxe.sql.parameter.TgBindParameters; -import com.tsurugidb.iceaxe.sql.parameter.TgBindVariable; -import com.tsurugidb.iceaxe.sql.parameter.TgParameterMapping; -import com.tsurugidb.iceaxe.sql.result.TgResultMapping; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; - -/** - * select boundary value test - */ -class DbSelectBoundaryValueTest extends DbTestTableTester { - - @BeforeAll - static void beforeAll(TestInfo info) throws Exception { - var LOG = LoggerFactory.getLogger(DbSelectBoundaryValueTest.class); - logInitStart(LOG, info); - - dropTestTable(); - createTable(); - insertTable(); - - logInitEnd(LOG, info); - } - - private static void createTable() throws IOException, InterruptedException { - var sql = "create table " + TEST // - + "(" // - + " int4 int," // - + " int8 bigint," // - + " float4 float," // - + " float8 double" // - + ")"; - executeDdl(getSession(), sql); - } - - private static class TestEntity { - private int int4; - private long int8; - private float float4; - private double float8; - - public TestEntity() { - } - - public TestEntity(int int4, long int8, float float4, double float8) { - this.int4 = int4; - this.int8 = int8; - this.float4 = float4; - this.float8 = float8; - } - - public void setInt4(int int4) { - this.int4 = int4; - } - - public int getInt4() { - return this.int4; - } - - public void setInt8(long int8) { - this.int8 = int8; - } - - public long getInt8() { - return this.int8; - } - - public void setFloat4(float float4) { - this.float4 = float4; - } - - public float getFloat4() { - return this.float4; - } - - public void setFloat8(double float8) { - this.float8 = float8; - } - - public double getFloat8() { - return this.float8; - } - - @Override - public int hashCode() { - return Objects.hash(int4, int8, float4, float8); - } - - @Override - public boolean equals(Object obj) { - TestEntity other = (TestEntity) obj; - return int4 == other.int4 && int8 == other.int8 && Float.floatToIntBits(float4) == Float.floatToIntBits(other.float4) - && Double.doubleToLongBits(float8) == Double.doubleToLongBits(other.float8); - } - } - - private static final List TEST_ENTITY_LIST = List.of(// - new TestEntity(Integer.MIN_VALUE, Long.MIN_VALUE, Float.MIN_VALUE, Double.MIN_VALUE), // - new TestEntity(Integer.MIN_VALUE + 1, Long.MIN_VALUE + 1, Float.MIN_VALUE + Float.MIN_NORMAL, Double.MIN_VALUE + Double.MIN_NORMAL), // - new TestEntity(Integer.MIN_VALUE + 2, Long.MIN_VALUE + 2, Float.MIN_VALUE + Float.MIN_NORMAL * 2, Double.MIN_VALUE + Double.MIN_NORMAL * 2), // - new TestEntity(Integer.MAX_VALUE - 2, Long.MAX_VALUE - 2, Float.MAX_VALUE - Float.MIN_NORMAL * 2, Double.MAX_VALUE - Double.MIN_NORMAL * 2), // - new TestEntity(Integer.MAX_VALUE - 1, Long.MAX_VALUE - 1, Float.MAX_VALUE - Float.MIN_NORMAL, Double.MAX_VALUE - Double.MIN_NORMAL), // - new TestEntity(Integer.MAX_VALUE, Long.MAX_VALUE, Float.MAX_VALUE, Double.MAX_VALUE) // - ); - - private static void insertTable() throws IOException, InterruptedException { - var sql = "insert into " + TEST // - + "(int4, int8, float4, float8)" // - + "values(:int4, :int8, :float4, :float8)"; - var parameterMapping = TgParameterMapping.of(TestEntity.class) // - .addInt("int4", TestEntity::getInt4) // - .addLong("int8", TestEntity::getInt8) // - .addFloat("float4", TestEntity::getFloat4) // - .addDouble("float8", TestEntity::getFloat8); - - var session = getSession(); - var tm = createTransactionManagerOcc(session, 3); - try (var ps = session.createStatement(sql, parameterMapping)) { - tm.execute(transaction -> { - for (var entity : TEST_ENTITY_LIST) { - transaction.executeAndGetCount(ps, entity); - } - }); - } - } - - @ParameterizedTest - @ValueSource(ints = { Integer.MIN_VALUE, Integer.MIN_VALUE + 1, Integer.MAX_VALUE - 1, Integer.MAX_VALUE }) - void selectInt4(int value) throws Exception { - var variable = TgBindVariable.ofInt("value"); - test("int4 =:value", entity -> entity.getInt4() == value, value, variable); - test("int4<>:value", entity -> entity.getInt4() != value, value, variable); - test("int4>=:value", entity -> entity.getInt4() >= value, value, variable); - test("int4> :value", entity -> entity.getInt4() > value, value, variable); - test("int4<=:value", entity -> entity.getInt4() <= value, value, variable); - test("int4< :value", entity -> entity.getInt4() < value, value, variable); - } - - @ParameterizedTest - @ValueSource(longs = { Long.MIN_VALUE, Long.MIN_VALUE + 1, Long.MAX_VALUE - 1, Long.MAX_VALUE }) - void selectInt8(long value) throws Exception { - var variable = TgBindVariable.ofLong("value"); - test("int8 =:value", entity -> entity.getInt8() == value, value, variable); - test("int8<>:value", entity -> entity.getInt8() != value, value, variable); - test("int8>=:value", entity -> entity.getInt8() >= value, value, variable); - test("int8> :value", entity -> entity.getInt8() > value, value, variable); - test("int8<=:value", entity -> entity.getInt8() <= value, value, variable); - test("int8< :value", entity -> entity.getInt8() < value, value, variable); - } - - @ParameterizedTest - @ValueSource(floats = { Float.MIN_VALUE, Float.MIN_VALUE + Float.MIN_NORMAL, Float.MAX_VALUE - Float.MIN_NORMAL, Float.MAX_VALUE, Float.NEGATIVE_INFINITY, Float.POSITIVE_INFINITY, Float.NaN }) - void selectFloat4(float value) throws Exception { - var variable = TgBindVariable.ofFloat("value"); - test("float4 =:value", entity -> entity.getFloat4() == value, value, variable); - test("float4<>:value", entity -> entity.getFloat4() != value, value, variable); - test("float4>=:value", entity -> entity.getFloat4() >= value, value, variable); - test("float4> :value", entity -> entity.getFloat4() > value, value, variable); - test("float4<=:value", entity -> entity.getFloat4() <= value, value, variable); - test("float4< :value", entity -> entity.getFloat4() < value, value, variable); - } - - @ParameterizedTest - @ValueSource(doubles = { Double.MIN_VALUE, Double.MIN_VALUE + Double.MIN_NORMAL, Double.MIN_VALUE - Double.MIN_NORMAL, Double.MAX_VALUE, Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY, - Double.NaN }) - void selectFloat8(double value) throws Exception { - var variable = TgBindVariable.ofDouble("value"); - test("float8 =:value", entity -> entity.getFloat8() == value, value, variable); - test("float8<>:value", entity -> entity.getFloat8() != value, value, variable); - test("float8>=:value", entity -> entity.getFloat8() >= value, value, variable); - test("float8> :value", entity -> entity.getFloat8() > value, value, variable); - test("float8<=:value", entity -> entity.getFloat8() <= value, value, variable); - test("float8< :value", entity -> entity.getFloat8() < value, value, variable); - } - - private static void test(String where, Predicate expectedPredicate, T value, TgBindVariable variable) throws IOException, InterruptedException { - var sql = "select int4, int8, float4, float8 from " + TEST // - + " where " + where // - + " order by int4"; - var parameterMapping = TgParameterMapping.of(variable); - var resultMapping = TgResultMapping.of(TestEntity::new) // - .addInt("int4", TestEntity::setInt4) // - .addLong("int8", TestEntity::setInt8) // - .addFloat("float4", TestEntity::setFloat4) // - .addDouble("float8", TestEntity::setFloat8); - - var expected = TEST_ENTITY_LIST.stream().filter(expectedPredicate).collect(Collectors.toList()); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql, parameterMapping, resultMapping)) { - var parameter = TgBindParameters.of(variable.bind(value)); - var actual = tm.executeAndGetList(ps, parameter); - assertEquals(expected, actual); - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectCountTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectCountTest.java deleted file mode 100755 index 65fa7b99..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectCountTest.java +++ /dev/null @@ -1,162 +0,0 @@ -package com.tsurugidb.iceaxe.test.select; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; - -import java.io.IOException; -import java.text.MessageFormat; -import java.util.concurrent.atomic.AtomicInteger; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; - -import com.tsurugidb.iceaxe.sql.TgDataType; -import com.tsurugidb.iceaxe.sql.parameter.TgParameterMapping; -import com.tsurugidb.iceaxe.sql.result.TgResultMapping; -import com.tsurugidb.iceaxe.test.util.DbTestConnector; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.transaction.function.TsurugiTransactionAction; -import com.tsurugidb.iceaxe.transaction.manager.TgTmSetting; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; - -/** - * select count test - */ -class DbSelectCountTest extends DbTestTableTester { - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTestTable(); - - logInitEnd(info); - } - - @ParameterizedTest - @ValueSource(ints = { 0, 3 }) - void count_converter(int size) throws Exception { - count(size, TgResultMapping.of(record -> record.nextInt())); - } - - @ParameterizedTest - @ValueSource(ints = { 0, 3 }) - void count_singleColumn_class(int size) throws Exception { - count(size, TgResultMapping.ofSingle(int.class)); - } - - @ParameterizedTest - @ValueSource(ints = { 0, 3 }) - void count_singleColumn_type(int size) throws Exception { - count(size, TgResultMapping.ofSingle(TgDataType.INT)); - } - - private void count(int size, TgResultMapping resultMapping) throws IOException, InterruptedException { - insertTestTable(size); - - var sql = "select count(*) from " + TEST; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql, resultMapping)) { - int count = tm.executeAndFindRecord(ps).get(); - assertEquals(size, count); - } - } - - @Test - @Disabled // TODO remove Disabled. countがinsert/commit件数と一致しないことがある - void countWhileInsertOcc() throws Exception { - countWhileInsert(TgTxOption.ofOCC()); - } - - @Test - void countWhileInserLtx() throws Exception { - countWhileInsert(TgTxOption.ofLTX()); - } - - @Test - void countWhileInserRtx() throws Exception { - countWhileInsert(TgTxOption.ofRTX()); - } - - private static final int COMMIT_SIZE = 100; - - private void countWhileInsert(TgTxOption txOption) throws Exception { - var sql = "select count(*) from " + TEST; - - var session = getSession(); - var tm = session.createTransactionManager(TgTmSetting.ofAlways(txOption)); - try (var ps = session.createQuery(sql, TgParameterMapping.of(), TgResultMapping.ofSingle(int.class))) { - var insertThread = new InsertThread(); - insertThread.start(); - - Throwable occurred = null; - try { - while (!insertThread.end) { - tm.execute(transaction -> { - int count = transaction.executeAndFindRecord(ps, null).orElse(0); - if (count % COMMIT_SIZE != 0) { - fail(MessageFormat.format("count={0} (COMMIT_SIZE={1})", count, COMMIT_SIZE)); - } - }); - } - } catch (Throwable e) { - occurred = e; - throw e; - } finally { - insertThread.join(); - - var e = insertThread.exception; - if (e != null) { - if (occurred != null) { - occurred.addSuppressed(e); - } else { - if (e instanceof Exception) { - throw (Exception) e; - } - throw new Exception(e); - } - } - } - } - } - - private static class InsertThread extends Thread { - private volatile boolean end = false; - private Throwable exception; - - @Override - public void run() { - try { - execute(); - } catch (Throwable e) { - this.exception = e; - } finally { - this.end = true; - } - } - - private void execute() throws Exception { - try (var session = DbTestConnector.createSession()) { - var tm = session.createTransactionManager(TgTxOption.ofOCC()); - try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING)) { - var foo = new AtomicInteger(0); - for (int i = 0; i < 20; i++) { - tm.execute((TsurugiTransactionAction) transaction -> { - for (int j = 0; j < COMMIT_SIZE; j++) { - var entity = createTestEntity(foo.getAndIncrement()); - transaction.executeAndGetCount(ps, entity); - } - }); - } - } - } - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectDateTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectDateTest.java deleted file mode 100755 index 49881fca..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectDateTest.java +++ /dev/null @@ -1,95 +0,0 @@ -package com.tsurugidb.iceaxe.test.select; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import java.io.IOException; -import java.time.LocalDate; - -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.slf4j.LoggerFactory; - -import com.tsurugidb.iceaxe.sql.parameter.TgBindParameters; -import com.tsurugidb.iceaxe.sql.parameter.TgBindVariable; -import com.tsurugidb.iceaxe.sql.parameter.TgParameterMapping; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; - -/** - * select date test - */ -class DbSelectDateTest extends DbTestTableTester { - - @BeforeAll - static void beforeAll(TestInfo info) throws Exception { - var LOG = LoggerFactory.getLogger(DbSelectDateTest.class); - logInitStart(LOG, info); - - dropTestTable(); - createTable(); - insertTable(4); - - logInitEnd(LOG, info); - } - - private static void createTable() throws IOException, InterruptedException { - var sql = "create table " + TEST // - + "(" // - + " value date" // - + ")"; - executeDdl(getSession(), sql); - } - - private static void insertTable(int size) throws Exception { - var variable = TgBindVariable.ofDate("value"); - var sql = "insert into " + TEST + "(value) values (" + variable + ")"; - var mapping = TgParameterMapping.of(variable); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(sql, mapping)) { - tm.execute(transaction -> { - for (int i = 0; i < size; i++) { - var date = LocalDate.of(2022, 10, 1 + i); - var parameter = TgBindParameters.of(variable.bind(date)); - transaction.executeAndGetCount(ps, parameter); - } - return; - }); - } - } - - @Test - void whereEq() throws Exception { - var variable = TgBindVariable.ofDate("value"); - var sql = "select * from " + TEST + " where value=" + variable; - var mapping = TgParameterMapping.of(variable); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql, mapping)) { - var date = LocalDate.of(2022, 10, 2); - var parameter = TgBindParameters.of(variable.bind(date)); - var entity = tm.executeAndFindRecord(ps, parameter).get(); - assertEquals(date, entity.getDate("value")); - } - } - - @Test - void whereRange() throws Exception { - var start = TgBindVariable.ofDate("start"); - var end = TgBindVariable.ofDate("end"); - var sql = "select * from " + TEST + " where " + start + "<=value and value<=" + end + " order by value"; - var mapping = TgParameterMapping.of(start, end); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql, mapping)) { - var parameter = TgBindParameters.of(start.bind(LocalDate.of(2022, 10, 2)), end.bind(LocalDate.of(2022, 10, 3))); - var list = tm.executeAndGetList(ps, parameter); - assertEquals(2, list.size()); - assertEquals(LocalDate.of(2022, 10, 2), list.get(0).getDate("value")); - assertEquals(LocalDate.of(2022, 10, 3), list.get(1).getDate("value")); - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectEmptyTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectEmptyTest.java deleted file mode 100755 index bdffe3eb..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectEmptyTest.java +++ /dev/null @@ -1,137 +0,0 @@ -package com.tsurugidb.iceaxe.test.select; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; - -import java.io.IOException; -import java.util.List; - -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.slf4j.LoggerFactory; - -import com.tsurugidb.iceaxe.sql.result.TgResultMapping; -import com.tsurugidb.iceaxe.sql.result.TsurugiResultEntity; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.tsubakuro.channel.common.connection.wire.impl.ResponseBox; - -/** - * select empty-table test - */ -class DbSelectEmptyTest extends DbTestTableTester { - - private static final int ATTEMPT_SIZE = ResponseBox.responseBoxSize() + 100; - - @BeforeAll - static void beforeAll(TestInfo info) throws Exception { - var LOG = LoggerFactory.getLogger(DbSelectEmptyTest.class); - logInitStart(LOG, info); - - dropTestTable(); - createTestTable(); - - logInitEnd(LOG, info); - } - - @Test - void selectEmpty() throws Exception { - var sql = "select * from " + TEST; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql)) { - for (int i = 0; i < ATTEMPT_SIZE; i++) { - List list = tm.executeAndGetList(ps); - assertEquals(List.of(), list); - } - } - } - - @Test - void selectEmptySameTx() throws Exception { - var sql = "select * from " + TEST; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql)) { - tm.execute(transaction -> { - for (int i = 0; i < ATTEMPT_SIZE; i++) { - List list = transaction.executeAndGetList(ps); - assertEquals(List.of(), list); - } - return; - }); - } - } - - @Test - void selectCount() throws Exception { - selectCount(""); - } - - void selectCount(String where) throws IOException, InterruptedException { - var sql = "select count(*) from " + TEST + where; - var resultMapping = TgResultMapping.of(record -> record.nextInt()); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql, resultMapping)) { - int count = tm.executeAndFindRecord(ps).get(); - assertEquals(0, count); - } - } - - @Test - void selectSum() throws Exception { - selectSum(""); - } - - void selectSum(String where) throws IOException, InterruptedException { - var sql = "select sum(bar) as sum, min(zzz) as zzz from " + TEST + where; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql)) { - TsurugiResultEntity entity = tm.executeAndFindRecord(ps).get(); - assertNull(entity.getIntOrNull("sum")); - assertNull(entity.getStringOrNull("zzz")); - } - } - - @Test - void selectKeyCount() throws Exception { - selectKeyCount(""); - } - - void selectKeyCount(String where) throws IOException, InterruptedException { - var sql = "select foo, count(*) from " + TEST // - + where // - + " group by foo"; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql)) { - var list = tm.executeAndGetList(ps); - assertEquals(0, list.size()); - } - } - - @Test - void selectKeySum() throws Exception { - selectKeySum(""); - } - - void selectKeySum(String where) throws IOException, InterruptedException { - var sql = "select foo, sum(bar) as sum, min(zzz) as zzz from " + TEST // - + where // - + " group by foo"; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql)) { - var list = tm.executeAndGetList(ps); - assertEquals(0, list.size()); - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectErrorTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectErrorTest.java deleted file mode 100755 index 220952f5..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectErrorTest.java +++ /dev/null @@ -1,224 +0,0 @@ -package com.tsurugidb.iceaxe.test.select; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertThrowsExactly; - -import java.io.IOException; -import java.io.UncheckedIOException; -import java.util.concurrent.atomic.AtomicBoolean; - -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.RepeatedTest; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.slf4j.LoggerFactory; - -import com.tsurugidb.iceaxe.exception.IceaxeErrorCode; -import com.tsurugidb.iceaxe.exception.TsurugiIOException; -import com.tsurugidb.iceaxe.sql.parameter.TgBindParameters; -import com.tsurugidb.iceaxe.sql.parameter.TgBindVariable; -import com.tsurugidb.iceaxe.sql.parameter.TgParameterMapping; -import com.tsurugidb.iceaxe.sql.result.TsurugiQueryResult; -import com.tsurugidb.iceaxe.sql.result.TsurugiResultEntity; -import com.tsurugidb.iceaxe.test.util.DbTestConnector; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.transaction.manager.TgTmSetting; -import com.tsurugidb.iceaxe.transaction.manager.exception.TsurugiTmIOException; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; -import com.tsurugidb.tsubakuro.sql.SqlServiceCode; - -/** - * select error test - */ -class DbSelectErrorTest extends DbTestTableTester { - - private static final int SIZE = 100; - - @BeforeAll - static void beforeAll(TestInfo info) throws Exception { - var LOG = LoggerFactory.getLogger(DbSelectErrorTest.class); - logInitStart(LOG, info); - - dropTestTable(); - createTestTable(); - insertTestTable(SIZE); - - logInitEnd(LOG, info); - } - - @Test - void undefinedColumnName() throws Exception { - var sql = "select hoge from " + TEST; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql)) { - var e = assertThrowsExactly(TsurugiTmIOException.class, () -> { - tm.executeAndGetList(ps); - }); - assertEqualsCode(SqlServiceCode.SYMBOL_ANALYZE_EXCEPTION, e); - assertContains("compile failed with error:variable_not_found message:\"hoge\" location:(unknown)", e.getMessage()); - } - } - - @Test - void aggregateWithoutGroupBy() throws Exception { - var sql = "select foo, sum(bar) as bar from " + TEST; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql)) { - var e = assertThrowsExactly(TsurugiTmIOException.class, () -> { - tm.executeAndGetList(ps); - }); - assertEqualsCode(SqlServiceCode.COMPILE_EXCEPTION, e); - assertContains("compile failed with error:invalid_aggregation_column message:\"target column must be aggregated\" location:(unknown)", e.getMessage()); // TODO カラム名が欲しい - } - } - - @Test - void ps0ExecuteAfterClose() throws Exception { - var sql = "select * from " + TEST; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - var ps = session.createQuery(sql); - ps.close(); - var e = assertThrowsExactly(TsurugiIOException.class, () -> { - tm.executeAndGetList(ps); - }); - assertEqualsCode(IceaxeErrorCode.PS_ALREADY_CLOSED, e); - } - - @Test - void ps1ExecuteAfterClose() throws Exception { - var foo = TgBindVariable.ofInt("foo"); - var sql = "select * from " + TEST + " where foo=" + foo; - var parameterMapping = TgParameterMapping.of(foo); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - var ps = session.createQuery(sql, parameterMapping); - ps.close(); - var parameter = TgBindParameters.of(foo.bind(1)); - var e = assertThrowsExactly(TsurugiIOException.class, () -> { - tm.executeAndGetList(ps, parameter); - }); - assertEqualsCode(IceaxeErrorCode.PS_ALREADY_CLOSED, e); - } - - @Test - void ps0ExecuteAfterTxFutureClose() throws Exception { - ps0ExecuteAfterTxClose(false); - } - - @Test - void ps0ExecuteAfterTxClose() throws Exception { - ps0ExecuteAfterTxClose(true); - } - - private void ps0ExecuteAfterTxClose(boolean getLow) throws IOException, InterruptedException { - var sql = "select * from " + TEST; - - var session = getSession(); - try (var ps = session.createQuery(sql)) { - var transaction = session.createTransaction(TgTxOption.ofOCC()); - if (getLow) { - transaction.getLowTransaction(); - } - transaction.close(); - var e = assertThrowsExactly(TsurugiIOException.class, () -> { - transaction.executeAndGetList(ps); - }); - assertEqualsCode(IceaxeErrorCode.TX_ALREADY_CLOSED, e); - assertEquals("transaction already closed", e.getMessage()); - } - } - - @Test - void selectAfterTransactionClose() throws Exception { - var sql = "select * from " + TEST; - - var session = getSession(); - try (var ps = session.createQuery(sql)) { - TsurugiQueryResult result; - try (var transaction = session.createTransaction(TgTxOption.ofOCC())) { - result = transaction.executeQuery(ps); - } - var e = assertThrowsExactly(IOException.class, () -> { - result.getRecordList(); - }); - assertEquals("resultSet already closed", e.getMessage()); - } - } - - @Test - void selectInTransactionClose() throws Exception { - var sql = "select * from " + TEST; - - var session = getSession(); - try (var ps = session.createQuery(sql)) { - try (var transaction = session.createTransaction(TgTxOption.ofOCC())) { - try (var result = transaction.executeQuery(ps)) { - var i = result.iterator(); - i.next(); - transaction.close(); - var e = assertThrowsExactly(UncheckedIOException.class, () -> { - while (i.hasNext()) { - i.next(); - } - }); - assertEquals("resultSet already closed", e.getMessage()); - } - } - } - } - - @RepeatedTest(60) - @Disabled // TODO remove Disabled. 挙動が不安定 - void closeWithSelectThread() throws Throwable { - Thread thread; - Throwable[] threadException = { null }; - try (var session = DbTestConnector.createSession()) { - var started = new AtomicBoolean(false); - var ended = new AtomicBoolean(false); - thread = new Thread(() -> { - try { - var setting = TgTmSetting.of(TgTxOption.ofOCC()); - var tm = session.createTransactionManager(setting); - tm.execute(transaction -> { - try (var ps = session.createQuery(SELECT_SQL)) { - started.set(true); - transaction.executeAndGetList(ps); - } - }); - } catch (Throwable e) { - threadException[0] = e; - } finally { - ended.set(true); - } - }); - thread.start(); - while (!started.get() && !ended.get()) { - } - } // session close (with resultSet, transaction, statement close) - - thread.join(); - - Throwable e = threadException[0]; - assertNotNull(e); - String message = e.getMessage(); - if (message != null) { - try { - assertContains("already closed", message); - } catch (Throwable t) { - t.addSuppressed(e); - throw t; - } - return; - } - throw e; - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectExplainTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectExplainTest.java deleted file mode 100755 index 39ef6df2..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectExplainTest.java +++ /dev/null @@ -1,94 +0,0 @@ -package com.tsurugidb.iceaxe.test.select; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertThrowsExactly; - -import java.util.stream.Collectors; -import java.util.stream.Stream; - -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.slf4j.LoggerFactory; - -import com.tsurugidb.iceaxe.sql.explain.TgStatementMetadata; -import com.tsurugidb.iceaxe.sql.parameter.TgBindParameters; -import com.tsurugidb.iceaxe.sql.parameter.TgBindVariable; -import com.tsurugidb.iceaxe.sql.parameter.TgParameterMapping; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.sql.proto.SqlCommon.AtomType; - -/** - * explain select test - */ -class DbSelectExplainTest extends DbTestTableTester { - - private static final int SIZE = 4; - - @BeforeAll - static void beforeAll(TestInfo info) throws Exception { - var LOG = LoggerFactory.getLogger(DbSelectExplainTest.class); - logInitStart(LOG, info); - - dropTestTable(); - createTestTable(); - insertTestTable(SIZE); - - logInitEnd(LOG, info); - } - - @Test - void pareparedStatement() throws Exception { - var sql = "select * from " + TEST; - - var session = getSession(); - try (var ps = session.createQuery(sql)) { - assertThrowsExactly(UnsupportedOperationException.class, () -> { - var result = ps.explain(); - assertExplain(result); - }); // TODO explain実装待ち - } - } - - @Test - void psParameter() throws Exception { - var foo = TgBindVariable.ofInt("foo"); - var sql = "select * from " + TEST + " where foo=" + foo; - var parameterMapping = TgParameterMapping.of(foo); - - var session = getSession(); - try (var ps = session.createQuery(sql, parameterMapping)) { - var parameter = TgBindParameters.of(foo.bind(1)); - var result = ps.explain(parameter); - assertExplain(result); - } - } - - private static void assertExplain(TgStatementMetadata actual) throws Exception { - assertNotNull(actual.getLowPlanGraph()); - - var list = actual.getLowColumnList(); - assertEquals(3, list.size()); - var c0 = list.get(0); - assertEquals("foo", c0.getName()); - assertEquals(AtomType.INT4, c0.getAtomType()); - var c1 = list.get(1); - assertEquals("bar", c1.getName()); - assertEquals(AtomType.INT8, c1.getAtomType()); - var c2 = list.get(2); - assertEquals("zzz", c2.getName()); - assertEquals(AtomType.CHARACTER, c2.getAtomType()); - } - - @Test - void largeResult() throws Exception { - var sql = "select * from " + Stream.generate(() -> TEST).limit(6).collect(Collectors.joining(",")); - - var session = getSession(); - try (var ps = session.createQuery(sql, TgParameterMapping.of())) { - var result = ps.explain(TgBindParameters.of()); - assertNotNull(result.getLowPlanGraph()); - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectFetchTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectFetchTest.java deleted file mode 100755 index def3932a..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectFetchTest.java +++ /dev/null @@ -1,68 +0,0 @@ -package com.tsurugidb.iceaxe.test.select; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import java.io.IOException; - -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.slf4j.LoggerFactory; - -import com.tsurugidb.iceaxe.sql.parameter.TgBindParameters; -import com.tsurugidb.iceaxe.sql.parameter.TgBindVariable; -import com.tsurugidb.iceaxe.sql.parameter.TgParameterMapping; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; - -/** - * select cursor fetch test - */ -class DbSelectFetchTest extends DbTestTableTester { - - private static final int SIZE = 8300; - - @BeforeAll - static void beforeAll(TestInfo info) throws Exception { - var LOG = LoggerFactory.getLogger(DbSelectFetchTest.class); - logInitStart(LOG, info); - - dropTestTable(); - createTestTable(); - insertTestTable(SIZE); - - dropTable(TEST2); - createTest2Table(); - - logInitEnd(LOG, info); - } - - private static final String TEST2 = "test2"; - - private static void createTest2Table() throws IOException, InterruptedException { - var sql = CREATE_TEST_SQL.replace(TEST, TEST2); - executeDdl(getSession(), sql); - } - - @Test - void fetch() throws Exception { - var cond = TgBindVariable.ofInt("foo"); - var sql = SELECT_SQL + " where foo=" + cond; - var parameterMapping = TgParameterMapping.of(cond); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery("select foo from " + TEST); // - var ps2 = session.createQuery(sql, parameterMapping, SELECT_MAPPING); // - var ps3 = session.createStatement(INSERT_SQL.replace(TEST, TEST2), INSERT_MAPPING)) { - tm.execute(transaction -> { - transaction.executeAndForEach(ps, fetch -> { - int foo = fetch.getInt("foo"); - var parameter = TgBindParameters.of(cond.bind(foo)); - var entity = transaction.executeAndFindRecord(ps2, parameter).get(); - transaction.executeAndGetCount(ps3, entity); - }); - }); - assertEquals(SIZE, selectCountFrom(TEST2)); - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectFewTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectFewTest.java deleted file mode 100755 index fe4ddd2c..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectFewTest.java +++ /dev/null @@ -1,86 +0,0 @@ -package com.tsurugidb.iceaxe.test.select; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import java.util.Optional; - -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.slf4j.LoggerFactory; - -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.test.util.TestEntity; -import com.tsurugidb.iceaxe.transaction.TgCommitType; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; -import com.tsurugidb.tsubakuro.channel.common.connection.wire.impl.ResponseBox; - -/** - * select few record test - */ -class DbSelectFewTest extends DbTestTableTester { - - private static final int ATTEMPT_SIZE = ResponseBox.responseBoxSize() + 200; - - @BeforeAll - static void beforeAll(TestInfo info) throws Exception { - var LOG = LoggerFactory.getLogger(DbSelectFewTest.class); - logInitStart(LOG, info); - - dropTestTable(); - createTestTable(); - insertTestTable(4); - - logInitEnd(LOG, info); - } - - @Test - void selectZero() throws Exception { - var sql = "select * from " + TEST; - - var session = getSession(); - try (var ps = session.createQuery(sql)) { - for (int i = 0; i < ATTEMPT_SIZE; i++) { - try (var transaction = session.createTransaction(TgTxOption.ofOCC())) { - try (var result = ps.execute(transaction)) { - // result.close without read - } - transaction.commit(TgCommitType.DEFAULT); - } - } - } - } - - @Test - void selectOne() throws Exception { - var expected = createTestEntity(0); - var sql = "select * from " + TEST + " order by foo"; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql, SELECT_MAPPING)) { - for (int i = 0; i < ATTEMPT_SIZE; i++) { - Optional entity = tm.executeAndFindRecord(ps); - assertEquals(expected, entity.get()); - } - } - } - - @Test - void selectOneSameTx() throws Exception { - var expected = createTestEntity(0); - var sql = "select * from " + TEST + " order by foo"; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql, SELECT_MAPPING)) { - tm.execute(transaction -> { - for (int i = 0; i < ATTEMPT_SIZE; i++) { - Optional entity = transaction.executeAndFindRecord(ps); - assertEquals(expected, entity.get()); - } - return; - }); - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectIrregularTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectIrregularTest.java deleted file mode 100755 index b2a5c72b..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectIrregularTest.java +++ /dev/null @@ -1,46 +0,0 @@ -package com.tsurugidb.iceaxe.test.select; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.slf4j.LoggerFactory; - -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; - -/** - * irregular select test - */ -class DbSelectIrregularTest extends DbTestTableTester { - - private static final int SIZE = 4; - - @BeforeAll - static void beforeAll(TestInfo info) throws Exception { - var LOG = LoggerFactory.getLogger(DbSelectIrregularTest.class); - logInitStart(LOG, info); - - dropTestTable(); - createTestTable(); - insertTestTable(SIZE); - - logInitEnd(LOG, info); - } - - @Test - void closePsBeforeCloseRs() throws Exception { - var sql = "select * from " + TEST; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - tm.execute(transaction -> { - var ps = session.createQuery(sql); - var result = ps.execute(transaction); - ps.close(); - var list = result.getRecordList(); - assertEquals(SIZE, list.size()); - result.close(); - }); - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectJoinTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectJoinTest.java deleted file mode 100755 index 9ff86d44..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectJoinTest.java +++ /dev/null @@ -1,270 +0,0 @@ -package com.tsurugidb.iceaxe.test.select; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; - -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.slf4j.LoggerFactory; - -import com.tsurugidb.iceaxe.sql.parameter.TgParameterMapping; -import com.tsurugidb.iceaxe.sql.parameter.mapping.TgEntityParameterMapping; -import com.tsurugidb.iceaxe.sql.result.TsurugiResultEntity; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; - -/** - * select join test - */ -class DbSelectJoinTest extends DbTestTableTester { - - // table name - private static final String MASTER = "master"; - private static final String DETAIL = "detail"; - - @BeforeAll - static void beforeAll(TestInfo info) throws Exception { - var LOG = LoggerFactory.getLogger(DbSelectJoinTest.class); - logInitStart(LOG, info); - - dropMasterDetail(); - createMasterDetail(); - insertMasterDetail(); - - logInitEnd(LOG, info); - } - - private static void dropMasterDetail() throws IOException, InterruptedException { - dropTable(MASTER); - dropTable(DETAIL); - } - - private static void createMasterDetail() throws IOException, InterruptedException { - var session = getSession(); - { - var sql = "create table " + MASTER // - + "(" // - + " m_id int," // - + " m_name varchar(10)," // - + " primary key(m_id)" // - + ")"; - executeDdl(session, sql); - } - { - var sql = "create table " + DETAIL // - + "(" // - + " d_id bigint," // - + " d_master_id int," // foreign key to MASTER - + " d_memo varchar(100)," // - + " primary key(d_id)" // - + ")"; - executeDdl(session, sql); - } - } - - private static final List MASTER_LIST = List.of( // - new MasterEntity(1, "aaa"), // - new MasterEntity(2, "bbb"), // - new MasterEntity(3, "ccc")); - private static final Map MASTER_MAP = MASTER_LIST.stream().collect(Collectors.toMap(e -> e.getId(), e -> e)); - - private static final List DETAIL_LIST = List.of( // - new DetailEntity(11, 1, "a1"), // - new DetailEntity(12, 1, "a2"), // - new DetailEntity(13, 1, "a3"), // - new DetailEntity(21, 2, "b1"), // - new DetailEntity(40, 4, "master nothing"), // - new DetailEntity(90, null, "master null")); - - private static void insertMasterDetail() throws IOException, InterruptedException { - var session = getSession(); - var tm = createTransactionManagerOcc(session, 3); - tm.execute(transaction -> { - try (var ps = session.createStatement(MASTER_INSERT_SQL, MASTER_MAPPING)) { - for (var entity : MASTER_LIST) { - transaction.executeAndGetCount(ps, entity); - } - } - try (var ps = session.createStatement(DETAIL_INSERT_SQL, DETAIL_MAPPING)) { - for (var entity : DETAIL_LIST) { - transaction.executeAndGetCount(ps, entity); - } - } - }); - } - - private static class MasterEntity { - private int id; - private String name; - - public MasterEntity(int id, String name) { - this.id = id; - this.name = name; - } - - public int getId() { - return id; - } - - public String getName() { - return name; - } - } - - private static final String MASTER_INSERT_SQL = "insert into " + MASTER // - + " (m_id, m_name)" // - + " values(:m_id, :m_name)"; - private static final TgEntityParameterMapping MASTER_MAPPING = TgParameterMapping.of(MasterEntity.class) // - .addInt("m_id", MasterEntity::getId) // - .addString("m_name", MasterEntity::getName); - - private static class DetailEntity { - private long id; - private Integer masterId; - private String memo; - - public DetailEntity(long id, Integer masterId, String memo) { - this.id = id; - this.masterId = masterId; - this.memo = memo; - } - - public long getId() { - return id; - } - - public Integer getMasterId() { - return masterId; - } - - public String getMemo() { - return memo; - } - } - - private static final String DETAIL_INSERT_SQL = "insert into " + DETAIL // - + " (d_id, d_master_id, d_memo)" // - + " values(:d_id, :d_master_id, :d_memo)"; - private static final TgEntityParameterMapping DETAIL_MAPPING = TgParameterMapping.of(DetailEntity.class) // - .addLong("d_id", DetailEntity::getId) // - .addInt("d_master_id", DetailEntity::getMasterId) // - .addString("d_memo", DetailEntity::getMemo); - - @Test - void simpleJoin() throws Exception { - var sql = "select * from " + DETAIL + " d, " + MASTER + " m\n" // - + "where m.m_id = d.d_master_id\n" // - + "order by d_id"; - - var expectedList = new ArrayList(); - for (var detail : DETAIL_LIST) { - var master = MASTER_MAP.get(detail.getMasterId()); - if (master != null) { - expectedList.add(new MasterDetailPair(master, detail)); - } - } - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql)) { - List list = tm.executeAndGetList(ps); - assertEqualsMasterDetail(expectedList, list); - } - } - - @Test - void innerJoin() throws Exception { - var sql = "select * from " + DETAIL + " d\n" // - + "inner join " + MASTER + " m on m.m_id = d.d_master_id\n" // - + "order by d_id"; - - var expectedList = new ArrayList(); - for (var detail : DETAIL_LIST) { - var master = MASTER_MAP.get(detail.getMasterId()); - if (master != null) { - expectedList.add(new MasterDetailPair(master, detail)); - } - } - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql)) { - List list = tm.executeAndGetList(ps); - assertEqualsMasterDetail(expectedList, list); - } - } - - @Test - void leftJoin() throws Exception { - var sql = "select * from " + DETAIL + " d\n" // - + "left join " + MASTER + " m on m.m_id = d.d_master_id\n" // - + "order by d_id"; - - var expectedList = new ArrayList(); - for (var detail : DETAIL_LIST) { - var master = MASTER_MAP.get(detail.getMasterId()); - expectedList.add(new MasterDetailPair(master, detail)); - } - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql)) { - List list = tm.executeAndGetList(ps); - assertEquals(4, list.size()); // TODO left join実装待ち -// assertEqualsMasterDetail(expectedList, list); - } - } - - private static class MasterDetailPair implements Comparable { - private MasterEntity master; - private DetailEntity detail; - - public MasterDetailPair(MasterEntity master, DetailEntity detail) { - this.master = master; - this.detail = detail; - } - - @Override - public int compareTo(MasterDetailPair that) { - if (this.detail == null && that.detail == null) { - return Long.compare(this.master.getId(), that.master.getId()); - } - if (this.detail == null) { - return 1; - } - if (that.detail == null) { - return -1; - } - - return Long.compare(this.detail.getId(), that.detail.getId()); - } - } - - private void assertEqualsMasterDetail(List expectedList, List actualList) { - Collections.sort(expectedList); - assertEquals(expectedList.size(), actualList.size()); - int i = 0; - for (var pair : expectedList) { - var actual = actualList.get(i++); - var detail = pair.detail; - assertEquals(detail.getId(), actual.getLong("d_id")); - assertEquals(detail.getMasterId(), actual.getIntOrNull("d_master_id")); - assertEquals(detail.getMemo(), actual.getString("d_memo")); - var master = pair.master; - if (master != null) { - assertEquals(master.getId(), actual.getInt("m_id")); - assertEquals(master.getName(), actual.getString("m_name")); - } else { - assertNull(actual.getIntOrNull("m_id")); - assertNull(actual.getIntOrNull("m_name")); - } - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectLiteralTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectLiteralTest.java deleted file mode 100755 index 9b6b0d04..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectLiteralTest.java +++ /dev/null @@ -1,109 +0,0 @@ -package com.tsurugidb.iceaxe.test.select; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertThrowsExactly; - -import java.io.IOException; -import java.math.BigDecimal; -import java.util.function.Consumer; - -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; -import org.slf4j.LoggerFactory; - -import com.tsurugidb.iceaxe.sql.result.TsurugiResultEntity; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.transaction.manager.exception.TsurugiTmIOException; -import com.tsurugidb.tsubakuro.sql.SqlServiceCode; - -/** - * select literal test - */ -class DbSelectLiteralTest extends DbTestTableTester { - - @BeforeAll - static void beforeAll(TestInfo info) throws Exception { - var LOG = LoggerFactory.getLogger(DbSelectLiteralTest.class); - logInitStart(LOG, info); - - dropTestTable(); - createTestTable(); - insertTestTable(1); - - logInitEnd(LOG, info); - } - - private static final String COLUMN = "c"; - - @Test - void nullLiteral() throws Exception { - test("null", entity -> { - assertNull(entity.getStringOrNull(COLUMN)); - assertNull(entity.getIntOrNull(COLUMN)); - }); - } - - @Test - void longLiteral() throws Exception { - long literal = 1; - test(Long.toString(literal), entity -> { - assertEquals(literal, entity.getLong(COLUMN)); - - assertEquals((int) literal, entity.getInt(COLUMN)); - assertEquals(BigDecimal.valueOf(literal), entity.getDecimal(COLUMN)); - }); - } - - @Test - void doubleLiteral() throws Exception { - double literal = 12.3; - test(Double.toString(literal), entity -> { - assertEquals(literal, entity.getDouble(COLUMN)); - - assertEquals(BigDecimal.valueOf(literal), entity.getDecimal(COLUMN)); - }); - } - - @Test - void doubleLiteralError() throws Exception { - var e = assertThrowsExactly(TsurugiTmIOException.class, () -> { - test("1e2", entity -> { - }); - }); - assertEqualsCode(SqlServiceCode.SYNTAX_EXCEPTION, e); - assertContains("parsing statement failed: missing K_FROM at 'e2' (:1:8)", e.getMessage()); - } - - @Test - void stringLiteral() throws Exception { - String literal = "abc"; - test(String.format("'%s'", literal), entity -> { - assertEquals(literal, entity.getString(COLUMN)); - }); - } - - @ParameterizedTest - @ValueSource(booleans = { true, false }) - void booleanLiteral(boolean literal) throws Exception { - test(Boolean.toString(literal), entity -> { - assertEquals(literal, entity.getBoolean(COLUMN)); - }); - } - - // TODO date literal - - private static void test(String literal, Consumer assertion) throws IOException, InterruptedException { - var sql = "select " + literal + " as " + COLUMN + " from " + TEST; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql)) { - var entity = tm.executeAndFindRecord(ps).get(); - assertion.accept(entity); - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectOtherTxTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectOtherTxTest.java deleted file mode 100755 index 6ebca62b..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectOtherTxTest.java +++ /dev/null @@ -1,134 +0,0 @@ -package com.tsurugidb.iceaxe.test.select; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrowsExactly; -import static org.junit.jupiter.api.Assertions.fail; - -import java.io.IOException; -import java.text.MessageFormat; -import java.util.List; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.RepeatedTest; -import org.junit.jupiter.api.TestInfo; - -import com.tsurugidb.iceaxe.session.TsurugiSession; -import com.tsurugidb.iceaxe.sql.TsurugiSqlPreparedStatement; -import com.tsurugidb.iceaxe.sql.TsurugiSqlQuery; -import com.tsurugidb.iceaxe.sql.parameter.TgBindParameters; -import com.tsurugidb.iceaxe.sql.parameter.TgBindVariable; -import com.tsurugidb.iceaxe.sql.parameter.TgBindVariable.TgBindVariableLong; -import com.tsurugidb.iceaxe.sql.parameter.TgParameterMapping; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.test.util.TestEntity; -import com.tsurugidb.iceaxe.transaction.TgCommitType; -import com.tsurugidb.iceaxe.transaction.TsurugiTransaction; -import com.tsurugidb.iceaxe.transaction.exception.TsurugiTransactionException; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; -import com.tsurugidb.tsubakuro.sql.SqlServiceCode; - -/** - * select LTX (update other transaction) test - */ -class DbSelectOtherTxTest extends DbTestTableTester { - - private static final int SIZE = 4; - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTestTable(); - insertTestTable(SIZE); - - logInitEnd(info); - } - - @RepeatedTest(8) - void select1() throws Exception { - var selectSql = "select * from " + TEST + " where foo=1"; - var bar = TgBindVariable.ofLong("bar"); - var updateSql = "update " + TEST + " set bar=" + bar + " where foo=1"; - - var session = getSession(); - try (var selectPs = session.createQuery(selectSql, SELECT_MAPPING); // - var updatePs = session.createStatement(updateSql, TgParameterMapping.of(bar))) { - updateOtherTxOcc(session, updatePs, bar, 111); - - try (var tx = session.createTransaction(TgTxOption.ofLTX(TEST))) { - tx.getLowTransaction(); - - var e = assertThrowsExactly(TsurugiTransactionException.class, () -> { - updateOtherTxOcc(session, updatePs, bar, 222); - }); - var code = findDiagnosticCode(e); - var expectedCode = List.of(SqlServiceCode.CC_EXCEPTION, SqlServiceCode.CONFLICT_ON_WRITE_PRESERVE_EXCEPTION); - if (!expectedCode.contains(code)) { - fail(MessageFormat.format("expected: {1} but was: <{0}>", code, expectedCode)); - } - - var entity2 = select(tx, selectPs); - assertEquals(111L, entity2.getBar()); - - tx.commit(TgCommitType.DEFAULT); - } - - try (var tx = session.createTransaction(TgTxOption.ofOCC())) { - var entity3 = select(tx, selectPs); - assertEquals(111L, entity3.getBar()); - - tx.commit(TgCommitType.DEFAULT); - } - } - } - - @RepeatedTest(4) - void select2() throws Exception { - var selectSql = "select * from " + TEST + " where foo=1"; - var bar = TgBindVariable.ofLong("bar"); - var updateSql = "update " + TEST + " set bar=" + bar + " where foo=1"; - - var session = getSession(); - try (var selectPs = session.createQuery(selectSql, SELECT_MAPPING); // - var updatePs = session.createStatement(updateSql, TgParameterMapping.of(bar))) { - updateOtherTxOcc(session, updatePs, bar, 111); - - try (var tx = session.createTransaction(TgTxOption.ofLTX(TEST))) { - var entity1 = select(tx, selectPs); - assertEquals(111L, entity1.getBar()); - - var e = assertThrowsExactly(TsurugiTransactionException.class, () -> { - updateOtherTxOcc(session, updatePs, bar, 222); - }); - assertEqualsCode(SqlServiceCode.CC_EXCEPTION, e); - - var entity2 = select(tx, selectPs); - assertEquals(111L, entity2.getBar()); - - tx.commit(TgCommitType.DEFAULT); - } - - try (var tx = session.createTransaction(TgTxOption.ofOCC())) { - var entity3 = select(tx, selectPs); - assertEquals(111L, entity3.getBar()); - - tx.commit(TgCommitType.DEFAULT); - } - } - } - - private void updateOtherTxOcc(TsurugiSession session, TsurugiSqlPreparedStatement updatePs, TgBindVariableLong bar, long value) - throws IOException, TsurugiTransactionException, InterruptedException { - try (var tx = session.createTransaction(TgTxOption.ofOCC())) { - var parameter = TgBindParameters.of(bar.bind(value)); - int count = tx.executeAndGetCount(updatePs, parameter); - assertUpdateCount(1, count); - tx.commit(TgCommitType.DEFAULT); - } - } - - private TestEntity select(TsurugiTransaction tx, TsurugiSqlQuery selectPs) throws IOException, TsurugiTransactionException, InterruptedException { - return tx.executeAndFindRecord(selectPs).get(); - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectTest.java deleted file mode 100755 index 50b4fbd6..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectTest.java +++ /dev/null @@ -1,157 +0,0 @@ -package com.tsurugidb.iceaxe.test.select; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.util.List; -import java.util.stream.Collectors; - -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.slf4j.LoggerFactory; - -import com.tsurugidb.iceaxe.sql.result.TgResultMapping; -import com.tsurugidb.iceaxe.sql.result.TsurugiResultEntity; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.test.util.TestEntity; - -/** - * select all record test - */ -class DbSelectTest extends DbTestTableTester { - - private static final int SIZE = 4; - - @BeforeAll - static void beforeAll(TestInfo info) throws Exception { - var LOG = LoggerFactory.getLogger(DbSelectTest.class); - logInitStart(LOG, info); - - dropTestTable(); - createTestTable(); - insertTestTable(SIZE); - - logInitEnd(LOG, info); - } - - @Test - void selectAllByResultEntity() throws Exception { - var sql = "select * from " + TEST; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql)) { - List list = tm.executeAndGetList(ps); - assertResultEntity(list); - } - } - - private static void assertResultEntity(List actualList) { - assertEquals(SIZE, actualList.size()); - var actualMap = actualList.stream().collect(Collectors.toMap(r -> r.getIntOrNull("foo"), r -> r)); - for (int i = 0; i < SIZE; i++) { - var actual = actualMap.get(i); - var expected = createTestEntity(i); - assertEquals(expected.getFoo(), actual.getIntOrNull("foo")); - assertEquals(expected.getBar(), actual.getLongOrNull("bar")); - assertEquals(expected.getZzz(), actual.getStringOrNull("zzz")); - - assertEquals(List.of("foo", "bar", "zzz"), actual.getNameList()); - assertEquals("foo", actual.getName(0)); - assertEquals("bar", actual.getName(1)); - assertEquals("zzz", actual.getName(2)); - assertThrows(IndexOutOfBoundsException.class, () -> actual.getName(-1)); - assertThrows(IndexOutOfBoundsException.class, () -> actual.getName(3)); - } - } - - @Test - void selectAllByTestEntity() throws Exception { - var sql = "select * from " + TEST; - var resultMapping = TgResultMapping.of(TestEntity::new) // - .addInt("foo", TestEntity::setFoo) // - .addLong("bar", TestEntity::setBar) // - .addString("zzz", TestEntity::setZzz); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql, resultMapping)) { - List list = tm.executeAndGetList(ps); - assertTestEntity(list); - } - } - - @Test - void selectColumnsByTestEntity() throws Exception { - var sql = "select " + TEST_COLUMNS + " from " + TEST; - var resultMapping = TgResultMapping.of(TestEntity::new) // - .addInt(TestEntity::setFoo) // - .addLong(TestEntity::setBar) // - .addString(TestEntity::setZzz); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql, resultMapping)) { - List list = tm.executeAndGetList(ps); - assertTestEntity(list); - } - } - - private static void assertTestEntity(List actualList) { - assertEquals(SIZE, actualList.size()); - var actualMap = actualList.stream().collect(Collectors.toMap(r -> r.getFoo(), r -> r)); - for (int i = 0; i < SIZE; i++) { - var actual = actualMap.get(i); - var expected = createTestEntity(i); - assertEquals(expected, actual); - } - } - - @Test - void selectColumn_convert() throws Exception { - var sql = "select foo from " + TEST; - var resultMapping = TgResultMapping.of(record -> record.nextInt()); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql, resultMapping)) { - List list = tm.executeAndGetList(ps); - assertColumn(list); - } - } - - @Test - void selectColumn_single() throws Exception { - var sql = "select foo from " + TEST; - var resultMapping = TgResultMapping.ofSingle(int.class); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql, resultMapping)) { - List list = tm.executeAndGetList(ps); - assertColumn(list); - } - } - - @Test - void selectMultiColumn_single() throws Exception { - var sql = "select foo, bar, zzz from " + TEST; - var resultMapping = TgResultMapping.ofSingle(int.class); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql, resultMapping)) { - List list = tm.executeAndGetList(ps); - assertColumn(list); - } - } - - private static void assertColumn(List actualList) { - assertEquals(SIZE, actualList.size()); - for (int i = 0; i < SIZE; i++) { - assertTrue(actualList.contains(i)); - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectWhereExpressionTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectWhereExpressionTest.java deleted file mode 100755 index 33f9018b..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectWhereExpressionTest.java +++ /dev/null @@ -1,145 +0,0 @@ -package com.tsurugidb.iceaxe.test.select; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrowsExactly; - -import java.util.List; -import java.util.stream.Collectors; - -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; -import org.slf4j.LoggerFactory; - -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.test.util.TestEntity; -import com.tsurugidb.iceaxe.transaction.manager.exception.TsurugiTmIOException; -import com.tsurugidb.tsubakuro.sql.SqlServiceCode; - -/** - * select where expression test - */ -class DbSelectWhereExpressionTest extends DbTestTableTester { - - private static final int SIZE = 10; - private static TestEntity NULL_ENTITY = new TestEntity(123, null, null); - - @BeforeAll - static void beforeAll(TestInfo info) throws Exception { - var LOG = LoggerFactory.getLogger(DbSelectWhereExpressionTest.class); - logInitStart(LOG, info); - - dropTestTable(); - createTestTable(); - insertTestTable(SIZE); - insertTestTable(NULL_ENTITY); - - logInitEnd(LOG, info); - } - - @Test - void invalidExpression() throws Exception { - var sql = SELECT_SQL + " where foo != 1"; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql)) { - var e = assertThrowsExactly(TsurugiTmIOException.class, () -> { - tm.executeAndGetList(ps); - }); - assertEqualsCode(SqlServiceCode.SYNTAX_EXCEPTION, e); - assertContains("parsing statement failed: mismatched input '!=' expecting (:1:41)", e.getMessage()); - } - } - - @ParameterizedTest - @ValueSource(strings = { "=", "<>", "<", ">", "<=", ">=" }) - void eqNull(String expression) throws Exception { - var sql = SELECT_SQL + " where bar " + expression + " null"; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql)) { - var list = tm.executeAndGetList(ps); - assertEquals(0, list.size()); - } - } - - @Test - void isNull() throws Exception { - var sql = SELECT_SQL + " where bar is null"; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql, SELECT_MAPPING)) { - var e = assertThrowsExactly(TsurugiTmIOException.class, () -> { - var list = tm.executeAndGetList(ps); - assertEquals(1, list.size()); - var entity = list.get(0); - assertEquals(NULL_ENTITY, entity); - }); - assertEqualsCode(SqlServiceCode.SYNTAX_EXCEPTION, e); // TODO is null実装待ち - } - } - - @Test - void isNotNull() throws Exception { - var sql = SELECT_SQL + " where bar is not null" + " order by foo"; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql, SELECT_MAPPING)) { - var e = assertThrowsExactly(TsurugiTmIOException.class, () -> { - var list = tm.executeAndGetList(ps); - assertEquals(SIZE, list.size()); - for (int i = 0; i < SIZE; i++) { - assertEquals(i, list.get(i).getFoo()); - } - }); - assertEqualsCode(SqlServiceCode.SYNTAX_EXCEPTION, e); // TODO is not null実装待ち - } - } - - @Test - void in() throws Exception { - var expectedList = List.of(2, 4, 5, 7); - var sql = SELECT_SQL + " where foo in (" + expectedList.stream().map(n -> Integer.toString(n)).collect(Collectors.joining(",")) + ") order by foo"; -// var sql = SELECT_SQL + " where " + expectedList.stream().map(n -> "foo=" + n).collect(Collectors.joining(" or ")) + " order by foo"; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql, SELECT_MAPPING)) { - var e = assertThrowsExactly(TsurugiTmIOException.class, () -> { - var list = tm.executeAndGetList(ps); - assertEquals(expectedList.size(), list.size()); - for (int i = 0; i < list.size(); i++) { - assertEquals(expectedList.get(i), list.get(i).getFoo()); - } - }); - assertEqualsCode(SqlServiceCode.SYNTAX_EXCEPTION, e); // TODO in実装待ち - } - } - - @Test - void between() throws Exception { - int s = 4; - int e = 6; - var sql = SELECT_SQL + " where foo between " + s + " and " + e + " order by foo"; -// var sql = SELECT_SQL + " where " + s + " <= foo and foo <= " + e + " order by foo"; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql, SELECT_MAPPING)) { - var e0 = assertThrowsExactly(TsurugiTmIOException.class, () -> { - var list = tm.executeAndGetList(ps); - assertEquals(e - s + 1, list.size()); - for (int i = 0; i < list.size(); i++) { - assertEquals(s + i, list.get(i).getFoo()); - } - }); - assertEqualsCode(SqlServiceCode.SYNTAX_EXCEPTION, e0); // TODO between実装待ち - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectWhileInsertTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectWhileInsertTest.java deleted file mode 100755 index 7f2a5e49..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/select/DbSelectWhileInsertTest.java +++ /dev/null @@ -1,173 +0,0 @@ -package com.tsurugidb.iceaxe.test.select; - -import java.util.ArrayList; -import java.util.concurrent.Callable; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; -import java.util.concurrent.atomic.AtomicInteger; - -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.RepeatedTest; -import org.junit.jupiter.api.TestInfo; -import org.slf4j.LoggerFactory; - -import com.tsurugidb.iceaxe.session.TsurugiSession; -import com.tsurugidb.iceaxe.sql.parameter.TgBindParameters; -import com.tsurugidb.iceaxe.sql.parameter.TgBindVariable; -import com.tsurugidb.iceaxe.sql.parameter.TgParameterMapping; -import com.tsurugidb.iceaxe.test.util.DbTestConnector; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.transaction.manager.TgTmSetting; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; - -/** - * select while delete-insert test - */ -class DbSelectWhileInsertTest extends DbTestTableTester { - - private static final int SIZE = 100; - - private static final int INSERT_THREAD_SIZE = 1; - private static final int SELECT_THREAD_SIZE = 6; - - private static final int INSERT_ATTEMPT_SIZE = 4; - - @BeforeAll - static void beforeAll(TestInfo info) throws Exception { - var LOG = LoggerFactory.getLogger(DbSelectWhileInsertTest.class); - logInitStart(LOG, info); - - dropTestTable(); - createTestTable(); - insertTestTable(SIZE); - - logInitEnd(LOG, info); - } - - @RepeatedTest(3) - void test() throws Throwable { - var service = Executors.newFixedThreadPool(INSERT_THREAD_SIZE + SELECT_THREAD_SIZE); - var sessionList = new ArrayList(INSERT_THREAD_SIZE + SELECT_THREAD_SIZE); - var futureList = new ArrayList>(INSERT_THREAD_SIZE + SELECT_THREAD_SIZE); - Throwable re = null; - try { - var ltxThreadCounter = new AtomicInteger(INSERT_THREAD_SIZE); - for (int i = 0; i < INSERT_THREAD_SIZE; i++) { - var session = DbTestConnector.createSession(); - sessionList.add(session); - var future = service.submit(new InsertThread(session, ltxThreadCounter)); - futureList.add(future); - } - for (int i = 0; i < SELECT_THREAD_SIZE; i++) { - var session = DbTestConnector.createSession(); - sessionList.add(session); - var future = service.submit(new SelectThread(session, ltxThreadCounter)); - futureList.add(future); - } - } catch (Throwable e) { - re = e; - throw e; - } finally { - for (var future : futureList) { - try { - future.get(); - // test success if no error - } catch (Exception e) { - if (re == null) { - re = e; - } else { - re.addSuppressed(e); - } - } - } - for (var session : sessionList) { - try { - session.close(); - } catch (Exception e) { - if (re == null) { - re = e; - } else { - re.addSuppressed(e); - } - } - } - if (re != null) { - throw re; - } - } - } - - private static class InsertThread implements Callable { - private final TsurugiSession session; - private final AtomicInteger threadCounter; - - public InsertThread(TsurugiSession session, AtomicInteger threadCounter) { - this.session = session; - this.threadCounter = threadCounter; - } - - @Override - public Void call() throws Exception { - try { - execute(); - } finally { - threadCounter.decrementAndGet(); - } - return null; - } - - private void execute() throws Exception { - var key = TgBindVariable.ofInt("foo"); - var deleteSql = "delete from " + TEST + " where foo=" + key; - var deleteMapping = TgParameterMapping.of(key); - try (var deletePs = session.createStatement(deleteSql, deleteMapping); // - var insertPs = session.createStatement(INSERT_SQL, INSERT_MAPPING)) { - var setting = TgTmSetting.ofAlways(TgTxOption.ofOCC()); - var tm = session.createTransactionManager(setting); - - for (int n = 0; n < INSERT_ATTEMPT_SIZE; n++) { - for (int i = 0; i < SIZE; i++) { - int foo = i; - tm.execute(transaction -> { - var parameter = TgBindParameters.of(key.bind(foo)); - transaction.executeAndGetCount(deletePs, parameter); - }); - tm.execute(transaction -> { - var parameter = createTestEntity(foo); - transaction.executeAndGetCount(insertPs, parameter); - }); - } - } - } - } - } - - private static class SelectThread implements Callable { - private final TsurugiSession session; - private final AtomicInteger threadCounter; - - public SelectThread(TsurugiSession session, AtomicInteger threadCounter) { - this.session = session; - this.threadCounter = threadCounter; - } - - @Override - public Void call() throws Exception { - execute(); - return null; - } - - private void execute() throws Exception { - try (var selectPs = session.createQuery(SELECT_SQL, TgParameterMapping.of(), SELECT_MAPPING)) { - var setting = TgTmSetting.ofAlways(TgTxOption.ofRTX()); - var tm = session.createTransactionManager(setting); - - while (threadCounter.get() > 0) { - tm.execute(transaction -> { - transaction.executeAndGetList(selectPs, null); - }); - } - } - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/session/DbMultiSessionTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/session/DbMultiSessionTest.java deleted file mode 100755 index 9fa4592f..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/session/DbMultiSessionTest.java +++ /dev/null @@ -1,203 +0,0 @@ -package com.tsurugidb.iceaxe.test.session; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrowsExactly; -import static org.junit.jupiter.api.Assertions.fail; - -import java.io.IOException; -import java.text.MessageFormat; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.CopyOnWriteArrayList; -import java.util.concurrent.atomic.AtomicBoolean; - -import org.junit.jupiter.api.Test; - -import com.tsurugidb.iceaxe.exception.IceaxeErrorCode; -import com.tsurugidb.iceaxe.exception.TsurugiIOException; -import com.tsurugidb.iceaxe.session.TsurugiSession; -import com.tsurugidb.iceaxe.test.util.DbTestConnector; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; - -/** - * multiple session test - */ -class DbMultiSessionTest extends DbTestTableTester { - - private static final int ATTEMPT_SIZE = 260; - - // TODO 最大接続数は、実行環境(tsurugidbの構成定義ファイルの内容)によって変わるので、本当はDBサーバーから取得したい - private static final int EXPECTED_SESSION_SIZE = getSystemProperty("tsurugi.dbtest.expected.session.size", 104); - - @Test - void limit() throws Exception { - closeStaticSession(); - - var sessionList = new ArrayList(); - Throwable occurred = null; - try { - for (int i = 0; i < ATTEMPT_SIZE; i++) { - TsurugiSession session; - try { - session = DbTestConnector.createSession(); - } catch (IOException e) { - assertEquals("the server has declined the connection request", e.getMessage()); - int count = sessionList.size(); - if (count < EXPECTED_SESSION_SIZE) { - fail(MessageFormat.format("less session.size expected: {1} but was: {0}", count, EXPECTED_SESSION_SIZE)); - } - return; - } - sessionList.add(session); - } - - limit(sessionList); - } catch (Throwable e) { - occurred = e; - throw e; - } finally { - var exceptionList = new ArrayList(); - for (var session : sessionList) { - try { - session.close(); - } catch (Exception e) { - exceptionList.add(e); - } - } - if (!exceptionList.isEmpty()) { - var e = new Exception("session close error. errorCount=" + exceptionList.size()); - exceptionList.forEach(e::addSuppressed); - if (occurred != null) { - occurred.addSuppressed(e); - } else { - throw e; - } - } - } - } - - private void limit(List list) { - int count = 0; - for (var session : list) { - if (session.isAlive()) { - count++; - } else { - var e = assertThrowsExactly(TsurugiIOException.class, () -> { - session.getLowSqlClient(); - }); - assertEqualsCode(IceaxeErrorCode.SESSION_LOW_ERROR, e); - var c = e.getCause(); - assertEquals("the server has declined the connection request", c.getMessage()); - } - } - if (count < EXPECTED_SESSION_SIZE) { - fail(MessageFormat.format("less session.size expected: {1} but was: {0}", count, EXPECTED_SESSION_SIZE)); - } - } - - @Test - void manySession1() throws Exception { - manySession(false, false); - } - - @Test - void manySession2() throws Exception { - manySession(true, false); - } - - @Test - void manySession3() throws Exception { - manySession(false, true); - } - - private void manySession(boolean sqlClient, boolean transaction) throws IOException, InterruptedException { - LOG.debug("create session start"); - var list = new ArrayList(); - try { - for (int i = 0; i < 60; i++) { - var session = DbTestConnector.createSession(); - list.add(session); - - if (sqlClient) { - session.getLowSqlClient(); - } - } - LOG.debug("create session end"); - - if (transaction) { - LOG.debug("createTransaction start"); - int i = 0; - for (var session : list) { - LOG.debug("createTransaction {}", i++); - try (var tx = session.createTransaction(TgTxOption.ofOCC())) { - tx.getLowTransaction(); - } - } - LOG.debug("createTransaction end"); - } - } finally { - LOG.debug("close session start"); - for (var session : list) { - session.close(); - } - LOG.debug("close session end"); - } - } - - @Test - void multiThread() { - LOG.debug("create session start"); - var sessionList = new CopyOnWriteArrayList(); - try { - var threadList = new ArrayList(); - var alive = new AtomicBoolean(true); - for (int i = 0; i < 60; i++) { - var thread = new Thread(() -> { - TsurugiSession session; - try { - session = DbTestConnector.createSession(); - sessionList.add(session); - session.getLowSqlClient(); - } catch (Exception e) { - LOG.warn("connect error. {}: {}", e.getClass().getName(), e.getMessage()); - return; - } - - while (alive.get()) { - try { - Thread.sleep(100); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } - } - }); - threadList.add(thread); - thread.start(); - } - LOG.debug("create session end"); - - alive.set(false); - - LOG.debug("thread join start"); - for (var thread : threadList) { - try { - thread.join(); - } catch (Exception e) { - LOG.warn("join error", e); - } - } - LOG.debug("thread join end"); - } finally { - LOG.debug("close session start"); - for (var session : sessionList) { - try { - session.close(); - } catch (Exception e) { - LOG.warn("close error", e); - } - } - LOG.debug("close session end"); - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/session/DbSessionErrorTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/session/DbSessionErrorTest.java deleted file mode 100755 index ff2ff058..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/session/DbSessionErrorTest.java +++ /dev/null @@ -1,92 +0,0 @@ -package com.tsurugidb.iceaxe.test.session; - -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertThrowsExactly; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import org.junit.jupiter.api.Test; - -import com.tsurugidb.iceaxe.exception.IceaxeErrorCode; -import com.tsurugidb.iceaxe.exception.TsurugiIOException; -import com.tsurugidb.iceaxe.session.TsurugiSession; -import com.tsurugidb.iceaxe.sql.parameter.TgParameterMapping; -import com.tsurugidb.iceaxe.test.util.DbTestConnector; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; - -/** - * session error test - */ -class DbSessionErrorTest extends DbTestTableTester { - - @Test - void createQueryAfterClose() throws Exception { - var session = createClosedSession(); - - var e = assertThrowsExactly(TsurugiIOException.class, () -> { - session.createQuery(SELECT_SQL); - }); - assertEqualsCode(IceaxeErrorCode.SESSION_ALREADY_CLOSED, e); - } - - @Test - void createPreparedQueryAfterClose() throws Exception { - var session = createClosedSession(); - - var e = assertThrowsExactly(TsurugiIOException.class, () -> { - session.createQuery(SELECT_SQL, TgParameterMapping.of()); - }); - assertEqualsCode(IceaxeErrorCode.SESSION_ALREADY_CLOSED, e); - } - - @Test - void createStatementAfterClose() throws Exception { - var session = createClosedSession(); - - var e = assertThrowsExactly(TsurugiIOException.class, () -> { - session.createStatement(INSERT_SQL); - }); - assertEqualsCode(IceaxeErrorCode.SESSION_ALREADY_CLOSED, e); - } - - @Test - void createPreparedStatementAfterClose() throws Exception { - var session = createClosedSession(); - - var e = assertThrowsExactly(TsurugiIOException.class, () -> { - session.createStatement(INSERT_SQL, INSERT_MAPPING); - }); - assertEqualsCode(IceaxeErrorCode.SESSION_ALREADY_CLOSED, e); - } - - @Test - void createTransactionAfterClose() throws Exception { - var session = createClosedSession(); - - var e = assertThrowsExactly(TsurugiIOException.class, () -> { - session.createTransaction(TgTxOption.ofOCC()); - }); - assertEqualsCode(IceaxeErrorCode.SESSION_ALREADY_CLOSED, e); - } - - @Test - void craeteTmAfterClose() throws Exception { - var session = createClosedSession(); - - session.createTransactionManager(); // not thrown - } - - private TsurugiSession createClosedSession() throws Exception { - var session = DbTestConnector.createSession(); - try { - assertTrue(session.isAlive()); - } catch (Throwable t) { - session.close(); - throw t; - } - session.close(); - assertFalse(session.isAlive()); - - return session; - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/session/DbSessionTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/session/DbSessionTest.java deleted file mode 100755 index 5078549d..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/session/DbSessionTest.java +++ /dev/null @@ -1,76 +0,0 @@ -package com.tsurugidb.iceaxe.test.session; - -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.slf4j.LoggerFactory; - -import com.tsurugidb.iceaxe.sql.parameter.TgParameterMapping; -import com.tsurugidb.iceaxe.test.util.DbTestConnector; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; - -/** - * session test - */ -class DbSessionTest extends DbTestTableTester { - - @BeforeAll - static void beforeAll(TestInfo info) throws Exception { - var LOG = LoggerFactory.getLogger(DbSessionTest.class); - logInitStart(LOG, info); - - dropTestTable(); - createTestTable(); - - logInitEnd(LOG, info); - } - - @Test - void doNothing() throws Exception { - try (var session = DbTestConnector.createSession()) { - // do nothing - } - } - - @Test - void isAlive() throws Exception { - // session作成直後にisAlive呼び出し - try (var session = DbTestConnector.createSession()) { - assertTrue(session.isAlive()); - } - } - - @Test - void preparedStatementOnly() throws Exception { - var sql = "select * from " + TEST; - try (var session = DbTestConnector.createSession()) { - try (var ps = session.createQuery(sql, TgParameterMapping.of())) { - assertTrue(session.isAlive()); - } - } - } - - @Test - void transactionOnly() throws Exception { - try (var session = DbTestConnector.createSession()) { - try (var transaction = session.createTransaction(TgTxOption.ofOCC())) { - assertTrue(session.isAlive()); - } - } - } - - @Test - void closeTwice() throws Exception { - try (var session = DbTestConnector.createSession()) { - session.getLowSqlClient(); - assertTrue(session.isAlive()); - - session.close(); - assertFalse(session.isAlive()); - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/session/DbSessionTxFileLoggerTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/session/DbSessionTxFileLoggerTest.java deleted file mode 100755 index fc2b8b30..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/session/DbSessionTxFileLoggerTest.java +++ /dev/null @@ -1,201 +0,0 @@ -package com.tsurugidb.iceaxe.test.session; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.fail; - -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.FileVisitResult; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.SimpleFileVisitor; -import java.nio.file.attribute.BasicFileAttributes; -import java.util.Collections; -import java.util.List; -import java.util.stream.Collectors; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; - -import com.tsurugidb.iceaxe.session.event.logging.file.TsurugiSessionTxFileLogConfig; -import com.tsurugidb.iceaxe.session.event.logging.file.TsurugiSessionTxFileLogger; -import com.tsurugidb.iceaxe.sql.parameter.TgBindParameters; -import com.tsurugidb.iceaxe.sql.parameter.TgBindVariable; -import com.tsurugidb.iceaxe.sql.parameter.TgParameterMapping; -import com.tsurugidb.iceaxe.test.util.DbTestConnector; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; - -/** - * {@link TsurugiSessionTxFileLogger} test - */ -class DbSessionTxFileLoggerTest extends DbTestTableTester { - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - - logInitEnd(info); - } - - @Test - void testLog1() throws Exception { - execute(TsurugiSessionTxFileLogConfig.EXPLAIN_BOTH, false); - } - - @Test - void testLog1Exception() throws Exception { - execute(TsurugiSessionTxFileLogConfig.EXPLAIN_BOTH, true); - } - - @Test - void testLog2() throws Exception { - execute(TsurugiSessionTxFileLogConfig.EXPLAIN_NOTHING, false); - } - - @Test - void testLog2Exception() throws Exception { - execute(TsurugiSessionTxFileLogConfig.EXPLAIN_NOTHING, true); - } - - private static final String ERROR_MESSAGE = "exception by test"; - - private void execute(int writeExplain, boolean throwException) throws IOException, InterruptedException { - var logDir = Files.createTempDirectory("iceaxe-dbtest.tx-log."); - LOG.debug("logDir={}", logDir); - - var config = TsurugiSessionTxFileLogConfig.of(logDir).writeExplain(writeExplain); - try (var session = DbTestConnector.createSession()) { - session.addEventListener(new TsurugiSessionTxFileLogger(config)); - - var foo = TgBindVariable.ofInt("foo"); - - var tm = session.createTransactionManager(TgTxOption.ofOCC()); - tm.executeDdl(CREATE_TEST_SQL); - try (var ps1 = session.createStatement(INSERT_SQL, INSERT_MAPPING); // - var ps2 = session.createQuery(SELECT_SQL, SELECT_MAPPING); // - var ps3 = session.createQuery(SELECT_SQL + " where foo=" + foo, TgParameterMapping.of(foo), SELECT_MAPPING); // - var ps4 = session.createStatement("delete from " + TEST)) { - tm.execute(transaction -> { - var entity = createTestEntity(1); - transaction.executeAndGetCount(ps1, entity); - - var list = transaction.executeAndGetList(ps2); - assertEquals(1, list.size()); - assertEquals(entity, list.get(0)); - - var record = transaction.executeAndFindRecord(ps3, TgBindParameters.of(foo.bind(entity.getFoo()))).get(); - assertEquals(entity, record); - - transaction.executeAndGetCount(ps4); - - if (throwException) { - throw new RuntimeException(ERROR_MESSAGE); - } - }); - } catch (RuntimeException e) { - if (throwException) { - assertEquals(ERROR_MESSAGE, e.getMessage()); - } else { - throw e; - } - } - - assertLog(logDir, writeExplain, throwException); - } finally { - deleteDir(logDir); - } - } - - private void assertLog(Path logDir, int writeExplain, boolean throwException) throws IOException { - var list = listFiles(logDir); - assertEquals(2, list.size()); // createのトランザクションとDMLのトランザクションの2つ - - { - var list1 = listFiles(list.get(0)); - int logCount = 0; - int explainCount = 0; - for (var path : list1) { - String fileName = path.getFileName().toString(); - if (fileName.endsWith(".log")) { - logCount++; - String log1 = Files.readString(path, StandardCharsets.UTF_8); - assertContains(CREATE_TEST_SQL, log1); - } else if (fileName.endsWith(".json")) { - explainCount++; - } else { - fail(path.toString()); - } - } - - assertEquals(1, logCount); - if ((writeExplain & TsurugiSessionTxFileLogConfig.EXPLAIN_FILE) != 0) { - assertEquals(0, explainCount); // TODO explainCount==1 - } else { - assertEquals(0, explainCount); - } - } - { - var list2 = listFiles(list.get(1)); - int logCount = 0; - int explainCount = 0; - for (var path : list2) { - String fileName = path.getFileName().toString(); - if (fileName.endsWith(".log")) { - logCount++; - String log2 = Files.readString(path, StandardCharsets.UTF_8); - assertContains(INSERT_SQL, log2); - assertContains(SELECT_SQL, log2); - if (throwException) { - assertContains("rollback end", log2); - assertContains(ERROR_MESSAGE, log2); - } else { - assertContains("commit end", log2); - } - } else if (fileName.endsWith(".json")) { - explainCount++; - } else { - fail(path.toString()); - } - } - - assertEquals(1, logCount); - if ((writeExplain & TsurugiSessionTxFileLogConfig.EXPLAIN_FILE) != 0) { - assertEquals(2, explainCount); // TODO explainCount==4 - } else { - assertEquals(0, explainCount); - } - } - } - - private static List listFiles(Path dir) throws IOException { - try (var stream = Files.list(dir)) { - var list = stream.collect(Collectors.toList()); - Collections.sort(list); - return list; - } - } - - private void deleteDir(Path dir) { - try { - Files.walkFileTree(dir, new SimpleFileVisitor<>() { - @Override - public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { - Files.delete(file); - return FileVisitResult.CONTINUE; - } - - @Override - public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException { - Files.delete(dir); - return FileVisitResult.CONTINUE; - } - }); - } catch (Exception e) { - LOG.warn("delete temp-dir error", e); - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/sql/DbBindVariableTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/sql/DbBindVariableTest.java deleted file mode 100755 index 43bde5f4..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/sql/DbBindVariableTest.java +++ /dev/null @@ -1,184 +0,0 @@ -package com.tsurugidb.iceaxe.test.sql; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import java.io.IOException; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; - -import com.tsurugidb.iceaxe.sql.TgDataType; -import com.tsurugidb.iceaxe.sql.parameter.TgBindParameters; -import com.tsurugidb.iceaxe.sql.parameter.TgBindVariable; -import com.tsurugidb.iceaxe.sql.parameter.TgBindVariables; -import com.tsurugidb.iceaxe.sql.parameter.TgParameterMapping; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.test.util.TestEntity; - -/** - * bind variable test - */ -class DbBindVariableTest extends DbTestTableTester { - - private static final int SIZE = 4; - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTestTable(); - insertTestTable(SIZE); - - logInitEnd(info); - } - - @Test - void bindInsert() throws Exception { - bindInsert(":foo", ":bar", ":zzz"); - } - - @Test - void bindColonlessInsert1() throws Exception { - bindInsert("f", "b", "z"); - } - - @Test - void bindColonlessInsert2() throws Exception { - bindInsert("foo", "bar", "zzz"); - } - - private void bindInsert(String f, String b, String z) throws IOException, InterruptedException { - var sql = "insert into " + TEST // - + "(" + TEST_COLUMNS + ")" // - + "values(" + f + ", " + b + ", " + z + ")"; - var parameterMapping = TgParameterMapping.of(TestEntity.class) // - .addInt(trimColumn(f), TestEntity::getFoo) // - .addLong(trimColumn(b), TestEntity::getBar) // - .addString(trimColumn(z), TestEntity::getZzz); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(sql, parameterMapping)) { - var entity = createTestEntity(SIZE); - tm.executeAndGetCount(ps, entity); - } - - assertEqualsTestTable(SIZE + 1); - } - - @Test - void insertEntityWithBindVariable() throws Exception { - var foo = TgBindVariable.ofInt("foo"); - var bar = TgBindVariable.ofLong("bar"); - var zzz = TgBindVariable.ofString("zzz"); - var sql = "insert into " + TEST // - + "(" + TEST_COLUMNS + ")" // - + "values(" + TgBindVariables.toSqlNames(foo, bar, zzz) + ")"; - var parameterMapping = TgParameterMapping.of(TestEntity.class) // - .add(foo, TestEntity::getFoo) // - .add(bar, TestEntity::getBar) // - .add(zzz, TestEntity::getZzz); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(sql, parameterMapping)) { - var entity = createTestEntity(SIZE); - tm.executeAndGetCount(ps, entity); - } - - assertEqualsTestTable(SIZE + 1); - } - - @Test - void bindCSelect() throws Exception { - bindSelect(":bar"); - } - - @Test - void bindColonlessSelect1() throws Exception { - bindSelect("b"); - } - - @Test - void bindColonlessSelect2() throws Exception { - bindSelect("bar"); - } - - private void bindSelect(String b) throws IOException, InterruptedException { - var bar = TgBindVariable.ofInt(trimColumn(b)); - var sql = SELECT_SQL + " where foo=" + b; - var parameterMapping = TgParameterMapping.of(bar); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql, parameterMapping, SELECT_MAPPING)) { - int key = 2; - var parameter = TgBindParameters.of(bar.bind(key)); - var list = tm.executeAndGetList(ps, parameter); - - if (b.equals("bar")) { - assertEquals(SIZE, list.size()); - } else { - assertEquals(1, list.size()); - assertEquals(createTestEntity(key), list.get(0)); - } - } - } - - private static String trimColumn(String s) { - if (s.startsWith(":")) { - return s.substring(1); - } - return s; - } - - @Test - void singleVariable_class() throws Exception { - singleVariable(TgParameterMapping.ofSingle("foo", int.class)); - } - - @Test - void singleVariable_type() throws Exception { - singleVariable(TgParameterMapping.ofSingle("foo", TgDataType.INT)); - } - - private void singleVariable(TgParameterMapping parameterMapping) throws IOException, InterruptedException { - var sql = SELECT_SQL + " where foo = :foo"; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql, parameterMapping, SELECT_MAPPING)) { - int key = 2; - var list = tm.executeAndGetList(ps, key); - - assertEquals(1, list.size()); - assertEquals(createTestEntity(key), list.get(0)); - } - } - - @Test - void emptyBind() throws Exception { - var parameterMapping = TgParameterMapping.of(); - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(SELECT_SQL, parameterMapping, SELECT_MAPPING)) { - var list = tm.executeAndGetList(ps, TgBindParameters.of()); - - assertEquals(SIZE, list.size()); - } - } - - @Test - void emptyBindNull() throws Exception { - var parameterMapping = TgParameterMapping.of(); - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(SELECT_SQL, parameterMapping, SELECT_MAPPING)) { - var list = tm.executeAndGetList(ps, null); - - assertEquals(SIZE, list.size()); - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/sql/DbPsExecuteErrorTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/sql/DbPsExecuteErrorTest.java deleted file mode 100755 index 4a7a5a58..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/sql/DbPsExecuteErrorTest.java +++ /dev/null @@ -1,112 +0,0 @@ -package com.tsurugidb.iceaxe.test.sql; - -import static org.junit.jupiter.api.Assertions.assertThrowsExactly; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; - -import com.tsurugidb.iceaxe.exception.TsurugiIOException; -import com.tsurugidb.iceaxe.sql.parameter.TgBindParameters; -import com.tsurugidb.iceaxe.sql.parameter.TgBindVariable; -import com.tsurugidb.iceaxe.sql.parameter.TgParameterMapping; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.transaction.manager.exception.TsurugiTmIOException; -import com.tsurugidb.tsubakuro.sql.SqlServiceCode; - -/** - * PreparedStatement execute error test - */ -class DbPsExecuteErrorTest extends DbTestTableTester { - - private static final int SIZE = 4; - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTestTable(); - insertTestTable(SIZE); - - logInitEnd(info); - } - - @Test - void intsertByExecuteQuery() throws Exception { - var sql = "insert into " + TEST // - + "(" + TEST_COLUMNS + ")" // - + "values(123, 456, 'abc')"; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery(sql)) { - var e = assertThrowsExactly(TsurugiTmIOException.class, () -> { - tm.executeAndGetList(ps); - }); - assertEqualsCode(SqlServiceCode.INCONSISTENT_STATEMENT_EXCEPTION, e); - } - - assertEqualsTestTable(SIZE); - } - - @Test - void selectByExecuteStatement() throws Exception { - var sql = "select * from " + TEST; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(sql)) { - int count = tm.executeAndGetCount(ps); - assertUpdateCount(0, count); // TODO 0? - } - } - - @Test - void insertParameterSizeUnmatch() throws Exception { - var foo = TgBindVariable.ofInt("foo"); - var bar = TgBindVariable.ofLong("bar"); - var zzz = TgBindVariable.ofString("zzz"); - var sql = "insert into " + TEST // - + "(" + TEST_COLUMNS + ")" // - + "values(" + foo + ", " + bar + ", " + zzz + ")"; - var parameterMapping = TgParameterMapping.of(foo, bar, zzz); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(sql, parameterMapping)) { - var e = assertThrowsExactly(TsurugiTmIOException.class, () -> { - var parameter = TgBindParameters.of(foo.bind(123), bar.bind(456) /* ,zzz */); - tm.executeAndGetCount(ps, parameter); - }); - assertEqualsCode(SqlServiceCode.UNRESOLVED_PLACEHOLDER_EXCEPTION, e); - assertContains("Value is not assigned for host variable 'zzz'.", e.getMessage()); - } - - assertEqualsTestTable(SIZE); - } - - @Test - void insertParameterTypeMismatch() throws Exception { - var foo = TgBindVariable.ofLong("foo"); // INT4 <-> Int8 - var bar = TgBindVariable.ofInt("bar"); // INT8 <-> Int4 - var zzz = TgBindVariable.ofInt("zzzi"); // CHARACTER <-> Int4 - var sql = "insert into " + TEST // - + "(" + TEST_COLUMNS + ")" // - + "values(" + foo + ", " + bar + ", " + zzz + ")"; - var parameterMapping = TgParameterMapping.of(foo, bar, zzz); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(sql, parameterMapping)) { - var e = assertThrowsExactly(TsurugiIOException.class, () -> { - var parameter = TgBindParameters.of(foo.bind(123), bar.bind(456), zzz.bind(789)); - tm.executeAndGetCount(ps, parameter); - }); - assertEqualsCode(SqlServiceCode.TYPE_ANALYZE_EXCEPTION, e); - assertContains("compile failed with error:inconsistent_type message:\"int4() (expected: {character_string})\" location:(unknown)", e.getMessage()); // TODO カラム名の確認 - } - - assertEqualsTestTable(SIZE); - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/sql/DbQueryCommentTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/sql/DbQueryCommentTest.java deleted file mode 100755 index 55a821f5..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/sql/DbQueryCommentTest.java +++ /dev/null @@ -1,87 +0,0 @@ -package com.tsurugidb.iceaxe.test.sql; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import java.io.IOException; -import java.util.List; - -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.TestInfo; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; -import org.slf4j.LoggerFactory; - -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; - -/** - * query comment test - */ -class DbQueryCommentTest extends DbTestTableTester { - - private static final int SIZE = 4; - - @BeforeAll - static void beforeAll(TestInfo info) throws Exception { - var LOG = LoggerFactory.getLogger(DbQueryCommentTest.class); - logInitStart(LOG, info); - - dropTestTable(); - createTestTable(); - insertTestTable(SIZE); - - logInitEnd(LOG, info); - } - - @ParameterizedTest - @ValueSource(booleans = { false, true }) - void comment(boolean prepared) throws Exception { - var list1 = List.of("", "-- comment1\n", "/*comment1*/"); - var list2 = List.of("", "-- comment2\n", "/*comment2*/"); - var list3 = List.of("", "-- comment3", "-- comment3\n", "/*comment3*/"); - for (var c1 : list1) { - for (var c2 : list2) { - for (var c3 : list3) { - var sql = c1 // - + "select * from " + TEST + "\n" // - + c2 // - + "order by foo\n" // - + c3; - try { - test(sql, prepared); - } catch (Throwable e) { - LOG.error("sql=[{}]", sql, e); - throw e; - } - } - } - } - } - - private void test(String sql, boolean prepared) throws IOException, InterruptedException { - var session = getSession(); - var tm = createTransactionManagerOcc(session); - if (prepared) { - try (var ps = session.createQuery(sql, SELECT_MAPPING)) { - var list = tm.executeAndGetList(ps); - assertEquals(SIZE, list.size()); - for (int i = 0; i < SIZE; i++) { - var expected = createTestEntity(i); - var actual = list.get(i); - assertEquals(expected, actual); - } - } - } else { - try (var ps = session.createQuery(sql)) { - var list = tm.executeAndGetList(ps); - assertEquals(SIZE, list.size()); - for (int i = 0; i < SIZE; i++) { - var expected = createTestEntity(i); - var actual = list.get(i); - assertEquals(expected.getFoo(), actual.getInt("foo")); - assertEquals(expected.getBar(), actual.getLong("bar")); - assertEquals(expected.getZzz(), actual.getString("zzz")); - } - } - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/sql/DbStatementCommentTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/sql/DbStatementCommentTest.java deleted file mode 100755 index 04e87acd..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/sql/DbStatementCommentTest.java +++ /dev/null @@ -1,64 +0,0 @@ -package com.tsurugidb.iceaxe.test.sql; - -import java.io.IOException; -import java.util.List; - -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; - -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; - -/** - * statement comment test - */ -class DbStatementCommentTest extends DbTestTableTester { - - @ParameterizedTest - @ValueSource(booleans = { false, true }) - void comment(boolean prepared) throws Exception { - var list1 = List.of("", "-- comment1\n", "/*comment1*/"); - var list2 = List.of("", "-- comment2\n", "/*comment2*/"); - var list3 = List.of("", "-- comment3", "-- comment3\n", "/*comment3*/"); - for (var c1 : list1) { - for (var c2 : list2) { - for (var c3 : list3) { - dropTestTable(); - createTestTable(); - - var sql = c1 // - + "insert into " + TEST + "(foo, bar, zzz)\n" // - + c2 // - + "values(:foo, :bar, :zzz)\n" // - + c3; - try { - test(sql, prepared); - } catch (Throwable e) { - LOG.error("sql=[{}]", sql, e); - throw e; - } - } - } - } - } - - private void test(String sql, boolean prepared) throws IOException, InterruptedException { - var session = getSession(); - var tm = createTransactionManagerOcc(session); - var entity = createTestEntity(0); - if (prepared) { - try (var ps = session.createStatement(sql, INSERT_MAPPING)) { - int count = tm.executeAndGetCount(ps, entity); - assertUpdateCount(1, count); - } - } else { - var sqlr = sql.replace(":foo", Integer.toString(entity.getFoo())) // - .replace(":bar", Long.toString(entity.getBar())) // - .replace(":zzz", "'" + entity.getZzz() + "'"); - try (var ps = session.createStatement(sqlr)) { - int count = tm.executeAndGetCount(ps); - assertUpdateCount(1, count); - } - } - assertEqualsTestTable(1); - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/table/DbCreateTableErrorTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/table/DbCreateTableErrorTest.java deleted file mode 100755 index 2b9444f7..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/table/DbCreateTableErrorTest.java +++ /dev/null @@ -1,110 +0,0 @@ -package com.tsurugidb.iceaxe.test.table; - -import static org.junit.jupiter.api.Assertions.assertThrowsExactly; - -import java.io.IOException; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; - -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.transaction.manager.exception.TsurugiTmIOException; -import com.tsurugidb.tsubakuro.sql.SqlServiceCode; - -/** - * create table error test - */ -class DbCreateTableErrorTest extends DbTestTableTester { - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - - logInitEnd(info); - } - - @Test - void columnNotFoundPk() throws Exception { - var sql = "create table " + TEST // - + "(" // - + " foo int," // - + " bar bigint," // - + " zzz varchar(10)," // - + " primary key(goo)" // - + ")"; - var e = executeErrorDdl(sql); - assertEqualsCode(SqlServiceCode.SYMBOL_ANALYZE_EXCEPTION, e); - assertContains("compile failed with error:column_not_found message:\"primary key column \"goo\" is not found\" location:(unknown)", e.getMessage()); - } - - @Test - void duplicatePkDefinition() throws Exception { - var sql = "create table " + TEST // - + "(" // - + " foo int primary key," // - + " bar bigint," // - + " zzz varchar(10)," // - + " primary key(foo)" // - + ")"; - var e = executeErrorDdl(sql); - assertEqualsCode(SqlServiceCode.COMPILE_EXCEPTION, e); - assertContains("compile failed with error:invalid_default_value message:\"primary key definition must be upto one\" location:(unknown)", e.getMessage()); - } - - @Test - void duplicatePk() throws Exception { - var sql = "create table " + TEST // - + "(" // - + " foo int," // - + " bar bigint," // - + " zzz varchar(10)," // - + " primary key(foo, foo)" // - + ")"; - // TODO executeErrorDdl(sql) - executeDdl(getSession(), sql); - } - - @Test - void duplicateColumnName() throws Exception { - for (int i = 0; i <= 0b11; i++) { - var sql = getDuplicateColumnSql(i); - if (sql == null) { - continue; - } - - dropTestTable(); - try { - // TODO executeErrorDdl(sql) - executeDdl(getSession(), sql); - } catch (Throwable e) { - LOG.error("duplicateColumnName fail. ddl={}", sql, e); - throw e; - } - } - } - - private static String getDuplicateColumnSql(int pk) { - boolean pk1 = (pk & 0b1) != 0; - boolean pk2 = (pk & 0b10) != 0; - if (pk1 && pk2) { - return null; - } - - return "create table " + TEST // - + "(" // - + " foo int " + (pk1 ? " primary key" : "") + "," // - + " foo bigint" // - + (pk2 ? ",primary key(foo)" : "") // - + ")"; - } - - private static TsurugiTmIOException executeErrorDdl(String sql) throws IOException { - var tm = createTransactionManagerOcc(getSession()); - return assertThrowsExactly(TsurugiTmIOException.class, () -> { - tm.executeDdl(sql); - }); - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/table/DbCreateTableExplainTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/table/DbCreateTableExplainTest.java deleted file mode 100755 index 61765567..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/table/DbCreateTableExplainTest.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.tsurugidb.iceaxe.test.table; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrowsExactly; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; - -import com.tsurugidb.iceaxe.sql.explain.TgStatementMetadata; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.tsubakuro.explain.PlanGraphException; - -/** - * explain create table test - */ -class DbCreateTableExplainTest extends DbTestTableTester { - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - - logInitEnd(info); - } - - private static final String SQL = "create table " + TEST // - + "(" // - + " foo int," // - + " bar bigint," // - + " zzz varchar(10)," // - + " primary key(foo)" // - + ")"; - - @Test - void create() throws Exception { - var session = getSession(); - var helper = session.getExplainHelper(); - assertThrowsExactly(UnsupportedOperationException.class, () -> { - var result = helper.explain(session, SQL); - assertExplain(result); - }); // TODO explain実装待ち - } - - @Test - void createExists() throws Exception { - createTestTable(); - - var session = getSession(); - var helper = session.getExplainHelper(); - assertThrowsExactly(UnsupportedOperationException.class, () -> { - var result = helper.explain(session, SQL); - assertExplain(result); - }); // TODO explain実装待ち - } - - private static void assertExplain(TgStatementMetadata actual) throws Exception { - assertThrowsExactly(PlanGraphException.class, () -> { - actual.getLowPlanGraph(); - }); - - var list = actual.getLowColumnList(); - assertEquals(0, list.size()); - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/table/DbCreateTableTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/table/DbCreateTableTest.java deleted file mode 100755 index fac58b79..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/table/DbCreateTableTest.java +++ /dev/null @@ -1,196 +0,0 @@ -package com.tsurugidb.iceaxe.test.table; - -import static org.junit.jupiter.api.Assertions.assertThrowsExactly; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.io.IOException; -import java.util.function.Consumer; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; - -import com.tsurugidb.iceaxe.exception.TsurugiIOException; -import com.tsurugidb.iceaxe.sql.parameter.TgBindParameters; -import com.tsurugidb.iceaxe.sql.parameter.TgParameterMapping; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.transaction.exception.TsurugiTransactionException; -import com.tsurugidb.iceaxe.transaction.manager.exception.TsurugiTmIOException; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; -import com.tsurugidb.tsubakuro.sql.SqlServiceCode; - -/** - * create table test - */ -class DbCreateTableTest extends DbTestTableTester { - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - - logInitEnd(info); - } - - private static final String SQL = "create table " + TEST // - + "(" // - + " foo int," // - + " bar bigint," // - + " zzz varchar(10)," // - + " primary key(foo)" // - + ")"; - - @Test - void create() throws Exception { - var session = getSession(); - var tm = createTransactionManagerOcc(session); - tm.executeDdl(SQL); - } - - @Test - void createExists() throws Exception { - createTestTable(); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - var e = assertThrowsExactly(TsurugiTmIOException.class, () -> { - tm.executeDdl(SQL); - }); - assertEqualsCode(SqlServiceCode.COMPILE_EXCEPTION, e); - assertContains("compile failed with error:duplicate_table message:\"table `" + TEST + "' is already defined\" location:(unknown)", e.getMessage()); - } - - @Test - void alreadyExists() throws Exception { - var session = getSession(); - var txOption = TgTxOption.ofDDL(); - - // preparedStatementを作る際にテーブルが存在していないので、ERR_COMPILER_ERRORにならない - try (var ps = session.createStatement(CREATE_TEST_SQL, TgParameterMapping.of())) { - Thread.sleep(100); // preparedStatement作成がDBサーバー側で処理されるのを待つ - // テーブルを作成する - createTestTable(); - - // テーブルが作られた後にpreparedStatementのDDLを実行するとERR_ALREADY_EXISTSになる - try (var transaction = session.createTransaction(txOption)) { - var e = assertThrowsExactly(TsurugiTransactionException.class, () -> transaction.executeAndGetCount(ps, TgBindParameters.of())); - assertEqualsCode(SqlServiceCode.TARGET_ALREADY_EXISTS_EXCEPTION, e); - transaction.rollback(); - } - } - - try (var ps = session.createStatement(CREATE_TEST_SQL, TgParameterMapping.of())) { - try (var transaction = session.createTransaction(txOption)) { - var e = assertThrowsExactly(TsurugiIOException.class, () -> transaction.executeAndGetCount(ps, TgBindParameters.of())); - assertEqualsCode(SqlServiceCode.COMPILE_EXCEPTION, e); - assertContains("compile failed with error:duplicate_table message:\"table `" + TEST + "' is already defined\" location:(unknown)", e.getMessage()); - transaction.rollback(); - } - } - } - - @Test - void rollback() throws Exception { - var session = getSession(); - assertTrue(session.findTableMetadata(TEST).isEmpty()); - - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(SQL)) { - tm.execute(transaction -> { - transaction.executeAndGetCount(ps); - assertTrue(session.findTableMetadata(TEST).isPresent()); - transaction.rollback(); - assertTrue(session.findTableMetadata(TEST).isPresent()); - }); - } - - assertTrue(session.findTableMetadata(TEST).isPresent()); - } - - @ParameterizedTest - @ValueSource(booleans = { true, false }) - void repeatOcc(boolean hasPk) throws Exception { - repeat(TgTxOption.ofOCC(), hasPk, null); - } - - @ParameterizedTest - @ValueSource(booleans = { true, false }) - void repeatLtx(boolean hasPk) throws Exception { - Consumer error = null; - if (!hasPk) { - error = e -> { - assertEqualsCode(SqlServiceCode.LTX_WRITE_OPERATION_WITHOUT_WRITE_PRESERVE_EXCEPTION, e); - assertContains("Ltx write operation outside write preserve", e.getMessage()); // TODO エラー詳細情報(テーブル名) - }; - } - repeat(TgTxOption.ofLTX().includeDdl(false), hasPk, error); - } - - @ParameterizedTest - @ValueSource(booleans = { true, false }) - void repeatDdl(boolean hasPk) throws Exception { - repeat(TgTxOption.ofDDL(), hasPk, null); - } - - private void repeat(TgTxOption txOption, boolean hasPk, Consumer error) throws IOException, InterruptedException { - var createDdl = "create table " + TEST // - + "(" // - + " foo int," // - + " bar bigint," // - + " zzz varchar(10)" // - + (hasPk ? ", primary key(foo)" : "") // - + ")"; - var dropDdl = "drop table " + TEST; - - var session = getSession(); - var tm = session.createTransactionManager(txOption); - for (int i = 0; i < 100; i++) { - if (session.findTableMetadata(TEST).isPresent()) { - tm.executeDdl(dropDdl); - } - - if (error == null) { - tm.executeDdl(createDdl); - assertTrue(session.findTableMetadata(TEST).isPresent()); - } else { - var e = assertThrowsExactly(TsurugiTmIOException.class, () -> { - tm.executeDdl(createDdl); - }); - error.accept(e); - assertTrue(session.findTableMetadata(TEST).isEmpty()); - } - } - } - - @Test - void createLtxNoPk() throws Exception { - var createDdl = "create table " + TEST // - + "(" // - + " foo int," // - + " bar bigint," // - + " zzz varchar(10)" // - + ")"; - - var session = getSession(); - var tm = session.createTransactionManager(TgTxOption.ofLTX().includeDdl(false)); - var e = assertThrowsExactly(TsurugiTmIOException.class, () -> { - tm.execute(transaction -> { - var e1 = assertThrowsExactly(TsurugiTransactionException.class, () -> { - transaction.executeDdl(createDdl); - }); - assertEqualsCode(SqlServiceCode.LTX_WRITE_OPERATION_WITHOUT_WRITE_PRESERVE_EXCEPTION, e1); - assertContains("Ltx write operation outside write preserve", e1.getMessage()); // TODO エラー詳細情報(テーブル名) - - var e2 = assertThrowsExactly(TsurugiTransactionException.class, () -> { - transaction.executeDdl(createDdl); - }); - assertEqualsCode(SqlServiceCode.INACTIVE_TRANSACTION_EXCEPTION, e2); - assertContains("Current transaction is inactive (maybe aborted already.)", e2.getMessage()); - }); - }); - assertEqualsCode(SqlServiceCode.INACTIVE_TRANSACTION_EXCEPTION, e); - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/table/DbDropTableExplainTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/table/DbDropTableExplainTest.java deleted file mode 100755 index c1ee8a07..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/table/DbDropTableExplainTest.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.tsurugidb.iceaxe.test.table; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrowsExactly; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; - -import com.tsurugidb.iceaxe.sql.explain.TgStatementMetadata; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.tsubakuro.explain.PlanGraphException; - -/** - * explain drop table test - */ -class DbDropTableExplainTest extends DbTestTableTester { - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - - logInitEnd(info); - } - - private static final String SQL = "drop table " + TEST; - - @Test - void drop() throws Exception { - createTestTable(); - - var session = getSession(); - var helper = session.getExplainHelper(); - assertThrowsExactly(UnsupportedOperationException.class, () -> { - var result = helper.explain(session, SQL); - assertExplain(result); - }); // TODO explain実装待ち - } - - @Test - void dropNotExists() throws Exception { - var session = getSession(); - var helper = session.getExplainHelper(); - assertThrowsExactly(UnsupportedOperationException.class, () -> { - var result = helper.explain(session, SQL); - assertExplain(result); - }); // TODO explain実装待ち - } - - private static void assertExplain(TgStatementMetadata actual) throws Exception { - assertThrowsExactly(PlanGraphException.class, () -> { - actual.getLowPlanGraph(); - }); - - var list = actual.getLowColumnList(); - assertEquals(0, list.size()); - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/table/DbDropTableTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/table/DbDropTableTest.java deleted file mode 100755 index faa0989b..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/table/DbDropTableTest.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.tsurugidb.iceaxe.test.table; - -import static org.junit.jupiter.api.Assertions.assertThrowsExactly; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; - -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.transaction.manager.exception.TsurugiTmIOException; -import com.tsurugidb.tsubakuro.sql.SqlServiceCode; - -/** - * drop table test - */ -class DbDropTableTest extends DbTestTableTester { - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - - logInitEnd(info); - } - - private static final String SQL = "drop table " + TEST; - - @Test - void drop() throws Exception { - createTestTable(); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(SQL)) { - tm.executeAndGetCount(ps); - } - } - - @Test - void dropNotFound() throws Exception { - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(SQL)) { - var e = assertThrowsExactly(TsurugiTmIOException.class, () -> { - tm.executeAndGetCount(ps); - }); - assertEqualsCode(SqlServiceCode.SYMBOL_ANALYZE_EXCEPTION, e); - assertContains("compile failed with error:table_not_found message:\"table `" + TEST + "' is not found\" location:(unknown). TsurugiTransaction(OCC{}, iceaxeTxId=", e.getMessage()); - } - } - - @Test - void rollback() throws Exception { - createTestTable(); - - var session = getSession(); - assertTrue(session.findTableMetadata(TEST).isPresent()); - - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(SQL)) { - tm.execute(transaction -> { - transaction.executeAndGetCount(ps); - assertTrue(session.findTableMetadata(TEST).isEmpty()); - transaction.rollback(); - assertTrue(session.findTableMetadata(TEST).isEmpty()); - }); - } - - assertTrue(session.findTableMetadata(TEST).isEmpty()); - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/table/DbTableListTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/table/DbTableListTest.java deleted file mode 100755 index 63bdf44b..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/table/DbTableListTest.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.tsurugidb.iceaxe.test.table; - -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.util.List; - -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.slf4j.LoggerFactory; - -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; - -/** - * table list test - */ -class DbTableListTest extends DbTestTableTester { - - private static final String TEST2 = "test2"; - - @BeforeAll - static void beforeAll(TestInfo info) throws Exception { - var LOG = LoggerFactory.getLogger(DbTableListTest.class); - logInitStart(LOG, info); - - dropTestTable(); - dropTable(TEST2); - - logInitEnd(LOG, info); - } - - @Test - void getTableNameList() throws Exception { - var session = getSession(); - { - List tableList = session.getTableNameList(); - assertFalse(tableList.contains(TEST)); - assertFalse(tableList.contains(TEST2)); - } - { - createTestTable(); - - List tableList = session.getTableNameList(); - assertTrue(tableList.contains(TEST)); - assertFalse(tableList.contains(TEST2)); - } - { - executeDdl(session, CREATE_TEST_SQL.replace(TEST, TEST2)); - - List tableList = session.getTableNameList(); - assertTrue(tableList.contains(TEST)); - assertTrue(tableList.contains(TEST2)); - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/table/DbTableMetadataTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/table/DbTableMetadataTest.java deleted file mode 100755 index d2501d9a..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/table/DbTableMetadataTest.java +++ /dev/null @@ -1,92 +0,0 @@ -package com.tsurugidb.iceaxe.test.table; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; - -import com.tsurugidb.iceaxe.sql.TgDataType; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.sql.proto.SqlCommon; - -/** - * table metadata test - */ -class DbTableMetadataTest extends DbTestTableTester { - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - - logInitEnd(info); - } - - @Test - void found() throws Exception { - var session = getSession(); - { - var sql = "create table " + TEST // - + "(" // -// + " bool boolean," // - + " int4 int," // - + " long bigint," // - + " float4 real," // - + " double8 double," // - + " decimal10_2 decimal(10, 2)," // - + " ftext char(10)," // - + " vtext varchar(10)," // - + " fbytes binary(10)," // - + " vbytes varbinary(10)," // - + " date1 date," // - + " time1 time," // - + " date_time timestamp," // - + " offset_time time with time zone," // - + " offset_date_time timestamp with time zone," // - + " primary key(int4)" // - + ")"; - executeDdl(session, sql); - } - - var metadata = session.findTableMetadata(TEST).get(); - assertNull(metadata.getDatabaseName()); - assertNull(metadata.getSchemaName()); - assertEquals(TEST, metadata.getTableName()); - - var columnList = metadata.getLowColumnList(); - assertEquals(14, columnList.size()); - int i = 0; - assertColumn("int4", TgDataType.INT, columnList.get(i++)); - assertColumn("long", TgDataType.LONG, columnList.get(i++)); - assertColumn("float4", TgDataType.FLOAT, columnList.get(i++)); - assertColumn("double8", TgDataType.DOUBLE, columnList.get(i++)); - assertColumn("decimal10_2", TgDataType.DECIMAL, columnList.get(i++)); - assertColumn("ftext", TgDataType.STRING, columnList.get(i++)); - assertColumn("vtext", TgDataType.STRING, columnList.get(i++)); - assertColumn("fbytes", TgDataType.BYTES, columnList.get(i++)); - assertColumn("vbytes", TgDataType.BYTES, columnList.get(i++)); - assertColumn("date1", TgDataType.DATE, columnList.get(i++)); - assertColumn("time1", TgDataType.TIME, columnList.get(i++)); - assertColumn("date_time", TgDataType.DATE_TIME, columnList.get(i++)); - assertColumn("offset_time", TgDataType.TIME, columnList.get(i++)); -//TODO assertColumn("offset_time", TgDataType.OFFSET_TIME, columnList.get(i++)); - assertColumn("offset_date_time", TgDataType.DATE_TIME, columnList.get(i++)); -//TODO assertColumn("offset_date_time", TgDataType.OFFSET_DATE_TIME, columnList.get(i++)); - } - - private static void assertColumn(String name, TgDataType type, SqlCommon.Column column) { - assertEquals(name, column.getName()); - assertEquals(type.getLowDataType(), column.getAtomType()); - } - - @Test - void notFound() throws Exception { - var session = getSession(); - var metadataOpt = session.findTableMetadata(TEST); - assertTrue(metadataOpt.isEmpty()); - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbServerStopSessionTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbServerStopSessionTest.java deleted file mode 100755 index 9cb40f13..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbServerStopSessionTest.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.tsurugidb.iceaxe.test.timeout; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrowsExactly; - -import java.io.IOException; -import java.util.concurrent.TimeUnit; - -import org.junit.jupiter.api.RepeatedTest; -import org.junit.jupiter.api.Timeout; -import org.opentest4j.AssertionFailedError; - -import com.tsurugidb.iceaxe.TsurugiConnector; -import com.tsurugidb.iceaxe.session.TgSessionOption; -import com.tsurugidb.iceaxe.session.TgSessionOption.TgTimeoutKey; -import com.tsurugidb.iceaxe.session.TsurugiSession; - -/** - * server stop (session) test - */ -public class DbServerStopSessionTest extends DbTimetoutTest { - - private static final int EXPECTED_TIMEOUT = 1; - - // サーバーが停止した場合に即座にエラーが返ることを確認するテスト - @RepeatedTest(6) - @Timeout(value = EXPECTED_TIMEOUT, unit = TimeUnit.SECONDS) - void serverStop() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifySessionInfo(TgSessionOption sessionOption) { - sessionOption.setTimeout(TgTimeoutKey.DEFAULT, EXPECTED_TIMEOUT + 1, TimeUnit.SECONDS); - } - }); - } - - @Override - protected TsurugiSession createSession(PipeServerThtread pipeServer, TsurugiConnector connector, TimeoutModifier modifier) throws IOException { - pipeServer.setPipeWrite(false); - var session = super.createSession(pipeServer, connector, modifier); - pipeServer.close(); // server stop - return session; - } - - @Override - protected void clientTask(PipeServerThtread pipeServer, TsurugiSession session, TimeoutModifier modifier) throws Exception { - var e = assertThrowsExactly(IOException.class, () -> { - session.getLowSqlClient(); - }); - try { - assertEquals("Server crashed", e.getMessage()); - } catch (AssertionFailedError t) { - throw e; - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbServerStopTransactionTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbServerStopTransactionTest.java deleted file mode 100755 index 5c0e5b30..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbServerStopTransactionTest.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.tsurugidb.iceaxe.test.timeout; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrowsExactly; -import static org.junit.jupiter.api.Assertions.fail; - -import java.io.IOException; -import java.util.concurrent.TimeUnit; - -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.Timeout; - -import com.tsurugidb.iceaxe.session.TgSessionOption; -import com.tsurugidb.iceaxe.session.TgSessionOption.TgTimeoutKey; -import com.tsurugidb.iceaxe.session.TsurugiSession; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; - -/** - * server stop (transaction) test - */ -public class DbServerStopTransactionTest extends DbTimetoutTest { - - private static final int EXPECTED_TIMEOUT = 1; - - // サーバーが停止した場合に即座にエラーが返ることを確認するテスト - @Test - @Timeout(value = EXPECTED_TIMEOUT, unit = TimeUnit.SECONDS) - void serverStop() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifySessionInfo(TgSessionOption sessionOption) { - sessionOption.setTimeout(TgTimeoutKey.DEFAULT, EXPECTED_TIMEOUT + 1, TimeUnit.SECONDS); - } - }); - } - - @Override - protected void clientTask(PipeServerThtread pipeServer, TsurugiSession session, TimeoutModifier modifier) throws Exception { - session.getLowSqlClient(); - - pipeServer.setPipeWrite(false); - try (var transaction = session.createTransaction(TgTxOption.ofOCC())) { - pipeServer.close(); // server stop - - try { - transaction.getLowTransaction(); - } catch (IOException e) { - assertEquals("Server crashed", e.getMessage()); - return; - } finally { - pipeServer.setPipeWrite(true); - - var e = assertThrowsExactly(IOException.class, () -> { - session.close(); - }); - assertEquals("socket is already closed", e.getMessage()); - } - fail("didn't time out"); - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbSlotLimitTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbSlotLimitTest.java deleted file mode 100755 index e5194db1..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbSlotLimitTest.java +++ /dev/null @@ -1,98 +0,0 @@ -package com.tsurugidb.iceaxe.test.timeout; - -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.TimeUnit; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; - -import com.tsurugidb.iceaxe.session.TsurugiSession; -import com.tsurugidb.iceaxe.sql.TsurugiSqlQuery; -import com.tsurugidb.iceaxe.sql.result.TsurugiQueryResult; -import com.tsurugidb.iceaxe.sql.result.TsurugiResultEntity; -import com.tsurugidb.iceaxe.transaction.TsurugiTransaction; -import com.tsurugidb.iceaxe.transaction.exception.TsurugiTransactionException; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; -import com.tsurugidb.tsubakuro.channel.common.connection.wire.impl.ResponseBox; -import com.tsurugidb.tsubakuro.exception.ResponseTimeoutException; - -/** - * slot limit test - */ -public class DbSlotLimitTest extends DbTimetoutTest { - - private static final int ATTEMPT_SIZE = ResponseBox.responseBoxSize() + 100; - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTestTable(); - - logInitEnd(info); - } - - @Test - void slotLimit() throws Exception { - testTimeout(new TimeoutModifier()); - } - - @Override - protected void clientTask(PipeServerThtread pipeServer, TsurugiSession session, TimeoutModifier modifier) throws Exception { - var transaction = session.createTransaction(TgTxOption.ofOCC()); - try { - transaction.setCloseTimeout(1, TimeUnit.SECONDS); // TODO 本来はトランザクションはタイムアウトせず正常にクローズできて欲しい - transaction.getLowTransaction(); - - try (var ps = session.createQuery(SELECT_SQL)) { - var resultList = new ArrayList>(); - - pipeServer.setPipeWrite(false); - try { - execute(transaction, ps, resultList); - } finally { - pipeServer.setPipeWrite(true); - } - - int i = 0; - for (var result : resultList) { - LOG.trace("close i={}", i); - try { - result.close(); - } catch (ResponseTimeoutException e) { - // success - } - i++; - } - } - } finally { - try { - transaction.close(); - } catch (ResponseTimeoutException e) { - // TODO 本来はタイムアウトせず正常にクローズできて欲しい - LOG.warn("transaction.close() {}", e.getClass().getName()); - } - } - } - - private void execute(TsurugiTransaction transaction, TsurugiSqlQuery ps, List> resultList) - throws IOException, InterruptedException, TsurugiTransactionException { - for (int i = 0; i < ATTEMPT_SIZE; i++) { - LOG.trace("i={}", i); - try { - var result = ps.execute(transaction); - result.setRsConnectTimeout(1, TimeUnit.MILLISECONDS); - result.setRsCloseTimeout(1, TimeUnit.MILLISECONDS); - resultList.add(result); - } catch (Throwable t) { - LOG.error("excption occurred. i={}", i, t); - throw t; - } - } -// fail("slot limit over did not occur"); - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutExplainCloseTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutExplainCloseTest.java deleted file mode 100755 index 518cc1ce..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutExplainCloseTest.java +++ /dev/null @@ -1,127 +0,0 @@ -package com.tsurugidb.iceaxe.test.timeout; - -import java.io.IOException; -import java.util.List; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; - -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.slf4j.LoggerFactory; - -import com.tsurugidb.iceaxe.session.TgSessionOption; -import com.tsurugidb.iceaxe.session.TgSessionOption.TgTimeoutKey; -import com.tsurugidb.iceaxe.session.TsurugiSession; -import com.tsurugidb.iceaxe.sql.TsurugiSqlPrepared; -import com.tsurugidb.iceaxe.sql.explain.TsurugiExplainHelper; -import com.tsurugidb.iceaxe.sql.parameter.TgBindParameters; -import com.tsurugidb.iceaxe.sql.parameter.TgParameterMapping; -import com.tsurugidb.sql.proto.SqlRequest.Parameter; -import com.tsurugidb.tsubakuro.exception.ServerException; -import com.tsurugidb.tsubakuro.sql.PreparedStatement; -import com.tsurugidb.tsubakuro.sql.SqlClient; -import com.tsurugidb.tsubakuro.sql.StatementMetadata; -import com.tsurugidb.tsubakuro.util.FutureResponse; - -/** - * explain close timeout test - */ -public class DbTimeoutExplainCloseTest extends DbTimetoutTest { - - @BeforeAll - static void beforeAll(TestInfo info) throws Exception { - var LOG = LoggerFactory.getLogger(DbTimeoutExplainCloseTest.class); - logInitStart(LOG, info); - - dropTestTable(); - createTestTable(); - - logInitEnd(LOG, info); - } - - @Test - void timeoutDefault() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifySessionInfo(TgSessionOption sessionOption) { - sessionOption.setTimeout(TgTimeoutKey.DEFAULT, 1, TimeUnit.SECONDS); - } - }); - } - - @Test - void timeoutSpecified() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifySessionInfo(TgSessionOption sessionOption) { - sessionOption.setTimeout(TgTimeoutKey.EXPLAIN_CLOSE, 1, TimeUnit.SECONDS); - } - }); - } - - @Test - void timeoutSet() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifyPs(TsurugiSqlPrepared ps) { - ps.setExplainCloseTimeout(1, TimeUnit.SECONDS); - } - }); - } - - @Override - protected void clientTask(PipeServerThtread pipeServer, TsurugiSession session, TimeoutModifier modifier) throws Exception { - var helper = new TsurugiExplainHelper() { - @Override - protected FutureResponse explainLow(SqlClient lowSqlClient, PreparedStatement lowPs, List lowParameter) throws IOException { - var future = super.explainLow(lowSqlClient, lowPs, lowParameter); - return new FutureResponse() { - @Override - public boolean isDone() { - return future.isDone(); - } - - @Override - public StatementMetadata get() throws IOException, ServerException, InterruptedException { - throw new UnsupportedOperationException("do not use"); - } - - @Override - public StatementMetadata get(long timeout, TimeUnit unit) throws IOException, ServerException, InterruptedException, TimeoutException { - return future.get(timeout, unit); - } - - @Override - public void close() throws IOException, ServerException, InterruptedException { - pipeServer.setPipeWrite(false); - try { - future.close(); - } finally { - pipeServer.setPipeWrite(true); - } - } - }; - } - }; - session.setExplainHelper(helper); - - var sql = "select * from " + TEST; - var parameterMapping = TgParameterMapping.of(); - try (var ps = session.createQuery(sql, parameterMapping)) { - session.setExplainHelper(helper); - - var parameter = TgBindParameters.of(); - try { - ps.explain(parameter); - } catch (IOException e) { - // EXPLAIN_CLOSEはタイムアウトするような通信処理が無い -// assertInstanceOf(TimeoutException.class, e.getCause()); -// LOG.trace("timeout success"); -// return; - throw e; - } - } -// fail("didn't time out"); - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutExplainConnectTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutExplainConnectTest.java deleted file mode 100755 index f7d0b3bf..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutExplainConnectTest.java +++ /dev/null @@ -1,104 +0,0 @@ -package com.tsurugidb.iceaxe.test.timeout; - -import static org.junit.jupiter.api.Assertions.assertInstanceOf; -import static org.junit.jupiter.api.Assertions.fail; - -import java.io.IOException; -import java.util.List; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; - -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.slf4j.LoggerFactory; - -import com.tsurugidb.iceaxe.session.TgSessionOption; -import com.tsurugidb.iceaxe.session.TgSessionOption.TgTimeoutKey; -import com.tsurugidb.iceaxe.session.TsurugiSession; -import com.tsurugidb.iceaxe.sql.TsurugiSqlPrepared; -import com.tsurugidb.iceaxe.sql.explain.TsurugiExplainHelper; -import com.tsurugidb.iceaxe.sql.parameter.TgBindParameters; -import com.tsurugidb.iceaxe.sql.parameter.TgParameterMapping; -import com.tsurugidb.sql.proto.SqlRequest.Parameter; -import com.tsurugidb.tsubakuro.sql.PreparedStatement; -import com.tsurugidb.tsubakuro.sql.SqlClient; -import com.tsurugidb.tsubakuro.sql.StatementMetadata; -import com.tsurugidb.tsubakuro.util.FutureResponse; - -/** - * explain connect timeout test - */ -public class DbTimeoutExplainConnectTest extends DbTimetoutTest { - - @BeforeAll - static void beforeAll(TestInfo info) throws Exception { - var LOG = LoggerFactory.getLogger(DbTimeoutExplainConnectTest.class); - logInitStart(LOG, info); - - dropTestTable(); - createTestTable(); - - logInitEnd(LOG, info); - } - - @Test - void timeoutDefault() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifySessionInfo(TgSessionOption sessionOption) { - sessionOption.setTimeout(TgTimeoutKey.DEFAULT, 1, TimeUnit.SECONDS); - } - }); - } - - @Test - void timeoutSpecified() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifySessionInfo(TgSessionOption sessionOption) { - sessionOption.setTimeout(TgTimeoutKey.EXPLAIN_CONNECT, 1, TimeUnit.SECONDS); - } - }); - } - - @Test - void timeoutSet() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifyPs(TsurugiSqlPrepared ps) { - ps.setExplainConnectTimeout(1, TimeUnit.SECONDS); - } - }); - } - - @Override - protected void clientTask(PipeServerThtread pipeServer, TsurugiSession session, TimeoutModifier modifier) throws Exception { - var helper = new TsurugiExplainHelper() { - @Override - protected FutureResponse explainLow(SqlClient lowSqlClient, PreparedStatement lowPs, List lowParameter) throws IOException { - pipeServer.setPipeWrite(false); - return super.explainLow(lowSqlClient, lowPs, lowParameter); - } - }; - session.setExplainHelper(helper); - - var sql = "select * from " + TEST; - var parameterMapping = TgParameterMapping.of(); - try (var ps = session.createQuery(sql, parameterMapping)) { - modifier.modifyPs(ps); - - var parameter = TgBindParameters.of(); - try { - ps.explain(parameter); - } catch (IOException e) { - assertInstanceOf(TimeoutException.class, e.getCause()); - LOG.trace("timeout success"); - return; - } finally { - pipeServer.setPipeWrite(true); - } - } - fail("didn't time out"); - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutPsCloseTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutPsCloseTest.java deleted file mode 100755 index d08ef332..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutPsCloseTest.java +++ /dev/null @@ -1,90 +0,0 @@ -package com.tsurugidb.iceaxe.test.timeout; - -import static org.junit.jupiter.api.Assertions.assertInstanceOf; -import static org.junit.jupiter.api.Assertions.fail; - -import java.io.IOException; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; - -import com.tsurugidb.iceaxe.session.TgSessionOption; -import com.tsurugidb.iceaxe.session.TgSessionOption.TgTimeoutKey; -import com.tsurugidb.iceaxe.session.TsurugiSession; -import com.tsurugidb.iceaxe.sql.TsurugiSqlPrepared; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; - -/** - * PreparedStatement close timeout test - */ -public class DbTimeoutPsCloseTest extends DbTimetoutTest { - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTestTable(); - - logInitEnd(info); - } - - @Test - void timeoutDefault() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifySessionInfo(TgSessionOption sessionOption) { - sessionOption.setTimeout(TgTimeoutKey.DEFAULT, 1, TimeUnit.SECONDS); - } - }); - } - - @Test - void timeoutSpecified() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifySessionInfo(TgSessionOption sessionOption) { - sessionOption.setTimeout(TgTimeoutKey.PS_CLOSE, 1, TimeUnit.SECONDS); - } - }); - } - - @Test - void timeoutSet() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifyPs(TsurugiSqlPrepared ps) { - ps.setCloseTimeout(1, TimeUnit.SECONDS); - } - }); - } - - @Override - protected void clientTask(PipeServerThtread pipeServer, TsurugiSession session, TimeoutModifier modifier) throws Exception { - try (var transaction = session.createTransaction(TgTxOption.ofOCC())) { - transaction.getLowTransaction(); - - var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING); - modifier.modifyPs(ps); - - var entity = createTestEntity(0); - try (var result = ps.execute(transaction, entity)) { - } - - pipeServer.setPipeWrite(false); - try { - ps.close(); - } catch (IOException e) { - assertInstanceOf(TimeoutException.class, e.getCause()); - LOG.trace("timeout success"); - return; - } finally { - pipeServer.setPipeWrite(true); - } - fail("didn't time out"); - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutPsConnectTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutPsConnectTest.java deleted file mode 100755 index fd6a3e5e..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutPsConnectTest.java +++ /dev/null @@ -1,90 +0,0 @@ -package com.tsurugidb.iceaxe.test.timeout; - -import static org.junit.jupiter.api.Assertions.assertInstanceOf; -import static org.junit.jupiter.api.Assertions.fail; - -import java.io.IOException; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; - -import com.tsurugidb.iceaxe.session.TgSessionOption; -import com.tsurugidb.iceaxe.session.TgSessionOption.TgTimeoutKey; -import com.tsurugidb.iceaxe.session.TsurugiSession; -import com.tsurugidb.iceaxe.sql.TsurugiSqlPrepared; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; - -/** - * PreparedStatement connect timeout test - */ -public class DbTimeoutPsConnectTest extends DbTimetoutTest { - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTestTable(); - - logInitEnd(info); - } - - @Test - void timeoutDefault() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifySessionInfo(TgSessionOption sessionOption) { - sessionOption.setTimeout(TgTimeoutKey.DEFAULT, 1, TimeUnit.SECONDS); - } - }); - } - - @Test - void timeoutSpecified() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifySessionInfo(TgSessionOption sessionOption) { - sessionOption.setTimeout(TgTimeoutKey.PS_CONNECT, 1, TimeUnit.SECONDS); - } - }); - } - - @Test - void timeoutSet() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifyPs(TsurugiSqlPrepared ps) { - ps.setConnectTimeout(1, TimeUnit.SECONDS); - } - }); - } - - @Override - protected void clientTask(PipeServerThtread pipeServer, TsurugiSession session, TimeoutModifier modifier) throws Exception { - try (var transaction = session.createTransaction(TgTxOption.ofOCC())) { - transaction.getLowTransaction(); - - pipeServer.setPipeWrite(false); - try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING)) { - modifier.modifyPs(ps); - - var entity = createTestEntity(0); - try { - ps.execute(transaction, entity); - } catch (IOException e) { - assertInstanceOf(TimeoutException.class, e.getCause()); - LOG.trace("timeout success"); - return; - } finally { - pipeServer.setPipeWrite(true); - } - fail("didn't time out"); - } finally { - pipeServer.setPipeWrite(true); - } - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutResultCheckTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutResultCheckTest.java deleted file mode 100755 index b47493ab..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutResultCheckTest.java +++ /dev/null @@ -1,94 +0,0 @@ -package com.tsurugidb.iceaxe.test.timeout; - -import static org.junit.jupiter.api.Assertions.assertInstanceOf; -import static org.junit.jupiter.api.Assertions.fail; - -import java.io.IOException; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; - -import com.tsurugidb.iceaxe.session.TgSessionOption; -import com.tsurugidb.iceaxe.session.TgSessionOption.TgTimeoutKey; -import com.tsurugidb.iceaxe.session.TsurugiSession; -import com.tsurugidb.iceaxe.sql.result.TsurugiStatementResult; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; - -/** - * Result check timeout test - */ -public class DbTimeoutResultCheckTest extends DbTimetoutTest { - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTestTable(); - - logInitEnd(info); - } - - @Test - void timeoutDefault() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifySessionInfo(TgSessionOption sessionOption) { - sessionOption.setTimeout(TgTimeoutKey.DEFAULT, 1, TimeUnit.SECONDS); - } - }); - } - - @Test - void timeoutSpecified() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifySessionInfo(TgSessionOption sessionOption) { - sessionOption.setTimeout(TgTimeoutKey.RESULT_CHECK, 1, TimeUnit.SECONDS); - } - }); - } - - @Test - void timeoutSet() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifyStatementResult(TsurugiStatementResult result) { - result.setCheckTimeout(1, TimeUnit.SECONDS); - } - }); - } - - @Override - protected void clientTask(PipeServerThtread pipeServer, TsurugiSession session, TimeoutModifier modifier) throws Exception { - try (var transaction = session.createTransaction(TgTxOption.ofOCC())) { - transaction.getLowTransaction(); - - try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING)) { - var entity = createTestEntity(0); - ps.explain(entity); // getLowPreparedStatement() - - pipeServer.setPipeWrite(false); - try (var result = ps.execute(transaction, entity)) { - modifier.modifyStatementResult(result); - - try { - result.getUpdateCount(); - } catch (IOException e) { - assertInstanceOf(TimeoutException.class, e.getCause()); - LOG.trace("timeout success"); - return; - } finally { - pipeServer.setPipeWrite(true); - } - fail("didn't time out"); - } finally { - pipeServer.setPipeWrite(true); - } - } - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutResultCloseTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutResultCloseTest.java deleted file mode 100755 index 9a7514bb..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutResultCloseTest.java +++ /dev/null @@ -1,87 +0,0 @@ -package com.tsurugidb.iceaxe.test.timeout; - -import java.io.IOException; -import java.util.concurrent.TimeUnit; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; - -import com.tsurugidb.iceaxe.session.TgSessionOption; -import com.tsurugidb.iceaxe.session.TgSessionOption.TgTimeoutKey; -import com.tsurugidb.iceaxe.session.TsurugiSession; -import com.tsurugidb.iceaxe.sql.result.TsurugiStatementResult; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; - -/** - * Result close timeout test - */ -public class DbTimeoutResultCloseTest extends DbTimetoutTest { - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTestTable(); - - logInitEnd(info); - } - - @Test - void timeoutDefault() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifySessionInfo(TgSessionOption sessionOption) { - sessionOption.setTimeout(TgTimeoutKey.DEFAULT, 1, TimeUnit.SECONDS); - } - }); - } - - @Test - void timeoutSpecified() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifySessionInfo(TgSessionOption sessionOption) { - sessionOption.setTimeout(TgTimeoutKey.RESULT_CLOSE, 1, TimeUnit.SECONDS); - } - }); - } - - @Test - void timeoutSet() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifyStatementResult(TsurugiStatementResult result) { - result.setCloseTimeout(1, TimeUnit.SECONDS); - } - }); - } - - @Override - protected void clientTask(PipeServerThtread pipeServer, TsurugiSession session, TimeoutModifier modifier) throws Exception { - try (var transaction = session.createTransaction(TgTxOption.ofOCC())) { - transaction.getLowTransaction(); - - try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING)) { - var entity = createTestEntity(0); - var result = ps.execute(transaction, entity); - modifier.modifyStatementResult(result); - - pipeServer.setPipeWrite(false); - try { - result.close(); - } catch (IOException e) { - // RESULT_CLOSEはタイムアウトするような通信処理が無い -// assertInstanceOf(TimeoutException.class, e.getCause()); -// LOG.trace("timeout success"); -// return; - throw e; - } finally { - pipeServer.setPipeWrite(true); - } -// fail("didn't time out"); - } - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutRsCloseTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutRsCloseTest.java deleted file mode 100755 index afcde49e..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutRsCloseTest.java +++ /dev/null @@ -1,88 +0,0 @@ -package com.tsurugidb.iceaxe.test.timeout; - -import java.io.IOException; -import java.util.concurrent.TimeUnit; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; - -import com.tsurugidb.iceaxe.session.TgSessionOption; -import com.tsurugidb.iceaxe.session.TgSessionOption.TgTimeoutKey; -import com.tsurugidb.iceaxe.session.TsurugiSession; -import com.tsurugidb.iceaxe.sql.result.TsurugiQueryResult; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; - -/** - * {@link TsurugiQueryResult} close timeout test - */ -public class DbTimeoutRsCloseTest extends DbTimetoutTest { - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTestTable(); - - logInitEnd(info); - } - - @Test - void timeoutDefault() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifySessionInfo(TgSessionOption sessionOption) { - sessionOption.setTimeout(TgTimeoutKey.DEFAULT, 1, TimeUnit.SECONDS); - } - }); - } - - @Test - void timeoutSpecified() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifySessionInfo(TgSessionOption sessionOption) { - sessionOption.setTimeout(TgTimeoutKey.RS_CLOSE, 1, TimeUnit.SECONDS); - } - }); - } - - @Test - void timeoutSet() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifyQueryResult(TsurugiQueryResult result) { - result.setRsCloseTimeout(1, TimeUnit.SECONDS); - } - }); - } - - @Override - protected void clientTask(PipeServerThtread pipeServer, TsurugiSession session, TimeoutModifier modifier) throws Exception { - try (var transaction = session.createTransaction(TgTxOption.ofOCC())) { - transaction.getLowTransaction(); - - try (var ps = session.createQuery(SELECT_SQL)) { - var result = ps.execute(transaction); - modifier.modifyQueryResult(result); - - result.getLowResultSet(); - - pipeServer.setPipeWrite(false); - try { - result.close(); - } catch (IOException e) { - // RS_CLOSEはタイムアウトするような通信処理が無い -// assertInstanceOf(TimeoutException.class, e.getCause()); -// LOG.trace("timeout success"); -// return; - throw e; - } finally { - pipeServer.setPipeWrite(true); - } -// fail("didn't time out"); - } - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutRsConnectTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutRsConnectTest.java deleted file mode 100755 index 9718ab40..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutRsConnectTest.java +++ /dev/null @@ -1,91 +0,0 @@ -package com.tsurugidb.iceaxe.test.timeout; - -import static org.junit.jupiter.api.Assertions.assertInstanceOf; -import static org.junit.jupiter.api.Assertions.fail; - -import java.io.IOException; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; - -import com.tsurugidb.iceaxe.session.TgSessionOption; -import com.tsurugidb.iceaxe.session.TgSessionOption.TgTimeoutKey; -import com.tsurugidb.iceaxe.session.TsurugiSession; -import com.tsurugidb.iceaxe.sql.result.TsurugiQueryResult; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; - -/** - * {@link TsurugiQueryResult} connect timeout test - */ -public class DbTimeoutRsConnectTest extends DbTimetoutTest { - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTestTable(); - - logInitEnd(info); - } - - @Test - void timeoutDefault() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifySessionInfo(TgSessionOption sessionOption) { - sessionOption.setTimeout(TgTimeoutKey.DEFAULT, 1, TimeUnit.SECONDS); - } - }); - } - - @Test - void timeoutSpecified() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifySessionInfo(TgSessionOption sessionOption) { - sessionOption.setTimeout(TgTimeoutKey.RS_CONNECT, 1, TimeUnit.SECONDS); - } - }); - } - - @Test - void timeoutSet() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifyQueryResult(TsurugiQueryResult result) { - result.setRsConnectTimeout(1, TimeUnit.SECONDS); - } - }); - } - - @Override - protected void clientTask(PipeServerThtread pipeServer, TsurugiSession session, TimeoutModifier modifier) throws Exception { - try (var transaction = session.createTransaction(TgTxOption.ofOCC())) { - transaction.getLowTransaction(); - - try (var ps = session.createQuery(SELECT_SQL)) { - pipeServer.setPipeWrite(false); - try (var result = ps.execute(transaction)) { - modifier.modifyQueryResult(result); - - try { - result.getRecordList(); - } catch (IOException e) { - assertInstanceOf(TimeoutException.class, e.getCause()); - LOG.trace("timeout success"); - return; - } finally { - pipeServer.setPipeWrite(true); - } - fail("didn't time out"); - } finally { - pipeServer.setPipeWrite(true); - } - } - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutSessionCloseTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutSessionCloseTest.java deleted file mode 100755 index 9ced2195..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutSessionCloseTest.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.tsurugidb.iceaxe.test.timeout; - -import static org.junit.jupiter.api.Assertions.assertInstanceOf; -import static org.junit.jupiter.api.Assertions.fail; - -import java.io.IOException; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; - -import org.junit.jupiter.api.Test; - -import com.tsurugidb.iceaxe.session.TgSessionOption; -import com.tsurugidb.iceaxe.session.TgSessionOption.TgTimeoutKey; -import com.tsurugidb.iceaxe.session.TsurugiSession; - -/** - * session close timeout test - */ -public class DbTimeoutSessionCloseTest extends DbTimetoutTest { - - public DbTimeoutSessionCloseTest() { - super(false); - } - - @Test - void timeoutDefault() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifySessionInfo(TgSessionOption sessionOption) { - sessionOption.setTimeout(TgTimeoutKey.DEFAULT, 1, TimeUnit.SECONDS); - } - }); - } - - @Test - void timeoutSpecified() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifySessionInfo(TgSessionOption sessionOption) { - sessionOption.setTimeout(TgTimeoutKey.SESSION_CLOSE, 1, TimeUnit.SECONDS); - } - }); - } - - @Test - void timeoutSet() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifySession(TsurugiSession session) { - session.setCloseTimeout(1, TimeUnit.SECONDS); - } - }); - } - - @Override - protected void clientTask(PipeServerThtread pipeServer, TsurugiSession session, TimeoutModifier modifier) throws Exception { - session.getLowSqlClient(); - - pipeServer.setPipeWrite(false); - try { - session.close(); - } catch (IOException e) { - assertInstanceOf(TimeoutException.class, e.getCause()); - LOG.trace("timeout success"); - return; - } finally { - pipeServer.setPipeWrite(true); - } - fail("didn't time out"); - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutSessionConnectTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutSessionConnectTest.java deleted file mode 100755 index 4531c303..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutSessionConnectTest.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.tsurugidb.iceaxe.test.timeout; - -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertInstanceOf; -import static org.junit.jupiter.api.Assertions.fail; - -import java.io.IOException; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; - -import org.junit.jupiter.api.Test; - -import com.tsurugidb.iceaxe.TsurugiConnector; -import com.tsurugidb.iceaxe.session.TgSessionOption; -import com.tsurugidb.iceaxe.session.TgSessionOption.TgTimeoutKey; -import com.tsurugidb.iceaxe.session.TsurugiSession; - -/** - * session connect timeout test - */ -public class DbTimeoutSessionConnectTest extends DbTimetoutTest { - - @Test - void timeoutDefault() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifySessionInfo(TgSessionOption sessionOption) { - sessionOption.setTimeout(TgTimeoutKey.DEFAULT, 1, TimeUnit.SECONDS); - } - }); - } - - @Test - void timeoutSpecified() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifySessionInfo(TgSessionOption sessionOption) { - sessionOption.setTimeout(TgTimeoutKey.SESSION_CONNECT, 1, TimeUnit.SECONDS); - } - }); - } - - @Test - void timeoutSet() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifySession(TsurugiSession session) { - session.setConnectTimeout(1, TimeUnit.SECONDS); - } - }); - } - - @Override - protected TsurugiConnector getTsurugiConnector(PipeServerThtread pipeServer) { - pipeServer.setPipeWrite(false); - return super.getTsurugiConnector(pipeServer); - } - - @Override - protected void clientTask(PipeServerThtread pipeServer, TsurugiSession session, TimeoutModifier modifier) throws Exception { - try { - session.getLowSqlClient(); - } catch (IOException e) { - assertInstanceOf(TimeoutException.class, e.getCause()); - LOG.trace("timeout success"); - assertFalse(session.isAlive()); - return; - } finally { - pipeServer.setPipeWrite(true); - } - fail("didn't time out"); - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutTableListCloseTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutTableListCloseTest.java deleted file mode 100755 index aad0e3d5..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutTableListCloseTest.java +++ /dev/null @@ -1,90 +0,0 @@ -package com.tsurugidb.iceaxe.test.timeout; - -import java.io.IOException; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; - -import org.junit.jupiter.api.Test; - -import com.tsurugidb.iceaxe.metadata.TsurugiTableListHelper; -import com.tsurugidb.iceaxe.session.TgSessionOption; -import com.tsurugidb.iceaxe.session.TgSessionOption.TgTimeoutKey; -import com.tsurugidb.iceaxe.session.TsurugiSession; -import com.tsurugidb.tsubakuro.exception.ServerException; -import com.tsurugidb.tsubakuro.sql.SqlClient; -import com.tsurugidb.tsubakuro.sql.TableList; -import com.tsurugidb.tsubakuro.util.FutureResponse; - -/** - * table metadata close timeout test - */ -public class DbTimeoutTableListCloseTest extends DbTimetoutTest { - - @Test - void timeoutDefault() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifySessionInfo(TgSessionOption sessionOption) { - sessionOption.setTimeout(TgTimeoutKey.DEFAULT, 1, TimeUnit.SECONDS); - } - }); - } - - @Test - void timeoutSpecified() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifySessionInfo(TgSessionOption sessionOption) { - sessionOption.setTimeout(TgTimeoutKey.TABLE_LIST_CLOSE, 1, TimeUnit.SECONDS); - } - }); - } - - @Override - protected void clientTask(PipeServerThtread pipeServer, TsurugiSession session, TimeoutModifier modifier) throws Exception { - var helper = new TsurugiTableListHelper() { - @Override - protected FutureResponse getLowTableList(SqlClient lowSqlClient) throws IOException { - var future = super.getLowTableList(lowSqlClient); - return new FutureResponse() { - @Override - public boolean isDone() { - return future.isDone(); - } - - @Override - public TableList get() throws IOException, ServerException, InterruptedException { - throw new UnsupportedOperationException("do not use"); - } - - @Override - public TableList get(long timeout, TimeUnit unit) throws IOException, ServerException, InterruptedException, TimeoutException { - return future.get(timeout, unit); - } - - @Override - public void close() throws IOException, ServerException, InterruptedException { - pipeServer.setPipeWrite(false); - try { - future.close(); - } finally { - pipeServer.setPipeWrite(true); - } - } - }; - } - }; - session.setTableListHelper(helper); - - try { - session.getTableNameList(); - } catch (IOException e) { - // TABLE_LIST_CLOSEはタイムアウトするような通信処理が無い -// assertInstanceOf(TimeoutException.class, e.getCause()); -// LOG.trace("timeout success"); -// return; - throw e; - } -// fail("didn't time out"); - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutTableListConnectTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutTableListConnectTest.java deleted file mode 100755 index aa2a2125..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutTableListConnectTest.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.tsurugidb.iceaxe.test.timeout; - -import static org.junit.jupiter.api.Assertions.assertInstanceOf; -import static org.junit.jupiter.api.Assertions.fail; - -import java.io.IOException; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; - -import org.junit.jupiter.api.Test; - -import com.tsurugidb.iceaxe.metadata.TsurugiTableListHelper; -import com.tsurugidb.iceaxe.session.TgSessionOption; -import com.tsurugidb.iceaxe.session.TgSessionOption.TgTimeoutKey; -import com.tsurugidb.iceaxe.session.TsurugiSession; -import com.tsurugidb.tsubakuro.sql.SqlClient; -import com.tsurugidb.tsubakuro.sql.TableList; -import com.tsurugidb.tsubakuro.util.FutureResponse; - -/** - * table list connect timeout test - */ -public class DbTimeoutTableListConnectTest extends DbTimetoutTest { - - @Test - void timeoutDefault() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifySessionInfo(TgSessionOption sessionOption) { - sessionOption.setTimeout(TgTimeoutKey.DEFAULT, 1, TimeUnit.SECONDS); - } - }); - } - - @Test - void timeoutSpecified() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifySessionInfo(TgSessionOption sessionOption) { - sessionOption.setTimeout(TgTimeoutKey.TABLE_LIST_CONNECT, 1, TimeUnit.SECONDS); - } - }); - } - - @Override - protected void clientTask(PipeServerThtread pipeServer, TsurugiSession session, TimeoutModifier modifier) throws Exception { - var helper = new TsurugiTableListHelper() { - @Override - protected FutureResponse getLowTableList(SqlClient lowSqlClient) throws IOException { - pipeServer.setPipeWrite(false); - return super.getLowTableList(lowSqlClient); - } - }; - session.setTableListHelper(helper); - - try { - session.getTableNameList(); - } catch (IOException e) { - assertInstanceOf(TimeoutException.class, e.getCause()); - LOG.trace("timeout success"); - return; - } finally { - pipeServer.setPipeWrite(true); - } - fail("didn't time out"); - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutTableMetadataCloseTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutTableMetadataCloseTest.java deleted file mode 100755 index 89929aa2..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutTableMetadataCloseTest.java +++ /dev/null @@ -1,90 +0,0 @@ -package com.tsurugidb.iceaxe.test.timeout; - -import java.io.IOException; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; - -import org.junit.jupiter.api.Test; - -import com.tsurugidb.iceaxe.metadata.TsurugiTableMetadataHelper; -import com.tsurugidb.iceaxe.session.TgSessionOption; -import com.tsurugidb.iceaxe.session.TgSessionOption.TgTimeoutKey; -import com.tsurugidb.iceaxe.session.TsurugiSession; -import com.tsurugidb.tsubakuro.exception.ServerException; -import com.tsurugidb.tsubakuro.sql.SqlClient; -import com.tsurugidb.tsubakuro.sql.TableMetadata; -import com.tsurugidb.tsubakuro.util.FutureResponse; - -/** - * table metadata close timeout test - */ -public class DbTimeoutTableMetadataCloseTest extends DbTimetoutTest { - - @Test - void timeoutDefault() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifySessionInfo(TgSessionOption sessionOption) { - sessionOption.setTimeout(TgTimeoutKey.DEFAULT, 1, TimeUnit.SECONDS); - } - }); - } - - @Test - void timeoutSpecified() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifySessionInfo(TgSessionOption sessionOption) { - sessionOption.setTimeout(TgTimeoutKey.METADATA_CLOSE, 1, TimeUnit.SECONDS); - } - }); - } - - @Override - protected void clientTask(PipeServerThtread pipeServer, TsurugiSession session, TimeoutModifier modifier) throws Exception { - var helper = new TsurugiTableMetadataHelper() { - @Override - protected FutureResponse getLowTableMetadata(SqlClient lowSqlClient, String tableName) throws IOException { - var future = super.getLowTableMetadata(lowSqlClient, tableName); - return new FutureResponse() { - @Override - public boolean isDone() { - return future.isDone(); - } - - @Override - public TableMetadata get() throws IOException, ServerException, InterruptedException { - throw new UnsupportedOperationException("do not use"); - } - - @Override - public TableMetadata get(long timeout, TimeUnit unit) throws IOException, ServerException, InterruptedException, TimeoutException { - return future.get(timeout, unit); - } - - @Override - public void close() throws IOException, ServerException, InterruptedException { - pipeServer.setPipeWrite(false); - try { - future.close(); - } finally { - pipeServer.setPipeWrite(true); - } - } - }; - } - }; - session.setTableMetadataHelper(helper); - - try { - session.findTableMetadata(TEST); - } catch (IOException e) { - // METADATA_CLOSEはタイムアウトするような通信処理が無い -// assertInstanceOf(TimeoutException.class, e.getCause()); -// LOG.trace("timeout success"); -// return; - throw e; - } -// fail("didn't time out"); - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutTableMetadataConnectTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutTableMetadataConnectTest.java deleted file mode 100755 index 2b9e8936..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutTableMetadataConnectTest.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.tsurugidb.iceaxe.test.timeout; - -import static org.junit.jupiter.api.Assertions.assertInstanceOf; -import static org.junit.jupiter.api.Assertions.fail; - -import java.io.IOException; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; - -import org.junit.jupiter.api.Test; - -import com.tsurugidb.iceaxe.metadata.TsurugiTableMetadataHelper; -import com.tsurugidb.iceaxe.session.TgSessionOption; -import com.tsurugidb.iceaxe.session.TgSessionOption.TgTimeoutKey; -import com.tsurugidb.iceaxe.session.TsurugiSession; -import com.tsurugidb.tsubakuro.sql.SqlClient; -import com.tsurugidb.tsubakuro.sql.TableMetadata; -import com.tsurugidb.tsubakuro.util.FutureResponse; - -/** - * table metadata connect timeout test - */ -public class DbTimeoutTableMetadataConnectTest extends DbTimetoutTest { - - @Test - void timeoutDefault() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifySessionInfo(TgSessionOption sessionOption) { - sessionOption.setTimeout(TgTimeoutKey.DEFAULT, 1, TimeUnit.SECONDS); - } - }); - } - - @Test - void timeoutSpecified() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifySessionInfo(TgSessionOption sessionOption) { - sessionOption.setTimeout(TgTimeoutKey.METADATA_CONNECT, 1, TimeUnit.SECONDS); - } - }); - } - - @Override - protected void clientTask(PipeServerThtread pipeServer, TsurugiSession session, TimeoutModifier modifier) throws Exception { - var helper = new TsurugiTableMetadataHelper() { - @Override - protected FutureResponse getLowTableMetadata(SqlClient lowSqlClient, String tableName) throws IOException { - pipeServer.setPipeWrite(false); - return super.getLowTableMetadata(lowSqlClient, tableName); - } - }; - session.setTableMetadataHelper(helper); - - try { - session.findTableMetadata(TEST); - } catch (IOException e) { - assertInstanceOf(TimeoutException.class, e.getCause()); - LOG.trace("timeout success"); - return; - } finally { - pipeServer.setPipeWrite(true); - } - fail("didn't time out"); - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutTransactionBeginTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutTransactionBeginTest.java deleted file mode 100755 index ceb77632..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutTransactionBeginTest.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.tsurugidb.iceaxe.test.timeout; - -import static org.junit.jupiter.api.Assertions.assertInstanceOf; -import static org.junit.jupiter.api.Assertions.fail; - -import java.io.IOException; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; - -import org.junit.jupiter.api.Test; - -import com.tsurugidb.iceaxe.session.TgSessionOption; -import com.tsurugidb.iceaxe.session.TgSessionOption.TgTimeoutKey; -import com.tsurugidb.iceaxe.session.TsurugiSession; -import com.tsurugidb.iceaxe.transaction.TsurugiTransaction; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; - -/** - * transaction begin timeout test - */ -public class DbTimeoutTransactionBeginTest extends DbTimetoutTest { - - @Test - void timeoutDefault() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifySessionInfo(TgSessionOption sessionOption) { - sessionOption.setTimeout(TgTimeoutKey.DEFAULT, 1, TimeUnit.SECONDS); - } - }); - } - - @Test - void timeoutSpecified() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifySessionInfo(TgSessionOption sessionOption) { - sessionOption.setTimeout(TgTimeoutKey.TRANSACTION_BEGIN, 1, TimeUnit.SECONDS); - } - }); - } - - @Test - void timeoutSet() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifyTransaction(TsurugiTransaction transaction) { - transaction.setBeginTimeout(1, TimeUnit.SECONDS); - } - }); - } - - @Override - protected void clientTask(PipeServerThtread pipeServer, TsurugiSession session, TimeoutModifier modifier) throws Exception { - session.getLowSqlClient(); - - pipeServer.setPipeWrite(false); - try (var transaction = session.createTransaction(TgTxOption.ofOCC())) { - modifier.modifyTransaction(transaction); - - try { - transaction.getLowTransaction(); - } catch (IOException e) { - assertInstanceOf(TimeoutException.class, e.getCause()); - LOG.trace("timeout success"); - return; - } finally { - pipeServer.setPipeWrite(true); - } - fail("didn't time out"); - } finally { - pipeServer.setPipeWrite(true); - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutTransactionCloseTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutTransactionCloseTest.java deleted file mode 100755 index 1e6e0820..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutTransactionCloseTest.java +++ /dev/null @@ -1,74 +0,0 @@ -package com.tsurugidb.iceaxe.test.timeout; - -import static org.junit.jupiter.api.Assertions.assertInstanceOf; -import static org.junit.jupiter.api.Assertions.fail; - -import java.io.IOException; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; - -import org.junit.jupiter.api.Test; - -import com.tsurugidb.iceaxe.session.TgSessionOption; -import com.tsurugidb.iceaxe.session.TgSessionOption.TgTimeoutKey; -import com.tsurugidb.iceaxe.session.TsurugiSession; -import com.tsurugidb.iceaxe.transaction.TsurugiTransaction; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; - -/** - * transaction close timeout test - */ -public class DbTimeoutTransactionCloseTest extends DbTimetoutTest { - - @Test - void timeoutDefault() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifySessionInfo(TgSessionOption sessionOption) { - sessionOption.setTimeout(TgTimeoutKey.DEFAULT, 1, TimeUnit.SECONDS); - } - }); - } - - @Test - void timeoutSpecified() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifySessionInfo(TgSessionOption sessionOption) { - sessionOption.setTimeout(TgTimeoutKey.TRANSACTION_CLOSE, 1, TimeUnit.SECONDS); - } - }); - } - - @Test - void timeoutSet() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifyTransaction(TsurugiTransaction transaction) { - transaction.setCloseTimeout(1, TimeUnit.SECONDS); - } - }); - } - - @Override - protected void clientTask(PipeServerThtread pipeServer, TsurugiSession session, TimeoutModifier modifier) throws Exception { - session.getLowSqlClient(); - - var transaction = session.createTransaction(TgTxOption.ofOCC()); - modifier.modifyTransaction(transaction); - - transaction.getLowTransaction(); - - pipeServer.setPipeWrite(false); - try { - transaction.close(); - } catch (IOException e) { - assertInstanceOf(TimeoutException.class, e.getCause()); - LOG.trace("timeout success"); - return; - } finally { - pipeServer.setPipeWrite(true); - } - fail("didn't time out"); - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutTransactionCommitTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutTransactionCommitTest.java deleted file mode 100755 index 2d797dc4..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutTransactionCommitTest.java +++ /dev/null @@ -1,76 +0,0 @@ -package com.tsurugidb.iceaxe.test.timeout; - -import static org.junit.jupiter.api.Assertions.assertInstanceOf; -import static org.junit.jupiter.api.Assertions.fail; - -import java.io.IOException; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; - -import org.junit.jupiter.api.Test; - -import com.tsurugidb.iceaxe.session.TgSessionOption; -import com.tsurugidb.iceaxe.session.TgSessionOption.TgTimeoutKey; -import com.tsurugidb.iceaxe.session.TsurugiSession; -import com.tsurugidb.iceaxe.transaction.TgCommitType; -import com.tsurugidb.iceaxe.transaction.TsurugiTransaction; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; - -/** - * transaction commit timeout test - */ -public class DbTimeoutTransactionCommitTest extends DbTimetoutTest { - - @Test - void timeoutDefault() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifySessionInfo(TgSessionOption sessionOption) { - sessionOption.setTimeout(TgTimeoutKey.DEFAULT, 1, TimeUnit.SECONDS); - } - }); - } - - @Test - void timeoutSpecified() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifySessionInfo(TgSessionOption sessionOption) { - sessionOption.setTimeout(TgTimeoutKey.TRANSACTION_COMMIT, 1, TimeUnit.SECONDS); - } - }); - } - - @Test - void timeoutSet() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifyTransaction(TsurugiTransaction transaction) { - transaction.setCommitTimeout(1, TimeUnit.SECONDS); - } - }); - } - - @Override - protected void clientTask(PipeServerThtread pipeServer, TsurugiSession session, TimeoutModifier modifier) throws Exception { - session.getLowSqlClient(); - - try (var transaction = session.createTransaction(TgTxOption.ofOCC())) { - modifier.modifyTransaction(transaction); - - transaction.getLowTransaction(); - - pipeServer.setPipeWrite(false); - try { - transaction.commit(TgCommitType.DEFAULT); - } catch (IOException e) { - assertInstanceOf(TimeoutException.class, e.getCause()); - LOG.trace("timeout success"); - return; - } finally { - pipeServer.setPipeWrite(true); - } - fail("didn't time out"); - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutTransactionRollbackTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutTransactionRollbackTest.java deleted file mode 100755 index a430b4af..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutTransactionRollbackTest.java +++ /dev/null @@ -1,75 +0,0 @@ -package com.tsurugidb.iceaxe.test.timeout; - -import static org.junit.jupiter.api.Assertions.assertInstanceOf; -import static org.junit.jupiter.api.Assertions.fail; - -import java.io.IOException; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; - -import org.junit.jupiter.api.Test; - -import com.tsurugidb.iceaxe.session.TgSessionOption; -import com.tsurugidb.iceaxe.session.TgSessionOption.TgTimeoutKey; -import com.tsurugidb.iceaxe.session.TsurugiSession; -import com.tsurugidb.iceaxe.transaction.TsurugiTransaction; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; - -/** - * transaction rollback timeout test - */ -public class DbTimeoutTransactionRollbackTest extends DbTimetoutTest { - - @Test - void timeoutDefault() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifySessionInfo(TgSessionOption sessionOption) { - sessionOption.setTimeout(TgTimeoutKey.DEFAULT, 1, TimeUnit.SECONDS); - } - }); - } - - @Test - void timeoutSpecified() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifySessionInfo(TgSessionOption sessionOption) { - sessionOption.setTimeout(TgTimeoutKey.TRANSACTION_ROLLBACK, 1, TimeUnit.SECONDS); - } - }); - } - - @Test - void timeoutSet() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifyTransaction(TsurugiTransaction transaction) { - transaction.setRollbackTimeout(1, TimeUnit.SECONDS); - } - }); - } - - @Override - protected void clientTask(PipeServerThtread pipeServer, TsurugiSession session, TimeoutModifier modifier) throws Exception { - session.getLowSqlClient(); - - try (var transaction = session.createTransaction(TgTxOption.ofOCC())) { - modifier.modifyTransaction(transaction); - - transaction.getLowTransaction(); - - pipeServer.setPipeWrite(false); - try { - transaction.rollback(); - } catch (IOException e) { - assertInstanceOf(TimeoutException.class, e.getCause()); - LOG.trace("timeout success"); - return; - } finally { - pipeServer.setPipeWrite(true); - } - fail("didn't time out"); - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutTxStatusCloseTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutTxStatusCloseTest.java deleted file mode 100755 index 375f0250..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutTxStatusCloseTest.java +++ /dev/null @@ -1,93 +0,0 @@ -package com.tsurugidb.iceaxe.test.timeout; - -import java.io.IOException; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; - -import org.junit.jupiter.api.Test; - -import com.tsurugidb.iceaxe.session.TgSessionOption; -import com.tsurugidb.iceaxe.session.TgSessionOption.TgTimeoutKey; -import com.tsurugidb.iceaxe.session.TsurugiSession; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; -import com.tsurugidb.iceaxe.transaction.status.TsurugiTransactionStatusHelper; -import com.tsurugidb.tsubakuro.exception.ServerException; -import com.tsurugidb.tsubakuro.sql.SqlServiceException; -import com.tsurugidb.tsubakuro.sql.Transaction; -import com.tsurugidb.tsubakuro.util.FutureResponse; - -/** - * transaction status close timeout test - */ -public class DbTimeoutTxStatusCloseTest extends DbTimetoutTest { - - @Test - void timeoutDefault() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifySessionInfo(TgSessionOption sessionOption) { - sessionOption.setTimeout(TgTimeoutKey.DEFAULT, 1, TimeUnit.SECONDS); - } - }); - } - - @Test - void timeoutSpecified() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifySessionInfo(TgSessionOption sessionOption) { - sessionOption.setTimeout(TgTimeoutKey.TX_STATUS_CLOSE, 1, TimeUnit.SECONDS); - } - }); - } - - @Override - protected void clientTask(PipeServerThtread pipeServer, TsurugiSession session, TimeoutModifier modifier) throws Exception { - var helper = new TsurugiTransactionStatusHelper() { - @Override - protected FutureResponse getLowSqlServiceException(Transaction lowTx) throws IOException { - var future = super.getLowSqlServiceException(lowTx); - return new FutureResponse() { - @Override - public boolean isDone() { - return future.isDone(); - } - - @Override - public SqlServiceException get() throws IOException, ServerException, InterruptedException { - throw new UnsupportedOperationException("do not use"); - } - - @Override - public SqlServiceException get(long timeout, TimeUnit unit) throws IOException, ServerException, InterruptedException, TimeoutException { - return future.get(timeout, unit); - } - - @Override - public void close() throws IOException, ServerException, InterruptedException { - pipeServer.setPipeWrite(false); - try { - future.close(); - } finally { - pipeServer.setPipeWrite(true); - } - } - }; - } - }; - session.setTransactionStatusHelper(helper); - - try (var transaction = session.createTransaction(TgTxOption.ofOCC())) { - try { - transaction.getTransactionStatus(); - } catch (IOException e) { - // TABLE_LIST_CLOSEはタイムアウトするような通信処理が無い -// assertInstanceOf(TimeoutException.class, e.getCause()); -// LOG.trace("timeout success"); -// return; - throw e; - } -// fail("didn't time out"); - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutTxStatusConnectTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutTxStatusConnectTest.java deleted file mode 100755 index 2621e97c..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimeoutTxStatusConnectTest.java +++ /dev/null @@ -1,70 +0,0 @@ -package com.tsurugidb.iceaxe.test.timeout; - -import static org.junit.jupiter.api.Assertions.assertInstanceOf; -import static org.junit.jupiter.api.Assertions.fail; - -import java.io.IOException; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; - -import org.junit.jupiter.api.Test; - -import com.tsurugidb.iceaxe.session.TgSessionOption; -import com.tsurugidb.iceaxe.session.TgSessionOption.TgTimeoutKey; -import com.tsurugidb.iceaxe.session.TsurugiSession; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; -import com.tsurugidb.iceaxe.transaction.status.TsurugiTransactionStatusHelper; -import com.tsurugidb.tsubakuro.sql.SqlServiceException; -import com.tsurugidb.tsubakuro.sql.Transaction; -import com.tsurugidb.tsubakuro.util.FutureResponse; - -/** - * transaction status connect timeout test - */ -public class DbTimeoutTxStatusConnectTest extends DbTimetoutTest { - - @Test - void timeoutDefault() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifySessionInfo(TgSessionOption sessionOption) { - sessionOption.setTimeout(TgTimeoutKey.DEFAULT, 1, TimeUnit.SECONDS); - } - }); - } - - @Test - void timeoutSpecified() throws Exception { - testTimeout(new TimeoutModifier() { - @Override - public void modifySessionInfo(TgSessionOption sessionOption) { - sessionOption.setTimeout(TgTimeoutKey.TX_STATUS_CONNECT, 1, TimeUnit.SECONDS); - } - }); - } - - @Override - protected void clientTask(PipeServerThtread pipeServer, TsurugiSession session, TimeoutModifier modifier) throws Exception { - var helper = new TsurugiTransactionStatusHelper() { - @Override - protected FutureResponse getLowSqlServiceException(Transaction lowTx) throws IOException { - pipeServer.setPipeWrite(false); - return super.getLowSqlServiceException(lowTx); - } - }; - session.setTransactionStatusHelper(helper); - - try (var transaction = session.createTransaction(TgTxOption.ofOCC())) { - try { - transaction.getTransactionStatus(); - } catch (IOException e) { - assertInstanceOf(TimeoutException.class, e.getCause()); - LOG.trace("timeout success"); - return; - } finally { - pipeServer.setPipeWrite(true); - } - fail("didn't time out"); - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimetoutTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimetoutTest.java deleted file mode 100755 index b2610263..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/DbTimetoutTest.java +++ /dev/null @@ -1,112 +0,0 @@ -package com.tsurugidb.iceaxe.test.timeout; - -import java.io.IOException; -import java.util.concurrent.TimeUnit; - -import com.tsurugidb.iceaxe.TsurugiConnector; -import com.tsurugidb.iceaxe.session.TgSessionOption; -import com.tsurugidb.iceaxe.session.TgSessionOption.TgTimeoutKey; -import com.tsurugidb.iceaxe.session.TsurugiSession; -import com.tsurugidb.iceaxe.sql.TsurugiSqlPrepared; -import com.tsurugidb.iceaxe.sql.result.TsurugiQueryResult; -import com.tsurugidb.iceaxe.sql.result.TsurugiStatementResult; -import com.tsurugidb.iceaxe.test.util.DbTestConnector; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.transaction.TsurugiTransaction; - -public abstract class DbTimetoutTest extends DbTestTableTester { - - protected static class TimeoutModifier { - public void modifySessionInfo(TgSessionOption sessionOption) { - // do override - } - - public void modifySession(TsurugiSession session) { - // do override - } - - public void modifyPs(TsurugiSqlPrepared ps) { - // do override - } - - public void modifyTransaction(TsurugiTransaction transaction) { - // do override - } - - public void modifyQueryResult(TsurugiQueryResult result) { - // do override - } - - public void modifyStatementResult(TsurugiStatementResult result) { - // do override - } - } - - private final boolean closeSession; - - protected DbTimetoutTest() { - this(true); - } - - protected DbTimetoutTest(boolean closeSession) { - this.closeSession = closeSession; - } - - protected void testTimeout(TimeoutModifier modifier) throws IOException { - DbTestConnector.assumeEndpointTcp(); - - try (var pipeServer = new PipeServerThtread()) { - pipeServer.start(); - - var connector = getTsurugiConnector(pipeServer); - var session = createSession(pipeServer, connector, modifier); - AutoCloseable sessionCloser = () -> { - if (closeSession) { - session.close(); - } - }; - - try (sessionCloser) { - try { - clientTask(pipeServer, session, modifier); - } finally { - pipeServer.setPipeWrite(true); - } - } catch (IOException | RuntimeException | Error e) { - throw e; - } catch (Exception e) { - throw new RuntimeException(e); - } - } - } - - protected TsurugiConnector getTsurugiConnector(PipeServerThtread pipeServer) { - return pipeServer.getTsurugiConnector(); - } - - protected TsurugiSession createSession(PipeServerThtread pipeServer, TsurugiConnector connector, TimeoutModifier modifier) throws IOException { - var sessionOption = TgSessionOption.of(); - sessionOption.setTimeout(TgTimeoutKey.DEFAULT, 10, TimeUnit.SECONDS); - modifier.modifySessionInfo(sessionOption); - - TsurugiSession session = null; - try { - session = connector.createSession(sessionOption); - DbTestConnector.addSession(session); - modifier.modifySession(session); - } catch (Throwable t) { - if (session != null) { - try { - session.close(); - } catch (Throwable e) { - t.addSuppressed(e); - } - } - throw t; - } - assert session != null; - return session; - } - - protected abstract void clientTask(PipeServerThtread pipeServer, TsurugiSession session, TimeoutModifier modifier) throws Exception; -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/PipeServerThtread.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/PipeServerThtread.java deleted file mode 100755 index caf46a6d..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/PipeServerThtread.java +++ /dev/null @@ -1,117 +0,0 @@ -package com.tsurugidb.iceaxe.test.timeout; - -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.net.ServerSocket; -import java.net.Socket; -import java.util.concurrent.TimeUnit; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.tsurugidb.iceaxe.test.util.DbTestConnector; - -class PipeServerThtread extends TimeoutServerThread { - private static final Logger LOG = LoggerFactory.getLogger(PipeServerThtread.class); - - private volatile boolean write = true; - - public PipeServerThtread() { - super("iceaxe-dbtest.pipeServer"); - } - - @Override - protected void run(ServerSocket server) throws IOException, InterruptedException { - try (var dbSocket = DbTestConnector.createSocket(); // - var socket = server.accept()) { - new PipeStreamThread("iceaxe-dbtest.client->db", "send", socket, dbSocket).start(); - new PipeStreamThread("iceaxe-dbtest.db->client", "recv", dbSocket, socket).start(); - - setPriority(MIN_PRIORITY); - while (!isStop()) { - TimeUnit.MILLISECONDS.sleep(100); - } - } - } - - public void setPipeWrite(boolean write) { - if (this.write == write) { - return; - } - - try { - // フラグを切り替えるまでに行われた通信が処理されるまで充分待つ - TimeUnit.MILLISECONDS.sleep(300); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } - - this.write = write; - LOG.trace("pipeWrite={}", write); - } - - private class PipeStreamThread extends Thread { - private final Logger log = LoggerFactory.getLogger(getClass()); - - private final String role; - private final InputStream is; - private final OutputStream os; - - public PipeStreamThread(String name, String role, Socket inSocket, Socket outSocket) throws IOException { - super(name); - this.role = role; - this.is = inSocket.getInputStream(); - this.os = outSocket.getOutputStream(); - } - - @Override - public void run() { - setPriority(MAX_PRIORITY); - - byte[] buffer = new byte[1024]; - try { - for (;;) { - int len = is.read(buffer); - if (len < 0) { - break; - } - - if (write) { - debugDump(false, buffer, len); - os.write(buffer, 0, len); - } else { - debugDump(true, buffer, len); - } - } -// } catch (SocketException e) { -// LOG.trace(e.getMessage()); - } catch (Exception e) { - log.trace(e.getMessage(), e); - } - log.trace("PipeStreamThread end"); - } - - private void debugDump(boolean ignore, byte[] buffer, int length) { - if (!log.isDebugEnabled()) { - return; - } - - String message; - if (ignore) { - message = "ignore-" + role; - } else { - message = role; - } - - var sb = new StringBuilder(3 * length); - for (int i = 0; i < length; i++) { - if (sb.length() != 0) { - sb.append(" "); - } - sb.append(String.format("%02x", buffer[i])); - } - log.debug("{} len={}, buffer={}", message, String.format("%3d", length), sb); - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/TimeoutServerThread.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/TimeoutServerThread.java deleted file mode 100755 index 78abaefa..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/timeout/TimeoutServerThread.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.tsurugidb.iceaxe.test.timeout; - -import java.io.Closeable; -import java.io.IOException; -import java.io.UncheckedIOException; -import java.net.ServerSocket; -import java.net.URI; - -import com.tsurugidb.iceaxe.TsurugiConnector; - -abstract class TimeoutServerThread extends Thread implements Closeable { - - private final ServerSocket server; - private volatile boolean stop = false; - - public TimeoutServerThread(String name) { - super(name); - try { - this.server = new ServerSocket(0); - } catch (IOException e) { - throw new UncheckedIOException(e); - } - } - - public TsurugiConnector getTsurugiConnector() { - int port = server.getLocalPort(); - var endpoint = URI.create("tcp://localhost:" + port); - var connector = TsurugiConnector.of(endpoint); - return connector; - } - - @Override - public void run() { - try { - run(server); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } catch (IOException e) { - throw new UncheckedIOException(e); - } - } - - protected abstract void run(ServerSocket server) throws IOException, InterruptedException; - - protected final boolean isStop() { - return this.stop; - } - - @Override - public void close() throws IOException { - this.stop = true; - server.close(); - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbManagerCounterTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbManagerCounterTest.java deleted file mode 100755 index e6b07b06..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbManagerCounterTest.java +++ /dev/null @@ -1,242 +0,0 @@ -package com.tsurugidb.iceaxe.test.transaction; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertThrowsExactly; - -import java.io.IOException; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; - -import com.tsurugidb.iceaxe.sql.parameter.TgBindParameters; -import com.tsurugidb.iceaxe.sql.parameter.TgBindVariable; -import com.tsurugidb.iceaxe.sql.parameter.TgParameterMapping; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.transaction.function.TsurugiTransactionAction; -import com.tsurugidb.iceaxe.transaction.manager.TgTmSetting; -import com.tsurugidb.iceaxe.transaction.manager.TsurugiTransactionManager; -import com.tsurugidb.iceaxe.transaction.manager.event.counter.TgTmCount; -import com.tsurugidb.iceaxe.transaction.manager.event.counter.TgTmLabelCounter; -import com.tsurugidb.iceaxe.transaction.manager.event.counter.TgTmSimpleCounter; -import com.tsurugidb.iceaxe.transaction.manager.exception.TsurugiTmRetryOverIOException; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; -import com.tsurugidb.tsubakuro.sql.SqlServiceCode; - -/** - * {@link TsurugiTransactionManager} counter test - */ -class DbManagerCounterTest extends DbTestTableTester { - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTestTable(); - insertTestTable(3); - - logInitEnd(info); - } - - @Test - void simpleCounter() throws Exception { - var session = getSession(); - var tm = session.createTransactionManager(); - - var counter = new TgTmSimpleCounter(); - tm.addEventListener(counter); - - execute(tm); - assertSimpleCounter(counter); - } - - private void assertSimpleCounter(TgTmSimpleCounter counter) { - var count = counter.getCount(); - assertTotalCount(count); - } - - private void assertTotalCount(TgTmCount count) { - assertEquals(count.successCount() + count.failCount(), count.executeCount()); - assertEquals(count.commitCount() + count.rollbackCount(), count.transactionCount()); - assertEquals(6, count.execptionCount()); - assertEquals(4, count.retryCount()); - assertEquals(1, count.retryOverCount()); - assertEquals(2, count.beforeCommitCount()); - assertEquals(2, count.commitCount()); - assertEquals(10, count.rollbackCount()); - assertEquals(2, count.successCommitCount()); - assertEquals(4, count.successRollbackCount()); - assertEquals(2, count.failCount()); - } - - @Test - void labelCounter() throws Exception { - var session = getSession(); - var tm = session.createTransactionManager(); - - var counter = new TgTmLabelCounter(); - tm.addEventListener(counter); - - execute(tm); - assertLabelCounter(counter); - } - - private void assertLabelCounter(TgTmLabelCounter counter) { - assertEquals(5, counter.getCountMap().size()); - - { - assertFalse(counter.findCount("zzz").isPresent()); - - var tx1 = counter.findCount("tx1").get(); - assertEquals(tx1.successCount() + tx1.failCount(), tx1.executeCount()); - assertEquals(tx1.commitCount() + tx1.rollbackCount(), tx1.transactionCount()); - assertEquals(0, tx1.execptionCount()); - assertEquals(0, tx1.retryCount()); - assertEquals(0, tx1.retryOverCount()); - assertEquals(1, tx1.beforeCommitCount()); - assertEquals(1, tx1.commitCount()); - assertEquals(0, tx1.rollbackCount()); - assertEquals(1, tx1.successCommitCount()); - assertEquals(0, tx1.successRollbackCount()); - assertEquals(0, tx1.failCount()); - - var tx2 = counter.findCount("tx2").get(); - assertEquals(tx2.successCount() + tx2.failCount(), tx2.executeCount()); - assertEquals(tx2.commitCount() + tx2.rollbackCount(), tx2.transactionCount()); - assertEquals(3, tx2.execptionCount()); - assertEquals(2, tx2.retryCount()); - assertEquals(1, tx2.retryOverCount()); - assertEquals(0, tx2.beforeCommitCount()); - assertEquals(0, tx2.commitCount()); - assertEquals(3, tx2.rollbackCount()); - assertEquals(0, tx2.successCommitCount()); - assertEquals(0, tx2.successRollbackCount()); - assertEquals(1, tx2.failCount()); - - var tran1 = counter.findCount("tran1").get(); - assertEquals(tran1.successCount() + tran1.failCount(), tran1.executeCount()); - assertEquals(tran1.commitCount() + tran1.rollbackCount(), tran1.transactionCount()); - assertEquals(0, tran1.execptionCount()); - assertEquals(0, tran1.retryCount()); - assertEquals(0, tran1.retryOverCount()); - assertEquals(0, tran1.beforeCommitCount()); - assertEquals(0, tran1.commitCount()); - assertEquals(4, tran1.rollbackCount()); - assertEquals(0, tran1.successCommitCount()); - assertEquals(4, tran1.successRollbackCount()); - assertEquals(0, tran1.failCount()); - - var tran2 = counter.findCount("tran2").get(); - assertEquals(tran2.successCount() + tran2.failCount(), tran2.executeCount()); - assertEquals(tran2.commitCount() + tran2.rollbackCount(), tran2.transactionCount()); - assertEquals(2, tran2.execptionCount()); - assertEquals(2, tran2.retryCount()); - assertEquals(0, tran2.retryOverCount()); - assertEquals(1, tran2.beforeCommitCount()); - assertEquals(1, tran2.commitCount()); - assertEquals(2, tran2.rollbackCount()); - assertEquals(1, tran2.successCommitCount()); - assertEquals(0, tran2.successRollbackCount()); - assertEquals(0, tran2.failCount()); - - var empty = counter.findCount("").get(); - assertEquals(empty.successCount() + empty.failCount(), empty.executeCount()); - assertEquals(empty.commitCount() + empty.rollbackCount(), empty.transactionCount()); - assertEquals(1, empty.execptionCount()); - assertEquals(0, empty.retryCount()); - assertEquals(0, empty.retryOverCount()); - assertEquals(0, empty.beforeCommitCount()); - assertEquals(0, empty.commitCount()); - assertEquals(1, empty.rollbackCount()); - assertEquals(0, empty.successCommitCount()); - assertEquals(0, empty.successRollbackCount()); - assertEquals(1, empty.failCount()); - } - { - assertFalse(counter.findCountByPrefix("zzz").isPresent()); - - var tx = counter.findCountByPrefix("tx").get(); - var tx1 = counter.findCount("tx1").get(); - var tx2 = counter.findCount("tx2").get(); - assertEquals(tx1.executeCount() + tx2.executeCount(), tx.executeCount()); - assertEquals(tx1.execptionCount() + tx2.execptionCount(), tx.execptionCount()); - assertEquals(tx1.retryCount() + tx2.retryCount(), tx.retryCount()); - assertEquals(tx1.retryOverCount() + tx2.retryOverCount(), tx.retryOverCount()); - assertEquals(tx1.beforeCommitCount() + tx2.beforeCommitCount(), tx.beforeCommitCount()); - assertEquals(tx1.commitCount() + tx2.commitCount(), tx.commitCount()); - assertEquals(tx1.rollbackCount() + tx2.rollbackCount(), tx.rollbackCount()); - assertEquals(tx1.successCommitCount() + tx2.successCommitCount(), tx.successCommitCount()); - assertEquals(tx1.successRollbackCount() + tx2.successRollbackCount(), tx.successRollbackCount()); - assertEquals(tx1.failCount() + tx2.failCount(), tx.failCount()); - } - { - var count = counter.getCount(); - assertTotalCount(count); - } - } - - @Test - void mix() throws Exception { - var session = getSession(); - var tm = session.createTransactionManager(); - - var simpleCounter = new TgTmSimpleCounter(); - tm.addEventListener(simpleCounter); - var labelCounter = new TgTmLabelCounter(); - tm.addEventListener(labelCounter); - - execute(tm); - assertSimpleCounter(simpleCounter); - assertLabelCounter(labelCounter); - } - - private void execute(TsurugiTransactionManager tm) throws IOException, InterruptedException { - var setting1 = TgTmSetting.of(TgTxOption.ofLTX(TEST).label("tx1")); - var setting2 = TgTmSetting.ofAlways(TgTxOption.ofOCC().label("tx2"), 3); - var setting3 = TgTmSetting.ofAlways(TgTxOption.ofOCC().label("tran1")); - var setting4 = TgTmSetting.ofAlways(TgTxOption.ofOCC().label("tran2")); - var setting5 = TgTmSetting.ofAlways(TgTxOption.ofOCC()); // label(null) - - var bar = TgBindVariable.ofInt("bar"); - var sql = "update " + TEST + " set bar=" + bar + " where foo=1"; - var mapping = TgParameterMapping.of(bar); - - var session = tm.getSession(); - try (var ps = session.createStatement(sql, mapping)) { - tm.execute(setting1, tx1 -> { - var param1 = TgBindParameters.of(bar.bind(111)); - tx1.executeAndGetCount(ps, param1); - - var e = assertThrowsExactly(TsurugiTmRetryOverIOException.class, () -> { - tm.execute(setting2, tx2 -> { - var param2 = TgBindParameters.of(bar.bind(222)); - tx2.executeAndGetCount(ps, param2); - }); - }); - assertEqualsCode(SqlServiceCode.CC_EXCEPTION, e); - - for (int i = 0; i < 4; i++) { - tm.execute(setting3, tx3 -> { - tx3.rollback(); - }); - } - - tm.execute(setting4, tx4 -> { - if (tx4.getAttempt() < 2) { - var param4 = TgBindParameters.of(bar.bind(333)); - tx4.executeAndGetCount(ps, param4); - } - }); - - var e5 = assertThrowsExactly(RuntimeException.class, () -> { - tm.execute(setting5, (TsurugiTransactionAction) tx5 -> { - throw new RuntimeException("test5"); - }); - }); - assertEquals("test5", e5.getMessage()); - }); - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbManagerExceptionTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbManagerExceptionTest.java deleted file mode 100755 index f6408009..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbManagerExceptionTest.java +++ /dev/null @@ -1,90 +0,0 @@ -package com.tsurugidb.iceaxe.test.transaction; - -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertThrowsExactly; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.slf4j.LoggerFactory; - -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.transaction.exception.TsurugiTransactionException; -import com.tsurugidb.iceaxe.transaction.function.TsurugiTransactionAction; -import com.tsurugidb.iceaxe.transaction.manager.exception.TsurugiTmIOException; -import com.tsurugidb.iceaxe.transaction.manager.exception.TsurugiTmRetryOverIOException; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; -import com.tsurugidb.tsubakuro.sql.SqlServiceCode; - -/** - * {@link TsurugiTmIOException} test - */ -class DbManagerExceptionTest extends DbTestTableTester { - - private static final int SIZE = 4; - - @BeforeAll - static void beforeAll(TestInfo info) throws Exception { - var LOG = LoggerFactory.getLogger(DbManagerExceptionTest.class); - logInitStart(LOG, info); - - dropTestTable(); - createTestTable(); - insertTestTable(SIZE); - - logInitEnd(LOG, info); - } - - @Test - void exception() throws Exception { - var session = getSession(); - var tm = session.createTransactionManager(TgTxOption.ofOCC()); - var e = assertThrowsExactly(TsurugiTmIOException.class, () -> { - tm.execute(transaction -> { - try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING)) { - var entity = createTestEntity(1); - var e0 = assertThrowsExactly(TsurugiTransactionException.class, () -> { - transaction.executeAndGetCount(ps, entity); - }); - assertEqualsCode(SqlServiceCode.UNIQUE_CONSTRAINT_VIOLATION_EXCEPTION, e0); - // not throw e0 - } - }); - }); - assertEqualsCode(SqlServiceCode.INACTIVE_TRANSACTION_EXCEPTION, e); - var status = e.getTransactionStatus(); - assertTrue(status.isError()); - assertEqualsCode(SqlServiceCode.UNIQUE_CONSTRAINT_VIOLATION_EXCEPTION, status.getTransactionException()); - var next = e.getNextTmOption(); - assertFalse(next.isExecute()); - assertFalse(next.isRetryOver()); - } - - @Test - void retryException() throws Exception { - var session = getSession(); - var tm = session.createTransactionManager(TgTxOption.ofOCC()); - var e = assertThrowsExactly(TsurugiTmRetryOverIOException.class, () -> { - try (var ltx = session.createTransaction(TgTxOption.ofLTX(TEST))) { - tm.execute((TsurugiTransactionAction) transaction -> { - try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING)) { - var entity = createTestEntity(SIZE); - var e0 = assertThrowsExactly(TsurugiTransactionException.class, () -> { - transaction.executeAndGetCount(ps, entity); - }); - assertEqualsCode(SqlServiceCode.CONFLICT_ON_WRITE_PRESERVE_EXCEPTION, e0); - throw e0; - } - }); - } - }); - assertEqualsCode(SqlServiceCode.CONFLICT_ON_WRITE_PRESERVE_EXCEPTION, e); - var status = e.getTransactionStatus(); - assertTrue(status.isError()); - assertEqualsCode(SqlServiceCode.CONFLICT_ON_WRITE_PRESERVE_EXCEPTION, status.getTransactionException()); - var next = e.getNextTmOption(); - assertFalse(next.isExecute()); - assertTrue(next.isRetryOver()); - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbManagerExecuteDdlTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbManagerExecuteDdlTest.java deleted file mode 100755 index ccc6edc7..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbManagerExecuteDdlTest.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.tsurugidb.iceaxe.test.transaction; - -import static org.junit.jupiter.api.Assertions.assertTrue; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; - -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.transaction.manager.TgTmSetting; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; - -/** - * TransactionManager execute ddl test - */ -class DbManagerExecuteDdlTest extends DbTestTableTester { - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - - logInitEnd(info); - } - - @Test - void executeDdl0() throws Exception { - var session = getSession(); - assertTrue(session.findTableMetadata(TEST).isEmpty()); - - var tm = session.createTransactionManager(); - tm.executeDdl(CREATE_TEST_SQL); - - assertTrue(session.findTableMetadata(TEST).isPresent()); - } - - @Test - void executeDdlDefaultSetting() throws Exception { - var session = getSession(); - assertTrue(session.findTableMetadata(TEST).isEmpty()); - - var tm = session.createTransactionManager(TgTxOption.ofLTX()); - tm.executeDdl(CREATE_TEST_SQL); - - assertTrue(session.findTableMetadata(TEST).isPresent()); - } - - @Test - void executeDdl() throws Exception { - var session = getSession(); - assertTrue(session.findTableMetadata(TEST).isEmpty()); - - var tm = session.createTransactionManager(); - var setting = TgTmSetting.of(TgTxOption.ofLTX()); - tm.executeDdl(setting, CREATE_TEST_SQL); - - assertTrue(session.findTableMetadata(TEST).isPresent()); - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbManagerExecuteQueryTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbManagerExecuteQueryTest.java deleted file mode 100755 index 39b3d9b9..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbManagerExecuteQueryTest.java +++ /dev/null @@ -1,625 +0,0 @@ -package com.tsurugidb.iceaxe.test.transaction; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import java.util.ArrayList; -import java.util.List; - -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.slf4j.LoggerFactory; - -import com.tsurugidb.iceaxe.sql.parameter.TgBindParameters; -import com.tsurugidb.iceaxe.sql.parameter.TgBindVariable; -import com.tsurugidb.iceaxe.sql.parameter.TgParameterMapping; -import com.tsurugidb.iceaxe.sql.result.TsurugiResultEntity; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.test.util.TestEntity; -import com.tsurugidb.iceaxe.transaction.manager.TgTmSetting; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; - -/** - * TransactionManager execute query test - */ -class DbManagerExecuteQueryTest extends DbTestTableTester { - - private static final int SIZE = 4; - - @BeforeAll - static void beforeAll(TestInfo info) throws Exception { - var LOG = LoggerFactory.getLogger(DbManagerExecuteQueryTest.class); - logInitStart(LOG, info); - - dropTestTable(); - createTestTable(); - insertTestTable(SIZE); - - logInitEnd(LOG, info); - } - - @Test - void executeAndForEach_sql() throws Exception { - var sql = SELECT_SQL + " where foo < " + (SIZE - 1) + " order by foo"; - - var session = getSession(); - var tm = session.createTransactionManager(TgTxOption.ofOCC()); - - var list = new ArrayList(); - tm.executeAndForEach(sql, entity -> { - list.add(entity); - }); - - assertEqualsTestTableResultEntity(SIZE - 1, list); - } - - @Test - void executeAndForEach_setting_sql() throws Exception { - var sql = SELECT_SQL + " where foo < " + (SIZE - 1) + " order by foo"; - - var session = getSession(); - var tm = session.createTransactionManager(); - var setting = TgTmSetting.of(TgTxOption.ofOCC()); - - var list = new ArrayList(); - tm.executeAndForEach(setting, sql, entity -> { - list.add(entity); - }); - - assertEqualsTestTableResultEntity(SIZE - 1, list); - } - - @Test - void executeAndForEach_sql_result() throws Exception { - var sql = SELECT_SQL + " where foo < " + (SIZE - 1) + " order by foo"; - - var session = getSession(); - var tm = session.createTransactionManager(TgTxOption.ofOCC()); - - var list = new ArrayList(); - tm.executeAndForEach(sql, SELECT_MAPPING, entity -> { - list.add(entity); - }); - - assertEqualsTestTable(SIZE - 1, list); - } - - @Test - void executeAndForEach_setting_sql_result() throws Exception { - var sql = SELECT_SQL + " where foo < " + (SIZE - 1) + " order by foo"; - - var session = getSession(); - var tm = session.createTransactionManager(); - var setting = TgTmSetting.of(TgTxOption.ofOCC()); - - var list = new ArrayList(); - tm.executeAndForEach(setting, sql, SELECT_MAPPING, entity -> { - list.add(entity); - }); - - assertEqualsTestTable(SIZE - 1, list); - } - - @Test - void executeAndForEach_sql_parameter() throws Exception { - var foo = TgBindVariable.ofInt("foo"); - var sql = SELECT_SQL + " where foo < " + foo + " order by foo"; - var parameterMapping = TgParameterMapping.of(foo); - var parameter = TgBindParameters.of(foo.bind(SIZE - 1)); - - var session = getSession(); - var tm = session.createTransactionManager(TgTxOption.ofOCC()); - - var list = new ArrayList(); - tm.executeAndForEach(sql, parameterMapping, parameter, entity -> { - list.add(entity); - }); - - assertEqualsTestTableResultEntity(SIZE - 1, list); - } - - @Test - void executeAndForEach_setting_sql_parameter() throws Exception { - var foo = TgBindVariable.ofInt("foo"); - var sql = SELECT_SQL + " where foo < " + foo + " order by foo"; - var parameterMapping = TgParameterMapping.of(foo); - var parameter = TgBindParameters.of(foo.bind(SIZE - 1)); - - var session = getSession(); - var tm = session.createTransactionManager(); - var setting = TgTmSetting.of(TgTxOption.ofOCC()); - - var list = new ArrayList(); - tm.executeAndForEach(setting, sql, parameterMapping, parameter, entity -> { - list.add(entity); - }); - - assertEqualsTestTableResultEntity(SIZE - 1, list); - } - - @Test - void executeAndForEach_sql_parameter_result() throws Exception { - var foo = TgBindVariable.ofInt("foo"); - var sql = SELECT_SQL + " where foo < " + foo + " order by foo"; - var parameterMapping = TgParameterMapping.of(foo); - var parameter = TgBindParameters.of(foo.bind(SIZE - 1)); - - var session = getSession(); - var tm = session.createTransactionManager(TgTxOption.ofOCC()); - - var list = new ArrayList(); - tm.executeAndForEach(sql, parameterMapping, parameter, SELECT_MAPPING, entity -> { - list.add(entity); - }); - - assertEqualsTestTable(SIZE - 1, list); - } - - @Test - void executeAndForEach_setting_sql_parameter_result() throws Exception { - var foo = TgBindVariable.ofInt("foo"); - var sql = SELECT_SQL + " where foo < " + foo + " order by foo"; - var parameterMapping = TgParameterMapping.of(foo); - var parameter = TgBindParameters.of(foo.bind(SIZE - 1)); - - var session = getSession(); - var tm = session.createTransactionManager(); - var setting = TgTmSetting.of(TgTxOption.ofOCC()); - - var list = new ArrayList(); - tm.executeAndForEach(setting, sql, parameterMapping, parameter, SELECT_MAPPING, entity -> { - list.add(entity); - }); - - assertEqualsTestTable(SIZE - 1, list); - } - - @Test - void executeAndForEach_ps() throws Exception { - var sql = SELECT_SQL + " where foo < " + (SIZE - 1) + " order by foo"; - - var session = getSession(); - var tm = session.createTransactionManager(TgTxOption.ofOCC()); - - try (var ps = session.createQuery(sql)) { - var list = new ArrayList(); - tm.executeAndForEach(ps, entity -> { - list.add(entity); - }); - - assertEqualsTestTableResultEntity(SIZE - 1, list); - } - } - - @Test - void executeAndForEach_setting_ps() throws Exception { - var sql = SELECT_SQL + " where foo < " + (SIZE - 1) + " order by foo"; - - var session = getSession(); - var tm = session.createTransactionManager(); - var setting = TgTmSetting.of(TgTxOption.ofOCC()); - - try (var ps = session.createQuery(sql)) { - var list = new ArrayList(); - tm.executeAndForEach(setting, ps, entity -> { - list.add(entity); - }); - - assertEqualsTestTableResultEntity(SIZE - 1, list); - } - } - - @Test - void executeAndForEach_ps_parameter() throws Exception { - var foo = TgBindVariable.ofInt("foo"); - var sql = SELECT_SQL + " where foo < " + foo + " order by foo"; - var parameterMapping = TgParameterMapping.of(foo); - var parameter = TgBindParameters.of(foo.bind(SIZE - 1)); - - var session = getSession(); - var tm = session.createTransactionManager(TgTxOption.ofOCC()); - - try (var ps = session.createQuery(sql, parameterMapping)) { - var list = new ArrayList(); - tm.executeAndForEach(ps, parameter, entity -> { - list.add(entity); - }); - - assertEqualsTestTableResultEntity(SIZE - 1, list); - } - } - - @Test - void executeAndForEach_setting_ps_parameter() throws Exception { - var foo = TgBindVariable.ofInt("foo"); - var sql = SELECT_SQL + " where foo < " + foo + " order by foo"; - var parameterMapping = TgParameterMapping.of(foo); - var parameter = TgBindParameters.of(foo.bind(SIZE - 1)); - - var session = getSession(); - var tm = session.createTransactionManager(); - var setting = TgTmSetting.of(TgTxOption.ofOCC()); - - try (var ps = session.createQuery(sql, parameterMapping)) { - var list = new ArrayList(); - tm.executeAndForEach(setting, ps, parameter, entity -> { - list.add(entity); - }); - - assertEqualsTestTableResultEntity(SIZE - 1, list); - } - } - - @Test - void executeAndGetList_sql() throws Exception { - var sql = SELECT_SQL + " where foo < " + (SIZE - 1) + " order by foo"; - - var session = getSession(); - var tm = session.createTransactionManager(TgTxOption.ofOCC()); - - var list = tm.executeAndGetList(sql); - - assertEqualsTestTableResultEntity(SIZE - 1, list); - } - - @Test - void executeAndGetList_setting_sql() throws Exception { - var sql = SELECT_SQL + " where foo < " + (SIZE - 1) + " order by foo"; - - var session = getSession(); - var tm = session.createTransactionManager(); - var setting = TgTmSetting.of(TgTxOption.ofOCC()); - - var list = tm.executeAndGetList(setting, sql); - - assertEqualsTestTableResultEntity(SIZE - 1, list); - } - - @Test - void executeAndGetList_sql_result() throws Exception { - var sql = SELECT_SQL + " where foo < " + (SIZE - 1) + " order by foo"; - - var session = getSession(); - var tm = session.createTransactionManager(TgTxOption.ofOCC()); - - var list = tm.executeAndGetList(sql, SELECT_MAPPING); - - assertEqualsTestTable(SIZE - 1, list); - } - - @Test - void executeAndGetList_setting_sql_result() throws Exception { - var sql = SELECT_SQL + " where foo < " + (SIZE - 1) + " order by foo"; - - var session = getSession(); - var tm = session.createTransactionManager(); - var setting = TgTmSetting.of(TgTxOption.ofOCC()); - - var list = tm.executeAndGetList(setting, sql, SELECT_MAPPING); - - assertEqualsTestTable(SIZE - 1, list); - } - - @Test - void executeAndGetList_sql_parameter() throws Exception { - var foo = TgBindVariable.ofInt("foo"); - var sql = SELECT_SQL + " where foo < " + foo + " order by foo"; - var parameterMapping = TgParameterMapping.of(foo); - var parameter = TgBindParameters.of(foo.bind(SIZE - 1)); - - var session = getSession(); - var tm = session.createTransactionManager(TgTxOption.ofOCC()); - - var list = tm.executeAndGetList(sql, parameterMapping, parameter); - - assertEqualsTestTableResultEntity(SIZE - 1, list); - } - - @Test - void executeAndGetList_setting_sql_parameter() throws Exception { - var foo = TgBindVariable.ofInt("foo"); - var sql = SELECT_SQL + " where foo < " + foo + " order by foo"; - var parameterMapping = TgParameterMapping.of(foo); - var parameter = TgBindParameters.of(foo.bind(SIZE - 1)); - - var session = getSession(); - var tm = session.createTransactionManager(); - var setting = TgTmSetting.of(TgTxOption.ofOCC()); - - var list = tm.executeAndGetList(setting, sql, parameterMapping, parameter); - - assertEqualsTestTableResultEntity(SIZE - 1, list); - } - - @Test - void executeAndGetList_sql_parameter_result() throws Exception { - var foo = TgBindVariable.ofInt("foo"); - var sql = SELECT_SQL + " where foo < " + foo + " order by foo"; - var parameterMapping = TgParameterMapping.of(foo); - var parameter = TgBindParameters.of(foo.bind(SIZE - 1)); - - var session = getSession(); - var tm = session.createTransactionManager(TgTxOption.ofOCC()); - - var list = tm.executeAndGetList(sql, parameterMapping, parameter, SELECT_MAPPING); - - assertEqualsTestTable(SIZE - 1, list); - } - - @Test - void executeAndGetList_setting_sql_parameter_result() throws Exception { - var foo = TgBindVariable.ofInt("foo"); - var sql = SELECT_SQL + " where foo < " + foo + " order by foo"; - var parameterMapping = TgParameterMapping.of(foo); - var parameter = TgBindParameters.of(foo.bind(SIZE - 1)); - - var session = getSession(); - var tm = session.createTransactionManager(); - var setting = TgTmSetting.of(TgTxOption.ofOCC()); - - var list = tm.executeAndGetList(setting, sql, parameterMapping, parameter, SELECT_MAPPING); - - assertEqualsTestTable(SIZE - 1, list); - } - - @Test - void executeAndGetList_ps() throws Exception { - var sql = SELECT_SQL + " where foo < " + (SIZE - 1) + " order by foo"; - - var session = getSession(); - var tm = session.createTransactionManager(TgTxOption.ofOCC()); - - try (var ps = session.createQuery(sql)) { - var list = tm.executeAndGetList(ps); - - assertEqualsTestTableResultEntity(SIZE - 1, list); - } - } - - @Test - void executeAndGetList_setting_ps() throws Exception { - var sql = SELECT_SQL + " where foo < " + (SIZE - 1) + " order by foo"; - - var session = getSession(); - var tm = session.createTransactionManager(); - var setting = TgTmSetting.of(TgTxOption.ofOCC()); - - try (var ps = session.createQuery(sql)) { - var list = tm.executeAndGetList(setting, ps); - - assertEqualsTestTableResultEntity(SIZE - 1, list); - } - } - - @Test - void executeAndGetList_ps_parameter() throws Exception { - var foo = TgBindVariable.ofInt("foo"); - var sql = SELECT_SQL + " where foo < " + foo + " order by foo"; - var parameterMapping = TgParameterMapping.of(foo); - var parameter = TgBindParameters.of(foo.bind(SIZE - 1)); - - var session = getSession(); - var tm = session.createTransactionManager(TgTxOption.ofOCC()); - - try (var ps = session.createQuery(sql, parameterMapping)) { - var list = tm.executeAndGetList(ps, parameter); - - assertEqualsTestTableResultEntity(SIZE - 1, list); - } - } - - @Test - void executeAndGetList_setting_ps_parameter() throws Exception { - var foo = TgBindVariable.ofInt("foo"); - var sql = SELECT_SQL + " where foo < " + foo + " order by foo"; - var parameterMapping = TgParameterMapping.of(foo); - var parameter = TgBindParameters.of(foo.bind(SIZE - 1)); - - var session = getSession(); - var tm = session.createTransactionManager(); - var setting = TgTmSetting.of(TgTxOption.ofOCC()); - - try (var ps = session.createQuery(sql, parameterMapping)) { - var list = tm.executeAndGetList(setting, ps, parameter); - - assertEqualsTestTableResultEntity(SIZE - 1, list); - } - } - - @Test - void executeAndFindRecord_sql() throws Exception { - var sql = SELECT_SQL + " where foo = 1"; - - var session = getSession(); - var tm = session.createTransactionManager(TgTxOption.ofOCC()); - - var entity = tm.executeAndFindRecord(sql).get(); - - var expected = createTestEntity(1); - assertEqualsResultEntity(expected, entity); - } - - @Test - void executeAndFindRecord_setting_sql() throws Exception { - var sql = SELECT_SQL + " where foo = 1"; - - var session = getSession(); - var tm = session.createTransactionManager(); - var setting = TgTmSetting.of(TgTxOption.ofOCC()); - - var entity = tm.executeAndFindRecord(setting, sql).get(); - - var expected = createTestEntity(1); - assertEqualsResultEntity(expected, entity); - } - - @Test - void executeAndFindRecord_sql_result() throws Exception { - var sql = SELECT_SQL + " where foo = 1"; - - var session = getSession(); - var tm = session.createTransactionManager(TgTxOption.ofOCC()); - - var entity = tm.executeAndFindRecord(sql, SELECT_MAPPING).get(); - - var expected = createTestEntity(1); - assertEquals(expected, entity); - } - - @Test - void executeAndFindRecord_setting_sql_result() throws Exception { - var sql = SELECT_SQL + " where foo = 1"; - - var session = getSession(); - var tm = session.createTransactionManager(); - var setting = TgTmSetting.of(TgTxOption.ofOCC()); - - var entity = tm.executeAndFindRecord(setting, sql, SELECT_MAPPING).get(); - - var expected = createTestEntity(1); - assertEquals(expected, entity); - } - - @Test - void executeAndFindRecord_sql_parameter() throws Exception { - var foo = TgBindVariable.ofInt("foo"); - var sql = SELECT_SQL + " where foo = " + foo; - var parameterMapping = TgParameterMapping.of(foo); - var parameter = TgBindParameters.of(foo.bind(1)); - - var session = getSession(); - var tm = session.createTransactionManager(TgTxOption.ofOCC()); - - var entity = tm.executeAndFindRecord(sql, parameterMapping, parameter).get(); - - var expected = createTestEntity(1); - assertEqualsResultEntity(expected, entity); - } - - @Test - void executeAndFindRecord_setting_sql_parameter() throws Exception { - var foo = TgBindVariable.ofInt("foo"); - var sql = SELECT_SQL + " where foo = " + foo; - var parameterMapping = TgParameterMapping.of(foo); - var parameter = TgBindParameters.of(foo.bind(1)); - - var session = getSession(); - var tm = session.createTransactionManager(); - var setting = TgTmSetting.of(TgTxOption.ofOCC()); - - var entity = tm.executeAndFindRecord(setting, sql, parameterMapping, parameter).get(); - - var expected = createTestEntity(1); - assertEqualsResultEntity(expected, entity); - } - - @Test - void executeAndFindRecord_sql_parameter_result() throws Exception { - var foo = TgBindVariable.ofInt("foo"); - var sql = SELECT_SQL + " where foo = " + foo; - var parameterMapping = TgParameterMapping.of(foo); - var parameter = TgBindParameters.of(foo.bind(1)); - - var session = getSession(); - var tm = session.createTransactionManager(TgTxOption.ofOCC()); - - var entity = tm.executeAndFindRecord(sql, parameterMapping, parameter, SELECT_MAPPING).get(); - - var expected = createTestEntity(1); - assertEquals(expected, entity); - } - - @Test - void executeAndFindRecord_setting_sql_parameter_result() throws Exception { - var foo = TgBindVariable.ofInt("foo"); - var sql = SELECT_SQL + " where foo = " + foo; - var parameterMapping = TgParameterMapping.of(foo); - var parameter = TgBindParameters.of(foo.bind(1)); - - var session = getSession(); - var tm = session.createTransactionManager(); - var setting = TgTmSetting.of(TgTxOption.ofOCC()); - - var entity = tm.executeAndFindRecord(setting, sql, parameterMapping, parameter, SELECT_MAPPING).get(); - - var expected = createTestEntity(1); - assertEquals(expected, entity); - } - - @Test - void executeAndFindRecord_ps() throws Exception { - var sql = SELECT_SQL + " where foo = 1"; - - var session = getSession(); - var tm = session.createTransactionManager(TgTxOption.ofOCC()); - - try (var ps = session.createQuery(sql)) { - var entity = tm.executeAndFindRecord(ps).get(); - - var expected = createTestEntity(1); - assertEqualsResultEntity(expected, entity); - } - } - - @Test - void executeAndFindRecord_setting_ps() throws Exception { - var sql = SELECT_SQL + " where foo = 1"; - - var session = getSession(); - var tm = session.createTransactionManager(); - var setting = TgTmSetting.of(TgTxOption.ofOCC()); - - try (var ps = session.createQuery(sql)) { - var entity = tm.executeAndFindRecord(setting, ps).get(); - - var expected = createTestEntity(1); - assertEqualsResultEntity(expected, entity); - } - } - - @Test - void executeAndFindRecord_ps_parameter() throws Exception { - var foo = TgBindVariable.ofInt("foo"); - var sql = SELECT_SQL + " where foo = " + foo; - var parameterMapping = TgParameterMapping.of(foo); - var parameter = TgBindParameters.of(foo.bind(1)); - - var session = getSession(); - var tm = session.createTransactionManager(TgTxOption.ofOCC()); - - try (var ps = session.createQuery(sql, parameterMapping)) { - var entity = tm.executeAndFindRecord(ps, parameter).get(); - - var expected = createTestEntity(1); - assertEqualsResultEntity(expected, entity); - } - } - - @Test - void executeAndFindRecord_setting_ps_parameter() throws Exception { - var foo = TgBindVariable.ofInt("foo"); - var sql = SELECT_SQL + " where foo = " + foo; - var parameterMapping = TgParameterMapping.of(foo); - var parameter = TgBindParameters.of(foo.bind(1)); - - var session = getSession(); - var tm = session.createTransactionManager(); - var setting = TgTmSetting.of(TgTxOption.ofOCC()); - - try (var ps = session.createQuery(sql, parameterMapping)) { - var entity = tm.executeAndFindRecord(setting, ps, parameter).get(); - - var expected = createTestEntity(1); - assertEqualsResultEntity(expected, entity); - } - } - - private static void assertEqualsTestTableResultEntity(int expectedSize, List actualList) { - assertEquals(expectedSize, actualList.size()); - for (int i = 0; i < expectedSize; i++) { - var expected = createTestEntity(i); - var actual = actualList.get(i); - assertEqualsResultEntity(expected, actual); - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbManagerExecuteStatementTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbManagerExecuteStatementTest.java deleted file mode 100755 index 5e2485f8..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbManagerExecuteStatementTest.java +++ /dev/null @@ -1,148 +0,0 @@ -package com.tsurugidb.iceaxe.test.transaction; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; - -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.transaction.manager.TgTmSetting; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; - -/** - * TransactionManager execute statement test - */ -class DbManagerExecuteStatementTest extends DbTestTableTester { - - private static final int SIZE = 2; - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTestTable(); - insertTestTable(SIZE); - - logInitEnd(info); - } - - private static final String SQL; - static { - var entity = createTestEntity(SIZE); - SQL = "insert into " + TEST // - + " (" + TEST_COLUMNS + ")" // - + " values(" + entity.getFoo() + "," + entity.getBar() + ",'" + entity.getZzz() + "')"; - } - - @Test - void executeAndGetCount_sql() throws Exception { - var session = getSession(); - var tm = session.createTransactionManager(TgTxOption.ofOCC()); - - int result = tm.executeAndGetCount(SQL); - - assertUpdateCount(1, result); - assertEqualsTestTable(SIZE + 1); - } - - @Test - void executeAndGetCount_setting_sql() throws Exception { - var session = getSession(); - var tm = session.createTransactionManager(); - var setting = TgTmSetting.of(TgTxOption.ofOCC()); - - int result = tm.executeAndGetCount(setting, SQL); - - assertUpdateCount(1, result); - assertEqualsTestTable(SIZE + 1); - } - - @Test - void executeAndGetCount_sql_parameter() throws Exception { - var entity = createTestEntity(SIZE); - - var session = getSession(); - var tm = session.createTransactionManager(TgTxOption.ofOCC()); - - int result = tm.executeAndGetCount(INSERT_SQL, INSERT_MAPPING, entity); - - assertUpdateCount(1, result); - assertEqualsTestTable(SIZE + 1); - } - - @Test - void executeAndGetCount_setting_sql_parameter() throws Exception { - var entity = createTestEntity(SIZE); - - var session = getSession(); - var tm = session.createTransactionManager(); - var setting = TgTmSetting.of(TgTxOption.ofOCC()); - - int result = tm.executeAndGetCount(setting, INSERT_SQL, INSERT_MAPPING, entity); - - assertUpdateCount(1, result); - assertEqualsTestTable(SIZE + 1); - } - - @Test - void executeAndGetCount_ps() throws Exception { - var session = getSession(); - var tm = session.createTransactionManager(TgTxOption.ofOCC()); - - try (var ps = session.createStatement(SQL)) { - int result = tm.executeAndGetCount(ps); - - assertUpdateCount(1, result); - } - - assertEqualsTestTable(SIZE + 1); - } - - @Test - void executeAndGetCount_setting_ps() throws Exception { - var session = getSession(); - var tm = session.createTransactionManager(); - var setting = TgTmSetting.of(TgTxOption.ofOCC()); - - try (var ps = session.createStatement(SQL)) { - int result = tm.executeAndGetCount(setting, ps); - - assertUpdateCount(1, result); - } - - assertEqualsTestTable(SIZE + 1); - } - - @Test - void executeAndGetCount_ps_parameter() throws Exception { - var entity = createTestEntity(SIZE); - - var session = getSession(); - var tm = session.createTransactionManager(TgTxOption.ofOCC()); - - try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING)) { - int result = tm.executeAndGetCount(ps, entity); - - assertUpdateCount(1, result); - } - - assertEqualsTestTable(SIZE + 1); - } - - @Test - void executeAndGetCount_setting_ps_parameter() throws Exception { - var entity = createTestEntity(SIZE); - - var session = getSession(); - var tm = session.createTransactionManager(); - var setting = TgTmSetting.of(TgTxOption.ofOCC()); - - try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING)) { - int result = tm.executeAndGetCount(setting, ps, entity); - - assertUpdateCount(1, result); - } - - assertEqualsTestTable(SIZE + 1); - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbTransactionConflictLtxTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbTransactionConflictLtxTest.java deleted file mode 100755 index 9acf82d5..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbTransactionConflictLtxTest.java +++ /dev/null @@ -1,324 +0,0 @@ -package com.tsurugidb.iceaxe.test.transaction; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertInstanceOf; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertThrowsExactly; - -import java.util.concurrent.ExecutionException; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; - -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.transaction.TgCommitType; -import com.tsurugidb.iceaxe.transaction.exception.TsurugiTransactionException; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; -import com.tsurugidb.tsubakuro.sql.SqlServiceCode; - -/** - * transaction conflict test - */ -class DbTransactionConflictLtxTest extends DbTestTableTester { - - private static final int SIZE = 4; - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTestTable(); - insertTestTable(SIZE); - - logInitEnd(info); - } - - private static final TgTxOption OCC = TgTxOption.ofOCC(); - private static final TgTxOption LTX = TgTxOption.ofLTX(TEST); - private static final TgTxOption RTX = TgTxOption.ofRTX(); - - private static final int KEY = 1; - private static final long BAR_BEFORE = 1; - private static final long BAR_AFTER1 = 789; - private static final long BAR_AFTER2 = 999; - private static final String SELECT_SQL1 = SELECT_SQL + " where foo = " + KEY; - private static final String UPDATE_SQL1 = "update " + TEST + " set bar = " + BAR_AFTER1 + " where foo = " + KEY; - private static final String UPDATE_SQL2 = "update " + TEST + " set bar = " + BAR_AFTER2 + " where foo = " + KEY; - private static final String DELETE_SQL = "delete from " + TEST + " where foo = " + (SIZE - 1); - - @Test - void ltx_occR() throws Exception { - var session = getSession(); - try (var selectPs = session.createQuery(SELECT_SQL1, SELECT_MAPPING); // - var updatePs = session.createStatement(UPDATE_SQL1)) { - try (var tx1 = session.createTransaction(LTX)) { - tx1.executeAndGetCount(updatePs); - - try (var tx2 = session.createTransaction(OCC)) { - var e = assertThrows(TsurugiTransactionException.class, () -> { - tx2.executeAndFindRecord(selectPs).get(); - }); - assertEqualsCode(SqlServiceCode.CC_EXCEPTION, e); - - tx2.rollback(); - } - - var entity12 = tx1.executeAndFindRecord(selectPs).get(); - assertEquals(BAR_AFTER1, entity12.getBar()); - - tx1.commit(TgCommitType.DEFAULT); - } - } - } - - @Test - void ltx_rtx() throws Exception { - var session = getSession(); - try (var selectPs = session.createQuery(SELECT_SQL1, SELECT_MAPPING); // - var updatePs = session.createStatement(UPDATE_SQL1)) { - try (var tx1 = session.createTransaction(LTX)) { - tx1.executeAndGetCount(updatePs); - - try (var tx2 = session.createTransaction(RTX)) { - var entity2 = tx2.executeAndFindRecord(selectPs).get(); - assertEquals(BAR_BEFORE, entity2.getBar()); - - tx2.commit(TgCommitType.DEFAULT); - } - - var entity12 = tx1.executeAndFindRecord(selectPs).get(); - assertEquals(BAR_AFTER1, entity12.getBar()); - - tx1.commit(TgCommitType.DEFAULT); - } - } - } - - @Test - void ltx_occW() throws Exception { - var session = getSession(); - try (var selectPs = session.createQuery(SELECT_SQL1, SELECT_MAPPING); // - var updatePs = session.createStatement(UPDATE_SQL1); // - var updatePs2 = session.createStatement(UPDATE_SQL2)) { - try (var tx1 = session.createTransaction(LTX)) { - tx1.executeAndGetCount(updatePs); - - try (var tx2 = session.createTransaction(OCC)) { - var e = assertThrows(TsurugiTransactionException.class, () -> { - tx2.executeAndGetCount(updatePs2); - }); - assertEqualsCode(SqlServiceCode.CC_EXCEPTION, e); - - tx2.rollback(); - } - - var entity12 = tx1.executeAndFindRecord(selectPs).get(); - assertEquals(BAR_AFTER1, entity12.getBar()); - - tx1.commit(TgCommitType.DEFAULT); - } - } - } - - @Test - void ltx_ltx() throws Exception { - var session = getSession(); - try (var selectPs = session.createQuery(SELECT_SQL1, SELECT_MAPPING); // - var updatePs = session.createStatement(UPDATE_SQL1); // - var updatePs2 = session.createStatement(UPDATE_SQL2)) { - try (var tx1 = session.createTransaction(LTX)) { - tx1.executeAndGetCount(updatePs); - - try (var tx2 = session.createTransaction(LTX)) { - tx2.executeAndGetCount(updatePs2); - - var future2 = executeFuture(() -> { - tx2.commit(TgCommitType.DEFAULT); - return null; - }); - - var entity12 = tx1.executeAndFindRecord(selectPs).get(); - assertEquals(BAR_AFTER1, entity12.getBar()); - - Thread.sleep(100); - assertFalse(future2.isDone()); - tx1.commit(TgCommitType.DEFAULT); - - var e = assertThrowsExactly(ExecutionException.class, () -> { - future2.get(); - }); - assertInstanceOf(TsurugiTransactionException.class, e.getCause()); - assertEqualsCode(SqlServiceCode.CC_EXCEPTION, e); - } - } - } - - var list = selectAllFromTest(); - int i = 0; - for (var entity : list) { - var expected = createTestEntity(i++); - if (expected.getFoo() == KEY) { - expected.setBar(BAR_AFTER1); - } - assertEquals(expected, entity); - } - } - - @Test - void ltx_ltx2() throws Exception { - var session = getSession(); - try (var selectPs = session.createQuery(SELECT_SQL1, SELECT_MAPPING); // - var updatePs = session.createStatement(UPDATE_SQL1); // - var updatePs2 = session.createStatement(UPDATE_SQL2)) { - try (var tx1 = session.createTransaction(LTX)) { - tx1.executeAndGetCount(updatePs); - - try (var tx2 = session.createTransaction(LTX)) { - tx2.executeAndGetCount(updatePs2); - - var entity12 = tx1.executeAndFindRecord(selectPs).get(); - assertEquals(BAR_AFTER1, entity12.getBar()); - - tx1.commit(TgCommitType.DEFAULT); - - var e = assertThrows(TsurugiTransactionException.class, () -> { - tx2.commit(TgCommitType.DEFAULT); - }); - assertEqualsCode(SqlServiceCode.CC_EXCEPTION, e); - } - } - } - } - - @Test - void ltx_ltx3() throws Exception { - var session = getSession(); - try (var selectPs = session.createQuery(SELECT_SQL1, SELECT_MAPPING); // - var updatePs = session.createStatement("update " + TEST + " set bar = bar + 1 where foo = " + KEY); // - var updatePs2 = session.createStatement("update " + TEST + " set bar = bar + 2 where foo = " + KEY)) { - try (var tx1 = session.createTransaction(LTX)) { - tx1.executeAndGetCount(updatePs); - - try (var tx2 = session.createTransaction(LTX)) { - tx2.executeAndGetCount(updatePs2); - var entity22 = tx2.executeAndFindRecord(selectPs).get(); - assertEquals(BAR_BEFORE + 2, entity22.getBar()); - - var future2 = executeFuture(() -> { - tx2.commit(TgCommitType.DEFAULT); - return null; - }); - - var entity12 = tx1.executeAndFindRecord(selectPs).get(); - assertEquals(BAR_BEFORE + 1, entity12.getBar()); - - Thread.sleep(100); - assertFalse(future2.isDone()); - tx1.commit(TgCommitType.DEFAULT); - - var e = assertThrowsExactly(ExecutionException.class, () -> { - future2.get(); - }); - assertInstanceOf(TsurugiTransactionException.class, e.getCause()); - assertEqualsCode(SqlServiceCode.CC_EXCEPTION, e); - } - } - } - - var list = selectAllFromTest(); - int i = 0; - for (var entity : list) { - var expected = createTestEntity(i++); - if (expected.getFoo() == KEY) { - expected.setBar(BAR_BEFORE + 1); - } - assertEquals(expected, entity); - } - } - - @ParameterizedTest - @ValueSource(ints = { 1, -1 }) - void ltx_occW_phantom(int add) throws Exception { - var session = getSession(); - try (var selectPs = session.createQuery(SELECT_SQL, SELECT_MAPPING); // - var insertPs = session.createStatement(INSERT_SQL, INSERT_MAPPING); // - var deletePs = session.createStatement(DELETE_SQL)) { - try (var tx1 = session.createTransaction(LTX)) { - var list11 = tx1.executeAndGetList(selectPs); - assertEquals(SIZE, list11.size()); - - try (var tx2 = session.createTransaction(OCC)) { - var e = assertThrows(TsurugiTransactionException.class, () -> { - if (add > 0) { - var entity2 = createTestEntity(SIZE); - tx2.executeAndGetCount(insertPs, entity2); - } else { - tx2.executeAndGetCount(deletePs); - } - }); -//TODO assertEqualsCode(SqlServiceCode.CC_EXCEPTION, e); - var code = e.getDiagnosticCode(); - if (code == SqlServiceCode.CC_EXCEPTION || code == SqlServiceCode.CONFLICT_ON_WRITE_PRESERVE_EXCEPTION) { - // success - } else { - throw e; - } - - tx2.rollback(); - } - - var list12 = tx1.executeAndGetList(selectPs); - assertEquals(SIZE, list12.size()); - - tx1.commit(TgCommitType.DEFAULT); - } - } - } - - @ParameterizedTest - @ValueSource(ints = { 1, -1 }) - void ltx_ltx_phantom(int add) throws Exception { - var session = getSession(); - try (var selectPs = session.createQuery(SELECT_SQL, SELECT_MAPPING); // - var insertPs = session.createStatement(INSERT_SQL, INSERT_MAPPING); // - var deletePs = session.createStatement(DELETE_SQL)) { - try (var tx1 = session.createTransaction(LTX)) { - var list11 = tx1.executeAndGetList(selectPs); - assertEquals(SIZE, list11.size()); - - try (var tx2 = session.createTransaction(LTX)) { - if (add > 0) { - var entity2 = createTestEntity(SIZE); - tx2.executeAndGetCount(insertPs, entity2); - } else { - tx2.executeAndGetCount(deletePs); - } - - var future2 = executeFuture(() -> { - tx2.commit(TgCommitType.DEFAULT); - return null; - }); - - var list12 = tx1.executeAndGetList(selectPs); - assertEquals(SIZE, list12.size()); - - assertFalse(future2.isDone()); - tx1.commit(TgCommitType.DEFAULT); - - var e = assertThrowsExactly(ExecutionException.class, () -> { - future2.get(); - }); - assertInstanceOf(TsurugiTransactionException.class, e.getCause()); - assertEqualsCode(SqlServiceCode.CC_EXCEPTION, e); - } - } - } - - assertEqualsTestTable(SIZE); - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbTransactionConflictOccTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbTransactionConflictOccTest.java deleted file mode 100755 index 4d025c65..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbTransactionConflictOccTest.java +++ /dev/null @@ -1,332 +0,0 @@ -package com.tsurugidb.iceaxe.test.transaction; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; - -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.transaction.TgCommitType; -import com.tsurugidb.iceaxe.transaction.exception.TsurugiTransactionException; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; -import com.tsurugidb.tsubakuro.sql.SqlServiceCode; - -/** - * transaction conflict test - */ -class DbTransactionConflictOccTest extends DbTestTableTester { - - private static final int SIZE = 4; - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTestTable(); - insertTestTable(SIZE); - - logInitEnd(info); - } - - private static final TgTxOption OCC = TgTxOption.ofOCC(); - private static final TgTxOption LTX = TgTxOption.ofLTX(TEST); - private static final TgTxOption RTX = TgTxOption.ofRTX(); - - private static final int KEY = 1; - private static final long BAR_BEFORE = 1; - private static final long BAR_AFTER1 = 789; - private static final long BAR_AFTER2 = 999; - private static final String SELECT_SQL1 = SELECT_SQL + " where foo = " + KEY; - private static final String UPDATE_SQL1 = "update " + TEST + " set bar = " + BAR_AFTER1 + " where foo = " + KEY; - private static final String UPDATE_SQL2 = "update " + TEST + " set bar = " + BAR_AFTER2 + " where foo = " + KEY; - private static final String DELETE_SQL = "delete from " + TEST + " where foo = " + (SIZE - 1); - - @Test - void occR_occR() throws Exception { - occR_r(OCC); - } - - @Test - void occR_rtx() throws Exception { - occR_r(RTX); - } - - private void occR_r(TgTxOption txOption2) throws Exception { - var session = getSession(); - try (var selectPs = session.createQuery(SELECT_SQL1, SELECT_MAPPING)) { - try (var tx1 = session.createTransaction(OCC)) { - var entity11 = tx1.executeAndFindRecord(selectPs).get(); - assertEquals(BAR_BEFORE, entity11.getBar()); - - try (var tx2 = session.createTransaction(txOption2)) { - var entity2 = tx2.executeAndFindRecord(selectPs).get(); - assertEquals(BAR_BEFORE, entity2.getBar()); - - tx2.commit(TgCommitType.DEFAULT); - } - - var entity12 = tx1.executeAndFindRecord(selectPs).get(); - assertEquals(BAR_BEFORE, entity12.getBar()); - - tx1.commit(TgCommitType.DEFAULT); - } - } - } - - @Test - void occR_occW() throws Exception { - occR_w(OCC); - } - - @Test - void occR_ltx() throws Exception { - occR_w(LTX); - } - - private void occR_w(TgTxOption txOption2) throws Exception { - var session = getSession(); - try (var selectPs = session.createQuery(SELECT_SQL1, SELECT_MAPPING); // - var updatePs = session.createStatement(UPDATE_SQL1)) { - try (var tx1 = session.createTransaction(OCC)) { - var entity11 = tx1.executeAndFindRecord(selectPs).get(); - assertEquals(BAR_BEFORE, entity11.getBar()); - - try (var tx2 = session.createTransaction(txOption2)) { - tx2.executeAndGetCount(updatePs); - - tx2.commit(TgCommitType.DEFAULT); - } - - var entity12 = tx1.executeAndFindRecord(selectPs).get(); - assertEquals(BAR_AFTER1, entity12.getBar()); - - var e = assertThrows(TsurugiTransactionException.class, () -> { - tx1.commit(TgCommitType.DEFAULT); - }); - assertEqualsCode(SqlServiceCode.CC_EXCEPTION, e); - } - } - } - - @ParameterizedTest - @ValueSource(ints = { 1, -1 }) - void occR_occW_phantom(int add) throws Exception { - occR_w_phantom(OCC, add); - } - - @ParameterizedTest - @ValueSource(ints = { 1, -1 }) - void occR_ltx_phantom(int add) throws Exception { - occR_w_phantom(LTX, add); - } - - private void occR_w_phantom(TgTxOption txOption2, int add) throws Exception { - var session = getSession(); - try (var selectPs = session.createQuery(SELECT_SQL, SELECT_MAPPING); // - var insertPs = session.createStatement(INSERT_SQL, INSERT_MAPPING); // - var deletePs = session.createStatement(DELETE_SQL)) { - try (var tx1 = session.createTransaction(OCC)) { - var list11 = tx1.executeAndGetList(selectPs); - assertEquals(SIZE, list11.size()); - - try (var tx2 = session.createTransaction(txOption2)) { - if (add > 0) { - var entity2 = createTestEntity(SIZE); - tx2.executeAndGetCount(insertPs, entity2); - } else { - tx2.executeAndGetCount(deletePs); - } - - tx2.commit(TgCommitType.DEFAULT); - } - - var list12 = tx1.executeAndGetList(selectPs); - assertEquals(SIZE + add, list12.size()); - - var e = assertThrows(TsurugiTransactionException.class, () -> { - tx1.commit(TgCommitType.DEFAULT); - }); - assertEqualsCode(SqlServiceCode.CC_EXCEPTION, e); - } - } - - assertEqualsTestTable(SIZE + add); - } - - @Test - void occW_occR() throws Exception { - occW_r(OCC); - } - - @Test - void occW_rtx() throws Exception { - occW_r(RTX); - } - - private void occW_r(TgTxOption txOption2) throws Exception { - var session = getSession(); - try (var selectPs = session.createQuery(SELECT_SQL1, SELECT_MAPPING); // - var updatePs = session.createStatement(UPDATE_SQL1)) { - try (var tx1 = session.createTransaction(OCC)) { - tx1.executeAndGetCount(updatePs); - - try (var tx2 = session.createTransaction(txOption2)) { - var entity2 = tx2.executeAndFindRecord(selectPs).get(); - assertEquals(BAR_BEFORE, entity2.getBar()); - - tx2.commit(TgCommitType.DEFAULT); - } - - var entity12 = tx1.executeAndFindRecord(selectPs).get(); - assertEquals(BAR_AFTER1, entity12.getBar()); - - tx1.commit(TgCommitType.DEFAULT); - } - } - } - - @Test - void occW_occW() throws Exception { - occW_w(OCC); - } - - @Test - void occW_ltx() throws Exception { - occW_w(LTX); - } - - private void occW_w(TgTxOption txOption2) throws Exception { - var session = getSession(); - try (var selectPs = session.createQuery(SELECT_SQL1, SELECT_MAPPING); // - var updatePs = session.createStatement(UPDATE_SQL1); // - var updatePs2 = session.createStatement(UPDATE_SQL2)) { - try (var tx1 = session.createTransaction(OCC)) { - tx1.executeAndGetCount(updatePs); - - try (var tx2 = session.createTransaction(txOption2)) { - tx2.executeAndGetCount(updatePs2); - - tx2.commit(TgCommitType.DEFAULT); - } - - var entity12 = tx1.executeAndFindRecord(selectPs).get(); - assertEquals(BAR_AFTER1, entity12.getBar()); - - var e = assertThrows(TsurugiTransactionException.class, () -> { - tx1.commit(TgCommitType.DEFAULT); - }); - assertEqualsCode(SqlServiceCode.CC_EXCEPTION, e); - } - } - } - - @ParameterizedTest - @ValueSource(ints = { 0, 1, 2, 3 }) - void occW_ltx2(int position) throws Exception { - var session = getSession(); - try (var selectPs = session.createQuery(SELECT_SQL1, SELECT_MAPPING); // - var updatePs = session.createStatement(UPDATE_SQL1); // - var updatePs2 = session.createStatement(UPDATE_SQL2)) { - try (var tx1 = session.createTransaction(OCC)) { - var entity11 = tx1.executeAndFindRecord(selectPs).get(); - long expected = BAR_BEFORE; - assertEquals(expected, entity11.getBar()); - - if (position == 1) { - tx1.executeAndGetCount(updatePs); - expected = BAR_AFTER1; - } - try (var tx2 = session.createTransaction(LTX)) { - var entity2 = tx2.executeAndFindRecord(selectPs).get(); - assertEquals(BAR_BEFORE, entity2.getBar()); - - if (position == 2) { - var e = assertThrows(TsurugiTransactionException.class, () -> { - tx1.executeAndGetCount(updatePs); - }); - assertEqualsCode(SqlServiceCode.CC_EXCEPTION, e); - tx1.rollback(); - } - - tx2.executeAndGetCount(updatePs2); - - tx2.commit(TgCommitType.DEFAULT); - - if (position == 2) { - return; - } - if (position != 1) { - expected = BAR_AFTER2; - } - } - if (position == 3) { - tx1.executeAndGetCount(updatePs); - expected = BAR_AFTER1; - } - - var entity12 = tx1.executeAndFindRecord(selectPs).get(); - assertEquals(expected, entity12.getBar()); - - var e = assertThrows(TsurugiTransactionException.class, () -> { - tx1.commit(TgCommitType.DEFAULT); - }); - assertEqualsCode(SqlServiceCode.CC_EXCEPTION, e); - } - } - } - - @Test - void occW_occW3() throws Exception { - var session = getSession(); - try (var selectPs = session.createQuery(SELECT_SQL1, SELECT_MAPPING); // - var updatePs = session.createStatement(UPDATE_SQL1); // - var updatePs2 = session.createStatement(UPDATE_SQL2)) { - try (var tx1 = session.createTransaction(OCC)) { - tx1.executeAndGetCount(updatePs); - - try (var tx2 = session.createTransaction(OCC)) { - tx2.executeAndGetCount(updatePs2); - - var entity12 = tx1.executeAndFindRecord(selectPs).get(); - assertEquals(BAR_AFTER1, entity12.getBar()); - - tx1.commit(TgCommitType.DEFAULT); - - var e = assertThrows(TsurugiTransactionException.class, () -> { - tx2.commit(TgCommitType.DEFAULT); - }); - assertEqualsCode(SqlServiceCode.CC_EXCEPTION, e); - } - } - } - } - - @Test - void occW_ltx3() throws Exception { - var session = getSession(); - try (var selectPs = session.createQuery(SELECT_SQL1, SELECT_MAPPING); // - var updatePs = session.createStatement(UPDATE_SQL1); // - var updatePs2 = session.createStatement(UPDATE_SQL2)) { - try (var tx1 = session.createTransaction(OCC)) { - tx1.executeAndGetCount(updatePs); - - try (var tx2 = session.createTransaction(LTX)) { - tx2.executeAndGetCount(updatePs2); - - var e = assertThrows(TsurugiTransactionException.class, () -> { - tx1.executeAndFindRecord(selectPs).get(); - }); - assertEqualsCode(SqlServiceCode.CC_EXCEPTION, e); - tx1.rollback(); - - tx2.commit(TgCommitType.DEFAULT); - } - } - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbTransactionConflictRtxTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbTransactionConflictRtxTest.java deleted file mode 100755 index f7260477..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbTransactionConflictRtxTest.java +++ /dev/null @@ -1,208 +0,0 @@ -package com.tsurugidb.iceaxe.test.transaction; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; - -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.transaction.TgCommitType; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; - -/** - * transaction conflict test - */ -class DbTransactionConflictRtxTest extends DbTestTableTester { - - private static final int SIZE = 4; - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTestTable(); - insertTestTable(SIZE); - - logInitEnd(info); - } - - private static final TgTxOption OCC = TgTxOption.ofOCC(); - private static final TgTxOption LTX = TgTxOption.ofLTX(TEST); - private static final TgTxOption RTX = TgTxOption.ofRTX(); - - private static final int KEY = 1; - private static final long BAR_BEFORE = 1; - private static final long BAR_AFTER2 = 999; - private static final String SELECT_SQL1 = SELECT_SQL + " where foo = " + KEY; - private static final String UPDATE_SQL2 = "update " + TEST + " set bar = " + BAR_AFTER2 + " where foo = " + KEY; - private static final String DELETE_SQL = "delete from " + TEST + " where foo = " + (SIZE - 1); - - @Test - void rtx_occR() throws Exception { - rtx_r(OCC); - } - - @Test - void rtx_rtx() throws Exception { - rtx_r(RTX); - } - - private void rtx_r(TgTxOption txOption2) throws Exception { - var session = getSession(); - try (var selectPs = session.createQuery(SELECT_SQL1, SELECT_MAPPING)) { - try (var tx1 = session.createTransaction(RTX)) { - var entity11 = tx1.executeAndFindRecord(selectPs).get(); - assertEquals(BAR_BEFORE, entity11.getBar()); - - try (var tx2 = session.createTransaction(txOption2)) { - var entity2 = tx2.executeAndFindRecord(selectPs).get(); - assertEquals(BAR_BEFORE, entity2.getBar()); - - tx2.commit(TgCommitType.DEFAULT); - } - - var entity12 = tx1.executeAndFindRecord(selectPs).get(); - assertEquals(BAR_BEFORE, entity12.getBar()); - - tx1.commit(TgCommitType.DEFAULT); - } - } - } - - @Test - void rtx_occW() throws Exception { - var session = getSession(); - try (var selectPs = session.createQuery(SELECT_SQL1, SELECT_MAPPING); // - var updatePs2 = session.createStatement(UPDATE_SQL2)) { - try (var tx1 = session.createTransaction(RTX)) { - var entity11 = tx1.executeAndFindRecord(selectPs).get(); - assertEquals(BAR_BEFORE, entity11.getBar()); - - try (var tx2 = session.createTransaction(OCC)) { - tx2.executeAndGetCount(updatePs2); - - tx2.commit(TgCommitType.DEFAULT); - } - - var entity12 = tx1.executeAndFindRecord(selectPs).get(); - assertEquals(BAR_BEFORE, entity12.getBar()); - - tx1.commit(TgCommitType.DEFAULT); - } - } - } - - @Test - void rtx_ltx() throws Exception { - var session = getSession(); - try (var selectPs = session.createQuery(SELECT_SQL1, SELECT_MAPPING); // - var updatePs2 = session.createStatement(UPDATE_SQL2)) { - try (var tx1 = session.createTransaction(RTX)) { - var entity11 = tx1.executeAndFindRecord(selectPs).get(); - assertEquals(BAR_BEFORE, entity11.getBar()); - - try (var tx2 = session.createTransaction(LTX)) { - tx2.executeAndGetCount(updatePs2); - - tx2.commit(TgCommitType.DEFAULT); - - var entity12 = tx1.executeAndFindRecord(selectPs).get(); - assertEquals(BAR_BEFORE, entity12.getBar()); - - tx1.commit(TgCommitType.DEFAULT); - } - } - } - } - - @Test - void rtx_ltx2() throws Exception { - var session = getSession(); - try (var selectPs = session.createQuery(SELECT_SQL1, SELECT_MAPPING); // - var updatePs2 = session.createStatement(UPDATE_SQL2)) { - try (var tx1 = session.createTransaction(LTX)) { - var entity11 = tx1.executeAndFindRecord(selectPs).get(); - assertEquals(BAR_BEFORE, entity11.getBar()); - - try (var tx2 = session.createTransaction(LTX)) { - tx2.executeAndGetCount(updatePs2); - - var entity12 = tx1.executeAndFindRecord(selectPs).get(); - assertEquals(BAR_BEFORE, entity12.getBar()); - - tx1.commit(TgCommitType.DEFAULT); - - tx2.commit(TgCommitType.DEFAULT); - } - } - } - } - - @ParameterizedTest - @ValueSource(ints = { 1, -1 }) - void rtx_occW_phantom(int add) throws Exception { - var session = getSession(); - try (var selectPs = session.createQuery(SELECT_SQL, SELECT_MAPPING); // - var insertPs = session.createStatement(INSERT_SQL, INSERT_MAPPING); // - var deletePs = session.createStatement(DELETE_SQL)) { - try (var tx1 = session.createTransaction(RTX)) { - var list11 = tx1.executeAndGetList(selectPs); - assertEquals(SIZE, list11.size()); - - try (var tx2 = session.createTransaction(OCC)) { - if (add > 0) { - var entity2 = createTestEntity(SIZE); - tx2.executeAndGetCount(insertPs, entity2); - } else { - tx2.executeAndGetCount(deletePs); - } - - tx2.commit(TgCommitType.DEFAULT); - } - - var list12 = tx1.executeAndGetList(selectPs); - assertEquals(SIZE, list12.size()); - - tx1.commit(TgCommitType.DEFAULT); - } - } - - assertEqualsTestTable(SIZE + add); - } - - @ParameterizedTest - @ValueSource(ints = { 1, -1 }) - void rtx_ltx_phantom(int add) throws Exception { - var session = getSession(); - try (var selectPs = session.createQuery(SELECT_SQL, SELECT_MAPPING); // - var insertPs = session.createStatement(INSERT_SQL, INSERT_MAPPING); // - var deletePs = session.createStatement(DELETE_SQL)) { - try (var tx1 = session.createTransaction(RTX)) { - var list11 = tx1.executeAndGetList(selectPs); - assertEquals(SIZE, list11.size()); - - try (var tx2 = session.createTransaction(LTX)) { - if (add > 0) { - var entity2 = createTestEntity(SIZE); - tx2.executeAndGetCount(insertPs, entity2); - } else { - tx2.executeAndGetCount(deletePs); - } - - tx2.commit(TgCommitType.DEFAULT); - - var list12 = tx1.executeAndGetList(selectPs); - assertEquals(SIZE, list12.size()); - - tx1.commit(TgCommitType.DEFAULT); - } - } - } - - assertEqualsTestTable(SIZE + add); - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbTransactionErrorTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbTransactionErrorTest.java deleted file mode 100755 index 35217724..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbTransactionErrorTest.java +++ /dev/null @@ -1,223 +0,0 @@ -package com.tsurugidb.iceaxe.test.transaction; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertSame; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertThrowsExactly; -import static org.junit.jupiter.api.Assertions.fail; - -import java.io.IOException; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; - -import com.tsurugidb.iceaxe.exception.IceaxeErrorCode; -import com.tsurugidb.iceaxe.exception.TsurugiIOException; -import com.tsurugidb.iceaxe.test.util.DbTestConnector; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.transaction.TgCommitType; -import com.tsurugidb.iceaxe.transaction.exception.TsurugiTransactionException; -import com.tsurugidb.iceaxe.transaction.manager.exception.TsurugiTmIOException; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; -import com.tsurugidb.tsubakuro.sql.SqlServiceCode; - -/** - * Transaction error test - */ -class DbTransactionErrorTest extends DbTestTableTester { - - private static final int SIZE = 4; - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTestTable(); - insertTestTable(SIZE); - - logInitEnd(info); - } - - @Test - void notCommitRollback() throws Exception { - var sql = "insert into " + TEST // - + "(" + TEST_COLUMNS + ")" // - + "values(123, 456, 'abc')"; - - var session = getSession(); - try (var ps = session.createStatement(sql); // - var transaction = session.createTransaction(TgTxOption.ofOCC())) { - int count = transaction.executeAndGetCount(ps); - assertUpdateCount(1, count); - - // do not commit,rollback - } - - // expected: auto rollback - assertEqualsTestTable(SIZE); - } - - @Test - void writeToReadOnly() throws Exception { - var sql = "insert into " + TEST // - + "(" + TEST_COLUMNS + ")" // - + "values(123, 456, 'abc')"; - - var session = getSession(); - var tm = session.createTransactionManager(TgTxOption.ofRTX()); - try (var ps = session.createStatement(sql)) { - var e = assertThrowsExactly(TsurugiTmIOException.class, () -> tm.executeAndGetCount(ps)); - assertEqualsCode(SqlServiceCode.WRITE_OPERATION_BY_RTX_EXCEPTION, e); - assertContains("Write operation by rtx", e.getMessage()); - } - - // expected: auto rollback - assertEqualsTestTable(SIZE); - } - - @Test - void ltxWithoutWritePreserve() throws Exception { - var sql = "insert into " + TEST // - + "(" + TEST_COLUMNS + ")" // - + "values(123, 456, 'abc')"; - - var session = getSession(); - var tm = session.createTransactionManager(TgTxOption.ofLTX()); // no WritePreserve - try (var ps = session.createStatement(sql)) { - var e = assertThrowsExactly(TsurugiTmIOException.class, () -> tm.executeAndGetCount(ps)); - assertEqualsCode(SqlServiceCode.LTX_WRITE_OPERATION_WITHOUT_WRITE_PRESERVE_EXCEPTION, e); - assertContains("Ltx write operation outside write preserve", e.getMessage()); // TODO エラー詳細情報(テーブル名) - } - - // expected: auto rollback - assertEqualsTestTable(SIZE); - } - - @Test - void limitOver() throws Exception { - try (var session = DbTestConnector.createSession()) { - for (int i = 0; i < 1000; i++) { - LOG.trace("i={}", i); - var tx = session.createTransaction(TgTxOption.ofOCC()); - try { - tx.getLowTransaction(); - } catch (TsurugiIOException e) { - assertEqualsCode(SqlServiceCode.TRANSACTION_EXCEEDED_LIMIT_EXCEPTION, e); - assertContains("The number of transactions exceeded the limit", e.getMessage()); - return; - } - } - fail("TRANSACTION_EXCEEDED_LIMIT_EXCEPTION did not occur"); - } - } - - @Test - void executeAfterCommit() throws Exception { - var session = getSession(); - try (var transaction = session.createTransaction(TgTxOption.ofOCC())) { - - transaction.commit(TgCommitType.DEFAULT); - - try (var ps = session.createQuery(SELECT_SQL)) { - var e = assertThrows(IOException.class, () -> transaction.executeAndGetList(ps)); - assertEquals("transaction already closed", e.getMessage()); - } - } - } - - @Test - void executeAfterRollback() throws Exception { - var session = getSession(); - try (var transaction = session.createTransaction(TgTxOption.ofOCC())) { - - transaction.rollback(); - - try (var ps = session.createQuery(SELECT_SQL)) { - var e = assertThrows(IOException.class, () -> transaction.executeAndGetList(ps)); - assertEquals("transaction already closed", e.getMessage()); - } - } - } - - @Test - void executeAfterError() throws Exception { - var session = getSession(); - try (var transaction = session.createTransaction(TgTxOption.ofOCC())) { - try (var insertPs = session.createStatement(INSERT_SQL, INSERT_MAPPING); // - var selectPs = session.createQuery(SELECT_SQL)) { - var entity = createTestEntity(1); - var e1 = assertThrowsExactly(TsurugiTransactionException.class, () -> transaction.executeAndGetCount(insertPs, entity)); - assertEqualsCode(SqlServiceCode.UNIQUE_CONSTRAINT_VIOLATION_EXCEPTION, e1); - - var e2 = assertThrowsExactly(TsurugiTransactionException.class, () -> transaction.executeAndGetList(selectPs)); - assertEqualsCode(SqlServiceCode.INACTIVE_TRANSACTION_EXCEPTION, e2); - } - } - } - - @Test - void commitAfterClose() throws Exception { - var session = getSession(); - var transaction = session.createTransaction(TgTxOption.ofOCC()); - transaction.close(); - var e = assertThrowsExactly(TsurugiIOException.class, () -> transaction.commit(TgCommitType.DEFAULT)); - assertEqualsCode(IceaxeErrorCode.TX_ALREADY_CLOSED, e); - } - - @Test - void rollbackAfterClose() throws Exception { - var session = getSession(); - var transaction = session.createTransaction(TgTxOption.ofOCC()); - transaction.close(); - var e = assertThrowsExactly(TsurugiIOException.class, () -> transaction.rollback()); - assertEqualsCode(IceaxeErrorCode.TX_ALREADY_CLOSED, e); - } - - @Test - void addChildAfterClose() throws Exception { - var session = getSession(); - var transaction = session.createTransaction(TgTxOption.ofOCC()); - transaction.close(); - var e = assertThrowsExactly(TsurugiIOException.class, () -> { - transaction.addChild(() -> { - // dummy - }); - }); - assertEqualsCode(IceaxeErrorCode.TX_ALREADY_CLOSED, e); - } - - @Test - void getLowAfterFutureClose() throws Exception { - var session = getSession(); - var transaction = session.createTransaction(TgTxOption.ofOCC()); - transaction.close(); - var e = assertThrowsExactly(IOException.class, () -> { - transaction.getLowTransaction(); - }); - assertMatches("Future .+ is already closed", e.getMessage()); - } - - @Test - void getLowAfterClose() throws Exception { - var session = getSession(); - var transaction = session.createTransaction(TgTxOption.ofOCC()); - var lowTx = transaction.getLowTransaction(); - transaction.close(); - assertSame(lowTx, transaction.getLowTransaction()); - } - - @Test - void executeAfterClose() throws Exception { - var session = getSession(); - var transaction = session.createTransaction(TgTxOption.ofOCC()); - transaction.close(); - var e = assertThrowsExactly(TsurugiIOException.class, () -> { - transaction.executeLow(lowTx -> { - return null; // dummy - }); - }); - assertEqualsCode(IceaxeErrorCode.TX_ALREADY_CLOSED, e); - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbTransactionExceptionTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbTransactionExceptionTest.java deleted file mode 100755 index f9e8fc2c..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbTransactionExceptionTest.java +++ /dev/null @@ -1,124 +0,0 @@ -package com.tsurugidb.iceaxe.test.transaction; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertSame; -import static org.junit.jupiter.api.Assertions.assertThrows; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; - -import com.tsurugidb.iceaxe.sql.result.TsurugiStatementResult; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.test.util.TestEntity; -import com.tsurugidb.iceaxe.transaction.TsurugiTransaction.TgTxMethod; -import com.tsurugidb.iceaxe.transaction.exception.TsurugiTransactionException; -import com.tsurugidb.iceaxe.transaction.function.TsurugiTransactionAction; -import com.tsurugidb.iceaxe.transaction.manager.exception.TsurugiTmIOException; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; - -/** - * {@link TsurugiTransactionException} test - */ -class DbTransactionExceptionTest extends DbTestTableTester { - - private static final int SIZE = 4; - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTestTable(); - insertTestTable(SIZE); - - logInitEnd(info); - } - - @Test - void insertConstant() throws Exception { - var sql = "insert into " + TEST // - + "(" + TEST_COLUMNS + ")" // - + "values(1, 456, 'abc')"; - - var session = getSession(); - var tm = session.createTransactionManager(TgTxOption.ofOCC()); - try (var ps = session.createStatement(sql)) { - var e0 = assertThrows(TsurugiTmIOException.class, () -> tm.execute((TsurugiTransactionAction) transaction -> { - var e = assertThrows(TsurugiTransactionException.class, () -> transaction.executeAndGetCount(ps)); - assertEquals(transaction.getIceaxeTxId(), e.getIceaxeTxId()); - assertEquals(transaction.getIceaxeTmExecuteId(), e.getIceaxeTmExecuteId()); - assertEquals(transaction.getAttempt(), e.getAttempt()); - assertSame(transaction.getTransactionOption(), e.getTransactionOption()); - assertEquals(transaction.getTransactionId(), e.getTransactionId()); - assertEquals(TgTxMethod.EXECUTE_GET_COUNT, e.getTxMethod()); - assertSame(ps, e.getSqlDefinition()); - assertNull(e.getSqlParameter()); - - throw e; - })); - assertEquals(TgTxMethod.EXECUTE_GET_COUNT, e0.getTxMethod()); - assertSame(ps, e0.getSqlDefinition()); - assertNull(e0.getSqlParameter()); - } - } - - @Test - void insertParameter() throws Exception { - var entity = new TestEntity(1, 456, "abc"); - - var session = getSession(); - var tm = session.createTransactionManager(TgTxOption.ofOCC()); - try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING)) { - var e0 = assertThrows(TsurugiTmIOException.class, () -> tm.execute((TsurugiTransactionAction) transaction -> { - var e = assertThrows(TsurugiTransactionException.class, () -> transaction.executeAndGetCount(ps, entity)); - assertEquals(transaction.getIceaxeTxId(), e.getIceaxeTxId()); - assertEquals(transaction.getIceaxeTmExecuteId(), e.getIceaxeTmExecuteId()); - assertEquals(transaction.getAttempt(), e.getAttempt()); - assertSame(transaction.getTransactionOption(), e.getTransactionOption()); - assertEquals(transaction.getTransactionId(), e.getTransactionId()); - assertEquals(TgTxMethod.EXECUTE_GET_COUNT, e.getTxMethod()); - assertSame(ps, e.getSqlDefinition()); - assertSame(entity, e.getSqlParameter()); - - throw e; - })); - assertEquals(TgTxMethod.EXECUTE_GET_COUNT, e0.getTxMethod()); - assertSame(ps, e0.getSqlDefinition()); - assertSame(entity, e0.getSqlParameter()); - } - } - - @Test - void insertResult() throws Exception { - var entity = new TestEntity(1, 456, "abc"); - - var session = getSession(); - var tm = session.createTransactionManager(TgTxOption.ofOCC()); - try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING)) { - TsurugiStatementResult[] result = { null }; - var e0 = assertThrows(TsurugiTmIOException.class, () -> tm.execute((TsurugiTransactionAction) transaction -> { - var e = assertThrows(TsurugiTransactionException.class, () -> { - result[0] = transaction.executeStatement(ps, entity); - result[0].getUpdateCount(); - }); - assertEquals(transaction.getIceaxeTxId(), e.getIceaxeTxId()); - assertEquals(transaction.getIceaxeTmExecuteId(), e.getIceaxeTmExecuteId()); - assertEquals(transaction.getAttempt(), e.getAttempt()); - assertSame(transaction.getTransactionOption(), e.getTransactionOption()); - assertEquals(transaction.getTransactionId(), e.getTransactionId()); - assertNull(e.getTxMethod()); - assertSame(ps, e.getSqlDefinition()); - assertSame(entity, e.getSqlParameter()); - assertEquals(result[0].getIceaxeSqlExecuteId(), e.getIceaxeSqlExecuteId()); - - throw e; - })); - assertNull(e0.getTxMethod()); - assertSame(ps, e0.getSqlDefinition()); - assertSame(entity, e0.getSqlParameter()); - assertEquals(result[0].getIceaxeSqlExecuteId(), e0.getIceaxeSqlExecuteId()); - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbTransactionExecuteQueryTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbTransactionExecuteQueryTest.java deleted file mode 100755 index e1ebe902..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbTransactionExecuteQueryTest.java +++ /dev/null @@ -1,205 +0,0 @@ -package com.tsurugidb.iceaxe.test.transaction; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import java.util.List; - -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.slf4j.LoggerFactory; - -import com.tsurugidb.iceaxe.sql.parameter.TgBindParameters; -import com.tsurugidb.iceaxe.sql.parameter.TgBindVariable; -import com.tsurugidb.iceaxe.sql.parameter.TgParameterMapping; -import com.tsurugidb.iceaxe.sql.result.TgResultMapping; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.test.util.TestEntity; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; - -/** - * transaction execute query test - */ -class DbTransactionExecuteQueryTest extends DbTestTableTester { - - private static final int SIZE = 4; - - @BeforeAll - static void beforeAll(TestInfo info) throws Exception { - var LOG = LoggerFactory.getLogger(DbTransactionExecuteQueryTest.class); - logInitStart(LOG, info); - - dropTestTable(); - createTestTable(); - insertTestTable(SIZE); - - logInitEnd(LOG, info); - } - - @Test - void executeQuery() throws Exception { - var sql = SELECT_SQL + " where foo < " + (SIZE - 1) + " order by foo"; - - var session = getSession(); - try (var ps = session.createQuery(sql, SELECT_MAPPING); // - var transaction = session.createTransaction(TgTxOption.ofOCC())) { - try (var result = transaction.executeQuery(ps)) { - List list = result.getRecordList(); - assertEqualsTestTable(SIZE - 1, list); - } - } - } - - @Test - void executePreparedQuery() throws Exception { - var foo = TgBindVariable.ofInt("foo"); - var sql = SELECT_SQL + " where foo < " + foo + " order by foo"; - var parameterMapping = TgParameterMapping.of(foo); - - var session = getSession(); - try (var ps = session.createQuery(sql, parameterMapping, SELECT_MAPPING); // - var transaction = session.createTransaction(TgTxOption.ofOCC())) { - var parameter = TgBindParameters.of(foo.bind(SIZE - 1)); - try (var result = transaction.executeQuery(ps, parameter)) { - List list = result.getRecordList(); - assertEqualsTestTable(SIZE - 1, list); - } - } - } - - @Test - void executeAndForEach() throws Exception { - var sql = SELECT_SQL + " where foo < " + (SIZE - 1) + " order by foo"; - - var session = getSession(); - try (var ps = session.createQuery(sql, SELECT_MAPPING); // - var transaction = session.createTransaction(TgTxOption.ofOCC())) { - int[] count = { 0 }; - transaction.executeAndForEach(ps, entity -> { - var expected = createTestEntity(count[0]++); - assertEquals(expected, entity); - }); - assertEquals(SIZE - 1, count[0]); - } - } - - @Test - void executePreparedAndForEach() throws Exception { - var foo = TgBindVariable.ofInt("foo"); - var sql = SELECT_SQL + " where foo < " + foo + " order by foo"; - var parameterMapping = TgParameterMapping.of(foo); - - var session = getSession(); - try (var ps = session.createQuery(sql, parameterMapping, SELECT_MAPPING); // - var transaction = session.createTransaction(TgTxOption.ofOCC())) { - var parameter = TgBindParameters.of(foo.bind(SIZE - 1)); - int[] count = { 0 }; - transaction.executeAndForEach(ps, parameter, entity -> { - var expected = createTestEntity(count[0]++); - assertEquals(expected, entity); - }); - assertEquals(SIZE - 1, count[0]); - } - } - - @Test - void executeAndForEachRaw() throws Exception { - var sql = SELECT_SQL + " where foo < " + (SIZE - 1) + " order by foo"; - var resultMapping = TgResultMapping.of(record -> record); - - var session = getSession(); - try (var ps = session.createQuery(sql, resultMapping); // - var transaction = session.createTransaction(TgTxOption.ofOCC())) { - int[] count = { 0 }; - transaction.executeAndForEach(ps, record -> { - var expected = createTestEntity(count[0]++); - assertEquals(expected.getFoo(), record.nextIntOrNull()); - assertEquals(expected.getBar(), record.nextLongOrNull()); - assertEquals(expected.getZzz(), record.nextStringOrNull()); - }); - assertEquals(SIZE - 1, count[0]); - } - } - - @Test - void executePreparedAndForEachRaw() throws Exception { - var foo = TgBindVariable.ofInt("foo"); - var sql = SELECT_SQL + " where foo < " + foo + " order by foo"; - var parameterMapping = TgParameterMapping.of(foo); - var resultMapping = TgResultMapping.of(record -> record); - - var session = getSession(); - try (var ps = session.createQuery(sql, parameterMapping, resultMapping); // - var transaction = session.createTransaction(TgTxOption.ofOCC())) { - var parameter = TgBindParameters.of(foo.bind(SIZE - 1)); - int[] count = { 0 }; - transaction.executeAndForEach(ps, parameter, record -> { - var expected = createTestEntity(count[0]++); - assertEquals(expected.getFoo(), record.nextIntOrNull()); - assertEquals(expected.getBar(), record.nextLongOrNull()); - assertEquals(expected.getZzz(), record.nextStringOrNull()); - }); - assertEquals(SIZE - 1, count[0]); - } - } - - @Test - void executeAndFindRecord() throws Exception { - var sql = SELECT_SQL + " where foo = 1"; - - var session = getSession(); - try (var ps = session.createQuery(sql, SELECT_MAPPING); // - var transaction = session.createTransaction(TgTxOption.ofOCC())) { - var entity = transaction.executeAndFindRecord(ps).get(); - - var expected = createTestEntity(1); - assertEquals(expected, entity); - } - } - - @Test - void executePreparedAndFindRecord() throws Exception { - var foo = TgBindVariable.ofInt("foo"); - var sql = SELECT_SQL + " where foo = " + foo; - var parameterMapping = TgParameterMapping.of(foo); - - var session = getSession(); - try (var ps = session.createQuery(sql, parameterMapping, SELECT_MAPPING); // - var transaction = session.createTransaction(TgTxOption.ofOCC())) { - var parameter = TgBindParameters.of(foo.bind(1)); - var entity = transaction.executeAndFindRecord(ps, parameter).get(); - - var expected = createTestEntity(1); - assertEquals(expected, entity); - } - } - - @Test - void executeAndGetList() throws Exception { - var sql = SELECT_SQL + " where foo < " + (SIZE - 1) + " order by foo"; - - var session = getSession(); - try (var ps = session.createQuery(sql, SELECT_MAPPING); // - var transaction = session.createTransaction(TgTxOption.ofOCC())) { - var list = transaction.executeAndGetList(ps); - - assertEqualsTestTable(SIZE - 1, list); - } - } - - @Test - void executePreparedAndGetList() throws Exception { - var foo = TgBindVariable.ofInt("foo"); - var sql = SELECT_SQL + " where foo < " + foo + " order by foo"; - var parameterMapping = TgParameterMapping.of(foo); - - var session = getSession(); - try (var ps = session.createQuery(sql, parameterMapping, SELECT_MAPPING); // - var transaction = session.createTransaction(TgTxOption.ofOCC())) { - var parameter = TgBindParameters.of(foo.bind(SIZE - 1)); - var list = transaction.executeAndGetList(ps, parameter); - - assertEqualsTestTable(SIZE - 1, list); - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbTransactionExecuteStatementTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbTransactionExecuteStatementTest.java deleted file mode 100755 index 8bc1d71e..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbTransactionExecuteStatementTest.java +++ /dev/null @@ -1,150 +0,0 @@ -package com.tsurugidb.iceaxe.test.transaction; - -import java.util.ArrayList; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; - -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.test.util.TestEntity; -import com.tsurugidb.iceaxe.transaction.TgCommitType; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; - -/** - * transaction execute statement test - */ -class DbTransactionExecuteStatementTest extends DbTestTableTester { - - private static final int SIZE = 2; - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTestTable(); - insertTestTable(SIZE); - - logInitEnd(info); - } - - @Test - void executeStatement() throws Exception { - var entity = createTestEntity(SIZE); - var sql = "insert into " + TEST // - + " (" + TEST_COLUMNS + ")" // - + " values(" + entity.getFoo() + "," + entity.getBar() + ",'" + entity.getZzz() + "')"; - - var session = getSession(); - try (var ps = session.createStatement(sql); // - var transaction = session.createTransaction(TgTxOption.ofOCC())) { - try (var result = transaction.executeStatement(ps)) { - assertUpdateCount(1, result.getUpdateCount()); - - transaction.commit(TgCommitType.DEFAULT); - } - } - - assertEqualsTestTable(SIZE + 1); - } - - @Test - void executePreparedStatement() throws Exception { - var session = getSession(); - try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING); // - var transaction = session.createTransaction(TgTxOption.ofOCC())) { - var entity = createTestEntity(SIZE); - try (var result = transaction.executeStatement(ps, entity)) { - assertUpdateCount(1, result.getUpdateCount()); - - transaction.commit(TgCommitType.DEFAULT); - } - } - - assertEqualsTestTable(SIZE + 1); - } - - @Test - @Disabled // TODO remove Disabled. Waiting for implementation in jogasaki - void executeBatch() throws Exception { - int addSize = 4; - - var session = getSession(); - try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING); // - var transaction = session.createTransaction(TgTxOption.ofOCC())) { - var entityList = new ArrayList(addSize); - for (int i = 0; i < addSize; i++) { - var entity = createTestEntity(SIZE + i); - entityList.add(entity); - } - try (var result = transaction.executeBatch(ps, entityList)) { - assertUpdateCount(addSize, result.getUpdateCount()); - - transaction.commit(TgCommitType.DEFAULT); - } - } - - assertEqualsTestTable(SIZE + addSize); - } - - @Test - void executeAndGetCount() throws Exception { - var entity = createTestEntity(SIZE); - var sql = "insert into " + TEST // - + " (" + TEST_COLUMNS + ")" // - + " values(" + entity.getFoo() + "," + entity.getBar() + ",'" + entity.getZzz() + "')"; - - var session = getSession(); - try (var ps = session.createStatement(sql); // - var transaction = session.createTransaction(TgTxOption.ofOCC())) { - int result = transaction.executeAndGetCount(ps); - - assertUpdateCount(1, result); - - transaction.commit(TgCommitType.DEFAULT); - } - - assertEqualsTestTable(SIZE + 1); - } - - @Test - void executePreparedAndGetCount() throws Exception { - var session = getSession(); - try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING); // - var transaction = session.createTransaction(TgTxOption.ofOCC())) { - var entity = createTestEntity(SIZE); - int result = transaction.executeAndGetCount(ps, entity); - - assertUpdateCount(1, result); - - transaction.commit(TgCommitType.DEFAULT); - } - - assertEqualsTestTable(SIZE + 1); - } - - @Test - @Disabled // TODO remove Disabled. Waiting for implementation in jogasaki - void executeBatchAndGetCount() throws Exception { - int addSize = 4; - - var session = getSession(); - try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING); // - var transaction = session.createTransaction(TgTxOption.ofOCC())) { - var entityList = new ArrayList(addSize); - for (int i = 0; i < addSize; i++) { - var entity = createTestEntity(SIZE + i); - entityList.add(entity); - } - int result = transaction.executeAndGetCount(ps, entityList); - - assertUpdateCount(addSize, result); - - transaction.commit(TgCommitType.DEFAULT); - } - - assertEqualsTestTable(SIZE + addSize); - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbTransactionParallelTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbTransactionParallelTest.java deleted file mode 100755 index 78e8042b..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbTransactionParallelTest.java +++ /dev/null @@ -1,171 +0,0 @@ -package com.tsurugidb.iceaxe.test.transaction; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.io.IOException; -import java.util.concurrent.TimeUnit; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; - -import com.tsurugidb.iceaxe.sql.TsurugiSqlPreparedStatement; -import com.tsurugidb.iceaxe.test.util.DbTestConnector; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.test.util.TestEntity; -import com.tsurugidb.iceaxe.transaction.TgCommitType; -import com.tsurugidb.iceaxe.transaction.TsurugiTransaction; -import com.tsurugidb.iceaxe.transaction.exception.TsurugiTransactionException; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; - -/** - * parallel transaction test - */ -// トランザクションtx1とtx2がある。 -// tx1はtestテーブル、tx2はtest2テーブルのみにアクセスする。(お互いに無関係なテーブルの更新) -// tx1よりtx2の方が(処理件数が少ないので)早く終わる想定。 -// このとき、tx1の途中でtx2を開始したらどうなるか?というテスト -class DbTransactionParallelTest extends DbTestTableTester { - - private static final String TEST2 = "test2"; - private static final int TEST_SIZE = 100; - private static final int TEST2_SIZE = 2; - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTestTable(); - dropTable(TEST2); - createTest2Table(); - - logInitEnd(info); - } - - private static void createTest2Table() throws IOException, InterruptedException { - var sql = CREATE_TEST_SQL.replace(TEST, TEST2); - executeDdl(getSession(), sql); - } - - @Test - void testOccOcc() throws Exception { - test(TgTxOption.ofOCC(), TgTxOption.ofOCC(), false); - } - - @Test - void testOccLtx() throws Exception { - test(TgTxOption.ofOCC(), TgTxOption.ofLTX(TEST2), false); - } - - @Test - void testLtxOcc() throws Exception { - test(TgTxOption.ofLTX(TEST), TgTxOption.ofOCC(), false); - } - - @Test - void testLtxLtx() throws Exception { - // LTX同士の場合、tx2はtx1のコミットが終わるのを待つ - test(TgTxOption.ofLTX(TEST), TgTxOption.ofLTX(TEST2), true); - } - - @Test - @Disabled - void testLtxLtxReadArea() throws Exception { - // TODO read areaを指定するとtx2は早く終わるはず - var txOption1 = TgTxOption.ofLTX(TEST); - var txOption2 = TgTxOption.ofLTX(TEST2); - test(txOption1, txOption2, false); - } - - private void test(TgTxOption txOption1, TgTxOption txOption2, boolean wait) throws Exception { - Tx2Thread[] thread = { null }; - - try (var session = DbTestConnector.createSession(Long.MAX_VALUE, TimeUnit.NANOSECONDS); // - var ps1 = session.createStatement(INSERT_SQL, INSERT_MAPPING); // - var tx1 = session.createTransaction(txOption1)) { - try { -// LOG.info("tx1={}", tx1.getTransactionId()); - runInTransaction(tx1, ps1, thread, txOption2, wait); - tx1.commit(TgCommitType.DEFAULT); - } catch (Throwable t) { - tx1.rollback(); - throw t; - } - } - - if (wait) { - thread[0].join(); - assertTrue(thread[0].done); - } - - assertEqualsTestTable(TEST_SIZE); - assertEquals(TEST2_SIZE, selectCountFrom(TEST2)); - } - - private void runInTransaction(TsurugiTransaction tx1, TsurugiSqlPreparedStatement ps1, Tx2Thread[] thread, TgTxOption txOption2, boolean wait) - throws IOException, TsurugiTransactionException, InterruptedException { - for (int i = 0; i < TEST_SIZE; i++) { - if (i == 10) { - // トランザクションの途中で、無関係なテーブルを扱う別トランザクションを開始する - thread[0] = new Tx2Thread(txOption2); - thread[0].start(); - } - - var entity = createTestEntity(i); - tx1.executeAndGetCount(ps1, entity); - - if (i == TEST_SIZE - 1) { - if (wait) { - // tx2は(tx1が終わるのを待つので)まだ実行中 - assertFalse(thread[0].done); - } else { - // Tx2Threadの方が短いので、先に終わっているはず - thread[0].join(); - assertTrue(thread[0].done); - } - } - } - } - - private class Tx2Thread extends Thread { - - private final TgTxOption txOption2; - public boolean done = false; - - public Tx2Thread(TgTxOption txOption2) { - this.txOption2 = txOption2; - } - - @Override - public void run() { - try { - try (var session = DbTestConnector.createSession(Long.MAX_VALUE, TimeUnit.NANOSECONDS); // - var ps2 = session.createStatement(INSERT_SQL.replace(TEST, TEST2), INSERT_MAPPING); // - var tx2 = session.createTransaction(txOption2)) { - try { -// LOG.info("tx2={}", tx2.getTransactionId()); - runInTransaction(tx2, ps2); - tx2.commit(TgCommitType.DEFAULT); - } catch (Throwable t) { - tx2.rollback(); - throw t; - } - } - } catch (Exception e) { - throw new RuntimeException(e); - } - this.done = true; - } - - private void runInTransaction(TsurugiTransaction tx2, TsurugiSqlPreparedStatement ps2) throws IOException, TsurugiTransactionException, InterruptedException { - for (int i = 0; i < TEST2_SIZE; i++) { - var entity = createTestEntity(i); - tx2.executeAndGetCount(ps2, entity); - } - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbTransactionReadArea3Test.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbTransactionReadArea3Test.java deleted file mode 100755 index f88f3dee..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbTransactionReadArea3Test.java +++ /dev/null @@ -1,268 +0,0 @@ -package com.tsurugidb.iceaxe.test.transaction; - -import static org.junit.jupiter.api.Assertions.assertFalse; - -import java.io.IOException; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; - -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.test.util.TestEntity; -import com.tsurugidb.iceaxe.transaction.TgCommitType; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; - -/** - * transaction read area test - */ -class DbTransactionReadArea3Test extends DbTestTableTester { - - private static final String TABLE_A = TEST; - private static final String TABLE_B = "test2"; - private static final int SIZE = 4; - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTestTable(); - insertTestTable(SIZE); - - dropTable(TABLE_B); - createTest2Table(); - insertTest2Table(SIZE); - - logInitEnd(info); - } - - private static void createTest2Table() throws IOException, InterruptedException { - var sql = CREATE_TEST_SQL.replace(TEST, TABLE_B); - executeDdl(getSession(), sql); - } - - protected static void insertTest2Table(int size) throws IOException, InterruptedException { - var sql = INSERT_SQL.replace(TEST, TABLE_B); - - var session = getSession(); - var tm = createTransactionManagerOcc(session, 3); - try (var ps = session.createStatement(sql, INSERT_MAPPING)) { - tm.execute(transaction -> { - for (int i = 0; i < size; i++) { - var entity = createTestEntity(i); - transaction.executeAndGetCount(ps, entity); - } - return; - }); - } - } - - private static final TestEntity ENTITY0 = new TestEntity(SIZE + 1, 0, "0"); - private static final String INSERT_A_SQL = INSERT_SQL; - private static final String SELECT_A_SQL = SELECT_SQL + " where foo=" + ENTITY0.getFoo(); - private static final String INSERT_B_SQL = INSERT_SQL.replace(TEST, TABLE_B); - private static final String SELECT_B_SQL = SELECT_A_SQL.replace(TABLE_A, TABLE_B); - - @Test - void case1() throws Exception { - var session = getSession(); - try (var insertAps = session.createStatement(INSERT_A_SQL, INSERT_MAPPING); // - var selectAps = session.createQuery(SELECT_A_SQL, SELECT_MAPPING); // - var insertBps = session.createStatement(INSERT_B_SQL, INSERT_MAPPING)) { - try (var tx1 = session.createTransaction(TgTxOption.ofLTX(TABLE_A).label("t1"))) { - tx1.getTransactionId(); - try (var tx2 = session.createTransaction(TgTxOption.ofLTX().addExclusiveReadArea(TABLE_B).label("t2"))) { - tx2.getTransactionId(); - try (var tx3 = session.createTransaction(TgTxOption.ofLTX(TABLE_B).label("t3"))) { - tx3.getTransactionId(); - - tx1.executeAndGetCount(insertAps, ENTITY0); - - tx3.executeAndGetList(selectAps); - tx3.executeAndGetCount(insertBps, ENTITY0); - var future3 = executeFuture(() -> { - tx3.commit(TgCommitType.DEFAULT); - return null; - }); - - Thread.sleep(100); - assertFalse(future3.isDone()); - tx1.commit(TgCommitType.DEFAULT); - - future3.get(); - } - } - } - } - } - - @Test - void case11() throws Exception { - var session = getSession(); - try (var insertAps = session.createStatement(INSERT_A_SQL, INSERT_MAPPING); // - var selectAps = session.createQuery(SELECT_A_SQL, SELECT_MAPPING); // - var insertBps = session.createStatement(INSERT_B_SQL, INSERT_MAPPING)) { - try (var tx1 = session.createTransaction(TgTxOption.ofLTX(TABLE_A).label("t1"))) { - tx1.getTransactionId(); - try (var tx2 = session.createTransaction(TgTxOption.ofLTX().addExclusiveReadArea(TABLE_B).label("t2"))) { - tx2.getTransactionId(); - try (var tx3 = session.createTransaction(TgTxOption.ofLTX(TABLE_B).label("t3"))) { - tx3.getTransactionId(); - - tx3.executeAndGetList(selectAps); // yellow conflict - tx3.executeAndGetCount(insertBps, ENTITY0); - var future3 = executeFuture(() -> { - // t3はt1のWPを踏んだがyellowなので待つ - tx3.commit(TgCommitType.DEFAULT); - return null; - }); - - tx1.executeAndGetCount(insertAps, ENTITY0); - assertFalse(future3.isDone()); - tx1.commit(TgCommitType.DEFAULT); - - future3.get(); - } - } - } - } - } - - @Test - void case12() throws Exception { - var session = getSession(); - try (var insertAps = session.createStatement(INSERT_A_SQL, INSERT_MAPPING); // - var selectAps = session.createQuery(SELECT_A_SQL, SELECT_MAPPING); // - var insertBps = session.createStatement(INSERT_B_SQL, INSERT_MAPPING)) { - try (var tx1 = session.createTransaction(TgTxOption.ofLTX(TABLE_A).addExclusiveReadArea(TABLE_B).label("t1"))) { - tx1.getTransactionId(); - try (var tx2 = session.createTransaction(TgTxOption.ofLTX().addExclusiveReadArea(TABLE_B).label("t2"))) { - tx2.getTransactionId(); - try (var tx3 = session.createTransaction(TgTxOption.ofLTX(TABLE_B).label("t3"))) { - tx3.getTransactionId(); - - tx1.executeAndGetCount(insertAps, ENTITY0); - - tx3.executeAndGetList(selectAps); // red conflict - // t3はt1のred conflictが(未コミットだが)確定 - tx3.executeAndGetCount(insertBps, ENTITY0); - tx3.commit(TgCommitType.DEFAULT); // 前置確定+readなしなので、commitできる - - tx1.commit(TgCommitType.DEFAULT); - } - } - } - } - } - - @Test - void case2() throws Exception { - var session = getSession(); - try (var insertAps = session.createStatement(INSERT_A_SQL, INSERT_MAPPING); // - var selectAps = session.createQuery(SELECT_A_SQL, SELECT_MAPPING); // - var insertBps = session.createStatement(INSERT_B_SQL, INSERT_MAPPING); // - var selectBps = session.createQuery(SELECT_B_SQL, SELECT_MAPPING)) { - try (var tx1 = session.createTransaction(TgTxOption.ofLTX(TABLE_A).label("t1"))) { - tx1.getTransactionId(); - try (var tx2 = session.createTransaction(TgTxOption.ofLTX().label("t2"))) { - tx2.getTransactionId(); - try (var tx3 = session.createTransaction(TgTxOption.ofLTX(TABLE_B).label("t3"))) { - tx3.getTransactionId(); - - tx1.executeAndGetCount(insertAps, ENTITY0); - - tx3.executeAndGetList(selectAps); - tx3.executeAndGetCount(insertBps, ENTITY0); - var future3 = executeFuture(() -> { - tx3.commit(TgCommitType.DEFAULT); - return null; - }); - - tx1.commit(TgCommitType.DEFAULT); - - tx2.executeAndGetList(selectBps); - assertFalse(future3.isDone()); - tx2.commit(TgCommitType.DEFAULT); - - future3.get(); - } - } - } - } - } - - @Test - void case21() throws Exception { - var session = getSession(); - try (var insertAps = session.createStatement(INSERT_A_SQL, INSERT_MAPPING); // - var selectAps = session.createQuery(SELECT_A_SQL, SELECT_MAPPING); // - var insertBps = session.createStatement(INSERT_B_SQL, INSERT_MAPPING); // - var selectBps = session.createQuery(SELECT_B_SQL, SELECT_MAPPING)) { - try (var tx1 = session.createTransaction(TgTxOption.ofLTX(TABLE_A).label("t1"))) { - tx1.getTransactionId(); - try (var tx2 = session.createTransaction(TgTxOption.ofLTX().label("t2"))) { - tx2.getTransactionId(); - try (var tx3 = session.createTransaction(TgTxOption.ofLTX(TABLE_B).label("t3"))) { - tx3.getTransactionId(); - - tx3.executeAndGetList(selectAps); // yellow conflict - tx3.executeAndGetCount(insertBps, ENTITY0); - var future3 = executeFuture(() -> { - // t3はt1のWPを踏んだがyellowなので待つ - tx3.commit(TgCommitType.DEFAULT); - return null; - }); - - tx1.executeAndGetCount(insertAps, ENTITY0); - - assertFalse(future3.isDone()); - tx1.commit(TgCommitType.DEFAULT); - - assertFalse(future3.isDone()); - tx2.commit(TgCommitType.DEFAULT); - - future3.get(); - } - } - } - } - } - - @Test - void case22() throws Exception { - var session = getSession(); - try (var insertAps = session.createStatement(INSERT_A_SQL, INSERT_MAPPING); // - var selectAps = session.createQuery(SELECT_A_SQL, SELECT_MAPPING); // - var insertBps = session.createStatement(INSERT_B_SQL, INSERT_MAPPING); // - var selectBps = session.createQuery(SELECT_B_SQL, SELECT_MAPPING)) { - try (var tx1 = session.createTransaction(TgTxOption.ofLTX(TABLE_A).addExclusiveReadArea(TABLE_B).label("t1"))) { - tx1.getTransactionId(); - try (var tx2 = session.createTransaction(TgTxOption.ofLTX().label("t2"))) { - tx2.getTransactionId(); - try (var tx3 = session.createTransaction(TgTxOption.ofLTX(TABLE_B).label("t3"))) { - tx3.getTransactionId(); - - tx1.executeAndGetCount(insertAps, ENTITY0); - - tx3.executeAndGetList(selectAps); // red conflict - // t3 は t1 の red conflictが(未コミットだが)確定。ただしcommitまで確定待ち - tx3.executeAndGetCount(insertBps, ENTITY0); - var future3 = executeFuture(() -> { - tx3.commit(TgCommitType.DEFAULT); - return null; - }); - - assertFalse(future3.isDone()); - tx1.commit(TgCommitType.DEFAULT); - - assertFalse(future3.isDone()); - tx2.commit(TgCommitType.DEFAULT); - - future3.get(); - } - } - } - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbTransactionReadAreaTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbTransactionReadAreaTest.java deleted file mode 100755 index 37480b0d..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbTransactionReadAreaTest.java +++ /dev/null @@ -1,280 +0,0 @@ -package com.tsurugidb.iceaxe.test.transaction; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertThrowsExactly; - -import java.io.IOException; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; - -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.transaction.TgCommitType; -import com.tsurugidb.iceaxe.transaction.exception.TsurugiTransactionException; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; -import com.tsurugidb.tsubakuro.sql.SqlServiceCode; - -/** - * transaction read area test - */ -class DbTransactionReadAreaTest extends DbTestTableTester { - - private static final String TEST2 = "test2"; - private static final int SIZE = 4; - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTestTable(); - insertTestTable(SIZE); - - dropTable(TEST2); - createTest2Table(); - insertTest2Table(SIZE); - - logInitEnd(info); - } - - private static void createTest2Table() throws IOException, InterruptedException { - var sql = CREATE_TEST_SQL.replace(TEST, TEST2); - executeDdl(getSession(), sql); - } - - protected static void insertTest2Table(int size) throws IOException, InterruptedException { - var sql = INSERT_SQL.replace(TEST, TEST2); - - var session = getSession(); - var tm = createTransactionManagerOcc(session, 3); - try (var ps = session.createStatement(sql, INSERT_MAPPING)) { - tm.execute(transaction -> { - for (int i = 0; i < size; i++) { - var entity = createTestEntity(i); - transaction.executeAndGetCount(ps, entity); - } - return; - }); - } - } - - private static final TgTxOption LTX1 = TgTxOption.ofLTX(TEST).addInclusiveReadArea(TEST); - private static final TgTxOption LTX2 = TgTxOption.ofLTX(TEST2).addInclusiveReadArea(TEST2); - private static final TgTxOption LTX1_NOTHING = TgTxOption.ofLTX(TEST); - private static final TgTxOption LTX2_NOTHING = TgTxOption.ofLTX(TEST2); - - private static final int KEY = 1; - private static final String SELECT1_SQL = SELECT_SQL + " where foo = " + KEY; - private static final String SELECT2_SQL = SELECT1_SQL.replace(TEST, TEST2); - private static final String UPDATE1_SQL = "update " + TEST + " set bar=789 where foo = " + KEY; - private static final String UPDATE2_SQL = UPDATE1_SQL.replace(TEST, TEST2); - - @Test - void readArea1Select_nothing() throws Exception { - var session = getSession(); - try (var select2Ps = session.createQuery(SELECT2_SQL, SELECT_MAPPING)) { - try (var tx1 = session.createTransaction(LTX1_NOTHING)) { - for (int i = 0; i < 3; i++) { - tx1.executeAndGetList(select2Ps); - } - tx1.commit(TgCommitType.DEFAULT); - } - } - - assertEqualsTestTable(SIZE); - } - - @Test - void readArea1Select_error() throws Exception { - var session = getSession(); - try (var select2Ps = session.createQuery(SELECT2_SQL, SELECT_MAPPING)) { - try (var tx1 = session.createTransaction(LTX1)) { - var e1 = assertThrowsExactly(TsurugiTransactionException.class, () -> tx1.executeAndGetList(select2Ps)); - assertEqualsCode(SqlServiceCode.READ_OPERATION_ON_RESTRICTED_READ_AREA_EXCEPTION, e1); - assertContains("Read operation outside read area", e1.getMessage()); // TODO エラー詳細情報(テーブル名) - var e2 = assertThrowsExactly(TsurugiTransactionException.class, () -> tx1.executeAndGetList(select2Ps)); - assertEqualsCode(SqlServiceCode.INACTIVE_TRANSACTION_EXCEPTION, e2); - } - } - - assertEqualsTestTable(SIZE); - } - - @ParameterizedTest - @ValueSource(strings = { "nothing", "read_area" }) - void readArea1Update(String pattern) throws Exception { - var txOption = getTxOption(pattern); - var session = getSession(); - try (var update1Ps = session.createStatement(UPDATE1_SQL)) { - try (var tx1 = session.createTransaction(txOption)) { - for (int i = 1; i < 3; i++) { - tx1.executeAndGetCount(update1Ps); - } - tx1.commit(TgCommitType.DEFAULT); - } - } - - assertTable(TEST); - } - - @ParameterizedTest - @ValueSource(strings = { "nothing", "read_area" }) - void readArea1Update_error(String pattern) throws Exception { - var txOption = getTxOption(pattern); - var session = getSession(); - try (var update2Ps = session.createStatement(UPDATE2_SQL)) { - try (var tx1 = session.createTransaction(txOption)) { - var e1 = assertThrowsExactly(TsurugiTransactionException.class, () -> tx1.executeAndGetCount(update2Ps)); - if (pattern.equals("nothing")) { - assertEqualsCode(SqlServiceCode.LTX_WRITE_OPERATION_WITHOUT_WRITE_PRESERVE_EXCEPTION, e1); - assertContains("Ltx write operation outside write preserve", e1.getMessage()); // TODO エラー詳細情報(テーブル名) - } else { - assertEqualsCode(SqlServiceCode.READ_OPERATION_ON_RESTRICTED_READ_AREA_EXCEPTION, e1); - assertContains("Read operation outside read area", e1.getMessage()); // TODO エラー詳細情報(テーブル名) - } - var e2 = assertThrowsExactly(TsurugiTransactionException.class, () -> tx1.executeAndGetCount(update2Ps)); - assertEqualsCode(SqlServiceCode.INACTIVE_TRANSACTION_EXCEPTION, e2); - } - } - - assertEqualsTestTable(SIZE); - } - - private TgTxOption getTxOption(String pattern) { - switch (pattern) { - case "nothing": - return LTX1_NOTHING; - case "read_area": - return LTX1; - default: - throw new AssertionError(pattern); - } - } - - @Test - void readArea2_nothing() throws Exception { - var session = getSession(); - try (var update1Ps = session.createStatement(UPDATE1_SQL); // - var update2Ps = session.createStatement(UPDATE2_SQL)) { - try (var tx1 = session.createTransaction(LTX1_NOTHING)) { - tx1.executeAndGetCount(update1Ps); - - try (var tx2 = session.createTransaction(LTX2_NOTHING)) { - tx2.executeAndGetCount(update2Ps); - - var future2 = executeFuture(() -> { - tx2.commit(TgCommitType.DEFAULT); - return null; - }); - - Thread.sleep(100); - assertFalse(future2.isDone()); - tx1.commit(TgCommitType.DEFAULT); - - future2.get(); - } - } - } - - assertTable(TEST); - assertTable(TEST2); - } - - @Test - void readArea2() throws Exception { - var session = getSession(); - try (var update1Ps = session.createStatement(UPDATE1_SQL); // - var update2Ps = session.createStatement(UPDATE2_SQL)) { - try (var tx1 = session.createTransaction(LTX1)) { - tx1.executeAndGetCount(update1Ps); - - try (var tx2 = session.createTransaction(LTX2)) { - tx2.executeAndGetCount(update2Ps); - tx2.commit(TgCommitType.DEFAULT); - - tx1.commit(TgCommitType.DEFAULT); - } - } - } - - assertTable(TEST); - assertTable(TEST2); - } - - @Test - void readArea2_1() throws Exception { - var session = getSession(); - try (var select2Ps = session.createQuery(SELECT2_SQL); // - var update1Ps = session.createStatement(UPDATE1_SQL); // - var update2Ps = session.createStatement(UPDATE2_SQL)) { - var tx1Option = TgTxOption.ofLTX(TEST).addInclusiveReadArea(TEST, TEST2); - var tx2Option = TgTxOption.ofLTX(TEST2).addInclusiveReadArea(TEST2); - try (var tx1 = session.createTransaction(tx1Option)) { - tx1.executeAndGetList(select2Ps); - tx1.executeAndGetCount(update1Ps); - - try (var tx2 = session.createTransaction(tx2Option)) { - tx2.executeAndGetCount(update2Ps); - - var future2 = executeFuture(() -> { - tx2.commit(TgCommitType.DEFAULT); - return null; - }); - - Thread.sleep(100); - assertFalse(future2.isDone()); - tx1.commit(TgCommitType.DEFAULT); - - future2.get(); - } - } - } - - assertTable(TEST); - assertTable(TEST2); - } - - @Test - void readArea2_2() throws Exception { - var session = getSession(); - try (var select1Ps = session.createQuery(SELECT1_SQL); // - var update1Ps = session.createStatement(UPDATE1_SQL); // - var update2Ps = session.createStatement(UPDATE2_SQL)) { - var tx1Option = TgTxOption.ofLTX(TEST).addInclusiveReadArea(TEST); - var tx2Option = TgTxOption.ofLTX(TEST2).addInclusiveReadArea(TEST, TEST2); - try (var tx1 = session.createTransaction(tx1Option)) { - tx1.executeAndGetCount(update1Ps); - - try (var tx2 = session.createTransaction(tx2Option)) { - tx2.executeAndGetList(select1Ps); - tx2.executeAndGetCount(update2Ps); - tx2.commit(TgCommitType.DEFAULT); - } - - tx1.commit(TgCommitType.DEFAULT); - } - } - - assertTable(TEST); - assertTable(TEST2); - } - - private static void assertTable(String tableName) throws IOException, InterruptedException { - var session = getSession(); - try (var ps = session.createQuery(SELECT_SQL.replace(TEST, tableName), SELECT_MAPPING)) { - var tm = createTransactionManagerOcc(session); - var list = tm.executeAndGetList(ps); - for (var entity : list) { - if (entity.getFoo() == KEY) { - assertEquals(789L, entity.getBar()); - } else { - assertEquals(createTestEntity(entity.getFoo()), entity); - } - } - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbTransactionTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbTransactionTest.java deleted file mode 100755 index 78db0a4e..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/transaction/DbTransactionTest.java +++ /dev/null @@ -1,337 +0,0 @@ -package com.tsurugidb.iceaxe.test.transaction; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertThrowsExactly; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import java.io.IOException; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.RepeatedTest; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; - -import com.tsurugidb.iceaxe.exception.IceaxeErrorCode; -import com.tsurugidb.iceaxe.exception.TsurugiIOException; -import com.tsurugidb.iceaxe.session.TsurugiSession; -import com.tsurugidb.iceaxe.sql.parameter.TgBindParameters; -import com.tsurugidb.iceaxe.sql.parameter.TgParameterMapping; -import com.tsurugidb.iceaxe.test.util.DbTestConnector; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.transaction.TgCommitType; -import com.tsurugidb.iceaxe.transaction.TsurugiTransaction; -import com.tsurugidb.iceaxe.transaction.exception.TsurugiTransactionException; -import com.tsurugidb.iceaxe.transaction.function.TsurugiTransactionAction; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; -import com.tsurugidb.tsubakuro.channel.common.connection.wire.impl.ResponseBox; -import com.tsurugidb.tsubakuro.sql.SqlServiceCode; - -/** - * transaction test - */ -class DbTransactionTest extends DbTestTableTester { - - private static final int SIZE = 2; - private static final int ATTEMPT_SIZE = ResponseBox.responseBoxSize() + 100; - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTestTable(); - insertTestTable(SIZE); - - logInitEnd(info); - } - - @Test - void transactionId() throws Exception { - var session = getSession(); - try (var transaction = session.createTransaction(TgTxOption.ofOCC())) { - var id = transaction.getTransactionId(); - assertNotNull(id); - assertFalse(id.isEmpty()); - } - } - - @Test - void transactionStatus_normal() throws Exception { - var session = getSession(); - try (var transaction = session.createTransaction(TgTxOption.ofOCC())) { - var status = transaction.getTransactionStatus(); - assertTrue(status.isNormal()); - assertFalse(status.isError()); - assertNull(status.getDiagnosticCode()); - assertNull(status.getTransactionException()); - - try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING)) { - var entity = createTestEntity(SIZE); - transaction.executeAndGetCount(ps, entity); - - var status2 = transaction.getTransactionStatus(); - assertTrue(status2.isNormal()); - assertFalse(status2.isError()); - assertNull(status2.getDiagnosticCode()); - assertNull(status2.getTransactionException()); - } - } - } - - @Test - void transactionStatus_parseError() throws Exception { - var session = getSession(); - try (var transaction = session.createTransaction(TgTxOption.ofOCC())) { - String sql = "insertinto " + TEST + " values(" + SIZE + ", 1, 'a')"; // parse error - try (var ps = session.createStatement(sql)) { - var e = assertThrowsExactly(TsurugiTransactionException.class, () -> { - transaction.executeAndGetCount(ps); - }); - assertEqualsCode(SqlServiceCode.SYNTAX_EXCEPTION, e); - } - var status = transaction.getTransactionStatus(); - assertFalse(status.isNormal()); - assertTrue(status.isError()); - assertEquals(SqlServiceCode.SYNTAX_EXCEPTION, status.getDiagnosticCode()); - assertNotNull(status.getTransactionException()); - - try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING)) { - var entity = createTestEntity(SIZE); - var e = assertThrowsExactly(TsurugiTransactionException.class, () -> { - transaction.executeAndGetCount(ps, entity); - }); - assertEqualsCode(SqlServiceCode.INACTIVE_TRANSACTION_EXCEPTION, e); - assertContains("Current transaction is inactive (maybe aborted already.)", e.getMessage()); - } - var status2 = transaction.getTransactionStatus(); - assertFalse(status2.isNormal()); - assertTrue(status2.isError()); - assertEquals(SqlServiceCode.SYNTAX_EXCEPTION, status2.getDiagnosticCode()); - assertNotNull(status2.getTransactionException()); - } - } - - @Test - void transactionStatus_parseError_prepared() throws Exception { - var session = getSession(); - try (var transaction = session.createTransaction(TgTxOption.ofOCC())) { - String sql = "insertinto " + TEST + " values(" + SIZE + ", 1, 'a')"; // parse error - try (var ps = session.createStatement(sql, TgParameterMapping.of())) { - var e = assertThrowsExactly(TsurugiIOException.class, () -> { - transaction.executeAndGetCount(ps, TgBindParameters.of()); - }); - assertEqualsCode(SqlServiceCode.SYNTAX_EXCEPTION, e); - } - var status = transaction.getTransactionStatus(); - assertTrue(status.isNormal()); - assertFalse(status.isError()); - assertNull(status.getDiagnosticCode()); - assertNull(status.getTransactionException()); - - try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING)) { - var entity = createTestEntity(SIZE); - transaction.executeAndGetCount(ps, entity); - } - var status2 = transaction.getTransactionStatus(); - assertTrue(status2.isNormal()); - assertFalse(status2.isError()); - assertNull(status2.getDiagnosticCode()); - assertNull(status2.getTransactionException()); - } - } - - @Test - void transactionStatus_error() throws Exception { - var session = getSession(); - try (var transaction = session.createTransaction(TgTxOption.ofOCC())) { - try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING)) { - var entity = createTestEntity(0); - var e = assertThrowsExactly(TsurugiTransactionException.class, () -> { - transaction.executeAndGetCount(ps, entity); - }); - assertEqualsCode(SqlServiceCode.UNIQUE_CONSTRAINT_VIOLATION_EXCEPTION, e); - } - var status = transaction.getTransactionStatus(); - assertFalse(status.isNormal()); - assertTrue(status.isError()); - assertEquals(SqlServiceCode.UNIQUE_CONSTRAINT_VIOLATION_EXCEPTION, status.getDiagnosticCode()); - assertNotNull(status.getTransactionException()); - } - } - - @Test - void transactionStatus_error2() throws Exception { - var session = getSession(); - try (var transaction = session.createTransaction(TgTxOption.ofOCC())) { - try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING)) { - var entity = createTestEntity(0); - var e1 = assertThrowsExactly(TsurugiTransactionException.class, () -> { - transaction.executeAndGetCount(ps, entity); - }); - assertEqualsCode(SqlServiceCode.UNIQUE_CONSTRAINT_VIOLATION_EXCEPTION, e1); - - var e2 = assertThrowsExactly(TsurugiTransactionException.class, () -> { - transaction.executeAndGetCount(ps, entity); - }); - assertEqualsCode(SqlServiceCode.INACTIVE_TRANSACTION_EXCEPTION, e2); - assertContains("Current transaction is inactive (maybe aborted already.)", e2.getMessage()); - } - var status = transaction.getTransactionStatus(); - assertFalse(status.isNormal()); - assertTrue(status.isError()); - assertEquals(SqlServiceCode.UNIQUE_CONSTRAINT_VIOLATION_EXCEPTION, status.getDiagnosticCode()); - assertNotNull(status.getTransactionException()); - } - } - - @Test - void transactionStatus_afterCommit() throws Exception { - var session = getSession(); - try (var transaction = session.createTransaction(TgTxOption.ofOCC())) { - transaction.commit(TgCommitType.DEFAULT); - var status = transaction.getTransactionStatus(); - assertTrue(status.isNormal()); - assertNull(status.getTransactionException()); - } - } - - @Test - void transactionStatus_afterRollback() throws Exception { - var session = getSession(); - try (var transaction = session.createTransaction(TgTxOption.ofOCC())) { - transaction.rollback(); - var status = transaction.getTransactionStatus(); - assertTrue(status.isNormal()); - assertNull(status.getTransactionException()); - } - } - - @Test - void transactionStatus_afterClose() throws Exception { - var session = getSession(); - try (var transaction = session.createTransaction(TgTxOption.ofOCC())) { - transaction.close(); - var e = assertThrowsExactly(TsurugiIOException.class, () -> { - transaction.getTransactionStatus(); - }); - assertEqualsCode(IceaxeErrorCode.TX_ALREADY_CLOSED, e); - } - } - - @RepeatedTest(6) - void doNothing() throws Exception { - try (var session = DbTestConnector.createSession()) { - for (int i = 0; i < ATTEMPT_SIZE; i++) { - try (var tx = session.createTransaction(TgTxOption.ofOCC())) { - // do nothing - } - } - } - } - - @Test - void commit() throws Exception { - var session = getSession(); - try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING); // - var transaction = session.createTransaction(TgTxOption.ofOCC())) { - assertSelect(SIZE, session, transaction); - - var entity = createTestEntity(SIZE); - transaction.executeAndGetCount(ps, entity); - - assertSelect(SIZE + 1, session, transaction); - - transaction.commit(TgCommitType.DEFAULT); - } - - assertEqualsTestTable(SIZE + 1); - } - - @Test - void commitTm() throws Exception { - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING)) { - tm.execute(transaction -> { - assertSelect(SIZE, session, transaction); - - var entity = createTestEntity(SIZE); - transaction.executeAndGetCount(ps, entity); - - assertSelect(SIZE + 1, session, transaction); - }); - } - - assertEqualsTestTable(SIZE + 1); - } - - @Test - void rollback() throws Exception { - var session = getSession(); - try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING); // - var transaction = session.createTransaction(TgTxOption.ofOCC())) { - assertSelect(SIZE, session, transaction); - - var entity = createTestEntity(SIZE); - transaction.executeAndGetCount(ps, entity); - - assertSelect(SIZE + 1, session, transaction); - - transaction.rollback(); - } - - assertEqualsTestTable(SIZE); - } - - @Test - void rollbackTmByException() throws Exception { - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING)) { - assertThrowsExactly(IOException.class, () -> { - tm.execute((TsurugiTransactionAction) transaction -> { - assertSelect(SIZE, session, transaction); - - var entity = createTestEntity(SIZE); - transaction.executeAndGetCount(ps, entity); - - assertSelect(SIZE + 1, session, transaction); - - throw new IOException("test"); - }); - }); - } - - assertEqualsTestTable(SIZE); - } - - @Test - void rollbackTmExplicit() throws Exception { - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING)) { - tm.execute(transaction -> { - assertSelect(SIZE, session, transaction); - - var entity = createTestEntity(SIZE); - transaction.executeAndGetCount(ps, entity); - - assertSelect(SIZE + 1, session, transaction); - - transaction.rollback(); - }); - } - - assertEqualsTestTable(SIZE); - } - - private static void assertSelect(int expected, TsurugiSession session, TsurugiTransaction transaction) throws IOException, TsurugiTransactionException, InterruptedException { - try (var ps = session.createQuery(SELECT_SQL)) { - var list = transaction.executeAndGetList(ps); - assertEquals(expected, list.size()); - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/update/DbSelectUpdateTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/update/DbSelectUpdateTest.java deleted file mode 100755 index 53315fe4..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/update/DbSelectUpdateTest.java +++ /dev/null @@ -1,233 +0,0 @@ -package com.tsurugidb.iceaxe.test.update; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertInstanceOf; -import static org.junit.jupiter.api.Assertions.assertThrowsExactly; - -import java.util.concurrent.ExecutionException; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; - -import com.tsurugidb.iceaxe.sql.parameter.TgParameterMapping; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.transaction.TgCommitType; -import com.tsurugidb.iceaxe.transaction.exception.TsurugiTransactionException; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; -import com.tsurugidb.tsubakuro.sql.SqlServiceCode; - -/** - * 'select for update'-like test - */ -class DbSelectUpdateTest extends DbTestTableTester { - - private static final int SIZE = 4; - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTestTable(); - insertTestTable(SIZE); - - logInitEnd(info); - } - - @Test - void selectUpdate_occ_occ() throws Exception { - TgTxOption txOption1 = TgTxOption.ofOCC(); - TgTxOption txOption2 = TgTxOption.ofOCC(); - selectUpdate(txOption1, txOption2, true, false); - } - - @Test - void selectUpdate_occ_ltx() throws Exception { - TgTxOption txOption1 = TgTxOption.ofOCC(); - TgTxOption txOption2 = TgTxOption.ofLTX(TEST); - selectUpdate(txOption1, txOption2, true, false); - } - - @Test - void selectUpdate_ltx_occ() throws Exception { - TgTxOption txOption1 = TgTxOption.ofLTX(TEST); - TgTxOption txOption2 = TgTxOption.ofOCC(); - selectUpdate(txOption1, txOption2, false, true); - } - - private void selectUpdate(TgTxOption txOption1, TgTxOption txOption2, boolean tx1Error, boolean tx2Error) throws Exception { - int key = 2; - var selectSql = SELECT_SQL + " where foo=" + key; - var updateSql = "update " + TEST + " set bar=:bar where foo=" + key; - var updateMapping = TgParameterMapping.ofSingle("bar", long.class); - - var session = getSession(); - - try (var selectPs = session.createQuery(selectSql, SELECT_MAPPING); // - var updatePs = session.createStatement(updateSql, updateMapping)) { - try (var tx1 = session.createTransaction(txOption1.label("tx1"))) { - var entity = tx1.executeAndGetList(selectPs).get(0); - - try (var tx2 = session.createTransaction(txOption2.label("tx2"))) { - if (tx2Error) { - var e = assertThrowsExactly(TsurugiTransactionException.class, () -> { - tx2.executeAndGetCount(updatePs, 9L); - tx2.commit(TgCommitType.DEFAULT); - }); - assertEqualsCode(SqlServiceCode.CC_EXCEPTION, e); - tx2.rollback(); - } else { - tx2.executeAndGetCount(updatePs, 9L); - tx2.commit(TgCommitType.DEFAULT); - } - } - - tx1.executeAndGetCount(updatePs, entity.getBar() + 1); - if (tx1Error) { - var e = assertThrowsExactly(TsurugiTransactionException.class, () -> { - tx1.commit(TgCommitType.DEFAULT); - }); - assertEqualsCode(SqlServiceCode.CC_EXCEPTION, e); - } else { - tx1.commit(TgCommitType.DEFAULT); - } - } - } - } - - @Test - void selectUpdate_ltx_ltx() throws Exception { - TgTxOption txOption1 = TgTxOption.ofLTX(TEST); - TgTxOption txOption2 = TgTxOption.ofLTX(TEST); - - int key = 2; - var selectSql = SELECT_SQL + " where foo=" + key; - var updateSql = "update " + TEST + " set bar=:bar where foo=" + key; - var updateMapping = TgParameterMapping.ofSingle("bar", long.class); - - var session = getSession(); - - try (var selectPs = session.createQuery(selectSql, SELECT_MAPPING); // - var updatePs = session.createStatement(updateSql, updateMapping)) { - try (var tx1 = session.createTransaction(txOption1.label("tx1"))) { - var entity = tx1.executeAndGetList(selectPs).get(0); - - try (var tx2 = session.createTransaction(txOption2.label("tx2"))) { - tx2.executeAndGetCount(updatePs, 9L); - var future2 = executeFuture(() -> { - tx2.commit(TgCommitType.DEFAULT); - return null; - }); - - tx1.executeAndGetCount(updatePs, entity.getBar() + 1); - assertFalse(future2.isDone()); - tx1.commit(TgCommitType.DEFAULT); - - var e = assertThrowsExactly(ExecutionException.class, () -> { - future2.get(); - }); - assertInstanceOf(TsurugiTransactionException.class, e.getCause()); - assertEqualsCode(SqlServiceCode.CC_EXCEPTION, e); - } - } - } - - var list = selectAllFromTest(); - int i = 0; - for (var entity : list) { - var expected = createTestEntity(i++); - if (expected.getFoo() == key) { - expected.setBar(expected.getBar() + 1); - } - assertEquals(expected, entity); - } - } - - @Test - void selectUpdate_ltx_ltx2_seq() throws Exception { - TgTxOption txOption1 = TgTxOption.ofLTX(TEST); - TgTxOption txOption2 = TgTxOption.ofLTX(TEST); - - int key = 2; - var selectSql = SELECT_SQL + " where foo=" + key; - var updateSql = "update " + TEST + " set bar=:bar where foo=" + key; - var updateMapping = TgParameterMapping.ofSingle("bar", long.class); - - var session = getSession(); - - try (var selectPs = session.createQuery(selectSql, SELECT_MAPPING); // - var updatePs = session.createStatement(updateSql, updateMapping)) { - try (var tx1 = session.createTransaction(txOption1.label("tx1"))) { - var entity1 = tx1.executeAndGetList(selectPs).get(0); - tx1.executeAndGetCount(updatePs, entity1.getBar() + 1); - tx1.commit(TgCommitType.DEFAULT); - } - try (var tx2 = session.createTransaction(txOption2.label("tx2"))) { - var entity2 = tx2.executeAndGetList(selectPs).get(0); - tx2.executeAndGetCount(updatePs, entity2.getBar() + 9); - tx2.commit(TgCommitType.DEFAULT); - } - } - - var list = selectAllFromTest(); - int i = 0; - for (var entity : list) { - var expected = createTestEntity(i++); - if (expected.getFoo() == key) { - expected.setBar(expected.getBar() + 1 + 9); - } - assertEquals(expected, entity); - } - } - - @Test - void selectUpdate_ltx_ltx2() throws Exception { - TgTxOption txOption1 = TgTxOption.ofLTX(TEST); - TgTxOption txOption2 = TgTxOption.ofLTX(TEST); - - int key = 2; - var selectSql = SELECT_SQL + " where foo=" + key; - var updateSql = "update " + TEST + " set bar=:bar where foo=" + key; - var updateMapping = TgParameterMapping.ofSingle("bar", long.class); - - var session = getSession(); - - try (var selectPs = session.createQuery(selectSql, SELECT_MAPPING); // - var updatePs = session.createStatement(updateSql, updateMapping)) { - try (var tx1 = session.createTransaction(txOption1.label("tx1"))) { - var entity1 = tx1.executeAndGetList(selectPs).get(0); - - try (var tx2 = session.createTransaction(txOption2.label("tx2"))) { - var entity2 = tx2.executeAndGetList(selectPs).get(0); - tx2.executeAndGetCount(updatePs, entity2.getBar() + 9); - var future2 = executeFuture(() -> { - tx2.commit(TgCommitType.DEFAULT); - return null; - }); - - tx1.executeAndGetCount(updatePs, entity1.getBar() + 1); - assertFalse(future2.isDone()); - tx1.commit(TgCommitType.DEFAULT); - - var e = assertThrowsExactly(ExecutionException.class, () -> { - future2.get(); - }); - assertInstanceOf(TsurugiTransactionException.class, e.getCause()); - assertEqualsCode(SqlServiceCode.CC_EXCEPTION, e); - } - } - } - - var list = selectAllFromTest(); - int i = 0; - for (var entity : list) { - var expected = createTestEntity(i++); - if (expected.getFoo() == key) { - expected.setBar(expected.getBar() + 1); - } - assertEquals(expected, entity); - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/update/DbUpdate2Test.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/update/DbUpdate2Test.java deleted file mode 100755 index 1fc42026..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/update/DbUpdate2Test.java +++ /dev/null @@ -1,133 +0,0 @@ -package com.tsurugidb.iceaxe.test.update; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import java.io.IOException; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; - -import com.tsurugidb.iceaxe.sql.parameter.TgBindParameters; -import com.tsurugidb.iceaxe.sql.parameter.TgBindVariable; -import com.tsurugidb.iceaxe.sql.parameter.TgParameterMapping; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; - -/** - * update test - */ -class DbUpdate2Test extends DbTestTableTester { - - private static final int SIZE = 10; - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTable(); - insertTable(); - - logInitEnd(info); - } - - private static void createTable() throws IOException, InterruptedException { - var sql = "create table " + TEST // - + "(" // - + " pk int primary key," // - + " value1 int," // - + " value2 int" // - + ")"; - executeDdl(getSession(), sql); - } - - private static void insertTable() throws IOException, InterruptedException { - var sql = "insert into " + TEST // - + "(pk, value1, value2)" // - + "values(:pk, :value1, :value2)"; - var pk = TgBindVariable.ofInt("pk"); - var v1 = TgBindVariable.ofInt("value1"); - var v2 = TgBindVariable.ofInt("value2"); - var parameterMapping = TgParameterMapping.of(pk, v1, v2); - - var session = getSession(); - var tm = createTransactionManagerOcc(session, 3); - try (var ps = session.createStatement(sql, parameterMapping)) { - tm.execute(transaction -> { - for (int i = 0; i < SIZE; i++) { - var parameter = TgBindParameters.of(pk.bind(i), v1.bind(initValue1(i)), v2.bind(initValue2(i))); - transaction.executeAndGetCount(ps, parameter); - } - return; - }); - } - } - - private static int initValue1(int i) { - return i + 100; - } - - private static int initValue2(int i) { - return i * 2; - } - - @Test - void update() throws Exception { - var sql = "update " + TEST // - + " set" // - + " value1 = value1 + 1," // - + " value2 = value2 + value1"; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(sql)) { - int count = tm.executeAndGetCount(ps); - assertUpdateCount(SIZE, count); - } - - var selectSql = "select * from " + TEST + " order by pk"; - try (var ps = session.createQuery(selectSql)) { - var list = tm.executeAndGetList(ps); - assertEquals(SIZE, list.size()); - int i = 0; - for (var entity : list) { - int v1 = initValue1(i); - int v2 = initValue2(i); - assertEquals(i, entity.getInt("pk")); - assertEquals(v1 + 1, entity.getInt("value1")); - assertEquals(v2 + v1, entity.getInt("value2")); - i++; - } - } - } - - @Test - void swap() throws Exception { - var sql = "update " + TEST // - + " set" // - + " value1 = value2," // - + " value2 = value1"; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(sql)) { - int count = tm.executeAndGetCount(ps); - assertUpdateCount(SIZE, count); - } - - var selectSql = "select * from " + TEST + " order by pk"; - try (var ps = session.createQuery(selectSql)) { - var list = tm.executeAndGetList(ps); - assertEquals(SIZE, list.size()); - int i = 0; - for (var entity : list) { - int v1 = initValue1(i); - int v2 = initValue2(i); - assertEquals(i, entity.getInt("pk")); - assertEquals(v2, entity.getInt("value1")); - assertEquals(v1, entity.getInt("value2")); - i++; - } - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/update/DbUpdateDecimalTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/update/DbUpdateDecimalTest.java deleted file mode 100755 index c39e229c..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/update/DbUpdateDecimalTest.java +++ /dev/null @@ -1,130 +0,0 @@ -package com.tsurugidb.iceaxe.test.update; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrowsExactly; - -import java.io.IOException; -import java.math.BigDecimal; -import java.math.RoundingMode; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; - -import com.tsurugidb.iceaxe.sql.parameter.TgBindParameters; -import com.tsurugidb.iceaxe.sql.parameter.TgBindVariable; -import com.tsurugidb.iceaxe.sql.parameter.TgParameterMapping; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.transaction.exception.TsurugiTransactionException; -import com.tsurugidb.iceaxe.transaction.manager.exception.TsurugiTmIOException; -import com.tsurugidb.tsubakuro.sql.SqlServiceCode; - -/** - * update decimal test - */ -class DbUpdateDecimalTest extends DbTestTableTester { - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTable(); - - logInitEnd(info); - } - - private static void createTable() throws IOException, InterruptedException { - var sql = "create table " + TEST // - + "(" // - + " value decimal(5,2)" // - + ")"; - executeDdl(getSession(), sql); - } - - private static final String VNAME = "value"; - private static final String SQL = "insert into " + TEST + "(value) values(:" + VNAME + ")"; - - @ParameterizedTest - @ValueSource(booleans = { false, true }) - void div2(boolean cast) throws Exception { - div(2, cast, true); - } - - @Test - void div3() throws Exception { - div(3, false, false); - } - - @Test - void div3Cast() throws Exception { - div(3, true, true); - } - - private void div(int divValue, boolean cast, boolean expectedSuccess) throws IOException, InterruptedException { - insert(BigDecimal.ONE); - - var div = TgBindVariable.ofDecimal("div"); - String expression = "value / " + div; - if (cast) { - expression = "cast(" + expression + " as decimal(5,2))"; - } - var sql = "update " + TEST + " set value = " + expression; - var mapping = TgParameterMapping.of(div); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(sql, mapping)) { - var parameter = TgBindParameters.of(div.bind(divValue)); - if (expectedSuccess) { - int count = tm.executeAndGetCount(ps, parameter); - assertUpdateCount(1, count); - - var actual = selectValue(); - var expected = BigDecimal.ONE.divide(BigDecimal.valueOf(divValue), 2, RoundingMode.DOWN); - assertEquals(expected, actual); - } else { - var e0 = assertThrowsExactly(TsurugiTmIOException.class, () -> { - tm.execute(transaction -> { - var e = assertThrowsExactly(TsurugiTransactionException.class, () -> { - transaction.executeAndGetCount(ps, parameter); - }); - assertEqualsCode(SqlServiceCode.VALUE_EVALUATION_EXCEPTION, e); - assertContains("An error occurred in evaluating values", e.getMessage()); // TODO エラー詳細情報 - }); - }); - assertEqualsCode(SqlServiceCode.INACTIVE_TRANSACTION_EXCEPTION, e0); - - try (var selectPs = session.createQuery("select value from " + TEST)) { - var list = tm.executeAndGetList(selectPs); - assertEquals(1, list.size()); - assertEquals(BigDecimal.ONE.setScale(2), list.get(0).getDecimal("value")); - } - } - } - } - - private void insert(BigDecimal value) throws IOException, InterruptedException { - var variable = TgBindVariable.ofDecimal(VNAME); - var mapping = TgParameterMapping.of(variable); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(SQL, mapping)) { - var parameter = TgBindParameters.of(variable.bind(value)); - int count = tm.executeAndGetCount(ps, parameter); - assertUpdateCount(1, count); - } - } - - private BigDecimal selectValue() throws IOException, InterruptedException { - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createQuery("select value from " + TEST)) { - var entity = tm.executeAndFindRecord(ps).get(); - return entity.getDecimal(VNAME); - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/update/DbUpdateErrorTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/update/DbUpdateErrorTest.java deleted file mode 100755 index 3be1baed..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/update/DbUpdateErrorTest.java +++ /dev/null @@ -1,132 +0,0 @@ -package com.tsurugidb.iceaxe.test.update; - -import static org.junit.jupiter.api.Assertions.assertThrowsExactly; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; - -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.transaction.exception.TsurugiTransactionException; -import com.tsurugidb.iceaxe.transaction.manager.exception.TsurugiTmIOException; -import com.tsurugidb.tsubakuro.sql.SqlServiceCode; - -/** - * update error test - */ -class DbUpdateErrorTest extends DbTestTableTester { - - private static final int SIZE = 10; - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - if (!info.getDisplayName().equals("updateNullToNotNull()")) { - createTestTable(); - insertTestTable(SIZE); - } - - logInitEnd(info); - } - - @Test - void updatePKNull() throws Exception { - var sql = "update " + TEST // - + " set" // - + " foo = null" // primary key - + " where foo = 5"; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(sql)) { - var t = assertThrowsExactly(TsurugiTmIOException.class, () -> { - tm.execute(transaction -> { - var e = assertThrowsExactly(TsurugiTransactionException.class, () -> transaction.executeAndGetCount(ps)); - assertEqualsCode(SqlServiceCode.NOT_NULL_CONSTRAINT_VIOLATION_EXCEPTION, e); - assertContains("Null assigned for non-nullable field", e.getMessage()); // TODO エラー詳細情報の確認 - }); - }); - assertEqualsCode(SqlServiceCode.INACTIVE_TRANSACTION_EXCEPTION, t); - } - - assertEqualsTestTable(SIZE); - } - - @Test - void updatePKNullAll() throws Exception { - var sql = "update " + TEST // - + " set" // - + " foo = null"; // primary key - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(sql)) { - var t = assertThrowsExactly(TsurugiTmIOException.class, () -> { - tm.execute(transaction -> { - var e = assertThrowsExactly(TsurugiTransactionException.class, () -> transaction.executeAndGetCount(ps)); - assertEqualsCode(SqlServiceCode.NOT_NULL_CONSTRAINT_VIOLATION_EXCEPTION, e); - assertContains("Null assigned for non-nullable field", e.getMessage()); // TODO エラー詳細情報の確認 - }); - }); - assertEqualsCode(SqlServiceCode.INACTIVE_TRANSACTION_EXCEPTION, t); - } - - assertEqualsTestTable(SIZE); - } - - @ParameterizedTest - @ValueSource(ints = { 0, SIZE / 2, SIZE * 2 }) - void updatePKSameValue(int newPk) throws Exception { - var sql = "update " + TEST // - + " set" // - + " foo = " + newPk; // primary key - - var session = getSession(); - var tm = createTransactionManagerOcc(session, 2); - try (var ps = session.createStatement(sql)) { - var e = assertThrowsExactly(TsurugiTmIOException.class, () -> tm.executeAndGetCount(ps)); - assertEqualsCode(SqlServiceCode.UNIQUE_CONSTRAINT_VIOLATION_EXCEPTION, e); - assertContains("Unique constraint violation occurred", e.getMessage()); // TODO エラー詳細情報(カラム名あるいは制約名)の確認 - } - - assertEqualsTestTable(SIZE); - } - - @Test - void updateNullToNotNull() throws Exception { - var session = getSession(); - - var createSql = "create table " + TEST // - + "(" // - + " foo int not null," // - + " bar bigint not null," // - + " zzz varchar(10) not null," // - + " primary key(foo)" // - + ")"; - executeDdl(session, createSql); - insertTestTable(SIZE); - - var sql = "update " + TEST // - + " set" // - + " bar = null," // - + " zzz = null"; - - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(sql)) { - var t = assertThrowsExactly(TsurugiTmIOException.class, () -> { - tm.execute(transaction -> { - var e = assertThrowsExactly(TsurugiTransactionException.class, () -> transaction.executeAndGetCount(ps)); - assertEqualsCode(SqlServiceCode.NOT_NULL_CONSTRAINT_VIOLATION_EXCEPTION, e); - assertContains("Null assigned for non-nullable field", e.getMessage()); // TODO エラー詳細情報の確認 - }); - }); - assertEqualsCode(SqlServiceCode.INACTIVE_TRANSACTION_EXCEPTION, t); - } - - assertEqualsTestTable(SIZE); - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/update/DbUpdateExplainTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/update/DbUpdateExplainTest.java deleted file mode 100755 index fe40d989..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/update/DbUpdateExplainTest.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.tsurugidb.iceaxe.test.update; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertThrowsExactly; - -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.slf4j.LoggerFactory; - -import com.tsurugidb.iceaxe.sql.explain.TgStatementMetadata; -import com.tsurugidb.iceaxe.sql.parameter.TgBindParameters; -import com.tsurugidb.iceaxe.sql.parameter.TgBindVariable; -import com.tsurugidb.iceaxe.sql.parameter.TgParameterMapping; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; - -/** - * explain update test - */ -class DbUpdateExplainTest extends DbTestTableTester { - - @BeforeAll - static void beforeAll(TestInfo info) throws Exception { - var LOG = LoggerFactory.getLogger(DbUpdateExplainTest.class); - logInitStart(LOG, info); - - dropTestTable(); - createTestTable(); - - logInitEnd(LOG, info); - } - - @Test - void pareparedStatement() throws Exception { - var sql = "update " + TEST + " set bar=bar+1"; - - var session = getSession(); - try (var ps = session.createStatement(sql)) { - assertThrowsExactly(UnsupportedOperationException.class, () -> { - var result = ps.explain(); - assertExplain(result); - }); // TODO explain実装待ち - } - } - - @Test - void psParameter() throws Exception { - var add = TgBindVariable.ofLong("add"); - var sql = "update " + TEST + " set bar=bar+" + add; - var parameterMapping = TgParameterMapping.of(add); - - var session = getSession(); - try (var ps = session.createStatement(sql, parameterMapping)) { - var parameter = TgBindParameters.of(add.bind(1)); - var result = ps.explain(parameter); - assertExplain(result); - } - } - - private static void assertExplain(TgStatementMetadata actual) throws Exception { - assertNotNull(actual.getLowPlanGraph()); - - var list = actual.getLowColumnList(); - assertEquals(0, list.size()); - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/update/DbUpdateIrregularTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/update/DbUpdateIrregularTest.java deleted file mode 100755 index 26ddbada..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/update/DbUpdateIrregularTest.java +++ /dev/null @@ -1,62 +0,0 @@ -package com.tsurugidb.iceaxe.test.update; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; - -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; - -/** - * irregular update test - */ -class DbUpdateIrregularTest extends DbTestTableTester { - - private static final int SIZE = 10; - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTestTable(); - insertTestTable(SIZE); - - logInitEnd(info); - } - - @Test - void closePsBeforeCloseRc() throws Exception { - int number = SIZE / 2; - var sql = "update " + TEST // - + " set" // - + " bar = 0," // - + " zzz = 'aaa'" // - + " where foo = " + number; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - tm.execute(transaction -> { - var ps = session.createStatement(sql); - var result = ps.execute(transaction); - ps.close(); - int count = result.getUpdateCount(); - assertUpdateCount(1, count); - result.close(); - }); - - var list = selectAllFromTest(); - assertEquals(SIZE, list.size()); - for (var entity : list) { - if (entity.getFoo() == number) { - assertEquals(0L, entity.getBar()); - assertEquals("aaa", entity.getZzz()); - } else { - int i = entity.getFoo(); - var expected = createTestEntity(i); - assertEquals(expected, entity); - } - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/update/DbUpdateLongTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/update/DbUpdateLongTest.java deleted file mode 100755 index 7e9899b3..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/update/DbUpdateLongTest.java +++ /dev/null @@ -1,101 +0,0 @@ -package com.tsurugidb.iceaxe.test.update; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -import java.io.IOException; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; - -import com.tsurugidb.iceaxe.sql.parameter.TgBindParameters; -import com.tsurugidb.iceaxe.sql.parameter.TgBindVariable; -import com.tsurugidb.iceaxe.sql.parameter.TgBindVariables; -import com.tsurugidb.iceaxe.sql.parameter.TgParameterMapping; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.transaction.function.TsurugiTransactionAction; - -/** - * update long test - */ -class DbUpdateLongTest extends DbTestTableTester { - - private final int SIZE = 4; - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTable(); - insertTable(SIZE); - - logInitEnd(info); - } - - private static void createTable() throws IOException, InterruptedException { - var sql = "create table " + TEST // - + "(" // - + " pk int primary key," // - + " int_value int," // - + " long_value bigint" // - + ")"; - executeDdl(getSession(), sql); - } - - private static void insertTable(int size) throws IOException, InterruptedException { - var pk = TgBindVariable.ofInt("pk"); - var intValue = TgBindVariable.ofInt("intValue"); - var longValue = TgBindVariable.ofLong("longValue"); - var variables = TgBindVariables.of(pk, intValue, longValue); - var sql = "insert into " + TEST + " values(" + variables.getSqlNames() + ")"; - var mapping = TgParameterMapping.of(variables); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(sql, mapping)) { - tm.execute((TsurugiTransactionAction) transaction -> { - for (int i = 0; i < size; i++) { - var parameter = TgBindParameters.of(pk.bind(i), intValue.bind(10 + i), longValue.bind(100 + i)); - transaction.executeAndGetCount(ps, parameter); - } - }); - } - } - - @Test - @Disabled // TODO remove Disabled. intからbigintへセットするupdateがおかしい - void updateLongFromInt() throws Exception { - var sql = "update " + TEST + " set long_value = int_value"; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(sql)) { - tm.executeAndGetCount(ps); - } - - var list = tm.executeAndGetList("select * from " + TEST + " order by pk"); - assertEquals(SIZE, list.size()); - for (var entity : list) { - assertEquals(entity.getLong("int_value"), entity.getLong("long_value")); - } - } - - @Test - void updateIntFromLong() throws Exception { - var sql = "update " + TEST + " set int_value = long_value"; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(sql)) { - tm.executeAndGetCount(ps); - } - - var list = tm.executeAndGetList("select * from " + TEST + " order by pk"); - assertEquals(SIZE, list.size()); - for (var entity : list) { - assertEquals(entity.getLong("long_value"), entity.getLong("int_value")); - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/update/DbUpdateTest.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/update/DbUpdateTest.java deleted file mode 100755 index 52289b84..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/update/DbUpdateTest.java +++ /dev/null @@ -1,443 +0,0 @@ -package com.tsurugidb.iceaxe.test.update; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNull; -import static org.junit.jupiter.api.Assertions.assertThrowsExactly; - -import java.util.Collections; -import java.util.Comparator; -import java.util.stream.Collectors; -import java.util.stream.IntStream; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Disabled; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.TestInfo; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; - -import com.tsurugidb.iceaxe.sql.parameter.TgBindParameters; -import com.tsurugidb.iceaxe.sql.parameter.TgBindVariable; -import com.tsurugidb.iceaxe.sql.parameter.TgParameterMapping; -import com.tsurugidb.iceaxe.test.util.DbTestTableTester; -import com.tsurugidb.iceaxe.test.util.TestEntity; -import com.tsurugidb.iceaxe.transaction.manager.exception.TsurugiTmIOException; -import com.tsurugidb.iceaxe.transaction.manager.exception.TsurugiTmRetryOverIOException; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; -import com.tsurugidb.tsubakuro.sql.SqlServiceCode; - -/** - * update test - */ -class DbUpdateTest extends DbTestTableTester { - - private static final int SIZE = 10; - - @BeforeEach - void beforeEach(TestInfo info) throws Exception { - logInitStart(info); - - dropTestTable(); - createTestTable(); - insertTestTable(SIZE); - - logInitEnd(info); - } - - @Test - void updateAll() throws Exception { - var sql = "update " + TEST // - + " set" // - + " bar = 0," // - + " zzz = 'aaa'"; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(sql)) { - int count = tm.executeAndGetCount(ps); - assertUpdateCount(SIZE, count); - } - - var list = selectAllFromTest(); - assertEquals(SIZE, list.size()); - for (var entity : list) { - assertEquals(0L, entity.getBar()); - assertEquals("aaa", entity.getZzz()); - } - } - - @Test - void updateAllNull() throws Exception { - var sql = "update " + TEST // - + " set" // - + " bar = null," // - + " zzz = null"; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(sql)) { - int count = tm.executeAndGetCount(ps); - assertUpdateCount(SIZE, count); - } - - var list = selectAllFromTest(); - assertEquals(SIZE, list.size()); - for (var entity : list) { - assertNull(entity.getBar()); - assertNull(entity.getZzz()); - } - } - - @Test - void updateWhere() throws Exception { - var sql = "update " + TEST // - + " set" // - + " bar = 0," // - + " zzz = 'aaa'" // - + " where foo % 2 = 0"; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(sql)) { - int count = tm.executeAndGetCount(ps); - assertUpdateCount(SIZE, count); - } - - var list = selectAllFromTest(); - assertEquals(SIZE, list.size()); - int i = 0; - for (var entity : list) { - if (entity.getFoo() % 2 == 0) { - assertEquals(0L, entity.getBar()); - assertEquals("aaa", entity.getZzz()); - } else { - assertEquals(i, entity.getBar()); - assertEquals(Integer.toString(i), entity.getZzz()); - } - i++; - } - } - - @Test - void updateNothing() throws Exception { - var sql = "update " + TEST // - + " set" // - + " bar = 0," // - + " zzz = 'aaa'" // - + " where foo < 0"; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(sql)) { - int count = tm.executeAndGetCount(ps); - assertUpdateCount(0, count); - } - - assertEqualsTestTable(SIZE); - } - - @Test - void updateEntity() throws Exception { - var sql = "update " + TEST // - + " set" // - + " bar = :bar," // - + " zzz = :zzz" // - + " where foo = :foo"; - var parameterMapping = TgParameterMapping.of(TestEntity.class) // - .addInt("foo", TestEntity::getFoo) // - .addLong("bar", TestEntity::getBar) // - .addString("zzz", TestEntity::getZzz); - - var updateEntity = new TestEntity(5, 55, "go"); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(sql, parameterMapping)) { - int count = tm.executeAndGetCount(ps, updateEntity); - assertUpdateCount(1, count); - } - - var list = selectAllFromTest(); - assertEquals(SIZE, list.size()); - int i = 0; - for (var entity : list) { - if (entity.getFoo().equals(updateEntity.getFoo())) { - assertEquals(updateEntity, entity); - } else { - var expected = createTestEntity(i); - assertEquals(expected, entity); - } - i++; - } - } - - @Test - void updateExpression() throws Exception { - var sql = "update " + TEST // - + " set" // - + " bar = bar + 1"; - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(sql)) { - int count = tm.executeAndGetCount(ps); - assertUpdateCount(SIZE, count); - } - - var list = selectAllFromTest(); - assertEquals(SIZE, list.size()); - int i = 0; - for (var entity : list) { - var expected = new TestEntity(i, i + 1, Integer.toString(i)); - assertEquals(expected, entity); - i++; - } - } - - @ParameterizedTest - @ValueSource(ints = { -1, 0, 1, SIZE - 1, SIZE, SIZE + 1, -(SIZE - 1), -SIZE, -(SIZE + 1) }) - void updatePK_occ(int add) throws Exception { - String addText = (add >= 0) ? " + " + add : "" + add; - var sql = "update " + TEST // - + " set" // - + " foo = foo" + addText; // primary key - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(sql)) { - if (1 <= add && add <= SIZE - 1) { - var e = assertThrowsExactly(TsurugiTmIOException.class, () -> tm.executeAndGetCount(ps)); - assertEqualsCode(SqlServiceCode.UNIQUE_CONSTRAINT_VIOLATION_EXCEPTION, e); - assertEqualsTestTable(SIZE); - return; - } else { - int count = tm.executeAndGetCount(ps); - assertUpdateCount(SIZE, count); - } - } catch (TsurugiTmRetryOverIOException e) { - if (add == -1) { // TODO updatePK(-1) - return; - } - if (add >= SIZE) { // TODO updatePK(SIZE) - return; - } - if (add <= -(SIZE - 1)) { // TODO updatePK(-SIZE) - return; - } - throw e; - } - - var expectedList = IntStream.range(0, SIZE).mapToObj(i -> createTestEntity(i)).peek(e -> e.setFoo(e.getFoo() + add)).collect(Collectors.toList()); - Collections.sort(expectedList, Comparator.comparing(TestEntity::getFoo)); - assertEqualsTestTable(expectedList); - } - - @ParameterizedTest - @ValueSource(ints = { -1, 0, 1, SIZE - 1, SIZE, SIZE + 1, -(SIZE - 1), -SIZE, -(SIZE + 1) }) - void updatePK_ltx(int add) throws Exception { - String addText = (add >= 0) ? " + " + add : "" + add; - var sql = "update " + TEST // - + " set" // - + " foo = foo" + addText; // primary key - - var session = getSession(); - var tm = session.createTransactionManager(TgTxOption.ofLTX(TEST)); - try (var ps = session.createStatement(sql)) { - if (1 <= add && add <= SIZE - 1) { - var e = assertThrowsExactly(TsurugiTmIOException.class, () -> tm.executeAndGetCount(ps)); - assertEqualsCode(SqlServiceCode.UNIQUE_CONSTRAINT_VIOLATION_EXCEPTION, e); - assertEqualsTestTable(SIZE); - return; - } else { - int count = tm.executeAndGetCount(ps); - assertUpdateCount(SIZE, count); - } - } - - var expectedList = IntStream.range(0, SIZE).mapToObj(i -> createTestEntity(i)).peek(e -> e.setFoo(e.getFoo() + add)).collect(Collectors.toList()); - Collections.sort(expectedList, Comparator.comparing(TestEntity::getFoo)); - assertEqualsTestTable(expectedList); - } - - @Test - void updatePKNoChange() throws Exception { - var sql = "update " + TEST // - + " set" // - + " foo = foo"; // primary key - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(sql)) { - int count = tm.executeAndGetCount(ps); - assertUpdateCount(SIZE, count); - } - - assertEqualsTestTable(SIZE); - } - - @Test - @Disabled // TODO remove Disabled. implements mod() - void updatePKSwap() throws Exception { - var sql = "update " + TEST // - + " set" // - + " foo = foo - mod(foo, 2)*2 + 1"; // primary key - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - try (var ps = session.createStatement(sql)) { - int count = tm.executeAndGetCount(ps); - assertUpdateCount(SIZE, count); - } - - var expectedList = IntStream.range(0, SIZE).mapToObj(i -> createTestEntity(i)).peek(e -> e.setFoo(-(e.getFoo() % 2) + 1)).collect(Collectors.toList()); - Collections.sort(expectedList, Comparator.comparing(TestEntity::getFoo)); - assertEqualsTestTable(expectedList); - } - - @Test - void insertUpdate() throws Exception { - var insertEntity = new TestEntity(123, 456, "abc"); - var sql = "update " + TEST // - + " set" // - + " bar = 789" // - + " where foo = " + insertEntity.getFoo(); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - tm.execute(tranasction -> { - // insert - try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING)) { - int count = tranasction.executeAndGetCount(ps, insertEntity); - assertUpdateCount(1, count); - } - - // update - try (var ps = session.createStatement(sql)) { - int count = tranasction.executeAndGetCount(ps); - assertUpdateCount(1, count); - } - - // select - try (var ps = session.createQuery(SELECT_SQL, SELECT_MAPPING)) { - var list = tranasction.executeAndGetList(ps); - assertEquals(SIZE + 1, list.size()); - for (var entity : list) { - if (entity.getFoo().equals(insertEntity.getFoo())) { - assertEquals(789L, entity.getBar()); - } else { - assertEquals((long) entity.getFoo(), entity.getBar()); - } - } - } - }); - - var list = selectAllFromTest(); - assertEquals(SIZE + 1, list.size()); - for (var entity : list) { - if (entity.getFoo().equals(insertEntity.getFoo())) { - assertEquals(789L, entity.getBar()); - } else { - assertEquals((long) entity.getFoo(), entity.getBar()); - } - } - } - - @Test - void insertUpdateNoCheck() throws Exception { - var insertEntity = new TestEntity(123, 456, "abc"); - var sql = "update " + TEST // - + " set" // - + " bar = 789" // - + " where foo = " + insertEntity.getFoo(); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - tm.execute(tranasction -> { - // insert - try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING)) { - ps.execute(tranasction, insertEntity); // not get-count - } - // executeの結果を確認せずに次のSQLを実行すると、同一トランザクション内でもSQLの実行順序が保証されないらしい - - // update - try (var ps = session.createStatement(sql)) { - ps.execute(tranasction); // not get-count - } - - // select - try (var ps = session.createQuery(SELECT_SQL, SELECT_MAPPING)) { - var list = tranasction.executeAndGetList(ps); - assertEquals(SIZE + 1, list.size()); - for (var entity : list) { - if (entity.getFoo().equals(insertEntity.getFoo())) { - assertEquals(789L, entity.getBar()); - } else { - assertEquals((long) entity.getFoo(), entity.getBar()); - } - } - } - }); - - var list = selectAllFromTest(); - assertEquals(SIZE + 1, list.size()); - for (var entity : list) { - if (entity.getFoo().equals(insertEntity.getFoo())) { - assertEquals(789L, entity.getBar()); - } else { - assertEquals((long) entity.getFoo(), entity.getBar()); - } - } - } - - @Test - void updateUpdate() throws Exception { - int foo = 1; - var bar = TgBindVariable.ofLong("bar"); - var sql = "update " + TEST // - + " set" // - + " bar = " + bar // - + " where foo = " + foo; - var parameterMapping = TgParameterMapping.of(bar); - - var session = getSession(); - var tm = createTransactionManagerOcc(session); - tm.execute(tranasction -> { - // update - try (var ps = session.createStatement(sql, parameterMapping)) { - { - var parameter = TgBindParameters.of(bar.bind(101)); - int count = tranasction.executeAndGetCount(ps, parameter); - assertUpdateCount(1, count); - } - { - var parameter = TgBindParameters.of(bar.bind(102)); - int count = tranasction.executeAndGetCount(ps, parameter); - assertUpdateCount(1, count); - } - } - - // select - try (var ps = session.createQuery(SELECT_SQL, SELECT_MAPPING)) { - var list = tranasction.executeAndGetList(ps); - assertEquals(SIZE, list.size()); - for (var entity : list) { - if (entity.getFoo().equals(foo)) { - assertEquals(102L, entity.getBar()); - } else { - assertEquals((long) entity.getFoo(), entity.getBar()); - } - } - } - }); - - var list = selectAllFromTest(); - assertEquals(SIZE, list.size()); - for (var entity : list) { - if (entity.getFoo().equals(foo)) { - assertEquals(102L, entity.getBar()); - } else { - assertEquals((long) entity.getFoo(), entity.getBar()); - } - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/util/DbTestConnector.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/util/DbTestConnector.java deleted file mode 100755 index 6225a417..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/util/DbTestConnector.java +++ /dev/null @@ -1,139 +0,0 @@ -package com.tsurugidb.iceaxe.test.util; - -import static org.junit.jupiter.api.Assumptions.assumeTrue; - -import java.io.IOException; -import java.io.UncheckedIOException; -import java.net.Socket; -import java.net.URI; -import java.util.List; -import java.util.concurrent.CopyOnWriteArrayList; -import java.util.concurrent.TimeUnit; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.tsurugidb.iceaxe.TsurugiConnector; -import com.tsurugidb.iceaxe.session.TgSessionOption; -import com.tsurugidb.iceaxe.session.TgSessionOption.TgTimeoutKey; -import com.tsurugidb.iceaxe.session.TsurugiSession; -import com.tsurugidb.iceaxe.session.event.TsurugiSessionEventListener; -import com.tsurugidb.iceaxe.transaction.TsurugiTransaction; -import com.tsurugidb.iceaxe.transaction.event.TsurugiTransactionEventListener; - -public class DbTestConnector { - private static final Logger LOG = LoggerFactory.getLogger(DbTestConnector.class); - - private static final String SYSPROP_DBTEST_ENDPOINT = "tsurugi.dbtest.endpoint"; - - private static URI staticEndpoint; - private static final List staticSessionList = new CopyOnWriteArrayList<>(); - - private static URI getEndPoint() { - if (staticEndpoint == null) { - String endpoint = System.getProperty(SYSPROP_DBTEST_ENDPOINT, "tcp://localhost:12345"); - staticEndpoint = URI.create(endpoint); - } - return staticEndpoint; - } - - public static boolean isIpc() { - URI endpoint = getEndPoint(); - String scheme = endpoint.getScheme(); - return scheme.equals("ipc"); - } - - public static boolean isTcp() { - URI endpoint = getEndPoint(); - String scheme = endpoint.getScheme(); - return scheme.equals("tcp"); - } - - public static URI assumeEndpointTcp() { - URI endpoint = getEndPoint(); - String scheme = endpoint.getScheme(); - assumeTrue(scheme.equals("tcp"), "ednpoint is not tcp"); - return endpoint; - } - - public static TsurugiConnector createConnector() { - URI endpoint = getEndPoint(); - return TsurugiConnector.of(endpoint); - } - - public static TsurugiSession createSession() throws IOException { - return createSession(20, TimeUnit.SECONDS); - } - - public static TsurugiSession createSession(long time, TimeUnit unit) throws IOException { - var sessionOption = TgSessionOption.of(); - sessionOption.setTimeout(TgTimeoutKey.DEFAULT, time, unit); - - var connector = createConnector(); - var session = connector.createSession(sessionOption); - addSession(session); -// session.addEventListener(SESSION_LISTENER); - return session; - } - - public static void addSession(TsurugiSession session) { - staticSessionList.add(session); - session.addEventListener(SESSION_CLOSE_LISTENER); - } - - @SuppressWarnings("unused") - private static final TsurugiSessionEventListener SESSION_LISTENER = new TsurugiSessionEventListener() { - private final TsurugiTransactionEventListener TRANSACTION_LISTENER = new TsurugiTransactionEventListener() { - @Override - public void lowTransactionGetEnd(TsurugiTransaction transaction, String transactionId, Throwable occurred) { - LOG.info("transactionId={}, {}", transactionId, transaction.getTransactionOption()); - } - }; - - @Override - public void createTransaction(TsurugiTransaction transaction) { -// transaction.addEventListener(TRANSACTION_LISTENER); - try { - LOG.info("transactionId={}, {}", transaction.getTransactionId(), transaction.getTransactionOption()); - } catch (IOException | InterruptedException e) { - LOG.info("getTransactionId error {}", transaction.getTransactionOption(), e); - } - } - }; - - private static final TsurugiSessionEventListener SESSION_CLOSE_LISTENER = new TsurugiSessionEventListener() { - @Override - public void closeSession(TsurugiSession session, Throwable occurred) { - staticSessionList.remove(session); - } - }; - - public static Socket createSocket() { - URI endpoint = assumeEndpointTcp(); - try { - return new Socket(endpoint.getHost(), endpoint.getPort()); - } catch (IOException e) { - throw new UncheckedIOException(e); - } - } - - public static void closeLeakSession() { - int leak = 0; - var list = List.copyOf(staticSessionList); - for (var session : list) { - if (!session.isClosed()) { - LOG.error("session leak! {}", session); - leak++; - try { - session.close(); - } catch (Throwable e) { - LOG.warn("session close error", e); - } - } - } - - if (leak > 0) { - throw new AssertionError("session leak! " + leak); - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/util/DbTestSessions.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/util/DbTestSessions.java deleted file mode 100755 index 27c12eb3..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/util/DbTestSessions.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.tsurugidb.iceaxe.test.util; - -import java.io.Closeable; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.TimeUnit; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.tsurugidb.iceaxe.session.TsurugiSession; - -public class DbTestSessions implements Closeable { - private static final Logger LOG = LoggerFactory.getLogger(DbTestSessions.class); - - private final long timeout; - private final TimeUnit timeUnit; - private final List sessionList = new ArrayList<>(); - - public DbTestSessions() { - this.timeout = 0; - this.timeUnit = null; - } - - public DbTestSessions(long timeout, TimeUnit unit) { - this.timeout = timeout; - this.timeUnit = unit; - } - - public TsurugiSession createSession() throws IOException { - var session = (timeUnit == null) ? DbTestConnector.createSession() // - : DbTestConnector.createSession(timeout, timeUnit); - sessionList.add(session); - return session; - } - - @Override - public void close() throws IOException { - for (var session : sessionList) { - try { - session.close(); - } catch (Exception e) { - LOG.warn("session close error", e.getMessage()); - } - } - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/util/DbTestTableTester.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/util/DbTestTableTester.java deleted file mode 100755 index dc4bad64..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/util/DbTestTableTester.java +++ /dev/null @@ -1,492 +0,0 @@ -package com.tsurugidb.iceaxe.test.util; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; - -import java.io.Closeable; -import java.io.IOException; -import java.lang.reflect.Method; -import java.util.Arrays; -import java.util.List; -import java.util.concurrent.Callable; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.regex.Pattern; -import java.util.stream.Collectors; - -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.TestInfo; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.tsurugidb.iceaxe.exception.IceaxeErrorCode; -import com.tsurugidb.iceaxe.exception.TsurugiDiagnosticCodeProvider; -import com.tsurugidb.iceaxe.session.TsurugiSession; -import com.tsurugidb.iceaxe.sql.parameter.TgBindParameters; -import com.tsurugidb.iceaxe.sql.parameter.TgBindVariable; -import com.tsurugidb.iceaxe.sql.parameter.TgParameterMapping; -import com.tsurugidb.iceaxe.sql.parameter.mapping.TgEntityParameterMapping; -import com.tsurugidb.iceaxe.sql.result.TgResultMapping; -import com.tsurugidb.iceaxe.sql.result.TsurugiResultEntity; -import com.tsurugidb.iceaxe.sql.result.mapping.TgEntityResultMapping; -import com.tsurugidb.iceaxe.transaction.TsurugiTransaction; -import com.tsurugidb.iceaxe.transaction.manager.TgTmSetting; -import com.tsurugidb.iceaxe.transaction.manager.TsurugiTransactionManager; -import com.tsurugidb.iceaxe.transaction.manager.event.TsurugiTmEventListener; -import com.tsurugidb.iceaxe.transaction.manager.exception.TsurugiTmIOException; -import com.tsurugidb.iceaxe.transaction.option.TgTxOption; -import com.tsurugidb.tsubakuro.exception.DiagnosticCode; -import com.tsurugidb.tsubakuro.exception.ServerException; -import com.tsurugidb.tsubakuro.sql.SqlServiceCode; -import com.tsurugidb.tsubakuro.sql.SqlServiceException; -import com.tsurugidb.tsubakuro.sql.exception.SqlExecutionException; - -public class DbTestTableTester { - protected final Logger LOG = LoggerFactory.getLogger(getClass()); - - /** test (table name) */ - public static final String TEST = "test"; - public static final String TEST_COLUMNS = "foo, bar, zzz"; - public static final int ZZZ_SIZE = 10; - - private static TsurugiSession staticSession; - private static ExecutorService staticService; - - protected static TsurugiSession getSession() throws IOException { - synchronized (DbTestTableTester.class) { - if (staticSession == null) { - staticSession = DbTestConnector.createSession(); - } - } - return staticSession; - } - - protected static synchronized void closeStaticSession() throws IOException, InterruptedException { - if (staticSession != null) { - staticSession.close(); - staticSession = null; - } - } - - @AfterAll - static void testerAfterAll() throws IOException, InterruptedException { - try (var c1 = staticSession; var c2 = (Closeable) () -> { - if (staticService != null) { - staticService.shutdownNow(); - } - }) { - // close only - } finally { - staticSession = null; - staticService = null; - } - - DbTestConnector.closeLeakSession(); - } - - private static final boolean START_END_LOG_INFO = true; - - protected static void logInitStart(Logger log, TestInfo info) { - if (START_END_LOG_INFO) { - log.info("init all start"); - } else { - log.debug("init all start"); - } - } - - protected static void logInitEnd(Logger log, TestInfo info) { - if (START_END_LOG_INFO) { - log.info("init all end"); - } else { - log.debug("init all end"); - } - } - - protected void logInitStart(TestInfo info) { - if (START_END_LOG_INFO) { - LOG.info("{} init start", getDisplayName(info)); - } else { - LOG.debug("{} init start", getDisplayName(info)); - } - } - - protected void logInitEnd(TestInfo info) { - if (START_END_LOG_INFO) { - LOG.info("{} init end", getDisplayName(info)); - } else { - LOG.debug("{} init end", getDisplayName(info)); - } - } - - @BeforeEach - void tetsterBeforeEach(TestInfo info) { - if (START_END_LOG_INFO) { - LOG.info("{} start", getDisplayName(info)); - } else { - LOG.debug("{} start", getDisplayName(info)); - } - } - - @AfterEach - void testerAfterEach(TestInfo info) { - if (START_END_LOG_INFO) { - LOG.info("{} end", getDisplayName(info)); - } else { - LOG.debug("{} end", getDisplayName(info)); - } - } - - private static String getDisplayName(TestInfo info) { - String d = info.getDisplayName(); - String m = info.getTestMethod().map(Method::getName).orElse(null); - if (m != null && !d.startsWith(m)) { - return m + "() " + d; - } - return d; - } - - // property - - protected static String getSystemProperty(String key, String defaultValue) { - String property = System.getProperty(key); - return (property != null) ? property : defaultValue; - } - - protected static int getSystemProperty(String key, int defaultValue) { - String property = getSystemProperty(key, (String) null); - return (property != null) ? Integer.parseInt(property) : defaultValue; - } - - // utility - - protected static void dropTestTable() throws IOException, InterruptedException { - dropTable(TEST); - } - - protected static void dropTable(String tableName) throws IOException, InterruptedException { - if (existsTable(tableName)) { - var sql = "drop table " + tableName; - executeDdl(getSession(), sql, tableName); - } - } - - protected static boolean existsTable(String tableName) throws IOException, InterruptedException { - var session = getSession(); - var opt = session.findTableMetadata(tableName); - return opt.isPresent(); - } - - protected static final String CREATE_TEST_SQL = "create table " + TEST // - + "(" // - + " foo int," // - + " bar bigint," // - + " zzz varchar(" + ZZZ_SIZE + ")," // - + " primary key(foo)" // - + ")"; - - protected static void createTestTable() throws IOException, InterruptedException { - executeDdl(getSession(), CREATE_TEST_SQL, TEST); - } - - protected static void executeDdl(TsurugiSession session, String sql) throws IOException, InterruptedException { - String tableName; - if (sql.startsWith("create")) { - int s = sql.indexOf("table"); - int e = sql.indexOf('('); - tableName = sql.substring(s + "table".length(), e).trim(); - } else if (sql.startsWith("drop")) { - int s = sql.indexOf("table"); - tableName = sql.substring(s + "table".length()).trim(); - } else { - throw new IllegalArgumentException(sql); - } - executeDdl(session, sql, tableName); - } - - protected static void executeDdl(TsurugiSession session, String sql, String tableName) throws IOException, InterruptedException { - boolean workaround = false; - if (workaround) { - executeDdlWorkaround(session, sql, tableName); - return; - } - - var tm = createTransactionManagerOcc(session, "executeDdl", 1); - tm.executeDdl(sql); - } - - @Deprecated(forRemoval = true) - private static void executeDdlWorkaround(TsurugiSession session, String sql, String tableName) throws IOException, InterruptedException { - var tm = createTransactionManagerOcc(session, "executeDdlWorkaround", 3); - tm.addEventListener(new TsurugiTmEventListener() { - @Override - public void transactionException(TsurugiTransaction transaction, Throwable e) { - var log = LoggerFactory.getLogger(DbTestTableTester.class); - log.warn("executeDdl error. {}, sql={}", e.getMessage(), sql); - } - }); - try (var ps = session.createStatement(sql)) { - for (int i = 1;; i++) { - try { - tm.executeAndGetCount(ps); - return; - } catch (TsurugiTmIOException e) { - // duplicate_table(ERR_PHANTOM)が発生したら、リトライ - String message = e.getMessage(); - if (message.contains("ERR_COMPILER_ERROR: SQL--0005") && message.contains("translating statement failed: duplicate_table table")) { - var line = Arrays.stream(e.getStackTrace()).filter(elem -> { - String fullName = elem.getClassName(); - return fullName.startsWith("com.tsurugidb.iceaxe.test.") && fullName.endsWith("Test"); - }).findFirst().orElse(null); - var log = LoggerFactory.getLogger(DbTestTableTester.class); - log.warn("executeDdl duplicate_table retry{} at {}", i, line); - - // リトライ時にテーブルは消えているはずなので、自分でdropはしない - // dropTable(tableName); - continue; - } - throw e; - } - } - } - } - - protected static final String INSERT_SQL = "insert into " + TEST // - + "(" + TEST_COLUMNS + ")" // - + "values(:foo, :bar, :zzz)"; - protected static final TgEntityParameterMapping INSERT_MAPPING = TgParameterMapping.of(TestEntity.class) // - .addInt("foo", TestEntity::getFoo) // - .addLong("bar", TestEntity::getBar) // - .addString("zzz", TestEntity::getZzz); - - protected static void insertTestTable(int size) throws IOException, InterruptedException { - var session = getSession(); - var tm = createTransactionManagerOcc(session, "insertTestTable", 3); - try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING)) { - tm.execute(transaction -> { - for (int i = 0; i < size; i++) { - var entity = createTestEntity(i); - transaction.executeAndGetCount(ps, entity); - } - return; - }); - } - } - - protected static TestEntity createTestEntity(int i) { - return new TestEntity(i, i, Integer.toString(i)); - } - - protected static void insertTestTable(TestEntity entity) throws IOException, InterruptedException { - var session = getSession(); - var tm = createTransactionManagerOcc(session, "insertTestTable", 3); - try (var ps = session.createStatement(INSERT_SQL, INSERT_MAPPING)) { - tm.execute(transaction -> { - transaction.executeAndGetCount(ps, entity); - }); - } - } - - protected static final String SELECT_SQL = "select " + TEST_COLUMNS + " from " + TEST; - - protected static final TgEntityResultMapping SELECT_MAPPING = TgResultMapping.of(TestEntity::new) // - .addInt("foo", TestEntity::setFoo) // - .addLong("bar", TestEntity::setBar) // - .addString("zzz", TestEntity::setZzz); - - protected static Future executeFuture(Callable task) { - synchronized (DbTestTableTester.class) { - if (staticService == null) { - staticService = Executors.newCachedThreadPool(); - } - } - - var started = new AtomicBoolean(false); - var future = staticService.submit(() -> { - started.set(true); - return task.call(); - }); - while (!started.get()) { - try { - Thread.sleep(10); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } - } - return future; - } - - // transaction manager - - protected static TsurugiTransactionManager createTransactionManagerOcc(TsurugiSession session) { - return session.createTransactionManager(TgTxOption.ofOCC()); - } - - protected static TsurugiTransactionManager createTransactionManagerOcc(TsurugiSession session, int max) { - return session.createTransactionManager(TgTmSetting.ofAlways(TgTxOption.ofOCC(), max)); - } - - protected static TsurugiTransactionManager createTransactionManagerOcc(TsurugiSession session, String txLabel, int max) { - return session.createTransactionManager(TgTmSetting.ofAlways(TgTxOption.ofOCC().label(txLabel), max)); - } - - // assertion - - protected void assertEqualsCode(DiagnosticCode expected, Throwable actual) { - var code = findDiagnosticCode(actual); - assertEquals(expected, code); - - var expectedClass = findLowServerExceptionClass(expected); - if (expectedClass != null) { - var actualServerException = findLowServerException(actual); - assertEquals(expectedClass, actualServerException.getClass()); - } - } - - protected static DiagnosticCode findDiagnosticCode(Throwable t) { - { - var e = findTsurugiDiagnosticCodeProvider(t); - if (e != null) { - return e.getDiagnosticCode(); - } - } - { - var e = findLowServerException(t); - if (e != null) { - return e.getDiagnosticCode(); - } - } - return null; - } - - protected static TsurugiDiagnosticCodeProvider findTsurugiDiagnosticCodeProvider(Throwable t) { - for (; t != null; t = t.getCause()) { - if (t instanceof TsurugiDiagnosticCodeProvider) { - return (TsurugiDiagnosticCodeProvider) t; - } - } - return null; - } - - protected static ServerException findLowServerException(Throwable t) { - for (; t != null; t = t.getCause()) { - if (t instanceof ServerException) { - return (ServerException) t; - } - } - return null; - } - - protected static Class findLowServerExceptionClass(DiagnosticCode code) { - if (code instanceof IceaxeErrorCode) { - return null; - } - if (code instanceof SqlServiceCode) { - if (code == SqlServiceCode.SQL_SERVICE_EXCEPTION) { - return SqlServiceException.class; - } - - var name = toCamelCase(code.name()); - var className = SqlExecutionException.class.getPackage().getName() + "." + name; - try { - return Class.forName(className); - } catch (ClassNotFoundException e) { - throw new AssertionError(e); - } - } - throw new AssertionError(code); - } - - private static String toCamelCase(String snakeCase) { - return Arrays.stream(snakeCase.split("_")).map(s -> Character.toUpperCase(s.charAt(0)) + s.substring(1).toLowerCase()).collect(Collectors.joining()); - } - - protected static void assertEqualsTestTable(TestEntity... expected) throws IOException, InterruptedException { - var expectedList = List.of(expected); - assertEqualsTestTable(expectedList); - } - - protected static void assertEqualsTestTable(List expected) throws IOException, InterruptedException { - var actual = selectAllFromTest(); - assertEquals(expected, actual); - } - - protected static void assertEqualsTestTable(int expectedSize) throws IOException, InterruptedException { - var actualList = selectAllFromTest(); - assertEqualsTestTable(expectedSize, actualList); - } - - protected static void assertEqualsTestTable(int expectedSize, List actualList) { - assertEquals(expectedSize, actualList.size()); - for (int i = 0; i < expectedSize; i++) { - var expected = createTestEntity(i); - var actual = actualList.get(i); - assertEquals(expected, actual); - } - } - - protected static void assertEqualsResultEntity(TestEntity expected, TsurugiResultEntity actual) { - assertEquals(expected.getFoo(), actual.getIntOrNull("foo")); - assertEquals(expected.getBar(), actual.getLongOrNull("bar")); - assertEquals(expected.getZzz(), actual.getStringOrNull("zzz")); - } - - protected static List selectAllFromTest() throws IOException, InterruptedException { - var sql = SELECT_SQL + "\norder by " + TEST_COLUMNS; - - var session = getSession(); - var tm = createTransactionManagerOcc(session, "selectAllFromTest", 3); - try (var ps = session.createQuery(sql, SELECT_MAPPING)) { - return tm.executeAndGetList(ps); - } - } - - protected static TestEntity selectFromTest(int foo) throws IOException, InterruptedException { - var where1 = TgBindVariable.ofInt("foo"); - var sql = SELECT_SQL + " where foo=" + where1; - var session = getSession(); - var tm = createTransactionManagerOcc(session, "selectFromTest", 3); - try (var ps = session.createQuery(sql, TgParameterMapping.of(where1), SELECT_MAPPING)) { - var parameter = TgBindParameters.of(where1.bind(foo)); - return tm.executeAndFindRecord(ps, parameter).orElse(null); - } - } - - protected static int selectCountFromTest() throws IOException, InterruptedException { - return selectCountFrom(TEST); - } - - protected static int selectCountFrom(String tableName) throws IOException, InterruptedException { - var sql = "select count(*) from " + tableName; - var resultMapping = TgResultMapping.ofSingle(int.class); - - var session = getSession(); - var tm = createTransactionManagerOcc(session, "selectCountFrom", 3); - try (var ps = session.createQuery(sql, resultMapping)) { - return tm.executeAndFindRecord(ps).get(); - } - } - - protected static void assertContains(String expected, String actual) { - assertNotNull(actual); - if (actual.contains(expected)) { - return; // success - } - assertEquals(expected, actual, "not contains"); - } - - protected static void assertMatches(String expectedRegexp, String actual) { - assertNotNull(actual); - if (Pattern.matches(expectedRegexp, actual)) { - return; // success - } - assertEquals(expectedRegexp, actual, "unmatched"); - } - - protected static void assertUpdateCount(int expected, int actual) { - assertEquals(-1, actual); // TODO use expected (for updateCount) - } -} diff --git a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/util/TestEntity.java b/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/util/TestEntity.java deleted file mode 100755 index e9ebb322..00000000 --- a/modules/iceaxe-dbtest/src/test/java/com/tsurugidb/iceaxe/test/util/TestEntity.java +++ /dev/null @@ -1,77 +0,0 @@ -package com.tsurugidb.iceaxe.test.util; - -import java.util.Objects; - -/** - * example entity for 'test' table - */ -public class TestEntity { - - private Integer foo; - private Long bar; - private String zzz; - - public TestEntity() { - } - - public TestEntity(int foo, long bar, String zzz) { - this.foo = foo; - this.bar = bar; - this.zzz = zzz; - } - - public TestEntity(Integer foo, Long bar, String zzz) { - this.foo = foo; - this.bar = bar; - this.zzz = zzz; - } - - public Integer getFoo() { - return foo; - } - - public void setFoo(Integer foo) { - this.foo = foo; - } - - public Long getBar() { - return bar; - } - - public void setBar(Long bar) { - this.bar = bar; - } - - public String getZzz() { - return zzz; - } - - public void setZzz(String zzz) { - this.zzz = zzz; - } - - @Override - public int hashCode() { - return Objects.hash(bar, foo, zzz); - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - var other = (TestEntity) obj; - return Objects.equals(bar, other.bar) && Objects.equals(foo, other.foo) && Objects.equals(zzz, other.zzz); - } - - @Override - public String toString() { - return "TestEntity{foo=" + foo + ", bar=" + bar + ", zzz=" + zzz + "}"; - } -} diff --git a/modules/iceaxe-dbtest/src/test/resources/logback-test.xml b/modules/iceaxe-dbtest/src/test/resources/logback-test.xml deleted file mode 100755 index a4049539..00000000 --- a/modules/iceaxe-dbtest/src/test/resources/logback-test.xml +++ /dev/null @@ -1,50 +0,0 @@ - - - - - - %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n - - - - TRACE - - - - - logs/summary-${ts_sec}.log - false - - - %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n - - - - INFO - - - - - logs/detail-${ts_sec}.log - false - - - %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n - - - - - - - - - - - - diff --git a/settings.gradle b/settings.gradle index 9fd24a66..87bb673d 100644 --- a/settings.gradle +++ b/settings.gradle @@ -2,7 +2,6 @@ rootProject.name = 'iceaxe' include 'iceaxe-core' include 'iceaxe-examples' -include 'iceaxe-dbtest' rootProject.children.each { project -> project.projectDir = new File(settingsDir, "modules/${project.name}")