Skip to content

Commit 044ffa1

Browse files
committed
HBASE-22615 Make TestChoreService more robust to timing
* 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 <reidchan@apache.org> Signed-off-by: Sakthi <sakthivel.azhaku@gmail.com> (cherry picked from commit 9aee88e)
1 parent 36f4ab0 commit 044ffa1

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

hbase-common/src/test/java/org/apache/hadoop/hbase/TestChoreService.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -345,17 +345,17 @@ public void testChoreServiceConstruction() throws InterruptedException {
345345
public void testFrequencyOfChores() throws InterruptedException {
346346
final int period = 100;
347347
// Small delta that acts as time buffer (allowing chores to complete if running slowly)
348-
final int delta = 5;
348+
final int delta = period/5;
349349
ChoreService service = new ChoreService("testFrequencyOfChores");
350350
CountingChore chore = new CountingChore("countingChore", period);
351351
try {
352352
service.scheduleChore(chore);
353353

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

357-
Thread.sleep(10 * period);
358-
assertTrue(chore.getCountOfChoreCalls() == 21);
357+
Thread.sleep(10 * period + delta);
358+
assertEquals("20 periods have elapsed.", 21, chore.getCountOfChoreCalls());
359359
} finally {
360360
shutdownService(service);
361361
}
@@ -371,14 +371,14 @@ public void shutdownService(ChoreService service) throws InterruptedException {
371371
@Test (timeout=20000)
372372
public void testForceTrigger() throws InterruptedException {
373373
final int period = 100;
374-
final int delta = 10;
374+
final int delta = period/10;
375375
ChoreService service = new ChoreService("testForceTrigger");
376376
final CountingChore chore = new CountingChore("countingChore", period);
377377
try {
378378
service.scheduleChore(chore);
379379
Thread.sleep(10 * period + delta);
380380

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

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

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

398399
Thread.sleep(10 * period + delta);
399400

400401
// Be loosey-goosey. It used to be '26' but it was a big flakey relying on timing.
401-
assertTrue("" + chore.getCountOfChoreCalls(), chore.getCountOfChoreCalls() > 16);
402+
assertTrue("Expected at least 16 invocations, instead got " + chore.getCountOfChoreCalls(),
403+
chore.getCountOfChoreCalls() > 16);
402404
} finally {
403405
shutdownService(service);
404406
}
@@ -410,7 +412,7 @@ public void testCorePoolIncrease() throws InterruptedException {
410412
ChoreService service = new ChoreService("testCorePoolIncrease", initialCorePoolSize, false);
411413

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

416418
final int slowChorePeriod = 100;
@@ -694,7 +696,7 @@ public void testStopperForScheduledChores() throws InterruptedException {
694696
Stoppable stopperForGroup1 = new SampleStopper();
695697
Stoppable stopperForGroup2 = new SampleStopper();
696698
final int period = 100;
697-
final int delta = 10;
699+
final int delta = period/10;
698700

699701
try {
700702
ScheduledChore chore1_group1 = new DoNothingChore("c1g1", stopperForGroup1, period);

0 commit comments

Comments
 (0)