View Javadoc

1   /*
2    * $Id: AsyncRequestReplyRequesterTestCase.java 22671 2011-08-15 20:32:55Z pablo.lagreca $
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.routing.requestreply;
12  
13  import org.mule.MessageExchangePattern;
14  import org.mule.api.MuleContext;
15  import org.mule.api.MuleEvent;
16  import org.mule.api.MuleException;
17  import org.mule.api.config.MuleProperties;
18  import org.mule.api.context.WorkManager;
19  import org.mule.api.context.WorkManagerSource;
20  import org.mule.api.endpoint.InboundEndpoint;
21  import org.mule.api.lifecycle.InitialisationException;
22  import org.mule.api.processor.RequestReplyRequesterMessageProcessor;
23  import org.mule.api.routing.ResponseTimeoutException;
24  import org.mule.api.service.Service;
25  import org.mule.processor.LaxAsyncInterceptingMessageProcessor;
26  import org.mule.tck.SensingNullMessageProcessor;
27  import org.mule.tck.junit4.AbstractMuleContextTestCase;
28  
29  import java.beans.ExceptionListener;
30  import java.util.concurrent.atomic.AtomicInteger;
31  
32  import javax.resource.spi.work.Work;
33  
34  import org.junit.Test;
35  import org.mule.util.store.MuleObjectStoreManager;
36  
37  import static org.junit.Assert.assertEquals;
38  import static org.junit.Assert.fail;
39  
40  public class AsyncRequestReplyRequesterTestCase extends AbstractMuleContextTestCase
41      implements ExceptionListener
42  {
43  
44      TestAsyncRequestReplyRequester asyncReplyMP;
45  
46      @Override
47      protected void doSetUp() throws Exception
48      {
49          super.doSetUp();
50          muleContext.getRegistry().registerObject(MuleProperties.OBJECT_STORE_MANAGER, new MuleObjectStoreManager());
51      }
52  
53  
54      @Override
55      protected void doTearDown() throws Exception
56      {
57          if (asyncReplyMP != null)
58          {
59              asyncReplyMP.stop();
60              asyncReplyMP.dispose();
61          }
62          super.doTearDown();
63      }
64  
65      @Test
66      public void testSingleEventNoTimeout() throws Exception
67      {
68          asyncReplyMP = new TestAsyncRequestReplyRequester(muleContext);
69          SensingNullMessageProcessor target = getSensingNullMessageProcessor();
70  
71          asyncReplyMP.setListener(target);
72          asyncReplyMP.setReplySource(target.getMessageSource());
73  
74          MuleEvent event = getTestEvent(TEST_MESSAGE, getTestService());
75  
76          MuleEvent resultEvent = asyncReplyMP.process(event);
77  
78          // Can't assert same because we copy event when we receive async reply
79          assertEquals(event.getMessageAsString(), resultEvent.getMessageAsString());
80          assertEquals(event.getMessage().getUniqueId(), resultEvent.getMessage().getUniqueId());
81      }
82  
83      @Test
84      public void testSingleEventNoTimeoutAsync() throws Exception
85      {
86          asyncReplyMP = new TestAsyncRequestReplyRequester(muleContext);
87          SensingNullMessageProcessor target = getSensingNullMessageProcessor();
88          LaxAsyncInterceptingMessageProcessor asyncMP = new LaxAsyncInterceptingMessageProcessor(
89              new WorkManagerSource()
90              {
91                  public WorkManager getWorkManager() throws MuleException
92                  {
93                      return muleContext.getWorkManager();
94                  }
95              }
96          );
97  
98          asyncMP.setListener(target);
99          asyncReplyMP.setListener(asyncMP);
100         asyncReplyMP.setReplySource(target.getMessageSource());
101 
102         MuleEvent event = getTestEvent(TEST_MESSAGE, getTestService(),
103             getTestInboundEndpoint(MessageExchangePattern.ONE_WAY));
104 
105         MuleEvent resultEvent = asyncReplyMP.process(event);
106 
107         // Can't assert same because we copy event for async and also on async reply currently
108         assertEquals(event.getMessageAsString(), resultEvent.getMessageAsString());
109         assertEquals(event.getMessage().getUniqueId(), resultEvent.getMessage().getUniqueId());
110     }
111 
112     @Test
113     public void testSingleEventTimeout() throws Exception
114     {
115         asyncReplyMP = new TestAsyncRequestReplyRequester(muleContext);
116         asyncReplyMP.setTimeout(1);
117         SensingNullMessageProcessor target = getSensingNullMessageProcessor();
118         target.setWaitTime(3000);
119         LaxAsyncInterceptingMessageProcessor asyncMP = new LaxAsyncInterceptingMessageProcessor(
120             new WorkManagerSource()
121             {
122 
123                 public WorkManager getWorkManager() throws MuleException
124                 {
125                     return muleContext.getWorkManager();
126                 }
127             }
128         );
129 
130         asyncMP.setListener(target);
131         asyncReplyMP.setListener(asyncMP);
132         asyncReplyMP.setReplySource(target.getMessageSource());
133 
134         MuleEvent event = getTestEvent(TEST_MESSAGE, getTestService(),
135             getTestInboundEndpoint(MessageExchangePattern.ONE_WAY));
136 
137         try
138         {
139             asyncReplyMP.process(event);
140             fail("ResponseTimeoutException expected");
141         }
142         catch (Exception e)
143         {
144             assertEquals(ResponseTimeoutException.class, e.getClass());
145         }
146     }
147 
148     @Test
149     public void testMultiple() throws Exception
150     {
151         asyncReplyMP = new TestAsyncRequestReplyRequester(muleContext);
152         SensingNullMessageProcessor target = getSensingNullMessageProcessor();
153         target.setWaitTime(50);
154         LaxAsyncInterceptingMessageProcessor asyncMP = new LaxAsyncInterceptingMessageProcessor(
155             new WorkManagerSource()
156             {
157 
158                 public WorkManager getWorkManager() throws MuleException
159                 {
160                     return muleContext.getWorkManager();
161                 }
162             }
163         );
164 
165         asyncMP.setListener(target);
166         asyncReplyMP.setListener(asyncMP);
167         asyncReplyMP.setReplySource(target.getMessageSource());
168 
169         final InboundEndpoint inboundEndpoint = getTestInboundEndpoint(MessageExchangePattern.ONE_WAY);
170         final Service service = getTestService();
171 
172         final AtomicInteger count = new AtomicInteger();
173         for (int i = 0; i < 500; i++)
174         {
175             muleContext.getWorkManager().scheduleWork(new Work()
176             {
177                 public void run()
178                 {
179                     MuleEvent event;
180                     try
181                     {
182                         event = getTestEvent(TEST_MESSAGE, service, inboundEndpoint);
183                         MuleEvent resultEvent = asyncReplyMP.process(event);
184 
185                         // Can't assert same because we copy event for async currently
186                         assertEquals(event.getMessageAsString(), resultEvent.getMessageAsString());
187                         assertEquals(event.getMessage().getUniqueId(), resultEvent.getMessage().getUniqueId());
188                         count.incrementAndGet();
189                         logger.debug("Finished " + count.get());
190                     }
191                     catch (Exception e)
192                     {
193                         throw new RuntimeException(e);
194                     }
195                 }
196 
197                 public void release()
198                 {
199                     // nop
200                 }
201             });
202         }
203         while (count.get() < 500)
204         {
205             Thread.sleep(10);
206         }
207     }
208 
209     public void exceptionThrown(Exception e)
210     {
211         e.printStackTrace();
212         fail(e.getMessage());
213     }
214 
215     class TestAsyncRequestReplyRequester extends AbstractAsyncRequestReplyRequester
216     {
217         TestAsyncRequestReplyRequester(MuleContext muleContext) throws MuleException
218         {
219             setMuleContext(muleContext);
220             initialise();
221             start();
222         }
223     }
224 }