View Javadoc

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