2
* JBoss, Home of Professional Open Source
3
* Copyright 2005, JBoss Inc., and individual contributors as indicated
4
* by the @authors tag. See the copyright.txt in the distribution for a
5
* full listing of individual contributors.
7
* This is free software; you can redistribute it and/or modify it
8
* under the terms of the GNU Lesser General Public License as
9
* published by the Free Software Foundation; either version 2.1 of
10
* the License, or (at your option) any later version.
12
* This software is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this software; if not, write to the Free
19
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
22
package org.jboss.test.remoting.transport.socket.interrupt;
24
import java.net.InetAddress;
25
import java.util.HashMap;
28
import javax.management.MBeanServer;
30
import junit.framework.TestCase;
32
import org.apache.log4j.ConsoleAppender;
33
import org.apache.log4j.Level;
34
import org.apache.log4j.Logger;
35
import org.apache.log4j.PatternLayout;
36
import org.jboss.logging.XLevel;
37
import org.jboss.remoting.CannotConnectException;
38
import org.jboss.remoting.Client;
39
import org.jboss.remoting.InvocationRequest;
40
import org.jboss.remoting.InvokerLocator;
41
import org.jboss.remoting.ServerInvocationHandler;
42
import org.jboss.remoting.ServerInvoker;
43
import org.jboss.remoting.callback.InvokerCallbackHandler;
44
import org.jboss.remoting.transport.Connector;
45
import org.jboss.remoting.transport.PortUtil;
46
import org.jboss.remoting.transport.socket.MicroSocketClientInvoker;
50
* Unit test for JBREM-955.
52
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
53
* @version $Revision: 1.1 $
55
* Copyright Apr 25, 2008
58
public class InterruptedExceptionTestCase extends TestCase
60
private static Logger log = Logger.getLogger(InterruptedExceptionTestCase.class);
62
private static boolean firstTime = true;
63
private static String FAST = "fast";
65
protected String host;
67
protected String locatorURI;
68
protected InvokerLocator serverLocator;
69
protected Connector connector;
70
protected TestInvocationHandler invocationHandler;
73
public void setUp() throws Exception
78
Logger.getLogger("org.jboss.remoting").setLevel(XLevel.INFO);
79
Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
80
String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
81
PatternLayout layout = new PatternLayout(pattern);
82
ConsoleAppender consoleAppender = new ConsoleAppender(layout);
83
Logger.getRootLogger().addAppender(consoleAppender);
88
public void tearDown()
93
public void testInterruptedException() throws Throwable
95
log.info("entering " + getName());
101
InvokerLocator clientLocator = new InvokerLocator(locatorURI);
102
HashMap clientConfig = new HashMap();
103
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
104
clientConfig.put(MicroSocketClientInvoker.MAX_POOL_SIZE_FLAG, "1");
105
clientConfig.put("numberOfCallRetries", "1");
106
addExtraClientConfig(clientConfig);
107
Client client = new Client(clientLocator, clientConfig);
109
log.info("client is connected");
112
assertEquals(FAST, client.invoke(FAST));
113
log.info("connection is good");
115
InvokerThread t1 = new InvokerThread(client, "abc");
116
InvokerThread t2 = new InvokerThread(client, "xyz");
118
// Start first invocation.
120
log.info("started first invocation");
122
// Give first invocation time to start.
125
// Start second invocation.
127
log.info("started second invocation");
129
// Give second invocation time to start.
132
// Interrupt second invocation as it waits for a semaphore.
134
log.info("interrupted second invocation");
136
// Wait until second invocation throws an exception.
142
// Verify exception is an InterruptedException wrapped in a RuntimeExceptio.
143
Throwable t = t2.throwable;
144
log.info("throwable: " + t);
145
assertTrue(t instanceof RuntimeException);
146
assertFalse(t instanceof CannotConnectException);
147
assertTrue(t.getCause() instanceof InterruptedException);
151
log.info(getName() + " PASSES");
155
protected String getTransport()
161
protected void addExtraClientConfig(Map config) {}
162
protected void addExtraServerConfig(Map config) {}
165
protected void setupServer() throws Exception
167
host = InetAddress.getLocalHost().getHostAddress();
168
port = PortUtil.findFreePort(host);
169
locatorURI = getTransport() + "://" + host + ":" + port;
170
serverLocator = new InvokerLocator(locatorURI);
171
log.info("Starting remoting server with locator uri of: " + locatorURI);
172
HashMap config = new HashMap();
173
config.put(InvokerLocator.FORCE_REMOTE, "true");
174
addExtraServerConfig(config);
175
connector = new Connector(serverLocator, config);
177
invocationHandler = new TestInvocationHandler();
178
connector.addInvocationHandler("test", invocationHandler);
183
protected void shutdownServer() throws Exception
185
if (connector != null)
190
static class TestInvocationHandler implements ServerInvocationHandler
192
public void addListener(InvokerCallbackHandler callbackHandler) {}
193
public Object invoke(final InvocationRequest invocation) throws Throwable
195
Object o = invocation.getParameter();
200
return invocation.getParameter();
202
public void removeListener(InvokerCallbackHandler callbackHandler) {}
203
public void setMBeanServer(MBeanServer server) {}
204
public void setInvoker(ServerInvoker invoker) {}
208
static class InvokerThread extends Thread
215
public InvokerThread(Client client, String message)
217
this.client = client;
218
this.message = message;
219
setName("InvokerThread:" + counter++);
226
log.info("invocation succeeded: " + client.invoke(message));
231
log.error("invocation error: " + t.getMessage());
b'\\ No newline at end of file'