Skip to content

Commit

Permalink
HBASE-22615 Make TestChoreService more robust to timing
Browse files Browse the repository at this point in the history
* phrase fudge factor "deltas" in terms of the original period
* increase the delta allowed for chore timing from 5% to 20%
* improve some assertions

Closes #328

Signed-off-by: Reid Chan <[email protected]>
Signed-off-by: Sakthi <[email protected]>
  • Loading branch information
busbey committed Jun 22, 2019
1 parent 6d08ffc commit 9aee88e
Showing 1 changed file with 12 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -354,17 +354,17 @@ public void testChoreServiceConstruction() throws InterruptedException {
public void testFrequencyOfChores() throws InterruptedException {
final int period = 100;
// Small delta that acts as time buffer (allowing chores to complete if running slowly)
final int delta = 5;
final int delta = period/5;
ChoreService service = new ChoreService("testFrequencyOfChores");
CountingChore chore = new CountingChore("countingChore", period);
try {
service.scheduleChore(chore);

Thread.sleep(10 * period + delta);
assertTrue(chore.getCountOfChoreCalls() == 11);
assertEquals("10 periods have elapsed.", 11, chore.getCountOfChoreCalls());

Thread.sleep(10 * period);
assertTrue(chore.getCountOfChoreCalls() == 21);
Thread.sleep(10 * period + delta);
assertEquals("20 periods have elapsed.", 21, chore.getCountOfChoreCalls());
} finally {
shutdownService(service);
}
Expand All @@ -380,14 +380,14 @@ public void shutdownService(ChoreService service) throws InterruptedException {
@Test
public void testForceTrigger() throws InterruptedException {
final int period = 100;
final int delta = 10;
final int delta = period/10;
ChoreService service = new ChoreService("testForceTrigger");
final CountingChore chore = new CountingChore("countingChore", period);
try {
service.scheduleChore(chore);
Thread.sleep(10 * period + delta);

assertTrue(chore.getCountOfChoreCalls() == 11);
assertEquals("10 periods have elapsed.", 11, chore.getCountOfChoreCalls());

// Force five runs of the chore to occur, sleeping between triggers to ensure the
// chore has time to run
Expand All @@ -402,12 +402,14 @@ public void testForceTrigger() throws InterruptedException {
chore.triggerNow();
Thread.sleep(delta);

assertTrue("" + chore.getCountOfChoreCalls(), chore.getCountOfChoreCalls() == 16);
assertEquals("Trigger was called 5 times after 10 periods.", 16,
chore.getCountOfChoreCalls());

Thread.sleep(10 * period + delta);

// Be loosey-goosey. It used to be '26' but it was a big flakey relying on timing.
assertTrue("" + chore.getCountOfChoreCalls(), chore.getCountOfChoreCalls() > 16);
assertTrue("Expected at least 16 invocations, instead got " + chore.getCountOfChoreCalls(),
chore.getCountOfChoreCalls() > 16);
} finally {
shutdownService(service);
}
Expand All @@ -419,7 +421,7 @@ public void testCorePoolIncrease() throws InterruptedException {
ChoreService service = new ChoreService("testCorePoolIncrease", initialCorePoolSize, false);

try {
assertEquals("Should have a core pool of size: " + initialCorePoolSize, initialCorePoolSize,
assertEquals("Setting core pool size gave unexpected results.", initialCorePoolSize,
service.getCorePoolSize());

final int slowChorePeriod = 100;
Expand Down Expand Up @@ -703,7 +705,7 @@ public void testStopperForScheduledChores() throws InterruptedException {
Stoppable stopperForGroup1 = new SampleStopper();
Stoppable stopperForGroup2 = new SampleStopper();
final int period = 100;
final int delta = 10;
final int delta = period/10;

try {
ScheduledChore chore1_group1 = new DoNothingChore("c1g1", stopperForGroup1, period);
Expand Down

0 comments on commit 9aee88e

Please sign in to comment.