2
* JBoss, Home of Professional Open Source
3
* Copyright 2009, 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.bisocket.socketexception;
24
import java.io.IOException;
25
import java.lang.reflect.Field;
26
import java.net.InetAddress;
27
import java.net.ServerSocket;
28
import java.net.Socket;
29
import java.net.SocketException;
30
import java.util.HashMap;
34
import javax.net.ServerSocketFactory;
36
import org.apache.log4j.Logger;
37
import org.jboss.remoting.Client;
38
import org.jboss.remoting.InvokerLocator;
39
import org.jboss.remoting.callback.Callback;
40
import org.jboss.remoting.callback.HandleCallbackException;
41
import org.jboss.remoting.callback.InvokerCallbackHandler;
42
import org.jboss.remoting.transport.Connector;
43
import org.jboss.remoting.transport.bisocket.Bisocket;
44
import org.jboss.remoting.transport.socket.LRUPool;
45
import org.jboss.remoting.transport.socket.ServerThread;
46
import org.jboss.remoting.transport.socket.SocketServerInvoker;
47
import org.jboss.test.remoting.transport.socket.socketexception.SocketCreationExceptionTestCase;
50
* Unit tests for JBREM-1152.
52
* @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
55
* Copyright Sep 9, 2009
58
public class BisocketSocketCreationExceptionTestCase extends SocketCreationExceptionTestCase
60
private static Logger log = Logger.getLogger(BisocketSocketCreationExceptionTestCase.class);
63
public void testCallbackException() throws Throwable
65
log.info("entering " + getName());
68
setupServer(new TestServerSocketFactory(2, new SocketException(getName())));
71
InvokerLocator clientLocator = new InvokerLocator(locatorURI);
72
HashMap clientConfig = new HashMap();
73
clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
74
addExtraClientConfig(clientConfig);
75
Client client = new Client(clientLocator, clientConfig);
77
log.info("client is connected");
80
assertEquals("abc", client.invoke("abc"));
81
log.info("connection is good");
83
TestCallbackHandler callbackHandler = new TestCallbackHandler();
84
Map metadata = new HashMap();
85
metadata.put(Bisocket.IS_CALLBACK_SERVER, "true");
86
client.addListener(callbackHandler, metadata);
88
// Get client side ServerThread pool.
89
Set callbackConnectors = client.getCallbackConnectors(callbackHandler);
90
Connector callbackConnector = (Connector) callbackConnectors.iterator().next();
91
SocketServerInvoker serverInvoker = (SocketServerInvoker) callbackConnector.getServerInvoker();
92
Field field = SocketServerInvoker.class.getDeclaredField("clientpool");
93
field.setAccessible(true);
94
LRUPool clientpool = (LRUPool) field.get(serverInvoker);
96
// Verify MicroSocketClientInvoker retries invocation after failure to get a connection.
97
client.invoke(SEND_CALLBACK);
98
assertEquals(1, callbackHandler.received);
99
Set set = clientpool.getContents();
100
Object[] serverThreads = set.toArray();
101
for (int i = 0; i < serverThreads.length; i++)
103
ServerThread st = (ServerThread) serverThreads[i];
106
client.invoke(SEND_CALLBACK);
107
assertEquals(2, callbackHandler.received);
108
set = clientpool.getContents();
109
serverThreads = set.toArray();
110
for (int i = 0; i < serverThreads.length; i++)
112
ServerThread st = (ServerThread) serverThreads[i];
115
client.invoke(SEND_CALLBACK);
116
assertEquals(3, callbackHandler.received);
118
client.removeListener(callbackHandler);
121
log.info(getName() + " PASSES");
125
protected String getTransport()
131
static class TestCallbackHandler implements InvokerCallbackHandler
135
public void handleCallback(Callback callback) throws HandleCallbackException
137
log.info("received callback");
142
static public class TestServerSocketFactory extends ServerSocketFactory
144
int initialSuccesses;
145
IOException exception;
147
public TestServerSocketFactory()
149
this.initialSuccesses = -1;
150
this.exception = new IOException();
152
public TestServerSocketFactory(int initialSuccesses, IOException exception)
154
this.initialSuccesses = initialSuccesses;
155
this.exception = exception;
157
public ServerSocket createServerSocket() throws IOException
159
ServerSocket ss = new TestServerSocket(initialSuccesses, exception);
160
log.info(this + " returning: " + ss);
163
public ServerSocket createServerSocket(int port) throws IOException
165
ServerSocket ss = new TestServerSocket(port, initialSuccesses, exception);
166
log.info(this + " returning: " + ss);
170
public ServerSocket createServerSocket(int port, int backlog) throws IOException
172
ServerSocket ss = new TestServerSocket(port, backlog, initialSuccesses, exception);
173
log.info(this + " returning: " + ss);
177
public ServerSocket createServerSocket(int port, int backlog, InetAddress ifAddress) throws IOException
179
ServerSocket ss = new TestServerSocket(port, backlog, ifAddress, initialSuccesses, exception);
180
log.info(this + " returning: " + ss);
186
static class TestServerSocket extends ServerSocket
188
int initialSuccesses;
189
IOException exception;
192
public TestServerSocket(int initialSuccesses, IOException exception) throws IOException
195
this.initialSuccesses = initialSuccesses;
196
this.exception = exception;
198
public TestServerSocket(int port, int initialSuccesses, IOException exception) throws IOException
201
this.initialSuccesses = initialSuccesses;
202
this.exception = exception;
204
public TestServerSocket(int port, int backlog, int initialSuccesses, IOException exception) throws IOException
206
super(port, backlog);
207
this.initialSuccesses = initialSuccesses;
208
this.exception = exception;
210
public TestServerSocket(int port, int backlog, InetAddress bindAddr, int initialSuccesses, IOException exception) throws IOException
212
super(port, backlog, bindAddr);
213
this.initialSuccesses = initialSuccesses;
214
this.exception = exception;
216
public Socket accept() throws IOException
219
Socket s = super.accept();
220
log.info(this + " counter: " + counter);
221
if (counter > initialSuccesses && counter <= initialSuccesses + 2)
225
log.info(this + " returning: " + s);
228
public String toString()
230
return "TestServerSocket[" + getLocalPort() + "]";
b'\\ No newline at end of file'