1
2
3
4
5
6
7 package org.mule;
8
9 import org.mule.api.config.ThreadingProfile;
10 import org.mule.tck.junit4.AbstractMuleContextTestCase;
11 import org.mule.work.MuleWorkManager;
12
13 import javax.resource.spi.work.Work;
14
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
17 import org.junit.Test;
18
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21
22
23
24
25
26
27
28
29
30
31
32 public class MuleWorkManagerTestCase extends AbstractMuleContextTestCase
33 {
34 private final transient Log logger = LogFactory.getLog(getClass());
35
36 @Test
37 public void testDoWorkExecutesSynchronously() throws Exception
38 {
39 final Thread callerThread = Thread.currentThread();
40
41 MuleWorkManager wm = new MuleWorkManager(ThreadingProfile.DEFAULT_THREADING_PROFILE, null, 5000);
42 wm.setMuleContext(muleContext);
43
44 try
45 {
46 wm.start();
47
48 wm.doWork(new Work()
49 {
50 public void release()
51 {
52
53 }
54
55 public void run()
56 {
57 Thread calleeThread = Thread.currentThread();
58 assertEquals("WorkManager.doWork() should have been executed in the same thread.",
59 callerThread, calleeThread);
60 if (logger.isDebugEnabled())
61 {
62 logger.debug("WORK: " + Thread.currentThread());
63 }
64 }
65 });
66 if (logger.isDebugEnabled())
67 {
68 logger.debug("MAIN: " + Thread.currentThread());
69 }
70 }
71 finally
72 {
73 wm.dispose();
74 }
75
76 }
77
78 @Test
79 public void testScheduleWorkExecutesAsynchronously() throws Exception
80 {
81 final Thread callerThread = Thread.currentThread();
82
83 MuleWorkManager wm = new MuleWorkManager(ThreadingProfile.DEFAULT_THREADING_PROFILE, null, 5000);
84 wm.setMuleContext(muleContext);
85
86 try
87 {
88 wm.start();
89
90 wm.scheduleWork(new Work()
91 {
92 public void release()
93 {
94
95 }
96
97 public void run()
98 {
99 Thread calleeThread = Thread.currentThread();
100 assertFalse("WorkManager.scheduleWork() should have been executed in a different thread.",
101 callerThread.equals(calleeThread));
102 if (logger.isDebugEnabled())
103 {
104 logger.debug("WORK: " + Thread.currentThread());
105 }
106 }
107 });
108 if (logger.isDebugEnabled())
109 {
110 logger.debug("MAIN: " + Thread.currentThread());
111 }
112 }
113 finally
114 {
115 wm.dispose();
116 }
117
118 }
119
120 @Test
121 public void testStartWorkExecutesAsynchronously() throws Exception
122 {
123 final Thread callerThread = Thread.currentThread();
124
125 MuleWorkManager wm = new MuleWorkManager(ThreadingProfile.DEFAULT_THREADING_PROFILE, null, 5000);
126 wm.setMuleContext(muleContext);
127
128 try
129 {
130 wm.start();
131
132 wm.startWork(new Work()
133 {
134 public void release()
135 {
136
137 }
138
139 public void run()
140 {
141 Thread calleeThread = Thread.currentThread();
142 assertFalse("WorkManager.startWork() should have been executed in a different thread.",
143 callerThread.equals(calleeThread));
144 if (logger.isDebugEnabled())
145 {
146 logger.debug("WORK: " + Thread.currentThread());
147 }
148 }
149 });
150 if (logger.isDebugEnabled())
151 {
152 logger.debug("MAIN: " + Thread.currentThread());
153 }
154 }
155 finally
156 {
157 wm.dispose();
158 }
159
160 }
161
162 }