1
2
3
4
5
6
7
8
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
24
25
26
27
28
29
30
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
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
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
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 }