1
2
3
4
5
6
7
8
9
10
11 package org.mule.model.seda;
12
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertNotNull;
15 import static org.junit.Assert.assertTrue;
16 import static org.junit.Assert.fail;
17 import static org.mockito.Mockito.mock;
18 import static org.mockito.Mockito.when;
19
20 import org.mule.MessageExchangePattern;
21 import org.mule.api.MuleEventContext;
22 import org.mule.api.config.MuleProperties;
23 import org.mule.api.config.ThreadingProfile;
24 import org.mule.api.lifecycle.Callable;
25 import org.mule.api.registry.RegistrationException;
26 import org.mule.api.service.Service;
27 import org.mule.api.store.ListableObjectStore;
28 import org.mule.component.DefaultJavaComponent;
29 import org.mule.component.SimpleCallableJavaComponent;
30 import org.mule.config.ChainedThreadingProfile;
31 import org.mule.config.QueueProfile;
32 import org.mule.model.AbstractServiceTestCase;
33 import org.mule.object.PrototypeObjectFactory;
34 import org.mule.tck.MuleTestUtils;
35 import org.mule.util.concurrent.Latch;
36 import org.mule.util.queue.QueueManager;
37
38 import java.io.Serializable;
39 import java.util.concurrent.TimeUnit;
40
41 import junit.framework.AssertionFailedError;
42
43 import org.junit.Test;
44
45 public class SedaServiceTestCase extends AbstractServiceTestCase
46 {
47 private SedaService service;
48
49 public SedaServiceTestCase()
50 {
51 setStartContext(true);
52 }
53
54 @Override
55 protected void doSetUp() throws Exception
56 {
57 service = new SedaService(muleContext);
58 service.setName("test");
59 PrototypeObjectFactory factory = new PrototypeObjectFactory(Object.class);
60 service.setComponent(new DefaultJavaComponent(factory));
61 service.setModel(new SedaModel());
62 service.getModel().setMuleContext(muleContext);
63 service.getModel().initialise();
64 }
65
66 @Override
67 protected Service getService()
68 {
69 return service;
70 }
71
72
73
74
75
76
77 @Test
78 public void testQueueConfiguration() throws Exception
79 {
80 int capacity = 345;
81
82 QueueManager queueManager = muleContext.getQueueManager();
83
84 QueueManager mockTransactionalQueueManager = mock(QueueManager.class);
85 when(mockTransactionalQueueManager.getQueueSession()).thenReturn(queueManager.getQueueSession());
86
87
88
89 muleContext.getRegistry().registerObject(MuleProperties.OBJECT_QUEUE_MANAGER,
90 mockTransactionalQueueManager);
91
92 ListableObjectStore<Serializable> objectStore =
93 muleContext.getRegistry().lookupObject(MuleProperties.OBJECT_STORE_DEFAULT_PERSISTENT_NAME);
94 service.setQueueProfile(new QueueProfile(capacity, objectStore));
95
96 try
97 {
98 muleContext.getRegistry().registerService(service);
99 }
100 catch (RegistrationException e)
101 {
102 if (e.getCause().getCause().getCause() instanceof AssertionFailedError)
103 {
104 fail("Queue configuration does not match service queue profile");
105 }
106 else
107 {
108 throw e;
109 }
110 }
111 }
112
113 @Test
114 public void testSedaModelEventTimeoutDefault() throws Exception
115 {
116 service.initialise();
117
118 assertNotNull(service.getQueueTimeout());
119 assertTrue(service.getQueueTimeout() != 0);
120 }
121
122
123
124
125 @Test
126 public void testDispatchToPausedService() throws Exception
127 {
128 service.initialise();
129 service.start();
130 service.pause();
131 service.process(MuleTestUtils.getTestEvent("test",
132 getTestInboundEndpoint(MessageExchangePattern.ONE_WAY), muleContext));
133
134
135 }
136
137
138
139
140 @Test
141 public void testMaxActiveThreadsEqualsOneWhenExhaustedActionWait() throws Exception
142 {
143 final Latch latch = new Latch();
144 service.setName("testMaxActiveThreadsEqualsOne");
145 ChainedThreadingProfile threadingProfile = (ChainedThreadingProfile) muleContext.getDefaultServiceThreadingProfile();
146 threadingProfile.setMaxThreadsActive(1);
147 threadingProfile.setThreadWaitTimeout(200);
148 threadingProfile.setPoolExhaustedAction(ThreadingProfile.WHEN_EXHAUSTED_WAIT);
149 service.setThreadingProfile(threadingProfile);
150 final SimpleCallableJavaComponent component = new SimpleCallableJavaComponent(new Callable()
151 {
152 @Override
153 public Object onCall(MuleEventContext eventContext) throws Exception
154 {
155 latch.countDown();
156 return null;
157 }
158 });
159 component.setMuleContext(muleContext);
160 service.setComponent(component);
161 muleContext.getRegistry().registerService(service);
162
163 service.process(MuleTestUtils.getTestEvent("test", service, muleContext));
164 assertTrue(latch.await(200, TimeUnit.MILLISECONDS));
165
166
167 }
168
169
170
171
172 @Test
173 public void testDoThreadingFalse() throws Exception
174 {
175 final Latch latch = new Latch();
176 final String serviceName = "testDoThreadingFalse";
177
178 service.setName(serviceName);
179 ChainedThreadingProfile threadingProfile = (ChainedThreadingProfile) muleContext.getDefaultServiceThreadingProfile();
180 threadingProfile.setDoThreading(false);
181 service.setThreadingProfile(threadingProfile);
182 final Thread mainThread = Thread.currentThread();
183
184 final SimpleCallableJavaComponent component = new SimpleCallableJavaComponent(new Callable()
185 {
186 @Override
187 public Object onCall(MuleEventContext eventContext) throws Exception
188 {
189 assertEquals(mainThread, Thread.currentThread());
190 latch.countDown();
191 return null;
192 }
193 });
194 component.setMuleContext(muleContext);
195 service.setComponent(component);
196 muleContext.getRegistry().registerService(service);
197
198 service.process(MuleTestUtils.getTestEvent("test",
199 getTestInboundEndpoint(MessageExchangePattern.ONE_WAY), muleContext));
200
201 assertTrue(latch.await(200, TimeUnit.MILLISECONDS));
202 }
203
204
205
206
207 @Test
208 public void testDoThreadingTrue() throws Exception
209 {
210 final Latch latch = new Latch();
211 final String serviceName = "testDoThreadingFalse";
212
213 service.setName(serviceName);
214 ChainedThreadingProfile threadingProfile = (ChainedThreadingProfile) muleContext.getDefaultServiceThreadingProfile();
215 threadingProfile.setDoThreading(true);
216 service.setThreadingProfile(threadingProfile);
217 final SimpleCallableJavaComponent component = new SimpleCallableJavaComponent(new Callable()
218 {
219 @Override
220 public Object onCall(MuleEventContext eventContext) throws Exception
221 {
222 System.out.println(Thread.currentThread().getName());
223 assertTrue(Thread.currentThread().getName().startsWith(serviceName));
224 latch.countDown();
225 return null;
226 }
227 });
228 component.setMuleContext(muleContext);
229 service.setComponent(component);
230 muleContext.getRegistry().registerService(service);
231
232 service.process(MuleTestUtils.getTestEvent("test",
233 getTestInboundEndpoint(MessageExchangePattern.ONE_WAY), muleContext));
234
235 assertTrue(latch.await(200, TimeUnit.MILLISECONDS));
236 }
237 }