Skip to content

Fix test for being inside a transaction. #1969

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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;
Expand Down Expand Up @@ -483,20 +484,4 @@ public void fetchAndRemove(String id, AtomicInteger tryCount) {

}

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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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;
Expand Down Expand Up @@ -450,10 +451,10 @@ public Person declarativeFindReplaceTwicePersonCallback(Person person, AtomicInt
}

@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);
}

Expand Down Expand Up @@ -483,20 +484,4 @@ public void fetchAndRemove(String id, AtomicInteger tryCount) {

}

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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
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;
Expand Down Expand Up @@ -394,6 +396,7 @@ public Person declarativeSavePerson(Person person) {
@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());
Expand Down Expand Up @@ -483,20 +486,4 @@ public void fetchAndRemove(String id, AtomicInteger tryCount) {

}

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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ public static <T> Pair<List<T>, List<T>> comprisesNot(Iterable<T> source, T[] al
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;
Expand Down
Loading