View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.model.seda;
8   
9   import org.mule.MessageExchangePattern;
10  import org.mule.api.MuleEventContext;
11  import org.mule.api.config.MuleProperties;
12  import org.mule.api.config.ThreadingProfile;
13  import org.mule.api.lifecycle.Callable;
14  import org.mule.api.registry.RegistrationException;
15  import org.mule.api.service.Service;
16  import org.mule.component.DefaultJavaComponent;
17  import org.mule.component.SimpleCallableJavaComponent;
18  import org.mule.config.ChainedThreadingProfile;
19  import org.mule.config.QueueProfile;
20  import org.mule.model.AbstractServiceTestCase;
21  import org.mule.object.PrototypeObjectFactory;
22  import org.mule.tck.MuleTestUtils;
23  import org.mule.util.concurrent.Latch;
24  import org.mule.util.queue.QueueManager;
25  
26  import edu.emory.mathcs.backport.java.util.concurrent.TimeUnit;
27  import junit.framework.AssertionFailedError;
28  import org.junit.Test;
29  
30  import static org.junit.Assert.assertEquals;
31  import static org.junit.Assert.assertNotNull;
32  import static org.junit.Assert.assertTrue;
33  import static org.junit.Assert.fail;
34  import static org.mockito.Mockito.mock;
35  import static org.mockito.Mockito.when;
36  
37  public class SedaServiceTestCase extends AbstractServiceTestCase
38  {
39      private SedaService service;
40  
41      public SedaServiceTestCase()
42      {
43          setStartContext(true);
44      }
45  
46      @Override
47      protected void doSetUp() throws Exception
48      {
49          service = new SedaService(muleContext);
50          service.setName("test");
51          PrototypeObjectFactory factory = new PrototypeObjectFactory(Object.class);
52          service.setComponent(new DefaultJavaComponent(factory));
53          service.setModel(new SedaModel());
54          service.getModel().setMuleContext(muleContext);
55          service.getModel().initialise();
56      }
57  
58      @Override
59      protected Service getService()
60      {
61          return service;
62      }
63  
64      /**
65       * ENSURE THAT: 1) The queueProfile set on the SedaService is used to configure
66       * the queue that is used. 2) The queue used by the SedaService has the correct
67       * name.
68       */
69      @Test
70      public void testQueueConfiguration() throws Exception
71      {
72          boolean persistent = true;
73          int capacity = 345;
74  
75          QueueManager queueManager = muleContext.getQueueManager();
76  
77          QueueManager mockTransactionalQueueManager = mock(QueueManager.class);
78          when(mockTransactionalQueueManager.getQueueSession()).thenReturn(queueManager.getQueueSession());
79  
80          // Replace queueManager instance with mock via registry as it cannot be set
81          // once muleContext is initialized.
82          muleContext.getRegistry().registerObject(MuleProperties.OBJECT_QUEUE_MANAGER,
83              mockTransactionalQueueManager);
84  
85          service.setQueueProfile(new QueueProfile(capacity, persistent));
86  
87          try
88          {
89              muleContext.getRegistry().registerService(service);
90          }
91          catch (RegistrationException e)
92          {
93              if (e.getCause().getCause().getCause() instanceof AssertionFailedError)
94              {
95                  fail("Queue configuration does not match service queue profile");
96              }
97              else
98              {
99                  throw e;
100             }
101         }
102     }
103 
104     @Test
105     public void testSedaModelEventTimeoutDefault() throws Exception
106     {
107         service.initialise();
108 
109         assertNotNull(service.getQueueTimeout());
110         assertTrue(service.getQueueTimeout() != 0);
111     }
112 
113     /**
114      * SEE MULE-3684
115      */
116     @Test
117     public void testDispatchToPausedService() throws Exception
118     {
119         service.initialise();
120         service.start();
121         service.pause();
122         service.process(MuleTestUtils.getTestEvent("test",
123             getTestInboundEndpoint(MessageExchangePattern.ONE_WAY), muleContext));
124 
125         // This test will timeout and fail if dispatch() blocks
126     }
127 
128     /**
129      * SEE MULE-3974
130      */
131     @Test
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.process(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      * SEE MULE-3975
163      */
164     @Test
165     public void testDoThreadingFalse() throws Exception
166     {
167         final Latch latch = new Latch();
168         final String serviceName = "testDoThreadingFalse";
169 
170         service.setName(serviceName);
171         ChainedThreadingProfile threadingProfile = (ChainedThreadingProfile) muleContext.getDefaultServiceThreadingProfile();
172         threadingProfile.setDoThreading(false);
173         service.setThreadingProfile(threadingProfile);
174         final Thread mainThread = Thread.currentThread();
175 
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.process(MuleTestUtils.getTestEvent("test",
190             getTestInboundEndpoint(MessageExchangePattern.ONE_WAY), muleContext));
191 
192         assertTrue(latch.await(200, TimeUnit.MILLISECONDS));
193     }
194 
195     /**
196      * SEE MULE-3975
197      */
198     @Test
199     public void testDoThreadingTrue() throws Exception
200     {
201         final Latch latch = new Latch();
202         final String serviceName = "testDoThreadingFalse";
203 
204         service.setName(serviceName);
205         ChainedThreadingProfile threadingProfile = (ChainedThreadingProfile) muleContext.getDefaultServiceThreadingProfile();
206         threadingProfile.setDoThreading(true);
207         service.setThreadingProfile(threadingProfile);
208         final SimpleCallableJavaComponent component = new SimpleCallableJavaComponent(new Callable()
209         {
210             public Object onCall(MuleEventContext eventContext) throws Exception
211             {
212                 assertTrue(Thread.currentThread().getName().startsWith("seda." + serviceName));
213                 latch.countDown();
214                 return null;
215             }
216         });
217         component.setMuleContext(muleContext);
218         service.setComponent(component);
219         muleContext.getRegistry().registerService(service);
220 
221         service.process(MuleTestUtils.getTestEvent("test",
222             getTestInboundEndpoint(MessageExchangePattern.ONE_WAY), muleContext));
223 
224         assertTrue(latch.await(200, TimeUnit.MILLISECONDS));
225     }
226 }