Skip to content

Commit

Permalink
GEODE-7114: fix flaky tests by using GeodeAwaitility. (apache#3979)
Browse files Browse the repository at this point in the history
  • Loading branch information
pivotal-eshu authored Sep 3, 2019
1 parent dafa5f0 commit c79cd1d
Showing 1 changed file with 74 additions and 114 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static org.apache.geode.distributed.ConfigurationProperties.CACHE_XML_FILE;
import static org.apache.geode.test.dunit.Assert.fail;
import static org.apache.geode.test.util.ResourceUtils.createTempFileFromResource;
import static org.assertj.core.api.Assertions.assertThat;

import java.io.BufferedReader;
import java.io.BufferedWriter;
Expand All @@ -27,11 +28,13 @@
import java.io.OutputStreamWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLSyntaxErrorException;
import java.sql.Statement;
import java.util.Properties;

import javax.naming.Context;
import javax.sql.DataSource;
import javax.transaction.Status;
import javax.transaction.UserTransaction;

import org.junit.Test;
Expand All @@ -43,6 +46,7 @@
import org.apache.geode.internal.datasource.GemFireTransactionDataSource;
import org.apache.geode.internal.jta.CacheUtils;
import org.apache.geode.internal.jta.UserTransactionImpl;
import org.apache.geode.test.awaitility.GeodeAwaitility;
import org.apache.geode.test.dunit.Assert;
import org.apache.geode.test.dunit.AsyncInvocation;
import org.apache.geode.test.dunit.Host;
Expand All @@ -57,11 +61,10 @@

public class TransactionTimeOutDUnitTest extends JUnit4DistributedTestCase {

static DistributedSystem ds;
static Cache cache = null;
private static String tblName;
private static DistributedSystem ds;
private static Cache cache = null;

public static void init() throws Exception {
private void init() throws Exception {
Properties props = new Properties();
int pid = OSProcess.getId();
String path = File.createTempFile("dunit-cachejta_", ".xml").getAbsolutePath();
Expand All @@ -82,21 +85,7 @@ public static void init() throws Exception {
cache = CacheFactory.create(ds);
}

public static Cache getCache() {
return cache;
}

public static void startCache() {
try {
if (cache.isClosed()) {
cache = CacheFactory.create(ds);
}
} catch (Exception e) {
fail("Exception in cache creation due to ", e);
}
}

public static void closeCache() {
private void closeCache() {
try {
if (cache != null && !cache.isClosed()) {
cache.close();
Expand All @@ -109,24 +98,25 @@ public static void closeCache() {
}

@Override
public final void postSetUp() throws Exception {
public final void postSetUp() {
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
vm0.invoke(() -> TransactionTimeOutDUnitTest.init());
vm0.invoke(() -> init());
}

@Override
public final void preTearDown() throws Exception {
public final void preTearDown() {
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
vm0.invoke(() -> TransactionTimeOutDUnitTest.closeCache());
vm0.invoke(() -> closeCache());
}

public static void testTimeOut() throws Throwable {
@Test
public void testTimeOut() {
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
AsyncInvocation async1 = vm0.invokeAsync(() -> TransactionTimeOutDUnitTest.runTest1());
AsyncInvocation async2 = vm0.invokeAsync(() -> TransactionTimeOutDUnitTest.runTest2());
AsyncInvocation async1 = vm0.invokeAsync(() -> userTransactionCanBeTimedOut());
AsyncInvocation async2 = vm0.invokeAsync(() -> userTransactionCanBeTimedOut());

ThreadUtils.join(async1, 30 * 1000);
ThreadUtils.join(async2, 30 * 1000);
Expand All @@ -139,69 +129,63 @@ public static void testTimeOut() throws Throwable {
}

@Test
public void test1() {
public void startNewTransactionAfterExistingOneTimedOut() {
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
vm0.invoke(() -> TransactionTimeOutDUnitTest.runTest3());
vm0.invoke(() -> canStartANewUserTransactionAfterExistingOneTimedOut());
}

@Test
public void test2() {
public void testUserTransactionIsTimedOut() {
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
vm0.invoke(() -> TransactionTimeOutDUnitTest.runTest4());
vm0.invoke(() -> verifyUserTransactionIsTimedOut());
}

@Test
public void test3() {
public void testTransactionTimeoutCanBeSetMultipleTimes() {
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
vm0.invoke(() -> TransactionTimeOutDUnitTest.runTest5());
vm0.invoke(() -> transactionTimeoutCanBeSetMultipleTimes());
}

@Test
public void test4() {
public void testTransactionCanBeCommittedBeforeTimedOut() {
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
vm0.invoke(() -> TransactionTimeOutDUnitTest.runTest6());
vm0.invoke(() -> transactionCanBeCommittedBeforeTimedOut());
}

@Test
public void test5() {
public void testTransactionUpdatingExternalDataSourceIsCommittedBeforeTimedOut() {
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
vm0.invoke(() -> TransactionTimeOutDUnitTest.runTest7());
vm0.invoke(() -> transactionUpdatingExternalDataSourceIsCommittedBeforeTimedOut());
}

@Test
public void test6() {
public void testTimedOutTransactionIsNotCommitted() {
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
vm0.invoke(() -> TransactionTimeOutDUnitTest.runTest8());
vm0.invoke(() -> timedOutTransactionIsNotCommitted());
}

@Test
public void test7() {
public void testTransactionUpdatingExternalDataSourceIsTimedOut() {
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
vm0.invoke(() -> TransactionTimeOutDUnitTest.runTest9());
vm0.invoke(() -> transactionUpdatingExternalDataSourceIsTimedOut());
}

@Test
public void test8() {
Host host = Host.getHost(0);
VM vm0 = host.getVM(0);
vm0.invoke(() -> TransactionTimeOutDUnitTest.runTest10());
}

public static void runTest1() throws Exception {
private void userTransactionCanBeTimedOut() {
boolean exceptionOccurred = false;
try {
Context ctx = cache.getJNDIContext();
UserTransaction utx = (UserTransaction) ctx.lookup("java:/UserTransaction");
utx.begin();
assertThat(utx.getStatus() == Status.STATUS_ACTIVE);
utx.setTransactionTimeout(2);
Thread.sleep(6000);
waitUntilTransactionTimeout(utx);
try {
utx.commit();
} catch (Exception e) {
Expand All @@ -214,49 +198,34 @@ public static void runTest1() throws Exception {
}
}

public static void runTest2() throws Exception {
boolean exceptionOccurred = false;
try {
Context ctx = cache.getJNDIContext();
UserTransaction utx = (UserTransaction) ctx.lookup("java:/UserTransaction");
utx.begin();
utx.setTransactionTimeout(2);
Thread.sleep(6000);
try {
utx.commit();
} catch (Exception e) {
exceptionOccurred = true;
}
if (!exceptionOccurred)
fail("Exception did not occur although was supposed to occur");
} catch (Exception e) {
fail("failed in naming lookup: ", e);
}
private static void waitUntilTransactionTimeout(UserTransaction utx) {
GeodeAwaitility.await().pollInSameThread()
.until(() -> utx.getStatus() == Status.STATUS_NO_TRANSACTION);
}

public static void runTest3() {
private void canStartANewUserTransactionAfterExistingOneTimedOut() {
try {
Context ctx = cache.getJNDIContext();
UserTransaction utx = (UserTransaction) ctx.lookup("java:/UserTransaction");
utx.begin();
assertThat(utx.getStatus() == Status.STATUS_ACTIVE);
utx.setTransactionTimeout(2);
Thread.sleep(6000);
waitUntilTransactionTimeout(utx);
utx.begin();
utx.commit();
} catch (Exception e) {
fail("Exception in TestSetTransactionTimeOut due to ", e);
}
}

public static void runTest4() {
private void verifyUserTransactionIsTimedOut() {
boolean exceptionOccurred = true;
try {
Context ctx = cache.getJNDIContext();
UserTransaction utx = (UserTransaction) ctx.lookup("java:/UserTransaction");
utx = new UserTransactionImpl();
utx.setTransactionTimeout(2);
UserTransaction utx = new UserTransactionImpl();
utx.begin();
Thread.sleep(4000);
utx.setTransactionTimeout(2);
assertThat(utx.getStatus() == Status.STATUS_ACTIVE);
waitUntilTransactionTimeout(utx);
try {
utx.commit();
} catch (Exception e) {
Expand All @@ -270,18 +239,17 @@ public static void runTest4() {
}
}

public static void runTest5() {
private void transactionTimeoutCanBeSetMultipleTimes() {
boolean exceptionOccurred = true;
try {
Context ctx = cache.getJNDIContext();
UserTransaction utx = (UserTransaction) ctx.lookup("java:/UserTransaction");
utx = new UserTransactionImpl();
UserTransaction utx = new UserTransactionImpl();
utx.setTransactionTimeout(10);
utx.begin();
assertThat(utx.getStatus() == Status.STATUS_ACTIVE);
utx.setTransactionTimeout(8);
utx.setTransactionTimeout(6);
utx.setTransactionTimeout(2);
Thread.sleep(6000);
waitUntilTransactionTimeout(utx);
try {
utx.commit();
} catch (Exception e) {
Expand All @@ -295,34 +263,13 @@ public static void runTest5() {
}
}

public static void runTest6() {
boolean exceptionOccurred = true;
private void transactionCanBeCommittedBeforeTimedOut() {
try {
Context ctx = cache.getJNDIContext();
UserTransaction utx = (UserTransaction) ctx.lookup("java:/UserTransaction");
utx.setTransactionTimeout(4);
utx.begin();
Thread.sleep(6000);
try {
utx.commit();
} catch (Exception e) {
exceptionOccurred = false;
}
if (exceptionOccurred) {
fail("TimeOut did not rollback the transaction");
}
} catch (Exception e) {
fail("Exception in testExceptionOnCommitAfterTimeOut() due to ", e);
}
}

public static void runTest7() {
// boolean exceptionOccurred = true;
try {
Context ctx = cache.getJNDIContext();
UserTransaction utx = (UserTransaction) ctx.lookup("java:/UserTransaction");
utx.begin();
utx.setTransactionTimeout(6);
assertThat(utx.getStatus() == Status.STATUS_ACTIVE);
utx.setTransactionTimeout(30);
Thread.sleep(2000);
try {
utx.commit();
Expand All @@ -334,7 +281,7 @@ public static void runTest7() {
}
}

public static void runTest8() {
private void transactionUpdatingExternalDataSourceIsCommittedBeforeTimedOut() {
try {
Context ctx = cache.getJNDIContext();
DataSource ds2 = (DataSource) ctx.lookup("java:/SimpleDataSource");
Expand All @@ -348,9 +295,8 @@ public static void runTest8() {
Statement sm = conn.createStatement();
sm.execute(sql);
utx.setTransactionTimeout(30);
Thread.sleep(5000);
Thread.sleep(2000);
utx.setTransactionTimeout(20);
utx.setTransactionTimeout(10);
sql = "insert into newTable1 values (1)";
sm.execute(sql);
utx.commit();
Expand All @@ -367,7 +313,7 @@ public static void runTest8() {
}
}

public static void runTest9() {
private void timedOutTransactionIsNotCommitted() {
try {
boolean exceptionOccurred = false;
Context ctx = cache.getJNDIContext();
Expand All @@ -387,13 +333,12 @@ public static void runTest9() {
sql = "select * from newTable2 where id = 1";
ResultSet rs = sm.executeQuery(sql);
if (!rs.next())
fail("Transaction not committed");
sql = "drop table newTable2";
sm.execute(sql);
fail("Database not updated");
sm.close();
conn.close();
assertThat(utx.getStatus() == Status.STATUS_ACTIVE);
utx.setTransactionTimeout(1);
Thread.sleep(3000);
waitUntilTransactionTimeout(utx);
try {
utx.commit();
} catch (Exception e) {
Expand All @@ -402,12 +347,26 @@ public static void runTest9() {
if (!exceptionOccurred) {
fail("exception did not occur on commit although transaction timed out");
}
conn = ds.getConnection();
sm = conn.createStatement();
sql = "select * from newTable2 where id = 1";
boolean gotExpectedTableNotExistException = false;
try {
sm.executeQuery(sql);
} catch (SQLSyntaxErrorException expected) {
gotExpectedTableNotExistException = true;
}
if (!gotExpectedTableNotExistException) {
fail("Transaction should be timed out and not committed");
}
sm.close();
conn.close();
} catch (Exception e) {
fail("Exception occurred in test Commit due to ", e);
}
}

public static void runTest10() {
private void transactionUpdatingExternalDataSourceIsTimedOut() {
try {
boolean exceptionOccurred = false;
Context ctx = cache.getJNDIContext();
Expand All @@ -432,8 +391,9 @@ public static void runTest10() {
sm.execute(sql);
sm.close();
conn.close();
assertThat(utx.getStatus() == Status.STATUS_ACTIVE);
utx.setTransactionTimeout(1);
Thread.sleep(3000);
waitUntilTransactionTimeout(utx);
try {
utx.rollback();
} catch (Exception e) {
Expand Down

0 comments on commit c79cd1d

Please sign in to comment.