~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/socket/interrupt/InterruptedExceptionTestCase.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 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.
 
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.socket.interrupt;
 
23
 
 
24
import java.net.InetAddress;
 
25
import java.util.HashMap;
 
26
import java.util.Map;
 
27
 
 
28
import javax.management.MBeanServer;
 
29
 
 
30
import junit.framework.TestCase;
 
31
 
 
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;
 
47
 
 
48
 
 
49
/**
 
50
 * Unit test for JBREM-955.
 
51
 * 
 
52
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
 
53
 * @version $Revision: 1.1 $
 
54
 * <p>
 
55
 * Copyright Apr 25, 2008
 
56
 * </p>
 
57
 */
 
58
public class InterruptedExceptionTestCase extends TestCase
 
59
{
 
60
   private static Logger log = Logger.getLogger(InterruptedExceptionTestCase.class);
 
61
   
 
62
   private static boolean firstTime = true;
 
63
   private static String FAST = "fast";
 
64
   
 
65
   protected String host;
 
66
   protected int port;
 
67
   protected String locatorURI;
 
68
   protected InvokerLocator serverLocator;
 
69
   protected Connector connector;
 
70
   protected TestInvocationHandler invocationHandler;
 
71
 
 
72
   
 
73
   public void setUp() throws Exception
 
74
   {
 
75
      if (firstTime)
 
76
      {
 
77
         firstTime = false;
 
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);  
 
84
      }
 
85
   }
 
86
 
 
87
   
 
88
   public void tearDown()
 
89
   {
 
90
   }
 
91
   
 
92
   
 
93
   public void testInterruptedException() throws Throwable
 
94
   {
 
95
      log.info("entering " + getName());
 
96
      
 
97
      // Start server.
 
98
      setupServer();
 
99
      
 
100
      // Create client.
 
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);
 
108
      client.connect();
 
109
      log.info("client is connected");
 
110
      
 
111
      // Test connections.
 
112
      assertEquals(FAST, client.invoke(FAST));
 
113
      log.info("connection is good");
 
114
      
 
115
      InvokerThread t1 = new InvokerThread(client, "abc");
 
116
      InvokerThread t2 = new InvokerThread(client, "xyz");
 
117
      
 
118
      // Start first invocation.
 
119
      t1.start();
 
120
      log.info("started first invocation");
 
121
      
 
122
      // Give first invocation time to start.
 
123
      Thread.sleep(5000);
 
124
      
 
125
      // Start second invocation.
 
126
      t2.start();
 
127
      log.info("started second invocation");
 
128
      
 
129
      // Give second invocation time to start.
 
130
      Thread.sleep(5000);
 
131
      
 
132
      // Interrupt second invocation as it waits for a semaphore.
 
133
      t2.interrupt();
 
134
      log.info("interrupted second invocation");
 
135
 
 
136
      // Wait until second invocation throws an exception.
 
137
      synchronized (t2)
 
138
      {
 
139
         t2.wait(10000);
 
140
      }
 
141
      
 
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);
 
148
      
 
149
      client.disconnect();
 
150
      shutdownServer();
 
151
      log.info(getName() + " PASSES");
 
152
   }
 
153
   
 
154
   
 
155
   protected String getTransport()
 
156
   {
 
157
      return "socket";
 
158
   }
 
159
   
 
160
   
 
161
   protected void addExtraClientConfig(Map config) {}
 
162
   protected void addExtraServerConfig(Map config) {}
 
163
   
 
164
 
 
165
   protected void setupServer() throws Exception
 
166
   {
 
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);
 
176
      connector.create();
 
177
      invocationHandler = new TestInvocationHandler();
 
178
      connector.addInvocationHandler("test", invocationHandler);
 
179
      connector.start();
 
180
   }
 
181
   
 
182
   
 
183
   protected void shutdownServer() throws Exception
 
184
   {
 
185
      if (connector != null)
 
186
         connector.stop();
 
187
   }
 
188
   
 
189
   
 
190
   static class TestInvocationHandler implements ServerInvocationHandler
 
191
   {
 
192
      public void addListener(InvokerCallbackHandler callbackHandler) {}
 
193
      public Object invoke(final InvocationRequest invocation) throws Throwable
 
194
      {
 
195
         Object o = invocation.getParameter();
 
196
         if (!FAST.equals(o))
 
197
         {
 
198
            Thread.sleep(20000);
 
199
         }
 
200
         return invocation.getParameter();
 
201
      }
 
202
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
 
203
      public void setMBeanServer(MBeanServer server) {}
 
204
      public void setInvoker(ServerInvoker invoker) {}
 
205
   }
 
206
   
 
207
   
 
208
   static class InvokerThread extends Thread
 
209
   {
 
210
      static int counter;
 
211
      Client client;
 
212
      String message;
 
213
      Throwable throwable;
 
214
      
 
215
      public InvokerThread(Client client, String message)
 
216
      {
 
217
         this.client = client;
 
218
         this.message = message;
 
219
         setName("InvokerThread:" + counter++);
 
220
      }
 
221
      
 
222
      public void run()
 
223
      {
 
224
         try
 
225
         {
 
226
            log.info("invocation succeeded: " + client.invoke(message));
 
227
         }
 
228
         catch (Throwable t)
 
229
         {
 
230
            throwable = t;
 
231
            log.error("invocation error: " + t.getMessage());
 
232
         }
 
233
         
 
234
         synchronized (this)
 
235
         {
 
236
            notify();
 
237
         }
 
238
      }
 
239
   }
 
240
}
 
 
b'\\ No newline at end of file'