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

« back to all changes in this revision

Viewing changes to tests/org/jboss/test/remoting/transport/http/timeout/ReusedHttpURLConnectionsTestCase.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.http.timeout;
23
 
 
24
 
import java.net.InetAddress;
25
 
import java.net.SocketTimeoutException;
26
 
import java.util.HashMap;
27
 
import java.util.Map;
28
 
 
29
 
import javax.management.MBeanServer;
30
 
 
31
 
import junit.framework.TestCase;
32
 
 
33
 
import org.apache.log4j.ConsoleAppender;
34
 
import org.apache.log4j.Level;
35
 
import org.apache.log4j.Logger;
36
 
import org.apache.log4j.PatternLayout;
37
 
import org.jboss.remoting.Client;
38
 
import org.jboss.remoting.ConnectionListener;
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.Callback;
44
 
import org.jboss.remoting.callback.HandleCallbackException;
45
 
import org.jboss.remoting.callback.InvokerCallbackHandler;
46
 
import org.jboss.remoting.transport.Connector;
47
 
import org.jboss.remoting.transport.PortUtil;
48
 
 
49
 
 
50
 
/**
51
 
 * 
52
 
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
53
 
 * @version $Revision: 2495 $
54
 
 * <p>
55
 
 * Copyright May 31, 2007
56
 
 * </p>
57
 
 */
58
 
public class ReusedHttpURLConnectionsTestCase extends TestCase
59
 
{
60
 
   public static int port;
61
 
   
62
 
   private static Logger log = Logger.getLogger(ReusedHttpURLConnectionsTestCase.class);
63
 
   
64
 
   private static final String CALLBACK_DELAY_KEY =  "callbackDelay";
65
 
   private static boolean firstTime = true;
66
 
   
67
 
   // remoting server connector
68
 
   private Connector connector;
69
 
   private InvokerLocator serverLocator;
70
 
   private TestInvocationHandler invocationHandler;
71
 
 
72
 
   
73
 
   /**
74
 
    * Sets up target remoting server.
75
 
    */
76
 
   public void setUp() throws Exception
77
 
   {
78
 
      if (firstTime)
79
 
      {
80
 
         firstTime = false;
81
 
         Logger.getLogger("org.jboss.remoting").setLevel(Level.INFO);
82
 
         Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
83
 
         String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
84
 
         PatternLayout layout = new PatternLayout(pattern);
85
 
         ConsoleAppender consoleAppender = new ConsoleAppender(layout);
86
 
         Logger.getRootLogger().addAppender(consoleAppender);  
87
 
      }
88
 
   }
89
 
 
90
 
   
91
 
   public void tearDown()
92
 
   {
93
 
   }
94
 
   
95
 
   
96
 
   public void testLongThenShortTimeouts() throws Throwable
97
 
   {
98
 
      log.info("entering " + getName());
99
 
      
100
 
      // Start server.
101
 
      String host = InetAddress.getLocalHost().getHostAddress();
102
 
      port = PortUtil.findFreePort(host);
103
 
      String locatorURI = getTransport() + "://" + host + ":" + port + "?timeout=4000";
104
 
      serverLocator = new InvokerLocator(locatorURI);
105
 
      log.info("Starting remoting server with locator uri of: " + locatorURI);
106
 
      HashMap config = new HashMap();
107
 
      config.put(InvokerLocator.FORCE_REMOTE, "true");
108
 
      addExtraServerConfig(config);
109
 
      connector = new Connector(serverLocator, config);
110
 
      connector.create();
111
 
      invocationHandler = new TestInvocationHandler();
112
 
      connector.addInvocationHandler("sample", invocationHandler);
113
 
      connector.start();
114
 
      
115
 
      // Create client.
116
 
      HashMap clientConfig = new HashMap();
117
 
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
118
 
      addExtraClientConfig(clientConfig);
119
 
      Client client = new Client(serverLocator, clientConfig);
120
 
      client.connect();
121
 
      log.info("client is connected");
122
 
      
123
 
      // Test connection.
124
 
      assertEquals("test", client.invoke("test"));
125
 
      
126
 
      // Test connection timeout (4000).
127
 
      HashMap metadata = new HashMap();
128
 
      try
129
 
      {
130
 
         metadata.put(CALLBACK_DELAY_KEY, "6000");
131
 
         client.invoke("test", metadata);
132
 
         fail();
133
 
      }
134
 
      catch (Exception e)
135
 
      {
136
 
         if (e.getCause() instanceof SocketTimeoutException)
137
 
         {
138
 
            log.info(getName() + ": got first expected timeout");
139
 
         }
140
 
         else
141
 
         {
142
 
            log.error(getName() + ": got unexpected exception", e);
143
 
            fail();
144
 
         }
145
 
      }
146
 
      
147
 
      // Test per invocation timeout (1000).
148
 
      try
149
 
      {
150
 
         metadata.put("timeout", "1000");
151
 
         metadata.put(CALLBACK_DELAY_KEY, "3000");
152
 
         client.invoke("test", metadata);
153
 
         fail();
154
 
      }
155
 
      catch (Exception e)
156
 
      {
157
 
         if (e.getCause() instanceof SocketTimeoutException)
158
 
         {
159
 
            log.info(getName() + ": got second expected timeout");
160
 
         }
161
 
         else
162
 
         {
163
 
            log.error(getName() + ": got unexpected exception", e);
164
 
            fail();
165
 
         }
166
 
      }
167
 
      
168
 
      client.disconnect();
169
 
      connector.stop();
170
 
   }
171
 
   
172
 
   
173
 
   public void testShortThenLongTimeouts() throws Throwable
174
 
   {
175
 
      log.info("entering " + getName());
176
 
      
177
 
      // Start server.
178
 
      String host = InetAddress.getLocalHost().getHostAddress();
179
 
      port = PortUtil.findFreePort(host);
180
 
      String locatorURI = getTransport() + "://" + host + ":" + port + "?timeout=1000";
181
 
      serverLocator = new InvokerLocator(locatorURI);
182
 
      log.info("Starting remoting server with locator uri of: " + locatorURI);
183
 
      HashMap config = new HashMap();
184
 
      config.put(InvokerLocator.FORCE_REMOTE, "true");
185
 
      addExtraServerConfig(config);
186
 
      connector = new Connector(serverLocator, config);
187
 
      connector.create();
188
 
      invocationHandler = new TestInvocationHandler();
189
 
      connector.addInvocationHandler("sample", invocationHandler);
190
 
      connector.start();
191
 
      
192
 
      // Create client.
193
 
      HashMap clientConfig = new HashMap();
194
 
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
195
 
      addExtraClientConfig(clientConfig);
196
 
      Client client = new Client(serverLocator, clientConfig);
197
 
      client.connect();
198
 
      log.info("client is connected");
199
 
      
200
 
      // Test connection.
201
 
      assertEquals("test", client.invoke("test"));
202
 
      
203
 
      // Test connection timeout (1000).
204
 
      HashMap metadata = new HashMap();
205
 
      try
206
 
      {
207
 
         metadata.put(CALLBACK_DELAY_KEY, "3000");
208
 
         client.invoke("test", metadata);
209
 
         fail();
210
 
      }
211
 
      catch (Exception e)
212
 
      {
213
 
         if (e.getCause() instanceof SocketTimeoutException)
214
 
         {
215
 
            log.info(getName() + ": got first expected timeout");
216
 
         }
217
 
         else
218
 
         {
219
 
            log.error(getName() + ": got unexpected exception", e);
220
 
            fail();
221
 
         }
222
 
      }
223
 
      
224
 
      // Try per invocation timeout (4000).
225
 
      try
226
 
      {
227
 
         metadata.put("timeout", "4000");
228
 
         metadata.put(CALLBACK_DELAY_KEY, "6000");
229
 
         client.invoke("test", metadata);
230
 
         fail();
231
 
      }
232
 
      catch (Exception e)
233
 
      {
234
 
         if (e.getCause() instanceof SocketTimeoutException)
235
 
         {
236
 
            log.info(getName() + ": got second expected timeout");
237
 
         }
238
 
         else
239
 
         {
240
 
            log.error(getName() + ": got unexpected exception", e);
241
 
            fail();
242
 
         }
243
 
      }
244
 
      
245
 
      client.disconnect();
246
 
      connector.stop();
247
 
   }
248
 
   
249
 
   
250
 
   public void testShortThenZeroTimeouts() throws Throwable
251
 
   {
252
 
      log.info("entering " + getName());
253
 
      
254
 
      // Start server.
255
 
      String host = InetAddress.getLocalHost().getHostAddress();
256
 
      port = PortUtil.findFreePort(host);
257
 
      String locatorURI = getTransport() + "://" + host + ":" + port + "?timeout=1000";
258
 
      serverLocator = new InvokerLocator(locatorURI);
259
 
      log.info("Starting remoting server with locator uri of: " + locatorURI);
260
 
      HashMap config = new HashMap();
261
 
      config.put(InvokerLocator.FORCE_REMOTE, "true");
262
 
      addExtraServerConfig(config);
263
 
      connector = new Connector(serverLocator, config);
264
 
      connector.create();
265
 
      invocationHandler = new TestInvocationHandler();
266
 
      connector.addInvocationHandler("sample", invocationHandler);
267
 
      connector.start();
268
 
      
269
 
      // Create client.
270
 
      HashMap clientConfig = new HashMap();
271
 
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
272
 
      addExtraClientConfig(clientConfig);
273
 
      Client client = new Client(serverLocator, clientConfig);
274
 
      client.connect();
275
 
      log.info("client is connected");
276
 
      
277
 
      // Test connection.
278
 
      assertEquals("test", client.invoke("test"));
279
 
      
280
 
      // Test connection timeout (1000).
281
 
      HashMap metadata = new HashMap();
282
 
      try
283
 
      {
284
 
         metadata.put(CALLBACK_DELAY_KEY, "3000");
285
 
         client.invoke("test", metadata);
286
 
         fail();
287
 
      }
288
 
      catch (Exception e)
289
 
      {
290
 
         if (e.getCause() instanceof SocketTimeoutException)
291
 
         {
292
 
            log.info(getName() + ": got first expected timeout");
293
 
         }
294
 
         else
295
 
         {
296
 
            log.error(getName() + ": got unexpected exception", e);
297
 
            fail();
298
 
         }
299
 
      }
300
 
      
301
 
      // Try per invocation timeout (0).
302
 
      try
303
 
      {
304
 
         metadata.put("timeout", "0");
305
 
         metadata.put(CALLBACK_DELAY_KEY, "5000");
306
 
         assertEquals("test", client.invoke("test", metadata));
307
 
      }
308
 
      catch (Exception e)
309
 
      {
310
 
         log.error(getName() + ": got unexpected exception", e);
311
 
         fail();
312
 
      }
313
 
      
314
 
      client.disconnect();
315
 
      connector.stop();
316
 
   }
317
 
   
318
 
   
319
 
   public void testTimeoutsWithConnectionValidator() throws Throwable
320
 
   {
321
 
      log.info("entering " + getName());
322
 
      
323
 
      // Start server.
324
 
      String host = InetAddress.getLocalHost().getHostAddress();
325
 
      port = PortUtil.findFreePort(host);
326
 
      String locatorURI = getTransport() + "://" + host + ":" + port + "?timeout=4000";
327
 
      serverLocator = new InvokerLocator(locatorURI);
328
 
      log.info("Starting remoting server with locator uri of: " + locatorURI);
329
 
      HashMap config = new HashMap();
330
 
      config.put(InvokerLocator.FORCE_REMOTE, "true");
331
 
      addExtraServerConfig(config);
332
 
      connector = new Connector(serverLocator, config);
333
 
      connector.create();
334
 
      invocationHandler = new TestInvocationHandler();
335
 
      connector.addInvocationHandler("sample", invocationHandler);
336
 
      connector.start();
337
 
      
338
 
      // Create client.
339
 
      HashMap clientConfig = new HashMap();
340
 
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
341
 
      addExtraClientConfig(clientConfig);
342
 
      Client client = new Client(serverLocator, clientConfig);
343
 
      client.connect();
344
 
      ConnectionListener listener = new TestConnectionListener();
345
 
      client.addConnectionListener(listener);
346
 
      log.info("client is connected");
347
 
      
348
 
      // Test connection.
349
 
      assertEquals("test", client.invoke("test"));
350
 
      
351
 
      // Wait for a ping to occur.
352
 
      Thread.sleep(4000);
353
 
      
354
 
      // Try per invocation timeout (0).
355
 
      HashMap metadata = new HashMap();
356
 
      try
357
 
      {
358
 
         metadata.put("timeout", "0");
359
 
         metadata.put(CALLBACK_DELAY_KEY, "6000");
360
 
         assertEquals("test", client.invoke("test", metadata));
361
 
      }
362
 
      catch (Exception e)
363
 
      {
364
 
         log.error(getName() + ": got unexpected exception", e);
365
 
         fail();
366
 
      }
367
 
      
368
 
      client.disconnect();
369
 
      connector.stop();
370
 
   }
371
 
   
372
 
   
373
 
   protected String getTransport()
374
 
   {
375
 
      return "http";
376
 
   }
377
 
   
378
 
   
379
 
   protected void addExtraClientConfig(Map config) {}
380
 
   protected void addExtraServerConfig(Map config) {}
381
 
   
382
 
 
383
 
   static class TestInvocationHandler implements ServerInvocationHandler
384
 
   {
385
 
      public void addListener(InvokerCallbackHandler callbackHandler) {}
386
 
      
387
 
      public Object invoke(final InvocationRequest invocation) throws Throwable
388
 
      {
389
 
         Map requestMap = invocation.getRequestPayload();
390
 
         int delay = 0;
391
 
         String delayString = (String) requestMap.get(CALLBACK_DELAY_KEY);
392
 
         if (delayString != null)
393
 
            delay = Integer.parseInt(delayString);
394
 
         log.info("starting delay: " + delay);
395
 
         Thread.sleep(delay);
396
 
         log.info("ending delay");
397
 
         return invocation.getParameter();
398
 
      }
399
 
      
400
 
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
401
 
      public void setMBeanServer(MBeanServer server) {}
402
 
      public void setInvoker(ServerInvoker invoker) {}
403
 
   }
404
 
   
405
 
   static class TestInvokerCallbackHandler implements InvokerCallbackHandler
406
 
   {
407
 
      public void handleCallback(Callback callback) throws HandleCallbackException {}
408
 
   }
409
 
   
410
 
   static class TestConnectionListener implements ConnectionListener
411
 
   {
412
 
      public void handleConnectionException(Throwable throwable, Client client)
413
 
      {
414
 
      }
415
 
   }
416
 
}
 
 
b'\\ No newline at end of file'