The BaseTestCase ensures, that the threadLocal is cleared after each test. (#1712)

* The BaseTestCase ensures, that the threadLocal is cleared after each test.

* Change JdbcTransaction.toString to distingush between active and inactive transaction
This commit is contained in:
Roland Praml authored and Rob Bygrave committed 2019-05-18 11:28:46 +12:00
1 parent 2678f85506
commit 895b5ee4ff
3 files changed
+36 -1

No files matched your search

@@ -75,6 +75,14 @@ public final class DefaultTransactionThreadLocal {
return local.get().get(serverName); return local.get().get(serverName);
} }
/**
* Return all transactions of the current thread (active/inactive).
* This is intended for test/debugging purposes only!
*/
public static Map<String, SpiTransaction> currentTransactions() {
return local.get();
}
private static SpiTransaction obtain(String serverName) { private static SpiTransaction obtain(String serverName) {
SpiTransaction transaction = local.get().remove(serverName); SpiTransaction transaction = local.get().remove(serverName);
if (transaction == null) { if (transaction == null) {
@@ -314,7 +314,11 @@ public class JdbcTransaction implements SpiTransaction, TxnProfileEventCodes {
@Override @Override
public String toString() { public String toString() {
return logPrefix; if (active) {
return logPrefix;
} else {
return logPrefix + "(inactive)";
}
} }
@Override @Override
+23
View File
@@ -9,12 +9,18 @@ import io.ebean.meta.ServerMetrics;
import io.ebean.util.StringHelper; import io.ebean.util.StringHelper;
import io.ebeaninternal.api.SpiEbeanServer; import io.ebeaninternal.api.SpiEbeanServer;
import io.ebeaninternal.api.SpiQuery; import io.ebeaninternal.api.SpiQuery;
import io.ebeaninternal.api.SpiTransaction;
import io.ebeaninternal.server.core.HelpCreateQueryRequest; import io.ebeaninternal.server.core.HelpCreateQueryRequest;
import io.ebeaninternal.server.core.OrmQueryRequest; import io.ebeaninternal.server.core.OrmQueryRequest;
import io.ebeaninternal.server.deploy.BeanDescriptor; import io.ebeaninternal.server.deploy.BeanDescriptor;
import io.ebeaninternal.server.expression.platform.DbExpressionHandler; import io.ebeaninternal.server.expression.platform.DbExpressionHandler;
import io.ebeaninternal.server.expression.platform.DbExpressionHandlerFactory; import io.ebeaninternal.server.expression.platform.DbExpressionHandlerFactory;
import io.ebeaninternal.server.transaction.DefaultTransactionThreadLocal;
import org.avaje.agentloader.AgentLoader; import org.avaje.agentloader.AgentLoader;
import org.junit.After;
import org.junit.Rule;
import org.junit.rules.TestName;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -22,15 +28,32 @@ import org.tests.model.basic.Country;
import java.sql.Types; import java.sql.Types;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;
@RunWith(ConditionalTestRunner.class) @RunWith(ConditionalTestRunner.class)
public abstract class BaseTestCase { public abstract class BaseTestCase {
protected static Logger logger = LoggerFactory.getLogger(BaseTestCase.class); protected static Logger logger = LoggerFactory.getLogger(BaseTestCase.class);
@Rule public TestName name = new TestName();
@After
public void checkForLeak() {
Map<String, SpiTransaction> trans = DefaultTransactionThreadLocal.currentTransactions();
if (trans != null) {
if (!trans.isEmpty()) {
String msg = getClass().getSimpleName() + "." + name.getMethodName() + " did not clear threadScope:" + trans;
trans.clear(); // clear for next test
fail(msg);
}
}
}
/** /**
* this is the clock delta that may occur between testing machine and db server. * this is the clock delta that may occur between testing machine and db server.
* If the clock delta of DB server is in future, an "asOf" query may not find the * If the clock delta of DB server is in future, an "asOf" query may not find the