View Javadoc

1   /*
2    * $Id: HttpResponseTimeoutTestCase.java 22735 2011-08-25 16:02:35Z 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.transport.http.functional;
12  
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertNotNull;
15  import static org.junit.Assert.assertNull;
16  import static org.junit.Assert.assertTrue;
17  import static org.junit.Assert.fail;
18  
19  import org.mule.api.MuleMessage;
20  import org.mule.api.endpoint.InboundEndpoint;
21  import org.mule.api.transport.DispatchException;
22  import org.mule.module.client.MuleClient;
23  import org.mule.tck.AbstractServiceAndFlowTestCase;
24  import org.mule.tck.junit4.rule.DynamicPort;
25  import org.mule.util.ExceptionUtils;
26  
27  import java.net.SocketTimeoutException;
28  import java.util.Arrays;
29  import java.util.Collection;
30  import java.util.Date;
31  
32  import org.junit.Rule;
33  import org.junit.Test;
34  import org.junit.runners.Parameterized.Parameters;
35  
36  /**
37   * See MULE-4491 "Http outbound endpoint does not use responseTimeout attribute"
38   * See MULE-4743 "MuleClient.send() timeout is not respected with http transport"
39   */
40  public class HttpResponseTimeoutTestCase extends AbstractServiceAndFlowTestCase
41  {
42  
43      protected static String PAYLOAD = "Eugene";
44      protected static int DEFAULT_RESPONSE_TIMEOUT = 2000;
45      protected MuleClient muleClient;
46  
47      @Rule
48      public DynamicPort dynamicPort = new DynamicPort("port1");
49  
50      public HttpResponseTimeoutTestCase(ConfigVariant variant, String configResources)
51      {
52          super(variant, configResources);
53      }
54  
55      @Parameters
56      public static Collection<Object[]> parameters()
57      {
58          return Arrays.asList(new Object[][]{
59              {ConfigVariant.SERVICE, "http-response-timeout-config-service.xml"},
60              {ConfigVariant.FLOW, "http-response-timeout-config-flow.xml"}
61          });
62      }      
63          
64      protected String getPayload()
65      {
66          return PAYLOAD;
67      }
68  
69      protected String getProcessedPayload()
70      {
71          return getPayload() + " processed";
72      }
73      
74      @Override
75      protected void doSetUp() throws Exception
76      {
77          super.doSetUp();
78          muleClient = new MuleClient(muleContext);
79      }
80  
81      @Test
82      public void testDecreaseOutboundEndpointResponseTimeout() throws Exception
83      {
84          Date beforeCall = new Date();
85          MuleMessage result = muleClient.send("vm://decreaseTimeoutRequest", getPayload(), null);
86          assertNotNull(result);
87          assertNotNull(result.getExceptionPayload());
88          assertEquals(DispatchException.class, result.getExceptionPayload().getException().getClass());
89  
90          // If everything is good the connection will timeout after 5s and throw an
91          // exception. The original unprocessed message is returned in the response
92          // message.
93          Date afterCall = new Date();
94          assertTrue((afterCall.getTime() - beforeCall.getTime()) < DEFAULT_RESPONSE_TIMEOUT);
95      }
96  
97      @Test
98      public void testIncreaseOutboundEndpointResponseTimeout() throws Exception
99      {
100         Date beforeCall = new Date();
101         MuleMessage message = muleClient.send("vm://increaseTimeoutRequest", getPayload(), null);
102         Date afterCall = new Date();
103 
104         // If everything is good the connection will not timeout and the processed
105         // message will be returned as the response. There is no exception payload.
106         assertNotNull(message);
107         assertNull(message.getExceptionPayload());
108         assertNotNull(getPayload(), message.getPayloadAsString());
109         assertTrue((afterCall.getTime() - beforeCall.getTime()) > DEFAULT_RESPONSE_TIMEOUT);
110     }
111 
112     @Test
113     public void testDecreaseMuleClientSendResponseTimeout() throws Exception
114     {
115         Date beforeCall = new Date();
116         Date afterCall;
117 
118         try
119         {
120             muleClient.send(((InboundEndpoint) muleClient.getMuleContext().getRegistry().lookupObject("inDelayService")).getAddress(), getPayload(), null, 1000);
121             fail("SocketTimeoutException expected");
122         }
123         catch (Exception e)
124         {
125             assertTrue(ExceptionUtils.getRootCause(e) instanceof SocketTimeoutException);
126         }
127 
128         // Exception should have been thrown after timeout specified which is
129         // less than default.
130         afterCall = new Date();
131         assertTrue((afterCall.getTime() - beforeCall.getTime()) < DEFAULT_RESPONSE_TIMEOUT);
132     }
133 
134     @Test
135     public void testIncreaseMuleClientSendResponseTimeout() throws Exception
136     {
137         Date beforeCall = new Date();
138         MuleMessage message = muleClient.send(((InboundEndpoint) muleClient.getMuleContext().getRegistry().lookupObject("inDelayService")).getAddress(), getPayload(), null, 3000);
139         Date afterCall = new Date();
140 
141         // If everything is good the we'll have received a result after more than 10s
142         assertNotNull(message);
143         assertNull(message.getExceptionPayload());
144         assertNotNull(getProcessedPayload(), message.getPayloadAsString());
145         assertTrue((afterCall.getTime() - beforeCall.getTime()) > DEFAULT_RESPONSE_TIMEOUT);
146     }
147 
148 }