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