View Javadoc

1   /*
2    * $Id: MuleWorkManagerTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
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;
12  
13  import org.mule.api.config.ThreadingProfile;
14  import org.mule.tck.AbstractMuleTestCase;
15  import org.mule.work.MuleWorkManager;
16  
17  import javax.resource.spi.work.Work;
18  
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  
22  /**
23   * Tests the following behavior:
24   * <ol>
25   *  <li>ScheduleWorkExecutor - e.g. use the backing thread pool to execute work items asynchronously
26   *  <li>StartWorkExecutor - block till the work is started, then async
27   *  <li>SyncWorkExecutor - blocking executor, meaning we should be running in the very same thread.
28   *</ol>
29   * It's not really important to make a distinction between <code>scheduleWork()</code> and
30   * <code>startWork()</code> for this test, thus they just check for async execution.
31   */
32  public class MuleWorkManagerTestCase extends AbstractMuleTestCase
33  {
34      private final transient Log logger = LogFactory.getLog(getClass());
35  
36      public void testDoWorkExecutesSynchronously() throws Exception
37      {
38          final Thread callerThread = Thread.currentThread();
39  
40          MuleWorkManager wm = new MuleWorkManager(ThreadingProfile.DEFAULT_THREADING_PROFILE, null, 5000);
41          wm.setMuleContext(muleContext);
42          
43          try
44          {
45              wm.start();
46              
47              wm.doWork(new Work()
48              {
49                  public void release()
50                  {
51                      // no-op
52                  }
53  
54                  public void run()
55                  {
56                      Thread calleeThread = Thread.currentThread();
57                      assertEquals("WorkManager.doWork() should have been executed in the same thread.",
58                                   callerThread, calleeThread);
59                      if (logger.isDebugEnabled())
60                      {
61                          logger.debug("WORK: " + Thread.currentThread());
62                      }
63                  }
64              });
65              if (logger.isDebugEnabled())
66              {
67                  logger.debug("MAIN: " + Thread.currentThread());
68              }
69          }
70          finally
71          {
72              wm.dispose();
73          }
74  
75      }
76  
77      public void testScheduleWorkExecutesAsynchronously() throws Exception
78      {
79          final Thread callerThread = Thread.currentThread();
80  
81          MuleWorkManager wm = new MuleWorkManager(ThreadingProfile.DEFAULT_THREADING_PROFILE, null, 5000);
82          wm.setMuleContext(muleContext);
83  
84          try
85          {
86              wm.start();
87  
88              wm.scheduleWork(new Work()
89              {
90                  public void release()
91                  {
92                      // no-op
93                  }
94  
95                  public void run()
96                  {
97                      Thread calleeThread = Thread.currentThread();
98                      assertFalse("WorkManager.scheduleWork() should have been executed in a different thread.",
99                                  callerThread.equals(calleeThread));
100                     if (logger.isDebugEnabled())
101                     {
102                         logger.debug("WORK: " + Thread.currentThread());
103                     }
104                 }
105             });
106             if (logger.isDebugEnabled())
107             {
108                 logger.debug("MAIN: " + Thread.currentThread());
109             }
110         }
111         finally
112         {
113             wm.dispose();
114         }
115 
116     }
117 
118     public void testStartWorkExecutesAsynchronously() throws Exception
119     {
120         final Thread callerThread = Thread.currentThread();
121 
122         MuleWorkManager wm = new MuleWorkManager(ThreadingProfile.DEFAULT_THREADING_PROFILE, null, 5000);
123         wm.setMuleContext(muleContext);
124 
125         try
126         {
127             wm.start();
128 
129             wm.startWork(new Work()
130             {
131                 public void release()
132                 {
133                     // no-op
134                 }
135 
136                 public void run()
137                 {
138                     Thread calleeThread = Thread.currentThread();
139                     assertFalse("WorkManager.startWork() should have been executed in a different thread.",
140                                 callerThread.equals(calleeThread));
141                     if (logger.isDebugEnabled())
142                     {
143                         logger.debug("WORK: " + Thread.currentThread());
144                     }
145                 }
146             });
147             if (logger.isDebugEnabled())
148             {
149                 logger.debug("MAIN: " + Thread.currentThread());
150             }
151         }
152         finally
153         {
154             wm.dispose();
155         }
156 
157     }
158 
159 }