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