View Javadoc

1   /*
2    * $Id: SedaServiceTestCase.java 20534 2010-12-08 22:01:21Z dzapata $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.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.model.seda;
12  
13  import org.mule.MessageExchangePattern;
14  import org.mule.api.MuleEventContext;
15  import org.mule.api.config.MuleProperties;
16  import org.mule.api.config.ThreadingProfile;
17  import org.mule.api.lifecycle.Callable;
18  import org.mule.api.registry.RegistrationException;
19  import org.mule.api.service.Service;
20  import org.mule.component.DefaultJavaComponent;
21  import org.mule.component.SimpleCallableJavaComponent;
22  import org.mule.config.ChainedThreadingProfile;
23  import org.mule.config.QueueProfile;
24  import org.mule.model.AbstractServiceTestCase;
25  import org.mule.object.PrototypeObjectFactory;
26  import org.mule.tck.MuleTestUtils;
27  import org.mule.util.concurrent.Latch;
28  import org.mule.util.queue.QueueManager;
29  
30  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
31  import junit.framework.AssertionFailedError;
32  import static org.mockito.Mockito.mock;
33  import static org.mockito.Mockito.when;
34  
35  public class SedaServiceTestCase extends AbstractServiceTestCase
36  {
37      private SedaService service;
38      private Thread mainThread = Thread.currentThread();
39  
40      public SedaServiceTestCase()
41      {
42          setStartContext(true);
43      }
44  
45      @Override
46      protected void doSetUp() throws Exception
47      {
48          service = new SedaService(muleContext);
49          service.setName("test");
50          PrototypeObjectFactory factory = new PrototypeObjectFactory(Object.class);
51          service.setComponent(new DefaultJavaComponent(factory));
52          service.setModel(new SedaModel());
53          service.getModel().setMuleContext(muleContext);
54          service.getModel().initialise();
55      }
56  
57      @Override
58      protected Service getService()
59      {
60          return service;
61      }
62  
63      /**
64       * ENSURE THAT: 1) The queueProfile set on the SedaService is used to configure
65       * the queue that is used. 2) The queue used by the SedaService has the correct
66       * name.
67       */
68      public void testQueueConfiguration() throws Exception
69      {
70          boolean persistent = true;
71          int capacity = 345;
72  
73          QueueManager queueManager = muleContext.getQueueManager();
74  
75          QueueManager mockTransactionalQueueManager = mock(QueueManager.class);
76          when(mockTransactionalQueueManager.getQueueSession()).thenReturn(queueManager.getQueueSession());
77  
78          // Replace queueManager instance with mock via registry as it cannot be set
79          // once muleContext is initialized.
80          muleContext.getRegistry().registerObject(MuleProperties.OBJECT_QUEUE_MANAGER,
81              mockTransactionalQueueManager);
82  
83          service.setQueueProfile(new QueueProfile(capacity, persistent));
84  
85          try
86          {
87              muleContext.getRegistry().registerService(service);
88          }
89          catch (RegistrationException e)
90          {
91              if (e.getCause().getCause().getCause() instanceof AssertionFailedError)
92              {
93                  fail("Queue configuration does not match service queue profile");
94              }
95              else
96              {
97                  throw e;
98              }
99          }
100     }
101 
102     public void testSedaModelEventTimeoutDefault() throws Exception
103     {
104         service.initialise();
105 
106         assertNotNull(service.getQueueTimeout());
107         assertTrue(service.getQueueTimeout() != 0);
108     }
109 
110     /**
111      * SEE MULE-3684
112      * 
113      * @throws Exception
114      */
115     public void testDispatchToPausedService() throws Exception
116     {
117         service.initialise();
118         service.start();
119         service.pause();
120         service.dispatchEvent(MuleTestUtils.getTestEvent("test", 
121             getTestInboundEndpoint(MessageExchangePattern.ONE_WAY), muleContext));
122 
123         // This test will timeout and fail if dispatch() blocks
124 
125     }
126 
127     /**
128      * SEE MULE-3974
129      * 
130      * @throws Exception
131      */
132     public void testMaxActiveThreadsEqualsOneWhenExhaustedActionWait() throws Exception
133     {
134         final Latch latch = new Latch();
135         service.setName("testMaxActiveThreadsEqualsOne");
136         ChainedThreadingProfile threadingProfile = (ChainedThreadingProfile) muleContext.getDefaultServiceThreadingProfile();
137         threadingProfile.setMaxThreadsActive(1);
138         threadingProfile.setThreadWaitTimeout(200);
139         threadingProfile.setPoolExhaustedAction(ThreadingProfile.WHEN_EXHAUSTED_WAIT);
140         service.setThreadingProfile(threadingProfile);
141         final SimpleCallableJavaComponent component = new SimpleCallableJavaComponent(new Callable()
142         {
143 
144             public Object onCall(MuleEventContext eventContext) throws Exception
145             {
146                 latch.countDown();
147                 return null;
148             }
149         });
150         component.setMuleContext(muleContext);
151         service.setComponent(component);
152         muleContext.getRegistry().registerService(service);
153 
154         service.dispatchEvent(MuleTestUtils.getTestInboundEvent("test", service, muleContext));
155 
156         assertTrue(latch.await(200, TimeUnit.MILLISECONDS));
157 
158         // This test will fail with RejectedExcecutionException if dispatch() blocks
159 
160     }
161 
162     /**
163      * SEE MULE-3975
164      * 
165      * @throws Exception
166      */
167     public void testDoThreadingFalse() throws Exception
168     {
169         final Latch latch = new Latch();
170         final String serviceName = "testDoThreadingFalse";
171 
172         service.setName(serviceName);
173         ChainedThreadingProfile threadingProfile = (ChainedThreadingProfile) muleContext.getDefaultServiceThreadingProfile();
174         threadingProfile.setDoThreading(false);
175         service.setThreadingProfile(threadingProfile);
176         final SimpleCallableJavaComponent component = new SimpleCallableJavaComponent(new Callable()
177         {
178             public Object onCall(MuleEventContext eventContext) throws Exception
179             {
180                 assertEquals(mainThread, Thread.currentThread());
181                 latch.countDown();
182                 return null;
183             }
184         });
185         component.setMuleContext(muleContext);
186         service.setComponent(component);
187         muleContext.getRegistry().registerService(service);
188 
189         service.dispatchEvent(MuleTestUtils.getTestEvent("test", 
190             getTestInboundEndpoint(MessageExchangePattern.ONE_WAY), muleContext));
191 
192         assertTrue(latch.await(200, TimeUnit.MILLISECONDS));
193 
194     }
195 
196     /**
197      * SEE MULE-3975
198      * 
199      * @throws Exception
200      */
201     public void testDoThreadingTrue() throws Exception
202     {
203         final Latch latch = new Latch();
204         final String serviceName = "testDoThreadingFalse";
205 
206         service.setName(serviceName);
207         ChainedThreadingProfile threadingProfile = (ChainedThreadingProfile) muleContext.getDefaultServiceThreadingProfile();
208         threadingProfile.setDoThreading(true);
209         service.setThreadingProfile(threadingProfile);
210         final SimpleCallableJavaComponent component = new SimpleCallableJavaComponent(new Callable()
211         {
212             public Object onCall(MuleEventContext eventContext) throws Exception
213             {
214                 assertTrue(Thread.currentThread().getName().startsWith("seda." + serviceName));
215                 latch.countDown();
216                 return null;
217             }
218         });
219         component.setMuleContext(muleContext);
220         service.setComponent(component);
221         muleContext.getRegistry().registerService(service);
222 
223         service.dispatchEvent(MuleTestUtils.getTestEvent("test", 
224             getTestInboundEndpoint(MessageExchangePattern.ONE_WAY), muleContext));
225 
226         assertTrue(latch.await(200, TimeUnit.MILLISECONDS));
227 
228     }
229 
230 }