1
package org.jboss.test.remoting.transport.socket.timeout;
3
import java.lang.reflect.Field;
4
import java.net.InetAddress;
5
import java.util.HashMap;
6
import java.util.Iterator;
10
import javax.management.MBeanServer;
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;
30
* See javadoc for PerInvocationTimeoutTestRoot.
32
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
33
* @version $Revision: 2939 $
35
* Copyright Feb 6, 2007
38
public class SocketPerInvocationTimeoutTestCase extends PerInvocationTimeoutTestRoot
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.
45
public void testTimeoutReset() throws Throwable
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());
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());
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());
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
75
public void testNewSocketTimeout() throws Throwable
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);
86
connector.addInvocationHandler("test", new TestHandler());
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.
97
// Give the client a chance to connect to the server, then
98
// disable the server.
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();
110
ServerThread t = (ServerThread) it.next();
114
ssi.setMaxPoolSize(0);
115
log.info("server is disabled");
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);
131
Object response = client.invoke("test 1");
132
assertEquals("test 1", response);
134
class BooleanHolder {public boolean value;}
135
final BooleanHolder timedOut = new BooleanHolder();
136
timedOut.value = false;
144
// Wait for the server to be disabled.
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");
156
timedOut.value = true;
157
log.info("time out", e);
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.
167
log.info("testing timeout");
168
assertTrue(timedOut.value);
172
log.info(getName() + " PASSES");
176
protected String getTransport()
181
protected List getPool(ClientInvoker clientInvoker) throws Exception
184
if (clientInvoker instanceof MicroSocketClientInvoker)
186
Field field = MicroSocketClientInvoker.class.getDeclaredField("pool");
187
field.setAccessible(true);
188
pool = (List) field.get(clientInvoker);
194
public class TestHandler implements ServerInvocationHandler
196
public void setMBeanServer(MBeanServer server) {}
197
public void setInvoker(ServerInvoker invoker) {}
199
public Object invoke(InvocationRequest invocation) throws Throwable
201
return invocation.getParameter();
204
public void addListener(InvokerCallbackHandler callbackHandler) {}
205
public void removeListener(InvokerCallbackHandler callbackHandler) {}