View Javadoc

1   package com.atlassian.sal.core.scheduling;
2   
3   import com.atlassian.sal.api.scheduling.PluginJob;
4   import com.google.common.base.Predicate;
5   import junit.framework.TestCase;
6   
7   import java.util.Collections;
8   import java.util.Date;
9   import java.util.Map;
10  import java.util.concurrent.Executors;
11  import java.util.concurrent.ScheduledExecutorService;
12  import java.util.concurrent.atomic.AtomicInteger;
13  
14  public class ExecutorPluginSchedulerTest extends TestCase
15  {
16  
17      private ScheduledExecutorService executor;
18      private ExecutorPluginScheduler scheduler;
19  
20      protected void setUp() throws Exception
21      {
22          executor = Executors.newScheduledThreadPool(1);
23          scheduler = new ExecutorPluginScheduler(executor);
24      }
25  
26      @Override
27      public void tearDown() throws Exception
28      {
29          executor.shutdown();
30      }
31  
32      public void testSchedule() throws Exception
33      {
34          scheduler.scheduleJob("test1", TestPluginJob.class, Collections.<String, Object>emptyMap(), new Date(), 60000);
35          waitForCondition(new Predicate<AtomicInteger>()
36          {
37              @Override
38              public boolean apply(AtomicInteger input)
39              {
40                  return input.get() == 1;
41              }
42          }, 180000);
43          scheduler.unscheduleJob("test1");
44          assertJobIsNotScheduled("test1");
45  
46          scheduler.scheduleJob("test2", TestPluginJob.class, Collections.<String, Object>emptyMap(), new Date(), 500);
47          waitForCondition(new Predicate<AtomicInteger>()
48          {
49              @Override
50              public boolean apply(AtomicInteger input)
51              {
52                  return input.get() > 1;
53              }
54          }, 180000);
55          scheduler.unscheduleJob("test2");
56          assertJobIsNotScheduled("test2");
57      }
58  
59      public void testUnscheduleInexisting() throws Exception
60      {
61          assertJobIsNotScheduled("inexistingjob");
62      }
63  
64      private void waitForCondition(Predicate<AtomicInteger> condition, int timeoutInMs) throws InterruptedException
65      {
66          long start = System.currentTimeMillis();
67          while (!condition.apply(TestPluginJob.executionCount) && (System.currentTimeMillis() - start < timeoutInMs))
68          {
69              Thread.sleep(500);
70          }
71      }
72  
73      private void assertJobIsNotScheduled(String jobKey)
74      {
75          try
76          {
77              scheduler.unscheduleJob(jobKey);
78              fail("IllegalArgumentException should have thrown");
79          } catch (IllegalArgumentException e)
80          {
81              assertTrue(e.getMessage().contains("unknown job"));
82          }
83      }
84  
85      public static class TestPluginJob implements PluginJob
86      {
87          private static final AtomicInteger executionCount = new AtomicInteger(0);
88  
89          @Override
90          public void execute(Map<String, Object> jobDataMap)
91          {
92              executionCount.incrementAndGet();
93          }
94      }
95  }