Skip to content

Commit

Permalink
GEODE-1991: Removing sleeps from HARegionQueueJUnitTest
Browse files Browse the repository at this point in the history
Getting rid of a bunch of sleeps in HARegionQueueJUnitTest to fix a
bunch of tests with race conditions. Tests of expiration were sleeping
for short amounts of time and then asserting that expiration happened or
didn't. Changing these sleeps to use Awailitily.
  • Loading branch information
upthewaterspout committed Oct 13, 2016
1 parent 08adacd commit c2ddc96
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 262 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
import static org.junit.Assert.fail;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import com.jayway.awaitility.Awaitility;

import org.junit.Ignore;
import org.junit.Test;
Expand Down Expand Up @@ -82,39 +85,30 @@ protected HARegionQueue createHARegionQueue(String name,
*
*/
@Test
public void testBlockingPutAndTake()
public void testBlockingPutAndTake() throws InterruptedException, IOException, ClassNotFoundException
{
try {
HARegionQueueAttributes hrqa = new HARegionQueueAttributes();
hrqa.setBlockingQueueCapacity(1);
final HARegionQueue hrq = this.createHARegionQueue("testBlockingPutAndTake",
hrqa);
hrq.setPrimary(true);//fix for 40314 - capacity constraint is checked for primary only.
EventID id1 = new EventID(new byte[] { 1 }, 1, 1);
hrq.put(new ConflatableObject("key1", "val1", id1, false, "testing"));
Thread t1 = new Thread(new Runnable() {
public void run() {
try{
EventID id2 = new EventID(new byte[] { 1 }, 1, 2);
hrq.put(new ConflatableObject("key1", "val2", id2, false, "testing"));
}catch(Exception e) {
encounteredException=true;
}
HARegionQueueAttributes hrqa = new HARegionQueueAttributes();
hrqa.setBlockingQueueCapacity(1);
final HARegionQueue hrq = this.createHARegionQueue("testBlockingPutAndTake",
hrqa);
hrq.setPrimary(true);//fix for 40314 - capacity constraint is checked for primary only.
EventID id1 = new EventID(new byte[] { 1 }, 1, 1);
hrq.put(new ConflatableObject("key1", "val1", id1, false, "testing"));
Thread t1 = new Thread(new Runnable() {
public void run() {
try{
EventID id2 = new EventID(new byte[] { 1 }, 1, 2);
hrq.put(new ConflatableObject("key1", "val2", id2, false, "testing"));
}catch(Exception e) {
encounteredException=true;
}
});
t1.start();
Thread.sleep(4000);
assertTrue(t1.isAlive());
Conflatable conf = (Conflatable)hrq.take();
assertNotNull(conf);
Thread.sleep(2000);
assertTrue(!t1.isAlive());

}
catch (Exception e) {
e.printStackTrace();
fail("Test failed because of exception " + e);
}
}
});
t1.start();
Awaitility.await().atMost(1, TimeUnit.MINUTES).until(() -> t1.isAlive());
Conflatable conf = (Conflatable)hrq.take();
assertNotNull(conf);
Awaitility.await().atMost(1, TimeUnit.MINUTES).until(() -> !t1.isAlive());
}

/**
Expand All @@ -123,45 +117,37 @@ public void run() {
*
*/
@Test
public void testBlockingPutAndPeekRemove()
public void testBlockingPutAndPeekRemove() throws InterruptedException, IOException, ClassNotFoundException
{
try {
HARegionQueueAttributes hrqa = new HARegionQueueAttributes();
hrqa.setBlockingQueueCapacity(1);
final HARegionQueue hrq = this.createHARegionQueue(
"testBlockingPutAndPeekRemove", hrqa);
hrq.setPrimary(true);//fix for 40314 - capacity constraint is checked for primary only.
EventID id1 = new EventID(new byte[] { 1 }, 1, 1);
hrq.put(new ConflatableObject("key1", "val1", id1, false, "testing"));
Thread t1 = new Thread(new Runnable() {
public void run()
{
try {
EventID id2 = new EventID(new byte[] { 1 }, 1, 2);
hrq
.put(new ConflatableObject("key1", "val2", id2, false,
"testing"));
}
catch (Exception e) {
encounteredException = true;
}
HARegionQueueAttributes hrqa = new HARegionQueueAttributes();
hrqa.setBlockingQueueCapacity(1);
final HARegionQueue hrq = this.createHARegionQueue(
"testBlockingPutAndPeekRemove", hrqa);
hrq.setPrimary(true);//fix for 40314 - capacity constraint is checked for primary only.
EventID id1 = new EventID(new byte[] { 1 }, 1, 1);
hrq.put(new ConflatableObject("key1", "val1", id1, false, "testing"));
Thread t1 = new Thread(new Runnable() {
public void run()
{
try {
EventID id2 = new EventID(new byte[] { 1 }, 1, 2);
hrq
.put(new ConflatableObject("key1", "val2", id2, false,
"testing"));
}
});
t1.start();
Thread.sleep(4000);
assertTrue("put-thread expected to blocked, but was not ", t1.isAlive());
Conflatable conf = (Conflatable)hrq.peek();
assertNotNull(conf);
hrq.remove();
Thread.sleep(2000);
assertFalse("Put-thread blocked unexpectedly", t1.isAlive());
assertFalse("Exception occured in put-thread", encounteredException);
catch (Exception e) {
encounteredException = true;
}
}
});
t1.start();
Awaitility.await().atMost(1, TimeUnit.MINUTES).until(() -> t1.isAlive());
Conflatable conf = (Conflatable)hrq.peek();
assertNotNull(conf);
hrq.remove();
Awaitility.await().atMost(1, TimeUnit.MINUTES).until(() -> !t1.isAlive());
assertFalse("Exception occured in put-thread", encounteredException);

}
catch (Exception e) {
e.printStackTrace();
fail("Test failed because of exception " + e);
}
}

/**
Expand All @@ -173,42 +159,36 @@ public void run()
//expiry is not applicable on primary so marking this test as invalid.
@Ignore
@Test
public void testBlockingPutAndExpiry()
public void testBlockingPutAndExpiry() throws InterruptedException, IOException, ClassNotFoundException
{
try {
HARegionQueueAttributes hrqa = new HARegionQueueAttributes();
hrqa.setBlockingQueueCapacity(1);
hrqa.setExpiryTime(4);
final HARegionQueue hrq = this.createHARegionQueue(
"testBlockingPutAndExpiry", hrqa);

EventID id1 = new EventID(new byte[] { 1 }, 1, 1);
hrq.put(new ConflatableObject("key1", "val1", id1, false, "testing"));
Thread t1 = new Thread(new Runnable() {
public void run()
{
try {
EventID id2 = new EventID(new byte[] { 1 }, 1, 2);
hrq
.put(new ConflatableObject("key1", "val2", id2, false,
"testing"));
}
catch (Exception e) {
encounteredException = true;
}
HARegionQueueAttributes hrqa = new HARegionQueueAttributes();
hrqa.setBlockingQueueCapacity(1);
hrqa.setExpiryTime(1);
final HARegionQueue hrq = this.createHARegionQueue(
"testBlockingPutAndExpiry", hrqa);

EventID id1 = new EventID(new byte[] { 1 }, 1, 1);
long start = System.currentTimeMillis();
hrq.put(new ConflatableObject("key1", "val1", id1, false, "testing"));
Thread t1 = new Thread(new Runnable() {
public void run()
{
try {
EventID id2 = new EventID(new byte[] { 1 }, 1, 2);
hrq
.put(new ConflatableObject("key1", "val2", id2, false,
"testing"));
}
});
t1.start();
Thread.sleep(2000);
assertTrue("put-thread expected to blocked, but was not ", t1.isAlive());
Thread.sleep(2500);
catch (Exception e) {
encounteredException = true;
}
}
});
t1.start();
Awaitility.await().atMost(1, TimeUnit.MINUTES).until(() -> t1.isAlive());
waitAtLeast(1000, start, () -> {
assertFalse("Put-thread blocked unexpectedly", t1.isAlive());
assertFalse("Exception occured in put-thread", encounteredException);

}
catch (Exception e) {
e.printStackTrace();
fail("Test failed because of exception " + e);
}
});
assertFalse("Exception occured in put-thread", encounteredException);
}
}
Loading

0 comments on commit c2ddc96

Please sign in to comment.