View Javadoc

1   /*
2    * $Id: FutureMessageResultTestCase.java 22377 2011-07-11 12:41:42Z dirk.olmes $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.api;
12  
13  import org.mule.tck.junit4.AbstractMuleContextTestCase;
14  
15  import java.util.concurrent.Callable;
16  import java.util.concurrent.ExecutionException;
17  import java.util.concurrent.ExecutorService;
18  import java.util.concurrent.Executors;
19  import java.util.concurrent.RejectedExecutionException;
20  import java.util.concurrent.TimeoutException;
21  import org.junit.Test;
22  
23  import static org.junit.Assert.assertFalse;
24  import static org.junit.Assert.assertNull;
25  import static org.junit.Assert.assertTrue;
26  import static org.junit.Assert.fail;
27  
28  public class FutureMessageResultTestCase extends AbstractMuleContextTestCase
29  {
30      private static Callable Dummy = new Callable()
31      {
32          public Object call()
33          {
34              return null;
35          }
36      };
37  
38      private volatile boolean wasCalled;
39  
40      @Test
41      public void testCreation()
42      {
43          try
44          {
45              new FutureMessageResult(null, muleContext);
46              fail();
47          }
48          catch (NullPointerException npe)
49          {
50              // OK: see FutureTask(Callable)
51          }
52  
53          try
54          {
55              FutureMessageResult f = new FutureMessageResult(Dummy, muleContext);
56              f.setExecutor(null);
57              fail();
58          }
59          catch (IllegalArgumentException iex)
60          {
61              // OK: no null ExecutorService
62          }
63  
64      }
65  
66      @Test
67      public void testExecute() throws ExecutionException, InterruptedException, MuleException
68      {
69          Callable c = new Callable()
70          {
71              public Object call()
72              {
73                  wasCalled = true;
74                  return null;
75              }
76          };
77  
78          FutureMessageResult f = new FutureMessageResult(c, muleContext);
79          f.execute();
80  
81          assertNull(f.getMessage());
82          assertTrue(wasCalled);
83      }
84  
85      @Test
86      public void testExecuteWithShutdownExecutor()
87      {
88          ExecutorService e = Executors.newCachedThreadPool();
89          e.shutdown();
90  
91          FutureMessageResult f = new FutureMessageResult(Dummy, muleContext);
92          f.setExecutor(e);
93  
94          try
95          {
96              f.execute();
97              fail();
98          }
99          catch (RejectedExecutionException rex)
100         {
101             // OK: fail with shutdown Executor
102         }
103     }
104 
105     @Test
106     public void testExecuteWithTimeout()
107         throws ExecutionException, InterruptedException, MuleException
108     {
109         Callable c = new Callable()
110         {
111             public Object call() throws InterruptedException
112             {
113                 // I'm slow, have patience with me
114                 Thread.sleep(3000L);
115                 wasCalled = true;
116                 return null;
117             }
118         };
119 
120         FutureMessageResult f = new FutureMessageResult(c, muleContext);
121         f.execute();
122 
123         try
124         {
125             f.getMessage(500L);
126             fail();
127         }
128         catch (TimeoutException tex)
129         {
130             // OK: we did not wait long enough for our straggler, so let's tell him
131             // to forget about his task
132             f.cancel(true);
133         }
134         finally
135         {
136             assertFalse(wasCalled);
137         }
138     }
139 
140 }