View Javadoc

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