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

« back to all changes in this revision

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