1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase;
19
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertFalse;
23 import static org.junit.Assert.assertTrue;
24
25 import java.util.concurrent.TimeUnit;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.hadoop.hbase.TestChoreService.ScheduledChoreSamples.CountingChore;
30 import org.apache.hadoop.hbase.TestChoreService.ScheduledChoreSamples.DoNothingChore;
31 import org.apache.hadoop.hbase.TestChoreService.ScheduledChoreSamples.FailInitialChore;
32 import org.apache.hadoop.hbase.TestChoreService.ScheduledChoreSamples.SampleStopper;
33 import org.apache.hadoop.hbase.TestChoreService.ScheduledChoreSamples.SleepingChore;
34 import org.apache.hadoop.hbase.TestChoreService.ScheduledChoreSamples.SlowChore;
35 import org.apache.hadoop.hbase.testclassification.SmallTests;
36 import org.junit.Test;
37 import org.junit.experimental.categories.Category;
38
39 @Category(SmallTests.class)
40 public class TestChoreService {
41 private final Log LOG = LogFactory.getLog(this.getClass());
42
43
44
45
46 public static class ScheduledChoreSamples {
47
48
49
50 public static class SampleStopper implements Stoppable {
51 private boolean stopped = false;
52
53 @Override
54 public void stop(String why) {
55 stopped = true;
56 }
57
58 @Override
59 public boolean isStopped() {
60 return stopped;
61 }
62 }
63
64
65
66
67
68 public static class SlowChore extends ScheduledChore {
69 public SlowChore(String name, int period) {
70 this(name, new SampleStopper(), period);
71 }
72
73 public SlowChore(String name, Stoppable stopper, int period) {
74 super(name, stopper, period);
75 }
76
77 @Override
78 protected boolean initialChore() {
79 try {
80 Thread.sleep(getPeriod() * 2);
81 } catch (InterruptedException e) {
82 e.printStackTrace();
83 }
84 return true;
85 }
86
87 @Override
88 protected void chore() {
89 try {
90 Thread.sleep(getPeriod() * 2);
91 } catch (InterruptedException e) {
92
93 }
94 }
95 }
96
97
98
99
100 public static class DoNothingChore extends ScheduledChore {
101 public DoNothingChore(String name, int period) {
102 super(name, new SampleStopper(), period);
103 }
104
105 public DoNothingChore(String name, Stoppable stopper, int period) {
106 super(name, stopper, period);
107 }
108
109 @Override
110 protected void chore() {
111
112 }
113
114 }
115
116 public static class SleepingChore extends ScheduledChore {
117 private int sleepTime;
118
119 public SleepingChore(String name, int chorePeriod, int sleepTime) {
120 this(name, new SampleStopper(), chorePeriod, sleepTime);
121 }
122
123 public SleepingChore(String name, Stoppable stopper, int period, int sleepTime) {
124 super(name, stopper, period);
125 this.sleepTime = sleepTime;
126 }
127
128 @Override
129 protected boolean initialChore() {
130 try {
131 Thread.sleep(sleepTime);
132 } catch (InterruptedException e) {
133 e.printStackTrace();
134 }
135 return true;
136 }
137
138 @Override
139 protected void chore() {
140 try {
141 Thread.sleep(sleepTime);
142 } catch (Exception e) {
143 System.err.println(e.getStackTrace());
144 }
145 }
146 }
147
148 public static class CountingChore extends ScheduledChore {
149 private int countOfChoreCalls;
150 private boolean outputOnTicks = false;
151
152 public CountingChore(String name, int period) {
153 this(name, new SampleStopper(), period);
154 }
155
156 public CountingChore(String name, Stoppable stopper, int period) {
157 this(name, stopper, period, false);
158 }
159
160 public CountingChore(String name, Stoppable stopper, int period,
161 final boolean outputOnTicks) {
162 super(name, stopper, period);
163 this.countOfChoreCalls = 0;
164 this.outputOnTicks = outputOnTicks;
165 }
166
167 @Override
168 protected boolean initialChore() {
169 countOfChoreCalls++;
170 if (outputOnTicks) outputTickCount();
171 return true;
172 }
173
174 @Override
175 protected void chore() {
176 countOfChoreCalls++;
177 if (outputOnTicks) outputTickCount();
178 }
179
180 private void outputTickCount() {
181 System.out.println("Chore: " + getName() + ". Count of chore calls: " + countOfChoreCalls);
182 }
183
184 public int getCountOfChoreCalls() {
185 return countOfChoreCalls;
186 }
187
188 public boolean isOutputtingOnTicks() {
189 return outputOnTicks;
190 }
191
192 public void setOutputOnTicks(boolean o) {
193 outputOnTicks = o;
194 }
195 }
196
197
198
199
200
201 public static class FailInitialChore extends ScheduledChore {
202 private int numberOfFailures;
203 private int failureThreshold;
204
205
206
207
208
209 public FailInitialChore(String name, int period, int failThreshold) {
210 this(name, new SampleStopper(), period, failThreshold);
211 }
212
213 public FailInitialChore(String name, Stoppable stopper, int period, int failThreshold) {
214 super(name, stopper, period);
215 numberOfFailures = 0;
216 failureThreshold = failThreshold;
217 }
218
219 @Override
220 protected boolean initialChore() {
221 if (numberOfFailures < failureThreshold) {
222 numberOfFailures++;
223 return false;
224 } else {
225 return true;
226 }
227 }
228
229 @Override
230 protected void chore() {
231 assertTrue(numberOfFailures == failureThreshold);
232 cancel(false);
233 }
234
235 }
236 }
237
238 @Test (timeout=20000)
239 public void testInitialChorePrecedence() throws InterruptedException {
240 ChoreService service = ChoreService.getInstance("testInitialChorePrecedence");
241
242 final int period = 100;
243 final int failureThreshold = 5;
244
245 try {
246 ScheduledChore chore = new FailInitialChore("chore", period, failureThreshold);
247 service.scheduleChore(chore);
248
249 int loopCount = 0;
250 boolean brokeOutOfLoop = false;
251
252 while (!chore.isInitialChoreComplete() && chore.isScheduled()) {
253 Thread.sleep(failureThreshold * period);
254 loopCount++;
255 if (loopCount > 3) {
256 brokeOutOfLoop = true;
257 break;
258 }
259 }
260
261 assertFalse(brokeOutOfLoop);
262 } finally {
263 shutdownService(service);
264 }
265 }
266
267 @Test (timeout=20000)
268 public void testCancelChore() throws InterruptedException {
269 final int period = 100;
270 ScheduledChore chore1 = new DoNothingChore("chore1", period);
271 ChoreService service = ChoreService.getInstance("testCancelChore");
272 try {
273 service.scheduleChore(chore1);
274 assertTrue(chore1.isScheduled());
275
276 chore1.cancel(true);
277 assertFalse(chore1.isScheduled());
278 assertTrue(service.getNumberOfScheduledChores() == 0);
279 } finally {
280 shutdownService(service);
281 }
282 }
283
284 @Test (timeout=20000)
285 public void testScheduledChoreConstruction() {
286 final String NAME = "chore";
287 final int PERIOD = 100;
288 final long VALID_DELAY = 0;
289 final long INVALID_DELAY = -100;
290 final TimeUnit UNIT = TimeUnit.NANOSECONDS;
291
292 ScheduledChore chore1 =
293 new ScheduledChore(NAME, new SampleStopper(), PERIOD, VALID_DELAY, UNIT) {
294 @Override
295 protected void chore() {
296
297 }
298 };
299
300 assertEquals("Name construction failed", chore1.getName(), NAME);
301 assertEquals("Period construction failed", chore1.getPeriod(), PERIOD);
302 assertEquals("Initial Delay construction failed", chore1.getInitialDelay(), VALID_DELAY);
303 assertEquals("TimeUnit construction failed", chore1.getTimeUnit(), UNIT);
304
305 ScheduledChore invalidDelayChore =
306 new ScheduledChore(NAME, new SampleStopper(), PERIOD, INVALID_DELAY, UNIT) {
307 @Override
308 protected void chore() {
309
310 }
311 };
312
313 assertEquals("Initial Delay should be set to 0 when invalid", 0,
314 invalidDelayChore.getInitialDelay());
315 }
316
317 @Test (timeout=20000)
318 public void testChoreServiceConstruction() throws InterruptedException {
319 final int corePoolSize = 10;
320 final int defaultCorePoolSize = ChoreService.MIN_CORE_POOL_SIZE;
321
322 ChoreService customInit = new ChoreService("testChoreServiceConstruction_custom", corePoolSize);
323 try {
324 assertEquals(corePoolSize, customInit.getCorePoolSize());
325 } finally {
326 shutdownService(customInit);
327 }
328
329 ChoreService defaultInit = new ChoreService("testChoreServiceConstruction_default");
330 try {
331 assertEquals(defaultCorePoolSize, defaultInit.getCorePoolSize());
332 } finally {
333 shutdownService(defaultInit);
334 }
335
336 ChoreService invalidInit = new ChoreService("testChoreServiceConstruction_invalid", -10);
337 try {
338 assertEquals(defaultCorePoolSize, invalidInit.getCorePoolSize());
339 } finally {
340 shutdownService(invalidInit);
341 }
342 }
343
344 @Test (timeout=20000)
345 public void testFrequencyOfChores() throws InterruptedException {
346 final int period = 100;
347
348 final int delta = 5;
349 ChoreService service = ChoreService.getInstance("testFrequencyOfChores");
350 CountingChore chore = new CountingChore("countingChore", period);
351 try {
352 service.scheduleChore(chore);
353
354 Thread.sleep(10 * period + delta);
355 assertTrue(chore.getCountOfChoreCalls() == 11);
356
357 Thread.sleep(10 * period);
358 assertTrue(chore.getCountOfChoreCalls() == 21);
359 } finally {
360 shutdownService(service);
361 }
362 }
363
364 public void shutdownService(ChoreService service) throws InterruptedException {
365 service.shutdown();
366 while (!service.isTerminated()) {
367 Thread.sleep(100);
368 }
369 }
370
371 @Test (timeout=20000)
372 public void testForceTrigger() throws InterruptedException {
373 final int period = 100;
374 final int delta = 5;
375 ChoreService service = ChoreService.getInstance("testForceTrigger");
376 CountingChore chore = new CountingChore("countingChore", period);
377 try {
378 service.scheduleChore(chore);
379 Thread.sleep(10 * period + delta);
380
381 assertTrue(chore.getCountOfChoreCalls() == 11);
382
383
384
385 chore.triggerNow();
386 Thread.sleep(delta);
387 chore.triggerNow();
388 Thread.sleep(delta);
389 chore.triggerNow();
390 Thread.sleep(delta);
391 chore.triggerNow();
392 Thread.sleep(delta);
393 chore.triggerNow();
394 Thread.sleep(delta);
395
396 assertTrue(chore.getCountOfChoreCalls() == 16);
397
398 Thread.sleep(10 * period + delta);
399
400 assertTrue(chore.getCountOfChoreCalls() == 26);
401 } finally {
402 shutdownService(service);
403 }
404 }
405
406 @Test (timeout=20000)
407 public void testCorePoolIncrease() throws InterruptedException {
408 final int initialCorePoolSize = 3;
409 ChoreService service = new ChoreService("testCorePoolIncrease", initialCorePoolSize);
410
411 try {
412 assertEquals("Should have a core pool of size: " + initialCorePoolSize, initialCorePoolSize,
413 service.getCorePoolSize());
414
415 final int slowChorePeriod = 100;
416 SlowChore slowChore1 = new SlowChore("slowChore1", slowChorePeriod);
417 SlowChore slowChore2 = new SlowChore("slowChore2", slowChorePeriod);
418 SlowChore slowChore3 = new SlowChore("slowChore3", slowChorePeriod);
419
420 service.scheduleChore(slowChore1);
421 service.scheduleChore(slowChore2);
422 service.scheduleChore(slowChore3);
423
424 Thread.sleep(slowChorePeriod * 10);
425 assertEquals("Should not create more pools than scheduled chores", 3,
426 service.getCorePoolSize());
427
428 SlowChore slowChore4 = new SlowChore("slowChore4", slowChorePeriod);
429 service.scheduleChore(slowChore4);
430
431 Thread.sleep(slowChorePeriod * 10);
432 assertEquals("Chores are missing their start time. Should expand core pool size", 4,
433 service.getCorePoolSize());
434
435 SlowChore slowChore5 = new SlowChore("slowChore5", slowChorePeriod);
436 service.scheduleChore(slowChore5);
437
438 Thread.sleep(slowChorePeriod * 10);
439 assertEquals("Chores are missing their start time. Should expand core pool size", 5,
440 service.getCorePoolSize());
441 } finally {
442 shutdownService(service);
443 }
444 }
445
446 @Test(timeout = 30000)
447 public void testCorePoolDecrease() throws InterruptedException {
448 final int initialCorePoolSize = 3;
449 ChoreService service = new ChoreService("testCorePoolDecrease", initialCorePoolSize);
450 final int chorePeriod = 100;
451 try {
452
453
454 SlowChore slowChore1 = new SlowChore("slowChore1", chorePeriod);
455 SlowChore slowChore2 = new SlowChore("slowChore2", chorePeriod);
456 SlowChore slowChore3 = new SlowChore("slowChore3", chorePeriod);
457
458 service.scheduleChore(slowChore1);
459 service.scheduleChore(slowChore2);
460 service.scheduleChore(slowChore3);
461
462 Thread.sleep(chorePeriod * 10);
463 assertEquals("Should not create more pools than scheduled chores",
464 service.getNumberOfScheduledChores(), service.getCorePoolSize());
465
466 SlowChore slowChore4 = new SlowChore("slowChore4", chorePeriod);
467 service.scheduleChore(slowChore4);
468 Thread.sleep(chorePeriod * 10);
469 assertEquals("Chores are missing their start time. Should expand core pool size",
470 service.getNumberOfScheduledChores(), service.getCorePoolSize());
471
472 SlowChore slowChore5 = new SlowChore("slowChore5", chorePeriod);
473 service.scheduleChore(slowChore5);
474 Thread.sleep(chorePeriod * 10);
475 assertEquals("Chores are missing their start time. Should expand core pool size",
476 service.getNumberOfScheduledChores(), service.getCorePoolSize());
477 assertEquals(service.getNumberOfChoresMissingStartTime(), 5);
478
479
480
481 slowChore5.cancel();
482 Thread.sleep(chorePeriod * 10);
483 assertEquals(Math.max(ChoreService.MIN_CORE_POOL_SIZE, service.getNumberOfScheduledChores()),
484 service.getCorePoolSize());
485 assertEquals(service.getNumberOfChoresMissingStartTime(), 4);
486
487 slowChore4.cancel();
488 Thread.sleep(chorePeriod * 10);
489 assertEquals(Math.max(ChoreService.MIN_CORE_POOL_SIZE, service.getNumberOfScheduledChores()),
490 service.getCorePoolSize());
491 assertEquals(service.getNumberOfChoresMissingStartTime(), 3);
492
493 slowChore3.cancel();
494 Thread.sleep(chorePeriod * 10);
495 assertEquals(Math.max(ChoreService.MIN_CORE_POOL_SIZE, service.getNumberOfScheduledChores()),
496 service.getCorePoolSize());
497 assertEquals(service.getNumberOfChoresMissingStartTime(), 2);
498
499 slowChore2.cancel();
500 Thread.sleep(chorePeriod * 10);
501 assertEquals(Math.max(ChoreService.MIN_CORE_POOL_SIZE, service.getNumberOfScheduledChores()),
502 service.getCorePoolSize());
503 assertEquals(service.getNumberOfChoresMissingStartTime(), 1);
504
505 slowChore1.cancel();
506 Thread.sleep(chorePeriod * 10);
507 assertEquals(Math.max(ChoreService.MIN_CORE_POOL_SIZE, service.getNumberOfScheduledChores()),
508 service.getCorePoolSize());
509 assertEquals(service.getNumberOfChoresMissingStartTime(), 0);
510 } finally {
511 shutdownService(service);
512 }
513 }
514
515 @Test (timeout=20000)
516 public void testNumberOfRunningChores() throws InterruptedException {
517 ChoreService service = new ChoreService("testNumberOfRunningChores");
518
519 final int period = 100;
520 final int sleepTime = 5;
521
522 try {
523 DoNothingChore dn1 = new DoNothingChore("dn1", period);
524 DoNothingChore dn2 = new DoNothingChore("dn2", period);
525 DoNothingChore dn3 = new DoNothingChore("dn3", period);
526 DoNothingChore dn4 = new DoNothingChore("dn4", period);
527 DoNothingChore dn5 = new DoNothingChore("dn5", period);
528
529 service.scheduleChore(dn1);
530 service.scheduleChore(dn2);
531 service.scheduleChore(dn3);
532 service.scheduleChore(dn4);
533 service.scheduleChore(dn5);
534
535 Thread.sleep(sleepTime);
536 assertEquals("Scheduled chore mismatch", 5, service.getNumberOfScheduledChores());
537
538 dn1.cancel();
539 Thread.sleep(sleepTime);
540 assertEquals("Scheduled chore mismatch", 4, service.getNumberOfScheduledChores());
541
542 dn2.cancel();
543 dn3.cancel();
544 dn4.cancel();
545 Thread.sleep(sleepTime);
546 assertEquals("Scheduled chore mismatch", 1, service.getNumberOfScheduledChores());
547
548 dn5.cancel();
549 Thread.sleep(sleepTime);
550 assertEquals("Scheduled chore mismatch", 0, service.getNumberOfScheduledChores());
551 } finally {
552 shutdownService(service);
553 }
554 }
555
556 @Test (timeout=20000)
557 public void testNumberOfChoresMissingStartTime() throws InterruptedException {
558 ChoreService service = new ChoreService("testNumberOfChoresMissingStartTime");
559
560 final int period = 100;
561 final int sleepTime = 5 * period;
562
563 try {
564
565
566 SlowChore sc1 = new SlowChore("sc1", period);
567 SlowChore sc2 = new SlowChore("sc2", period);
568 SlowChore sc3 = new SlowChore("sc3", period);
569 SlowChore sc4 = new SlowChore("sc4", period);
570 SlowChore sc5 = new SlowChore("sc5", period);
571
572 service.scheduleChore(sc1);
573 service.scheduleChore(sc2);
574 service.scheduleChore(sc3);
575 service.scheduleChore(sc4);
576 service.scheduleChore(sc5);
577
578 Thread.sleep(sleepTime);
579 assertEquals(5, service.getNumberOfChoresMissingStartTime());
580
581 sc1.cancel();
582 Thread.sleep(sleepTime);
583 assertEquals(4, service.getNumberOfChoresMissingStartTime());
584
585 sc2.cancel();
586 sc3.cancel();
587 sc4.cancel();
588 Thread.sleep(sleepTime);
589 assertEquals(1, service.getNumberOfChoresMissingStartTime());
590
591 sc5.cancel();
592 Thread.sleep(sleepTime);
593 assertEquals(0, service.getNumberOfChoresMissingStartTime());
594 } finally {
595 shutdownService(service);
596 }
597 }
598
599
600
601
602
603
604 @Test (timeout=20000)
605 public void testMaximumChoreServiceThreads() throws InterruptedException {
606 ChoreService service = new ChoreService("testMaximumChoreServiceThreads");
607
608 final int period = 100;
609 final int sleepTime = 5 * period;
610
611 try {
612
613
614
615
616
617 SlowChore sc1 = new SlowChore("sc1", period);
618 SlowChore sc2 = new SlowChore("sc2", period);
619 SlowChore sc3 = new SlowChore("sc3", period);
620 SlowChore sc4 = new SlowChore("sc4", period);
621 SlowChore sc5 = new SlowChore("sc5", period);
622
623 service.scheduleChore(sc1);
624 service.scheduleChore(sc2);
625 service.scheduleChore(sc3);
626 service.scheduleChore(sc4);
627 service.scheduleChore(sc5);
628
629 Thread.sleep(sleepTime);
630 assertTrue(service.getCorePoolSize() <= service.getNumberOfScheduledChores());
631
632 SlowChore sc6 = new SlowChore("sc6", period);
633 SlowChore sc7 = new SlowChore("sc7", period);
634 SlowChore sc8 = new SlowChore("sc8", period);
635 SlowChore sc9 = new SlowChore("sc9", period);
636 SlowChore sc10 = new SlowChore("sc10", period);
637
638 service.scheduleChore(sc6);
639 service.scheduleChore(sc7);
640 service.scheduleChore(sc8);
641 service.scheduleChore(sc9);
642 service.scheduleChore(sc10);
643
644 Thread.sleep(sleepTime);
645 assertTrue(service.getCorePoolSize() <= service.getNumberOfScheduledChores());
646 } finally {
647 shutdownService(service);
648 }
649 }
650
651 @Test (timeout=20000)
652 public void testChangingChoreServices() throws InterruptedException {
653 final int period = 100;
654 final int sleepTime = 10;
655 ChoreService service1 = new ChoreService("testChangingChoreServices_1");
656 ChoreService service2 = new ChoreService("testChangingChoreServices_2");
657 ScheduledChore chore = new DoNothingChore("sample", period);
658
659 try {
660 assertFalse(chore.isScheduled());
661 assertFalse(service1.isChoreScheduled(chore));
662 assertFalse(service2.isChoreScheduled(chore));
663 assertTrue(chore.getChoreServicer() == null);
664
665 service1.scheduleChore(chore);
666 Thread.sleep(sleepTime);
667 assertTrue(chore.isScheduled());
668 assertTrue(service1.isChoreScheduled(chore));
669 assertFalse(service2.isChoreScheduled(chore));
670 assertFalse(chore.getChoreServicer() == null);
671
672 service2.scheduleChore(chore);
673 Thread.sleep(sleepTime);
674 assertTrue(chore.isScheduled());
675 assertFalse(service1.isChoreScheduled(chore));
676 assertTrue(service2.isChoreScheduled(chore));
677 assertFalse(chore.getChoreServicer() == null);
678
679 chore.cancel();
680 assertFalse(chore.isScheduled());
681 assertFalse(service1.isChoreScheduled(chore));
682 assertFalse(service2.isChoreScheduled(chore));
683 assertTrue(chore.getChoreServicer() == null);
684 } finally {
685 shutdownService(service1);
686 shutdownService(service2);
687 }
688 }
689
690 @Test (timeout=20000)
691 public void testTriggerNowFailsWhenNotScheduled() throws InterruptedException {
692 final int period = 100;
693
694 final int sleep = 5;
695 ChoreService service = new ChoreService("testTriggerNowFailsWhenNotScheduled");
696 CountingChore chore = new CountingChore("dn", period);
697
698 try {
699 assertFalse(chore.triggerNow());
700 assertTrue(chore.getCountOfChoreCalls() == 0);
701
702 service.scheduleChore(chore);
703 Thread.sleep(sleep);
704 assertEquals(1, chore.getCountOfChoreCalls());
705 Thread.sleep(period);
706 assertEquals(2, chore.getCountOfChoreCalls());
707 assertTrue(chore.triggerNow());
708 Thread.sleep(sleep);
709 assertTrue(chore.triggerNow());
710 Thread.sleep(sleep);
711 assertTrue(chore.triggerNow());
712 Thread.sleep(sleep);
713 assertEquals(5, chore.getCountOfChoreCalls());
714 } finally {
715 shutdownService(service);
716 }
717 }
718
719 @Test (timeout=20000)
720 public void testStopperForScheduledChores() throws InterruptedException {
721 ChoreService service = ChoreService.getInstance("testStopperForScheduledChores");
722 Stoppable stopperForGroup1 = new SampleStopper();
723 Stoppable stopperForGroup2 = new SampleStopper();
724 final int period = 100;
725 final int delta = 10;
726
727 try {
728 ScheduledChore chore1_group1 = new DoNothingChore("c1g1", stopperForGroup1, period);
729 ScheduledChore chore2_group1 = new DoNothingChore("c2g1", stopperForGroup1, period);
730 ScheduledChore chore3_group1 = new DoNothingChore("c3g1", stopperForGroup1, period);
731
732 ScheduledChore chore1_group2 = new DoNothingChore("c1g2", stopperForGroup2, period);
733 ScheduledChore chore2_group2 = new DoNothingChore("c2g2", stopperForGroup2, period);
734 ScheduledChore chore3_group2 = new DoNothingChore("c3g2", stopperForGroup2, period);
735
736 service.scheduleChore(chore1_group1);
737 service.scheduleChore(chore2_group1);
738 service.scheduleChore(chore3_group1);
739 service.scheduleChore(chore1_group2);
740 service.scheduleChore(chore2_group2);
741 service.scheduleChore(chore3_group2);
742
743 Thread.sleep(delta);
744 Thread.sleep(10 * period);
745 assertTrue(chore1_group1.isScheduled());
746 assertTrue(chore2_group1.isScheduled());
747 assertTrue(chore3_group1.isScheduled());
748 assertTrue(chore1_group2.isScheduled());
749 assertTrue(chore2_group2.isScheduled());
750 assertTrue(chore3_group2.isScheduled());
751
752 stopperForGroup1.stop("test stopping group 1");
753 Thread.sleep(period);
754 assertFalse(chore1_group1.isScheduled());
755 assertFalse(chore2_group1.isScheduled());
756 assertFalse(chore3_group1.isScheduled());
757 assertTrue(chore1_group2.isScheduled());
758 assertTrue(chore2_group2.isScheduled());
759 assertTrue(chore3_group2.isScheduled());
760
761 stopperForGroup2.stop("test stopping group 2");
762 Thread.sleep(period);
763 assertFalse(chore1_group1.isScheduled());
764 assertFalse(chore2_group1.isScheduled());
765 assertFalse(chore3_group1.isScheduled());
766 assertFalse(chore1_group2.isScheduled());
767 assertFalse(chore2_group2.isScheduled());
768 assertFalse(chore3_group2.isScheduled());
769 } finally {
770 shutdownService(service);
771 }
772 }
773
774 @Test (timeout=20000)
775 public void testShutdownCancelsScheduledChores() throws InterruptedException {
776 final int period = 100;
777 ChoreService service = new ChoreService("testShutdownCancelsScheduledChores");
778 ScheduledChore successChore1 = new DoNothingChore("sc1", period);
779 ScheduledChore successChore2 = new DoNothingChore("sc2", period);
780 ScheduledChore successChore3 = new DoNothingChore("sc3", period);
781
782 try {
783 assertTrue(service.scheduleChore(successChore1));
784 assertTrue(successChore1.isScheduled());
785 assertTrue(service.scheduleChore(successChore2));
786 assertTrue(successChore2.isScheduled());
787 assertTrue(service.scheduleChore(successChore3));
788 assertTrue(successChore3.isScheduled());
789 } finally {
790 shutdownService(service);
791 }
792
793 assertFalse(successChore1.isScheduled());
794 assertFalse(successChore2.isScheduled());
795 assertFalse(successChore3.isScheduled());
796 }
797
798 @Test (timeout=20000)
799 public void testShutdownWorksWhileChoresAreExecuting() throws InterruptedException {
800 final int period = 100;
801 final int sleep = 5 * period;
802 ChoreService service = new ChoreService("testShutdownWorksWhileChoresAreExecuting");
803 ScheduledChore slowChore1 = new SleepingChore("sc1", period, sleep);
804 ScheduledChore slowChore2 = new SleepingChore("sc2", period, sleep);
805 ScheduledChore slowChore3 = new SleepingChore("sc3", period, sleep);
806 try {
807 assertTrue(service.scheduleChore(slowChore1));
808 assertTrue(service.scheduleChore(slowChore2));
809 assertTrue(service.scheduleChore(slowChore3));
810
811 Thread.sleep(sleep / 2);
812 shutdownService(service);
813
814 assertFalse(slowChore1.isScheduled());
815 assertFalse(slowChore2.isScheduled());
816 assertFalse(slowChore3.isScheduled());
817 assertTrue(service.isShutdown());
818
819 Thread.sleep(5);
820 assertTrue(service.isTerminated());
821 } finally {
822 shutdownService(service);
823 }
824 }
825
826 @Test (timeout=20000)
827 public void testShutdownRejectsNewSchedules() throws InterruptedException {
828 final int period = 100;
829 ChoreService service = new ChoreService("testShutdownRejectsNewSchedules");
830 ScheduledChore successChore1 = new DoNothingChore("sc1", period);
831 ScheduledChore successChore2 = new DoNothingChore("sc2", period);
832 ScheduledChore successChore3 = new DoNothingChore("sc3", period);
833 ScheduledChore failChore1 = new DoNothingChore("fc1", period);
834 ScheduledChore failChore2 = new DoNothingChore("fc2", period);
835 ScheduledChore failChore3 = new DoNothingChore("fc3", period);
836
837 try {
838 assertTrue(service.scheduleChore(successChore1));
839 assertTrue(successChore1.isScheduled());
840 assertTrue(service.scheduleChore(successChore2));
841 assertTrue(successChore2.isScheduled());
842 assertTrue(service.scheduleChore(successChore3));
843 assertTrue(successChore3.isScheduled());
844 } finally {
845 shutdownService(service);
846 }
847
848 assertFalse(service.scheduleChore(failChore1));
849 assertFalse(failChore1.isScheduled());
850 assertFalse(service.scheduleChore(failChore2));
851 assertFalse(failChore2.isScheduled());
852 assertFalse(service.scheduleChore(failChore3));
853 assertFalse(failChore3.isScheduled());
854 }
855 }