View Javadoc

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