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

« back to all changes in this revision

Viewing changes to src/tests/org/jboss/test/remoting/transport/bisocket/socketexception/BisocketSocketCreationExceptionTestCase.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
/*
 
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.
 
6
*
 
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.
 
11
*
 
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.
 
16
*
 
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.
 
21
*/
 
22
package org.jboss.test.remoting.transport.bisocket.socketexception;
 
23
 
 
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;
 
31
import java.util.Map;
 
32
import java.util.Set;
 
33
 
 
34
import javax.net.ServerSocketFactory;
 
35
 
 
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;
 
48
 
 
49
/**
 
50
 * Unit tests for JBREM-1152.
 
51
 * 
 
52
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
 
53
 * @version $Rev$
 
54
 * <p>
 
55
 * Copyright Sep 9, 2009
 
56
 * </p>
 
57
 */
 
58
public class BisocketSocketCreationExceptionTestCase extends SocketCreationExceptionTestCase
 
59
{
 
60
   private static Logger log = Logger.getLogger(BisocketSocketCreationExceptionTestCase.class);
 
61
   
 
62
   
 
63
   public void testCallbackException() throws Throwable
 
64
   {
 
65
      log.info("entering " + getName());
 
66
      
 
67
      // Start server.
 
68
      setupServer(new TestServerSocketFactory(2, new SocketException(getName())));
 
69
      
 
70
      // Create client.
 
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);
 
76
      client.connect();
 
77
      log.info("client is connected");
 
78
      
 
79
      // Test connection.
 
80
      assertEquals("abc", client.invoke("abc"));
 
81
      log.info("connection is good");
 
82
      
 
83
      TestCallbackHandler callbackHandler = new TestCallbackHandler();
 
84
      Map metadata = new HashMap();
 
85
      metadata.put(Bisocket.IS_CALLBACK_SERVER, "true");
 
86
      client.addListener(callbackHandler, metadata);
 
87
      
 
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);
 
95
      
 
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++)
 
102
      {
 
103
         ServerThread st = (ServerThread) serverThreads[i];
 
104
         st.shutdown();
 
105
      }
 
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++)
 
111
      {
 
112
         ServerThread st = (ServerThread) serverThreads[i];
 
113
         st.shutdown();
 
114
      }
 
115
      client.invoke(SEND_CALLBACK);
 
116
      assertEquals(3, callbackHandler.received);
 
117
      
 
118
      client.removeListener(callbackHandler);
 
119
      client.disconnect();
 
120
      shutdownServer();
 
121
      log.info(getName() + " PASSES");
 
122
   }
 
123
   
 
124
   
 
125
   protected String getTransport()
 
126
   {
 
127
      return "bisocket";
 
128
   }
 
129
   
 
130
   
 
131
   static class TestCallbackHandler implements InvokerCallbackHandler
 
132
   {
 
133
      public int received;
 
134
      
 
135
      public void handleCallback(Callback callback) throws HandleCallbackException
 
136
      {
 
137
         log.info("received callback");
 
138
         received++;
 
139
      }  
 
140
   }
 
141
   
 
142
   static public class TestServerSocketFactory extends ServerSocketFactory
 
143
   {
 
144
      int initialSuccesses;
 
145
      IOException exception;
 
146
      
 
147
      public TestServerSocketFactory()
 
148
      {
 
149
         this.initialSuccesses = -1;
 
150
         this.exception = new IOException();
 
151
      }      
 
152
      public TestServerSocketFactory(int initialSuccesses, IOException exception)
 
153
      {
 
154
         this.initialSuccesses = initialSuccesses;
 
155
         this.exception = exception;
 
156
      }
 
157
      public ServerSocket createServerSocket() throws IOException
 
158
      {
 
159
         ServerSocket ss = new TestServerSocket(initialSuccesses, exception);
 
160
         log.info(this + " returning: " + ss);
 
161
         return ss;
 
162
      }
 
163
      public ServerSocket createServerSocket(int port) throws IOException
 
164
      {
 
165
         ServerSocket ss = new TestServerSocket(port, initialSuccesses, exception);
 
166
         log.info(this + " returning: " + ss);
 
167
         return ss;
 
168
      }
 
169
 
 
170
      public ServerSocket createServerSocket(int port, int backlog) throws IOException
 
171
      {
 
172
         ServerSocket ss = new TestServerSocket(port, backlog, initialSuccesses, exception);
 
173
         log.info(this + " returning: " + ss);
 
174
         return ss;
 
175
      }
 
176
 
 
177
      public ServerSocket createServerSocket(int port, int backlog, InetAddress ifAddress) throws IOException
 
178
      {
 
179
         ServerSocket ss = new TestServerSocket(port, backlog, ifAddress, initialSuccesses, exception);
 
180
         log.info(this + " returning: " + ss);
 
181
         return ss;
 
182
      }
 
183
   }
 
184
   
 
185
   
 
186
   static class TestServerSocket extends ServerSocket
 
187
   {
 
188
      int initialSuccesses;
 
189
      IOException exception;
 
190
      int counter;
 
191
 
 
192
      public TestServerSocket(int initialSuccesses, IOException exception) throws IOException
 
193
      {
 
194
         super();
 
195
         this.initialSuccesses = initialSuccesses;
 
196
         this.exception = exception;
 
197
      }
 
198
      public TestServerSocket(int port, int initialSuccesses, IOException exception) throws IOException
 
199
      {
 
200
         super(port);
 
201
         this.initialSuccesses = initialSuccesses;
 
202
         this.exception = exception;
 
203
      }
 
204
      public TestServerSocket(int port, int backlog, int initialSuccesses, IOException exception) throws IOException
 
205
      {
 
206
         super(port, backlog);
 
207
         this.initialSuccesses = initialSuccesses;
 
208
         this.exception = exception;
 
209
      }
 
210
      public TestServerSocket(int port, int backlog, InetAddress bindAddr, int initialSuccesses, IOException exception) throws IOException
 
211
      {
 
212
         super(port, backlog, bindAddr);
 
213
         this.initialSuccesses = initialSuccesses;
 
214
         this.exception = exception;
 
215
      }
 
216
      public Socket accept() throws IOException
 
217
      {
 
218
         ++counter;
 
219
         Socket s = super.accept();
 
220
         log.info(this + " counter: " + counter);
 
221
         if (counter > initialSuccesses && counter <= initialSuccesses + 2)
 
222
         {
 
223
            throw exception;
 
224
         }
 
225
         log.info(this + " returning: " + s);
 
226
         return s;
 
227
      }
 
228
      public String toString()
 
229
      {
 
230
         return "TestServerSocket[" + getLocalPort() + "]";
 
231
      }
 
232
   }
 
233
   
 
234
}
 
 
b'\\ No newline at end of file'