View Javadoc

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