1   /*
2    * $Id: MuleWorkManagerTestCase.java 10489 2008-01-23 17:53:38Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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);
41          
42          try
43          {
44              wm.start();
45              
46              wm.doWork(new Work()
47              {
48                  public void release()
49                  {
50                      // no-op
51                  }
52  
53                  public void run()
54                  {
55                      Thread calleeThread = Thread.currentThread();
56                      assertEquals("WorkManager.doWork() should have been executed in the same thread.",
57                                   callerThread, calleeThread);
58                      if (logger.isDebugEnabled())
59                      {
60                          logger.debug("WORK: " + Thread.currentThread());
61                      }
62                  }
63              });
64              if (logger.isDebugEnabled())
65              {
66                  logger.debug("MAIN: " + Thread.currentThread());
67              }
68          }
69          finally
70          {
71              wm.dispose();
72          }
73  
74      }
75  
76      public void testScheduleWorkExecutesAsynchronously() throws Exception
77      {
78          final Thread callerThread = Thread.currentThread();
79  
80          MuleWorkManager wm = new MuleWorkManager(ThreadingProfile.DEFAULT_THREADING_PROFILE, null);
81  
82          try
83          {
84              wm.start();
85  
86              wm.scheduleWork(new Work()
87              {
88                  public void release()
89                  {
90                      // no-op
91                  }
92  
93                  public void run()
94                  {
95                      Thread calleeThread = Thread.currentThread();
96                      assertFalse("WorkManager.scheduleWork() should have been executed in a different thread.",
97                                  callerThread.equals(calleeThread));
98                      if (logger.isDebugEnabled())
99                      {
100                         logger.debug("WORK: " + Thread.currentThread());
101                     }
102                 }
103             });
104             if (logger.isDebugEnabled())
105             {
106                 logger.debug("MAIN: " + Thread.currentThread());
107             }
108         }
109         finally
110         {
111             wm.dispose();
112         }
113 
114     }
115 
116     public void testStartWorkExecutesAsynchronously() throws Exception
117     {
118         final Thread callerThread = Thread.currentThread();
119 
120         MuleWorkManager wm = new MuleWorkManager(ThreadingProfile.DEFAULT_THREADING_PROFILE, null);
121 
122         try
123         {
124             wm.start();
125 
126             wm.startWork(new Work()
127             {
128                 public void release()
129                 {
130                     // no-op
131                 }
132 
133                 public void run()
134                 {
135                     Thread calleeThread = Thread.currentThread();
136                     assertFalse("WorkManager.startWork() should have been executed in a different thread.",
137                                 callerThread.equals(calleeThread));
138                     if (logger.isDebugEnabled())
139                     {
140                         logger.debug("WORK: " + Thread.currentThread());
141                     }
142                 }
143             });
144             if (logger.isDebugEnabled())
145             {
146                 logger.debug("MAIN: " + Thread.currentThread());
147             }
148         }
149         finally
150         {
151             wm.dispose();
152         }
153 
154     }
155 
156 }