~ubuntu-branches/ubuntu/raring/libjboss-remoting-java/raring

« back to all changes in this revision

Viewing changes to src/tests/org/jboss/test/remoting/transport/socket/ssl/timeout/SSLSocketPerInvocationTimeoutTestCase.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: package-import@ubuntu.com-20110909140103-hqokx61534tas9rg
Tags: 2.5.3.SP1-1
* Newer but not newest upstream release. Do not build samples.
* Change debian/watch to upstream's svn repo.
* Add patch to fix compile error caused by tomcat update.
  (Closes: #628303)
* Switch to source format 3.0.
* Switch to debhelper level 7.
* Remove useless Depends.
* Update Standards-Version: 3.9.2.
* Update README.source.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.jboss.test.remoting.transport.socket.ssl.timeout;
 
2
 
 
3
import java.lang.reflect.Field;
 
4
import java.net.InetAddress;
 
5
import java.util.HashMap;
 
6
import java.util.Iterator;
 
7
import java.util.List;
 
8
import java.util.Set;
 
9
 
 
10
import org.jboss.remoting.Client;
 
11
import org.jboss.remoting.InvokerLocator;
 
12
import org.jboss.remoting.ServerInvoker;
 
13
import org.jboss.remoting.transport.ClientInvoker;
 
14
import org.jboss.remoting.transport.Connector;
 
15
import org.jboss.remoting.transport.PortUtil;
 
16
import org.jboss.remoting.transport.socket.LRUPool;
 
17
import org.jboss.remoting.transport.socket.MicroSocketClientInvoker;
 
18
import org.jboss.remoting.transport.socket.ServerThread;
 
19
import org.jboss.remoting.transport.socket.SocketServerInvoker;
 
20
import org.jboss.remoting.transport.socket.SocketWrapper;
 
21
import org.jboss.test.remoting.timeout.SSLPerInvocationTimeoutTestRoot;
 
22
 
 
23
 
 
24
/**
 
25
 * See javadoc for PerInvocationTimeoutTestRoot.
 
26
 *   
 
27
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
 
28
 * @version $Revision: 2936 $
 
29
 * <p>
 
30
 * Copyright Feb 6, 2007
 
31
 * </p>
 
32
 */
 
33
public class SSLSocketPerInvocationTimeoutTestCase extends SSLPerInvocationTimeoutTestRoot
 
34
{
 
35
   /**
 
36
    * This test verifies that the timeout for a socket wrapper gets reset after
 
37
    * an invocation with an invocation specific timeout has been executed.
 
38
    */
 
39
   public void testTimeoutReset() throws Throwable
 
40
   {
 
41
      log.info("entering " + getName());
 
42
      ClientInvoker invoker = client.getInvoker();
 
43
      assertTrue(invoker instanceof MicroSocketClientInvoker);
 
44
      Field field = MicroSocketClientInvoker.class.getDeclaredField("pool");
 
45
      field.setAccessible(true);
 
46
      List pool = (List) field.get(invoker);
 
47
      assertEquals(0, pool.size());
 
48
      
 
49
      Object response = client.invoke(NO_WAIT);
 
50
      assertEquals(NO_WAIT, response);
 
51
      assertEquals(1, pool.size());
 
52
      SocketWrapper socket = (SocketWrapper) pool.get(0);
 
53
      assertEquals(CONFIGURED_TIMEOUT, socket.getTimeout());
 
54
      
 
55
      HashMap metadata = new HashMap();
 
56
      metadata.put(ServerInvoker.TIMEOUT, "1000");
 
57
      response = client.invoke(NO_WAIT, metadata);
 
58
      assertEquals(NO_WAIT, response);
 
59
      assertEquals(1, pool.size());
 
60
      socket = (SocketWrapper) pool.get(0);
 
61
      assertEquals(CONFIGURED_TIMEOUT, socket.getTimeout());
 
62
   }
 
63
   
 
64
   
 
65
   // This test verifies that a temporary timeout value gets passed when a new
 
66
   // socket wrapper is created.  It is important that the socket wrapper gets
 
67
   // the temporary timeout value because creating object streams entails i/o
 
68
   // on the socket.
 
69
   public void testNewSocketTimeout() throws Throwable
 
70
   {
 
71
      log.info("entering " + getName());
 
72
      String host = InetAddress.getLocalHost().getHostAddress();
 
73
      int port = PortUtil.findFreePort(host);
 
74
      String locatorURI = getTransport() + "://" + host + ":" + port;
 
75
      InvokerLocator locator = new InvokerLocator(locatorURI);
 
76
      HashMap serverConfig = new HashMap();
 
77
      addServerConfig(serverConfig);
 
78
      final Connector connector = new Connector(locator, serverConfig);
 
79
      connector.create();
 
80
      connector.addInvocationHandler("test", new TestHandler());
 
81
      connector.start();
 
82
      
 
83
      // Disable the server so that the client will need to create a new socket, and
 
84
      // so that the creation of the socket wrapper will fail.
 
85
      new Thread()
 
86
      {
 
87
         public void run()
 
88
         {
 
89
            try
 
90
            {
 
91
               // Give the client a chance to connect to the server, then 
 
92
               // disable the server.
 
93
               Thread.sleep(2000);
 
94
               ServerInvoker si = connector.getServerInvoker();
 
95
               assertTrue(si instanceof SocketServerInvoker);
 
96
               SocketServerInvoker ssi = (SocketServerInvoker) si;
 
97
               Field field = SocketServerInvoker.class.getDeclaredField("clientpool");
 
98
               field.setAccessible(true);
 
99
               LRUPool clientpool = (LRUPool) field.get(ssi);
 
100
               Set threads = clientpool.getContents();
 
101
               Iterator it = threads.iterator();
 
102
               while (it.hasNext())
 
103
               {
 
104
                  ServerThread t = (ServerThread) it.next();
 
105
                  t.shutdown();
 
106
               }
 
107
 
 
108
               ssi.setMaxPoolSize(0);
 
109
               log.info("server is disabled");
 
110
            }
 
111
            catch (Exception e)
 
112
            {
 
113
               log.info(e);
 
114
            }
 
115
         }
 
116
      }.start();
 
117
 
 
118
      HashMap clientConfig = new HashMap();
 
119
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
120
      clientConfig.put(ServerInvoker.TIMEOUT, "20000");
 
121
      clientConfig.put(Client.ENABLE_LEASE, "true");
 
122
      addClientConfig(clientConfig);
 
123
      final Client client = new Client(locator, clientConfig);
 
124
      client.connect();
 
125
      Object response = client.invoke("test 1");
 
126
      assertEquals("test 1", response);
 
127
      log.info("first invocation succeeded");
 
128
      
 
129
      class BooleanHolder {public boolean value;}
 
130
      final BooleanHolder timedOut = new BooleanHolder();
 
131
      timedOut.value = false;
 
132
      
 
133
      new Thread()
 
134
      {
 
135
         public void run()
 
136
         {
 
137
            try
 
138
            {
 
139
               // Wait for the server to be disabled.
 
140
               Thread.sleep(4000);
 
141
               
 
142
               // This invocation will require the creation of a new socket, which
 
143
               // should promptly time out.
 
144
               HashMap metadata = new HashMap();
 
145
               metadata.put(ServerInvoker.TIMEOUT, "1000");
 
146
               client.invoke("test 3", metadata);
 
147
               fail("failed to time out");
 
148
            }
 
149
            catch (Throwable e)
 
150
            {
 
151
               timedOut.value = true;
 
152
               log.info("time out", e);
 
153
            }
 
154
         }
 
155
      }.start();
 
156
      
 
157
      // It should take the Client a little while for LeasePinger's attempts to contact
 
158
      // the server to time out.  Wait for 4 seconds after the call to Client.invoke()
 
159
      // and then verify that the Client has timed out according to the temporary timeout
 
160
      // value 1000 instead of the configureed value 20000.
 
161
      Thread.sleep(8000);
 
162
      assertTrue(timedOut.value);
 
163
      
 
164
      client.disconnect();
 
165
      connector.stop();
 
166
      log.info(getName() + " PASSES");
 
167
   }
 
168
   
 
169
   
 
170
   protected String getTransport()
 
171
   {
 
172
      return "sslsocket";
 
173
   }
 
174
}