Fix test for being inside a transaction. (#1969)

Closes #1968.
This commit is contained in:
Michael Reiche
2024-10-03 09:37:09 -07:00
committed by mikereiche
parent 6eccb39e95
commit ce57f76ea4
4 changed files with 8 additions and 50 deletions

View File

@@ -23,6 +23,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.fail;
import static org.springframework.data.couchbase.transactions.util.TransactionTestUtil.assertNotInTransaction;
import static org.springframework.data.couchbase.util.Util.assertInAnnotationTransaction;
import java.util.ArrayList;
import java.util.List;
@@ -483,20 +484,4 @@ public class CouchbaseTransactionalTemplateCollectionDefaultScopeIntegrationTest
}
static void assertInAnnotationTransaction(boolean inTransaction) {
StackTraceElement[] stack = Thread.currentThread().getStackTrace();
for (StackTraceElement ste : stack) {
if (ste.getClassName().startsWith("org.springframework.transaction.interceptor")) {
if (inTransaction) {
return;
}
}
}
if (!inTransaction) {
return;
}
throw new RuntimeException(
"in transaction = " + (!inTransaction) + " but expected in annotation transaction = " + inTransaction);
}
}

View File

@@ -23,6 +23,7 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.fail;
import static org.springframework.data.couchbase.transactions.util.TransactionTestUtil.assertNotInTransaction;
import static org.springframework.data.couchbase.util.Util.assertInAnnotationTransaction;
import java.util.ArrayList;
import java.util.List;
@@ -450,10 +451,10 @@ public class CouchbaseTransactionalTemplateCollectionIntegrationTests extends Co
}
@Transactional(timeout = 2)
public Person replace(Person person, AtomicInteger tryCount) {
assertInAnnotationTransaction(true);
tryCount.incrementAndGet();
System.err.println("try: " + tryCount.get());
return personOperations.replaceById(Person.class).inScope(scopeName).inCollection(collectionName).one(person);
}
@@ -483,20 +484,4 @@ public class CouchbaseTransactionalTemplateCollectionIntegrationTests extends Co
}
static void assertInAnnotationTransaction(boolean inTransaction) {
StackTraceElement[] stack = Thread.currentThread().getStackTrace();
for (StackTraceElement ste : stack) {
if (ste.getClassName().startsWith("org.springframework.transaction.interceptor")) {
if (inTransaction) {
return;
}
}
}
if (!inTransaction) {
return;
}
throw new RuntimeException(
"in transaction = " + (!inTransaction) + " but expected in annotation transaction = " + inTransaction);
}
}

View File

@@ -22,7 +22,9 @@ import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.fail;
import static org.springframework.data.couchbase.transactions.util.TransactionTestUtil.assertInReactiveTransaction;
import static org.springframework.data.couchbase.transactions.util.TransactionTestUtil.assertNotInTransaction;
import static org.springframework.data.couchbase.util.Util.assertInAnnotationTransaction;
import java.util.ArrayList;
import java.util.List;
@@ -394,6 +396,7 @@ public class CouchbaseTransactionalTemplateIntegrationTests extends JavaIntegrat
@Transactional
public Person declarativeSavePersonWithThread(Person person, Thread thread) {
assertInAnnotationTransaction(true);
assertInReactiveTransaction();
long currentThreadId = Thread.currentThread().getId();
System.out.printf("Thread %d %s, started from %d %s%n", Thread.currentThread().getId(),
Thread.currentThread().getName(), thread.getId(), thread.getName());
@@ -483,20 +486,4 @@ public class CouchbaseTransactionalTemplateIntegrationTests extends JavaIntegrat
}
static void assertInAnnotationTransaction(boolean inTransaction) {
StackTraceElement[] stack = Thread.currentThread().getStackTrace();
for (StackTraceElement ste : stack) {
if (ste.getClassName().startsWith("org.springframework.transaction.interceptor")) {
if (inTransaction) {
return;
}
}
}
if (!inTransaction) {
return;
}
throw new RuntimeException(
"in transaction = " + (!inTransaction) + " but expected in annotation transaction = " + inTransaction);
}
}

View File

@@ -155,7 +155,8 @@ public class Util {
public static void assertInAnnotationTransaction(boolean inTransaction) {
StackTraceElement[] stack = Thread.currentThread().getStackTrace();
for (StackTraceElement ste : stack) {
if (ste.getClassName().startsWith("org.springframework.transaction.interceptor")
if (ste.getClassName()
.startsWith("org.springframework.data.couchbase.transaction.CouchbaseCallbackTransactionManager")
|| ste.getClassName().startsWith("org.springframework.data.couchbase.transaction.interceptor")) {
if (inTransaction) {
return;