View Javadoc

1   /*
2    * $Id: EndpointBridgingTestCase.java 10662 2008-02-01 13:10:14Z romikk $
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.test.integration.service;
12  
13  import org.mule.api.MuleException;
14  import org.mule.api.service.Service;
15  import org.mule.tck.FunctionalTestCase;
16  import org.mule.util.queue.FilePersistenceStrategy;
17  import org.mule.util.queue.Queue;
18  import org.mule.util.queue.QueueSession;
19  import org.mule.util.queue.TransactionalQueueManager;
20  import org.mule.util.xa.ResourceManagerSystemException;
21  
22  public class ServiceInFlightMessagesTestCase extends FunctionalTestCase
23  {
24      private static final int WAIT_TIME_MILLIS = 500;
25      protected static final int NUM_MESSAGES = 500;
26  
27      @Override
28      protected String getConfigResources()
29      {
30          return "org/mule/test/integration/service/service-inflight-messages.xml";
31      }
32  
33      public void testInFlightMessages() throws Exception
34      {
35          Service service = muleContext.getRegistry().lookupService("TestService");
36          populateSedaQueue(service, NUM_MESSAGES);
37  
38          muleContext.stop();
39  
40          assertNoLostMessages(NUM_MESSAGES, service);
41          // Seda queue is empty because queue is not persistent and therefore is
42          // emptied when service is stopped
43          assertSedaQueueEmpty(service);
44      }
45  
46      public void testInFlightMessagesPausedService() throws Exception
47      {
48          Service service = muleContext.getRegistry().lookupService("PausedTestService");
49          populateSedaQueue(service, NUM_MESSAGES);
50  
51          muleContext.stop();
52  
53          // All message were lost so both queues are empty.
54          assertSedaQueueEmpty(service);
55  
56          // TODO Enable the following assertion once MULE-4072 is fixed
57          // assertOutboundVMQueueEmpty();
58      }
59  
60      public void testInFlightStopPersistentMessages() throws Exception
61      {
62          Service service = muleContext.getRegistry().lookupService("TestPersistentQueueService");
63          populateSedaQueue(service, NUM_MESSAGES);
64  
65          Thread.sleep(WAIT_TIME_MILLIS);
66          muleContext.stop();
67          Thread.sleep(WAIT_TIME_MILLIS);
68  
69  
70          assertNoLostMessages(NUM_MESSAGES, service);
71          // Persistent queue is being used so seda queue is not emptied when service
72          // is stopped
73          assertSedaQueueNotEmpty(service);
74  
75          // Start, process some messages, stop and make sure no messages get lost.
76          muleContext.start();
77          Thread.sleep(WAIT_TIME_MILLIS);
78          muleContext.stop();
79  
80          assertNoLostMessages(NUM_MESSAGES, service);
81  
82          // Let mule finish up with the rest of the messages until seda queue is empty
83          muleContext.start();
84          Thread.sleep(WAIT_TIME_MILLIS * 8);
85          muleContext.stop();
86  
87          assertNoLostMessages(NUM_MESSAGES, service);
88          assertSedaQueueEmpty(service);
89      }
90  
91      public void testInFlightStopPersistentMessagesPausedService() throws Exception
92      {
93          Service service = muleContext.getRegistry().lookupService("PausedTestPersistentQueueService");
94          populateSedaQueue(service, NUM_MESSAGES);
95  
96          muleContext.stop();
97  
98          // Paused service does not process messages before or during stop().
99          // TODO Enable the following assertion once MULE-4072 is fixed
100         // assertOutboundVMQueueEmpty();
101         assertNoLostMessages(NUM_MESSAGES, service);
102 
103         // Start, process some messages, stop and make sure no messages get lost.
104         muleContext.start();
105         service.resume();
106         Thread.sleep(WAIT_TIME_MILLIS);
107         muleContext.stop();
108 
109         Thread.sleep(WAIT_TIME_MILLIS);
110 
111         assertNoLostMessages(NUM_MESSAGES, service);
112 
113         // Let mule finish up with the rest of the messages until seda queue is empty
114         muleContext.start();
115         Thread.sleep(WAIT_TIME_MILLIS * 14);
116         muleContext.stop();
117 
118         assertNoLostMessages(NUM_MESSAGES, service);
119         assertSedaQueueEmpty(service);
120     }
121 
122     public void testInFlightDisposePersistentMessages() throws Exception
123     {
124         Service service = muleContext.getRegistry().lookupService("TestPersistentQueueService");
125         populateSedaQueue(service, NUM_MESSAGES);
126 
127         muleContext.stop();
128         assertNoLostMessages(NUM_MESSAGES, service);
129 
130         // Dispose and restart Mule and let it run for a short while
131         muleContext.dispose();
132         muleContext = createMuleContext();
133         muleContext.start();
134         Thread.sleep(WAIT_TIME_MILLIS);
135         muleContext.stop();
136 
137         assertNoLostMessages(NUM_MESSAGES, service);
138 
139         // Let mule finish up with the rest of the messages until seda queue is empty
140         muleContext.start();
141         Thread.sleep(WAIT_TIME_MILLIS * 8);
142         muleContext.stop();
143 
144         assertNoLostMessages(NUM_MESSAGES, service);
145         assertSedaQueueEmpty(service);
146     }
147 
148     protected void populateSedaQueue(Service service, int numMessages) throws MuleException, Exception
149     {
150         for (int i = 0; i < numMessages; i++)
151         {
152             service.dispatchEvent(getTestEvent("test", service, getTestInboundEndpoint("test://test")));
153         }
154     }
155 
156     /**
157      * After each run the following should total 500 events: 1) Event still in SEDA
158      * queue 2) Events dispatched to outbound vm endpooint 3) Events that were unable
159      * to be sent to stopped service and raised exceptions
160      */
161     private synchronized void assertNoLostMessages(int numMessages, Service service)
162         throws ResourceManagerSystemException
163     {
164         QueueSession queueSession = getTestQueueSession();
165         
166         int outQueueSize = queueSession.getQueue("out").size();
167         
168         String serviceName = service.getName() + ".service";
169         int serviceQueueSize = queueSession.getQueue(serviceName).size();
170         
171         logger.info("SEDA Queue: " + outQueueSize + ", Outbound endpoint vm queue: " + serviceQueueSize);
172         assertEquals(numMessages, outQueueSize + serviceQueueSize);
173     }
174 
175     protected synchronized void assertSedaQueueEmpty(Service service) throws ResourceManagerSystemException
176     {
177         QueueSession queueSession = getTestQueueSession();
178         assertEquals(0, queueSession.getQueue(service.getName() + ".service").size());
179     }
180 
181     protected synchronized void assertSedaQueueNotEmpty(Service service)
182         throws ResourceManagerSystemException
183     {
184         QueueSession queueSession = getTestQueueSession();
185         final int size = queueSession.getQueue(service.getName() + ".service").size();
186         assertTrue(String.format("Seda queue for service '%s' is empty", service.getName()), size > 0);
187     }
188 
189     protected synchronized void assertOutboundVMQueueEmpty() throws ResourceManagerSystemException
190     {
191         QueueSession queueSession = getTestQueueSession();
192         assertEquals(0, queueSession.getQueue("out").size());
193     }
194 
195     protected synchronized void assertOutboundVMQueueNotEmpty() throws ResourceManagerSystemException
196     {
197         QueueSession queueSession = getTestQueueSession();
198         final Queue queue = queueSession.getQueue("out");
199         assertTrue(String.format("Qeueu '%s' is empty", queue.getName()), queue.size() > 0);
200     }
201 
202     protected QueueSession getTestQueueSession() throws ResourceManagerSystemException
203     {
204         TransactionalQueueManager tqm = new TransactionalQueueManager();
205         FilePersistenceStrategy fps = new FilePersistenceStrategy();
206         fps.setMuleContext(muleContext);
207         tqm.setPersistenceStrategy(fps);
208         tqm.start();
209         QueueSession queueSession = tqm.getQueueSession();
210         return queueSession;
211     }
212 }