HBASE-13575 TestChoreService has to make sure that the opened ChoreService is closed for each unit test (Stephen Yuan Jiang)

This commit is contained in:
stack 2015-04-27 14:23:57 -07:00
parent b96952beef
commit 2759fd0cdb
1 changed files with 357 additions and 319 deletions

View File

@ -241,6 +241,8 @@ public class TestChoreService {
final int period = 100; final int period = 100;
final int failureThreshold = 5; final int failureThreshold = 5;
try {
ScheduledChore chore = new FailInitialChore("chore", period, failureThreshold); ScheduledChore chore = new FailInitialChore("chore", period, failureThreshold);
service.scheduleChore(chore); service.scheduleChore(chore);
@ -257,23 +259,27 @@ public class TestChoreService {
} }
assertFalse(brokeOutOfLoop); assertFalse(brokeOutOfLoop);
} finally {
shutdownService(service); shutdownService(service);
} }
}
@Test (timeout=20000) @Test (timeout=20000)
public void testCancelChore() throws InterruptedException { public void testCancelChore() throws InterruptedException {
final int period = 100; final int period = 100;
ScheduledChore chore1 = new DoNothingChore("chore1", period); ScheduledChore chore1 = new DoNothingChore("chore1", period);
ChoreService service = ChoreService.getInstance("testCancelChore"); ChoreService service = ChoreService.getInstance("testCancelChore");
try {
service.scheduleChore(chore1); service.scheduleChore(chore1);
assertTrue(chore1.isScheduled()); assertTrue(chore1.isScheduled());
chore1.cancel(true); chore1.cancel(true);
assertFalse(chore1.isScheduled()); assertFalse(chore1.isScheduled());
assertTrue(service.getNumberOfScheduledChores() == 0); assertTrue(service.getNumberOfScheduledChores() == 0);
} finally {
shutdownService(service); shutdownService(service);
} }
}
@Test (timeout=20000) @Test (timeout=20000)
public void testScheduledChoreConstruction() { public void testScheduledChoreConstruction() {
@ -314,18 +320,25 @@ public class TestChoreService {
final int defaultCorePoolSize = ChoreService.MIN_CORE_POOL_SIZE; final int defaultCorePoolSize = ChoreService.MIN_CORE_POOL_SIZE;
ChoreService customInit = new ChoreService("testChoreServiceConstruction_custom", corePoolSize); ChoreService customInit = new ChoreService("testChoreServiceConstruction_custom", corePoolSize);
try {
assertEquals(corePoolSize, customInit.getCorePoolSize()); assertEquals(corePoolSize, customInit.getCorePoolSize());
} finally {
shutdownService(customInit);
}
ChoreService defaultInit = new ChoreService("testChoreServiceConstruction_default"); ChoreService defaultInit = new ChoreService("testChoreServiceConstruction_default");
try {
assertEquals(defaultCorePoolSize, defaultInit.getCorePoolSize()); assertEquals(defaultCorePoolSize, defaultInit.getCorePoolSize());
} finally {
shutdownService(defaultInit);
}
ChoreService invalidInit = new ChoreService("testChoreServiceConstruction_invalid", -10); ChoreService invalidInit = new ChoreService("testChoreServiceConstruction_invalid", -10);
try {
assertEquals(defaultCorePoolSize, invalidInit.getCorePoolSize()); assertEquals(defaultCorePoolSize, invalidInit.getCorePoolSize());
} finally {
shutdownService(customInit);
shutdownService(defaultInit);
shutdownService(invalidInit); shutdownService(invalidInit);
}
} }
@Test (timeout=20000) @Test (timeout=20000)
@ -335,6 +348,7 @@ public class TestChoreService {
final int delta = 5; final int delta = 5;
ChoreService service = ChoreService.getInstance("testFrequencyOfChores"); ChoreService service = ChoreService.getInstance("testFrequencyOfChores");
CountingChore chore = new CountingChore("countingChore", period); CountingChore chore = new CountingChore("countingChore", period);
try {
service.scheduleChore(chore); service.scheduleChore(chore);
Thread.sleep(10 * period + delta); Thread.sleep(10 * period + delta);
@ -342,9 +356,10 @@ public class TestChoreService {
Thread.sleep(10 * period); Thread.sleep(10 * period);
assertTrue(chore.getCountOfChoreCalls() == 21); assertTrue(chore.getCountOfChoreCalls() == 21);
} finally {
shutdownService(service); shutdownService(service);
} }
}
public void shutdownService(ChoreService service) throws InterruptedException { public void shutdownService(ChoreService service) throws InterruptedException {
service.shutdown(); service.shutdown();
@ -359,6 +374,7 @@ public class TestChoreService {
final int delta = 5; final int delta = 5;
ChoreService service = ChoreService.getInstance("testForceTrigger"); ChoreService service = ChoreService.getInstance("testForceTrigger");
CountingChore chore = new CountingChore("countingChore", period); CountingChore chore = new CountingChore("countingChore", period);
try {
service.scheduleChore(chore); service.scheduleChore(chore);
Thread.sleep(10 * period + delta); Thread.sleep(10 * period + delta);
@ -382,14 +398,17 @@ public class TestChoreService {
Thread.sleep(10 * period + delta); Thread.sleep(10 * period + delta);
assertTrue(chore.getCountOfChoreCalls() == 26); assertTrue(chore.getCountOfChoreCalls() == 26);
} finally {
shutdownService(service); shutdownService(service);
} }
}
@Test (timeout=20000) @Test (timeout=20000)
public void testCorePoolIncrease() throws InterruptedException { public void testCorePoolIncrease() throws InterruptedException {
final int initialCorePoolSize = 3; final int initialCorePoolSize = 3;
ChoreService service = new ChoreService("testCorePoolIncrease", initialCorePoolSize); ChoreService service = new ChoreService("testCorePoolIncrease", initialCorePoolSize);
try {
assertEquals("Should have a core pool of size: " + initialCorePoolSize, initialCorePoolSize, assertEquals("Should have a core pool of size: " + initialCorePoolSize, initialCorePoolSize,
service.getCorePoolSize()); service.getCorePoolSize());
@ -419,16 +438,17 @@ public class TestChoreService {
Thread.sleep(slowChorePeriod * 10); Thread.sleep(slowChorePeriod * 10);
assertEquals("Chores are missing their start time. Should expand core pool size", 5, assertEquals("Chores are missing their start time. Should expand core pool size", 5,
service.getCorePoolSize()); service.getCorePoolSize());
} finally {
shutdownService(service); shutdownService(service);
} }
}
@Test(timeout = 30000) @Test(timeout = 30000)
public void testCorePoolDecrease() throws InterruptedException { public void testCorePoolDecrease() throws InterruptedException {
final int initialCorePoolSize = 3; final int initialCorePoolSize = 3;
ChoreService service = new ChoreService("testCorePoolDecrease", initialCorePoolSize); ChoreService service = new ChoreService("testCorePoolDecrease", initialCorePoolSize);
final int chorePeriod = 100; final int chorePeriod = 100;
try {
// Slow chores always miss their start time and thus the core pool size should be at least as // Slow chores always miss their start time and thus the core pool size should be at least as
// large as the number of running slow chores // large as the number of running slow chores
SlowChore slowChore1 = new SlowChore("slowChore1", chorePeriod); SlowChore slowChore1 = new SlowChore("slowChore1", chorePeriod);
@ -487,9 +507,10 @@ public class TestChoreService {
assertEquals(Math.max(ChoreService.MIN_CORE_POOL_SIZE, service.getNumberOfScheduledChores()), assertEquals(Math.max(ChoreService.MIN_CORE_POOL_SIZE, service.getNumberOfScheduledChores()),
service.getCorePoolSize()); service.getCorePoolSize());
assertEquals(service.getNumberOfChoresMissingStartTime(), 0); assertEquals(service.getNumberOfChoresMissingStartTime(), 0);
} finally {
shutdownService(service); shutdownService(service);
} }
}
@Test (timeout=20000) @Test (timeout=20000)
public void testNumberOfRunningChores() throws InterruptedException { public void testNumberOfRunningChores() throws InterruptedException {
@ -498,6 +519,7 @@ public class TestChoreService {
final int period = 100; final int period = 100;
final int sleepTime = 5; final int sleepTime = 5;
try {
DoNothingChore dn1 = new DoNothingChore("dn1", period); DoNothingChore dn1 = new DoNothingChore("dn1", period);
DoNothingChore dn2 = new DoNothingChore("dn2", period); DoNothingChore dn2 = new DoNothingChore("dn2", period);
DoNothingChore dn3 = new DoNothingChore("dn3", period); DoNothingChore dn3 = new DoNothingChore("dn3", period);
@ -526,9 +548,10 @@ public class TestChoreService {
dn5.cancel(); dn5.cancel();
Thread.sleep(sleepTime); Thread.sleep(sleepTime);
assertEquals("Scheduled chore mismatch", 0, service.getNumberOfScheduledChores()); assertEquals("Scheduled chore mismatch", 0, service.getNumberOfScheduledChores());
} finally {
shutdownService(service); shutdownService(service);
} }
}
@Test (timeout=20000) @Test (timeout=20000)
public void testNumberOfChoresMissingStartTime() throws InterruptedException { public void testNumberOfChoresMissingStartTime() throws InterruptedException {
@ -537,6 +560,7 @@ public class TestChoreService {
final int period = 100; final int period = 100;
final int sleepTime = 5 * period; final int sleepTime = 5 * period;
try {
// Slow chores sleep for a length of time LONGER than their period. Thus, SlowChores // Slow chores sleep for a length of time LONGER than their period. Thus, SlowChores
// ALWAYS miss their start time since their execution takes longer than their period // ALWAYS miss their start time since their execution takes longer than their period
SlowChore sc1 = new SlowChore("sc1", period); SlowChore sc1 = new SlowChore("sc1", period);
@ -567,9 +591,10 @@ public class TestChoreService {
sc5.cancel(); sc5.cancel();
Thread.sleep(sleepTime); Thread.sleep(sleepTime);
assertEquals(0, service.getNumberOfChoresMissingStartTime()); assertEquals(0, service.getNumberOfChoresMissingStartTime());
} finally {
shutdownService(service); shutdownService(service);
} }
}
/** /**
* ChoreServices should never have a core pool size that exceeds the number of chores that have * ChoreServices should never have a core pool size that exceeds the number of chores that have
@ -583,6 +608,7 @@ public class TestChoreService {
final int period = 100; final int period = 100;
final int sleepTime = 5 * period; final int sleepTime = 5 * period;
try {
// Slow chores sleep for a length of time LONGER than their period. Thus, SlowChores // Slow chores sleep for a length of time LONGER than their period. Thus, SlowChores
// ALWAYS miss their start time since their execution takes longer than their period. // ALWAYS miss their start time since their execution takes longer than their period.
// Chores that miss their start time will trigger the onChoreMissedStartTime callback // Chores that miss their start time will trigger the onChoreMissedStartTime callback
@ -617,9 +643,10 @@ public class TestChoreService {
Thread.sleep(sleepTime); Thread.sleep(sleepTime);
assertTrue(service.getCorePoolSize() <= service.getNumberOfScheduledChores()); assertTrue(service.getCorePoolSize() <= service.getNumberOfScheduledChores());
} finally {
shutdownService(service); shutdownService(service);
} }
}
@Test (timeout=20000) @Test (timeout=20000)
public void testChangingChoreServices() throws InterruptedException { public void testChangingChoreServices() throws InterruptedException {
@ -629,6 +656,7 @@ public class TestChoreService {
ChoreService service2 = new ChoreService("testChangingChoreServices_2"); ChoreService service2 = new ChoreService("testChangingChoreServices_2");
ScheduledChore chore = new DoNothingChore("sample", period); ScheduledChore chore = new DoNothingChore("sample", period);
try {
assertFalse(chore.isScheduled()); assertFalse(chore.isScheduled());
assertFalse(service1.isChoreScheduled(chore)); assertFalse(service1.isChoreScheduled(chore));
assertFalse(service2.isChoreScheduled(chore)); assertFalse(service2.isChoreScheduled(chore));
@ -653,10 +681,11 @@ public class TestChoreService {
assertFalse(service1.isChoreScheduled(chore)); assertFalse(service1.isChoreScheduled(chore));
assertFalse(service2.isChoreScheduled(chore)); assertFalse(service2.isChoreScheduled(chore));
assertTrue(chore.getChoreServicer() == null); assertTrue(chore.getChoreServicer() == null);
} finally {
shutdownService(service1); shutdownService(service1);
shutdownService(service2); shutdownService(service2);
} }
}
@Test (timeout=20000) @Test (timeout=20000)
public void testTriggerNowFailsWhenNotScheduled() throws InterruptedException { public void testTriggerNowFailsWhenNotScheduled() throws InterruptedException {
@ -666,6 +695,7 @@ public class TestChoreService {
ChoreService service = new ChoreService("testTriggerNowFailsWhenNotScheduled"); ChoreService service = new ChoreService("testTriggerNowFailsWhenNotScheduled");
CountingChore chore = new CountingChore("dn", period); CountingChore chore = new CountingChore("dn", period);
try {
assertFalse(chore.triggerNow()); assertFalse(chore.triggerNow());
assertTrue(chore.getCountOfChoreCalls() == 0); assertTrue(chore.getCountOfChoreCalls() == 0);
@ -681,9 +711,10 @@ public class TestChoreService {
assertTrue(chore.triggerNow()); assertTrue(chore.triggerNow());
Thread.sleep(sleep); Thread.sleep(sleep);
assertEquals(5, chore.getCountOfChoreCalls()); assertEquals(5, chore.getCountOfChoreCalls());
} finally {
shutdownService(service); shutdownService(service);
} }
}
@Test (timeout=20000) @Test (timeout=20000)
public void testStopperForScheduledChores() throws InterruptedException { public void testStopperForScheduledChores() throws InterruptedException {
@ -693,6 +724,7 @@ public class TestChoreService {
final int period = 100; final int period = 100;
final int delta = 10; final int delta = 10;
try {
ScheduledChore chore1_group1 = new DoNothingChore("c1g1", stopperForGroup1, period); ScheduledChore chore1_group1 = new DoNothingChore("c1g1", stopperForGroup1, period);
ScheduledChore chore2_group1 = new DoNothingChore("c2g1", stopperForGroup1, period); ScheduledChore chore2_group1 = new DoNothingChore("c2g1", stopperForGroup1, period);
ScheduledChore chore3_group1 = new DoNothingChore("c3g1", stopperForGroup1, period); ScheduledChore chore3_group1 = new DoNothingChore("c3g1", stopperForGroup1, period);
@ -734,9 +766,10 @@ public class TestChoreService {
assertFalse(chore1_group2.isScheduled()); assertFalse(chore1_group2.isScheduled());
assertFalse(chore2_group2.isScheduled()); assertFalse(chore2_group2.isScheduled());
assertFalse(chore3_group2.isScheduled()); assertFalse(chore3_group2.isScheduled());
} finally {
shutdownService(service); shutdownService(service);
} }
}
@Test (timeout=20000) @Test (timeout=20000)
public void testShutdownCancelsScheduledChores() throws InterruptedException { public void testShutdownCancelsScheduledChores() throws InterruptedException {
@ -746,14 +779,16 @@ public class TestChoreService {
ScheduledChore successChore2 = new DoNothingChore("sc2", period); ScheduledChore successChore2 = new DoNothingChore("sc2", period);
ScheduledChore successChore3 = new DoNothingChore("sc3", period); ScheduledChore successChore3 = new DoNothingChore("sc3", period);
try {
assertTrue(service.scheduleChore(successChore1)); assertTrue(service.scheduleChore(successChore1));
assertTrue(successChore1.isScheduled()); assertTrue(successChore1.isScheduled());
assertTrue(service.scheduleChore(successChore2)); assertTrue(service.scheduleChore(successChore2));
assertTrue(successChore2.isScheduled()); assertTrue(successChore2.isScheduled());
assertTrue(service.scheduleChore(successChore3)); assertTrue(service.scheduleChore(successChore3));
assertTrue(successChore3.isScheduled()); assertTrue(successChore3.isScheduled());
} finally {
shutdownService(service); shutdownService(service);
}
assertFalse(successChore1.isScheduled()); assertFalse(successChore1.isScheduled());
assertFalse(successChore2.isScheduled()); assertFalse(successChore2.isScheduled());
@ -768,7 +803,7 @@ public class TestChoreService {
ScheduledChore slowChore1 = new SleepingChore("sc1", period, sleep); ScheduledChore slowChore1 = new SleepingChore("sc1", period, sleep);
ScheduledChore slowChore2 = new SleepingChore("sc2", period, sleep); ScheduledChore slowChore2 = new SleepingChore("sc2", period, sleep);
ScheduledChore slowChore3 = new SleepingChore("sc3", period, sleep); ScheduledChore slowChore3 = new SleepingChore("sc3", period, sleep);
try {
assertTrue(service.scheduleChore(slowChore1)); assertTrue(service.scheduleChore(slowChore1));
assertTrue(service.scheduleChore(slowChore2)); assertTrue(service.scheduleChore(slowChore2));
assertTrue(service.scheduleChore(slowChore3)); assertTrue(service.scheduleChore(slowChore3));
@ -783,9 +818,10 @@ public class TestChoreService {
Thread.sleep(5); Thread.sleep(5);
assertTrue(service.isTerminated()); assertTrue(service.isTerminated());
} finally {
shutdownService(service); shutdownService(service);
} }
}
@Test (timeout=20000) @Test (timeout=20000)
public void testShutdownRejectsNewSchedules() throws InterruptedException { public void testShutdownRejectsNewSchedules() throws InterruptedException {
@ -798,14 +834,16 @@ public class TestChoreService {
ScheduledChore failChore2 = new DoNothingChore("fc2", period); ScheduledChore failChore2 = new DoNothingChore("fc2", period);
ScheduledChore failChore3 = new DoNothingChore("fc3", period); ScheduledChore failChore3 = new DoNothingChore("fc3", period);
try {
assertTrue(service.scheduleChore(successChore1)); assertTrue(service.scheduleChore(successChore1));
assertTrue(successChore1.isScheduled()); assertTrue(successChore1.isScheduled());
assertTrue(service.scheduleChore(successChore2)); assertTrue(service.scheduleChore(successChore2));
assertTrue(successChore2.isScheduled()); assertTrue(successChore2.isScheduled());
assertTrue(service.scheduleChore(successChore3)); assertTrue(service.scheduleChore(successChore3));
assertTrue(successChore3.isScheduled()); assertTrue(successChore3.isScheduled());
} finally {
shutdownService(service); shutdownService(service);
}
assertFalse(service.scheduleChore(failChore1)); assertFalse(service.scheduleChore(failChore1));
assertFalse(failChore1.isScheduled()); assertFalse(failChore1.isScheduled());