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

« back to all changes in this revision

Viewing changes to tests/org/jboss/test/remoting/transport/socket/timeout/WriteTimeoutTestParent.java

  • Committer: Package Import Robot
  • Author(s): Torsten Werner
  • Date: 2011-09-09 14:01:03 UTC
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: package-import@ubuntu.com-20110909140103-o8ucrolqt5g25k57
Tags: upstream-2.5.3.SP1
ImportĀ upstreamĀ versionĀ 2.5.3.SP1

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.socket.timeout;
23
 
 
24
 
import java.io.IOException;
25
 
import java.io.OutputStream;
26
 
import java.lang.reflect.Constructor;
27
 
import java.lang.reflect.InvocationTargetException;
28
 
import java.net.InetAddress;
29
 
import java.net.ServerSocket;
30
 
import java.net.Socket;
31
 
import java.net.UnknownHostException;
32
 
import java.util.HashMap;
33
 
import java.util.Map;
34
 
import java.util.Set;
35
 
 
36
 
import javax.management.MBeanServer;
37
 
import javax.net.ServerSocketFactory;
38
 
import javax.net.SocketFactory;
39
 
import javax.net.ssl.SSLProtocolException;
40
 
 
41
 
import junit.framework.TestCase;
42
 
 
43
 
import org.apache.log4j.ConsoleAppender;
44
 
import org.apache.log4j.Level;
45
 
import org.apache.log4j.Logger;
46
 
import org.apache.log4j.PatternLayout;
47
 
import org.jboss.logging.XLevel;
48
 
import org.jboss.remoting.CannotConnectException;
49
 
import org.jboss.remoting.Client;
50
 
import org.jboss.remoting.InvocationFailureException;
51
 
import org.jboss.remoting.InvocationRequest;
52
 
import org.jboss.remoting.InvokerLocator;
53
 
import org.jboss.remoting.Remoting;
54
 
import org.jboss.remoting.ServerInvocationHandler;
55
 
import org.jboss.remoting.ServerInvoker;
56
 
import org.jboss.remoting.callback.Callback;
57
 
import org.jboss.remoting.callback.HandleCallbackException;
58
 
import org.jboss.remoting.callback.InvokerCallbackHandler;
59
 
import org.jboss.remoting.transport.Connector;
60
 
import org.jboss.remoting.transport.PortUtil;
61
 
import org.jboss.remoting.transport.bisocket.Bisocket;
62
 
import org.jboss.remoting.transport.socket.SocketServerInvoker;
63
 
import org.jboss.remoting.transport.socket.SocketWrapper;
64
 
 
65
 
 
66
 
/**
67
 
 * Unit tests for JBREM-1120.
68
 
 * 
69
 
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
70
 
 * @version $Rev$
71
 
 * <p>
72
 
 * Copyright Apr 22, 2009
73
 
 * </p>
74
 
 */
75
 
public abstract class WriteTimeoutTestParent extends TestCase
76
 
{
77
 
   private static Logger log = Logger.getLogger(WriteTimeoutTestParent.class);
78
 
   
79
 
   private static boolean firstTime = true;
80
 
   protected static int secondaryServerSocketPort;
81
 
   protected static boolean callbackTest;
82
 
   
83
 
   protected String host;
84
 
   protected int port;
85
 
   protected String locatorURI;
86
 
   protected InvokerLocator serverLocator;
87
 
   protected Connector connector;
88
 
   protected TestInvocationHandler invocationHandler;
89
 
 
90
 
   
91
 
   public void setUp() throws Exception
92
 
   {
93
 
      if (firstTime)
94
 
      {
95
 
         firstTime = false;
96
 
         Logger.getLogger("org.jboss.remoting").setLevel(Level.INFO);
97
 
         Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
98
 
         String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
99
 
         PatternLayout layout = new PatternLayout(pattern);
100
 
         ConsoleAppender consoleAppender = new ConsoleAppender(layout);
101
 
         Logger.getRootLogger().addAppender(consoleAppender);  
102
 
      }
103
 
      
104
 
      TestOutputStream.counter = 0;
105
 
      callbackTest = false;
106
 
   }
107
 
 
108
 
   
109
 
   public void tearDown()
110
 
   {
111
 
   }
112
 
   
113
 
   
114
 
   public void testClientWriteTimeout() throws Throwable
115
 
   {
116
 
      log.info("entering " + getName());
117
 
      
118
 
      // Start server.
119
 
      setupServer(false, false, "", -1, -1);
120
 
      
121
 
      // Create client.
122
 
      InvokerLocator clientLocator = new InvokerLocator(locatorURI);
123
 
      HashMap clientConfig = new HashMap();
124
 
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
125
 
      clientConfig.put(SocketWrapper.WRITE_TIMEOUT, "1000");
126
 
      SocketFactory sf = (SocketFactory) getSocketFactoryConstructor().newInstance(new Object[]{new Integer(5000), new Integer(1)});
127
 
      clientConfig.put(Remoting.CUSTOM_SOCKET_FACTORY, sf);
128
 
      addExtraClientConfig(clientConfig);
129
 
      Client client = new Client(clientLocator, clientConfig);
130
 
      client.connect();
131
 
      log.info("client is connected");
132
 
      
133
 
      log.info("**************************************");
134
 
      log.info("*** WorkerThread error is expected ***");
135
 
      log.info("**************************************");
136
 
      
137
 
      // Test client side write timeout.
138
 
      try
139
 
      {
140
 
         client.invoke("abc");
141
 
      }
142
 
      catch (InvocationFailureException e)
143
 
      {
144
 
         log.info(e.getMessage());
145
 
         assertNotNull(e.getMessage());
146
 
         assertTrue(e.getMessage().startsWith("Unable to perform invocation"));
147
 
         assertTrue(e.getCause() instanceof IOException);
148
 
         IOException ioe = (IOException) e.getCause();
149
 
         assertEquals("closed", ioe.getMessage());
150
 
         log.info("got expected Exception");
151
 
      }
152
 
      catch (Throwable t)
153
 
      {
154
 
         log.error("got unexpected Exception", t);
155
 
         fail("got unexpected Exception");
156
 
      }
157
 
      
158
 
      client.disconnect();
159
 
      shutdownServer();
160
 
      log.info(getName() + " PASSES");
161
 
   }
162
 
   
163
 
   
164
 
   public void testServerWriteTimeout() throws Throwable
165
 
   {
166
 
      log.info("entering " + getName());
167
 
      
168
 
      // Start server.
169
 
      setupServer(true, false, "1000", 5000, 1);
170
 
      
171
 
      // Create client.
172
 
      InvokerLocator clientLocator = new InvokerLocator(locatorURI);
173
 
      HashMap clientConfig = new HashMap();
174
 
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
175
 
      clientConfig.put("numberOfCallRetries", "1");
176
 
      clientConfig.put("timeout", "10000");
177
 
      addExtraClientConfig(clientConfig);
178
 
      Client client = new Client(clientLocator, clientConfig);
179
 
      client.connect();
180
 
      log.info("client is connected");
181
 
      
182
 
      log.info("**************************************");
183
 
      log.info("*** WorkerThread error is expected ***");
184
 
      log.info("**************************************");
185
 
      
186
 
      // Test server side write timeout.
187
 
      try
188
 
      {
189
 
         client.invoke("abc");
190
 
      }
191
 
      catch (InvocationFailureException e)
192
 
      {
193
 
         log.info(e.getMessage());
194
 
//         assertNotNull(e.getMessage());
195
 
//         assertTrue(e.getMessage().startsWith("Unable to perform invocation"));
196
 
//         assertTrue(e.getCause() instanceof EOFException);
197
 
         log.info("got expected Exception");
198
 
      }
199
 
      catch (Throwable t)
200
 
      {
201
 
         log.error("got unexpected Exception", t);
202
 
         fail("got unexpected Exception");
203
 
      }
204
 
      
205
 
      // Test server invoker state.
206
 
      Thread.sleep(4000);
207
 
      SocketServerInvoker serverInvoker = (SocketServerInvoker) connector.getServerInvoker();
208
 
      assertEquals(0, serverInvoker.getCurrentClientPoolSize());
209
 
      assertEquals(1, serverInvoker.getCurrentThreadPoolSize());
210
 
      log.info("used ServerThread has returned to threadPool");
211
 
      
212
 
      client.disconnect();
213
 
      shutdownServer();
214
 
      log.info(getName() + " PASSES");
215
 
   }
216
 
   
217
 
   
218
 
   public void testClientCallbackWriteTimeout() throws Throwable
219
 
   {
220
 
      log.info("entering " + getName());
221
 
      
222
 
      // Start server.
223
 
      if (isBisocket(getTransport()))
224
 
      {
225
 
         callbackTest = true;
226
 
      }
227
 
      setupServer(false, false, "", -1, -1);
228
 
      
229
 
      // Create client.
230
 
      InvokerLocator clientLocator = new InvokerLocator(locatorURI);
231
 
      HashMap clientConfig = new HashMap();
232
 
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
233
 
      clientConfig.put(SocketWrapper.WRITE_TIMEOUT, "1000");
234
 
      if (isBisocket(getTransport()))
235
 
      {
236
 
         SocketFactory sf = (SocketFactory) getSocketFactoryConstructor().newInstance(new Object[]{new Integer(5000), new Integer(1)});
237
 
         clientConfig.put(Remoting.CUSTOM_SOCKET_FACTORY, sf);
238
 
      }
239
 
      else
240
 
      {
241
 
         ServerSocketFactory ssf = (ServerSocketFactory) getServerSocketFactoryConstructor().newInstance(new Object[]{new Integer(5000), new Integer(-1)});
242
 
         clientConfig.put(Remoting.CUSTOM_SERVER_SOCKET_FACTORY, ssf);
243
 
      }
244
 
      addExtraClientConfig(clientConfig);
245
 
      Client client = new Client(clientLocator, clientConfig);
246
 
      client.connect();
247
 
      log.info("client is connected");
248
 
      
249
 
      // Test connection
250
 
      client.invoke("abc");
251
 
      log.info("connection is good");
252
 
      
253
 
      // Test client callback write timeout.
254
 
      log.info("registering callback handler");
255
 
      log.info("**************************************");
256
 
      log.info("*** WorkerThread error is expected ***");
257
 
      log.info("**************************************");
258
 
      TestCallbackHandler callbackHandler = new TestCallbackHandler();
259
 
      HashMap metadata = new HashMap();
260
 
      if (isBisocket(getTransport()))
261
 
      {
262
 
//         metadata.put(SocketWrapper.WRITE_TIMEOUT, "1000");
263
 
         metadata.put(Remoting.SOCKET_FACTORY_NAME, getSocketFactoryClassName());
264
 
         metadata.put("numberOfCallRetries", "1");
265
 
         metadata.put(Bisocket.IS_CALLBACK_SERVER, "true");
266
 
         metadata.put(Bisocket.PING_FREQUENCY, "11111111"); 
267
 
      }
268
 
      else
269
 
      {
270
 
//         metadata.put(SocketWrapper.WRITE_TIMEOUT, "1000");
271
 
         metadata.put(ServerInvoker.SERVER_SOCKET_FACTORY, getServerSocketFactoryClassName());
272
 
         metadata.put("numberOfCallRetries", "1");
273
 
      }
274
 
      client.addListener(callbackHandler, metadata, null, true);
275
 
      log.info("called Client.addListener()");
276
 
      
277
 
      // Test server invoker state.
278
 
      // Wait for local ServerThread to time out.  Might take a while in bisocket transports, since
279
 
      // the request to get a socket for the callback client invoker needs its own write on the
280
 
      // control socket.
281
 
      Thread.sleep(20000);
282
 
      log.info("back from sleep");
283
 
      Set callbackConnectors = client.getCallbackConnectors(callbackHandler);
284
 
      assertEquals(1, callbackConnectors.size());
285
 
      Connector callbackConnector = (Connector) callbackConnectors.iterator().next();
286
 
      SocketServerInvoker serverInvoker = (SocketServerInvoker) callbackConnector.getServerInvoker();
287
 
      assertEquals(0, serverInvoker.getCurrentClientPoolSize());
288
 
      assertEquals(1, serverInvoker.getCurrentThreadPoolSize());
289
 
      log.info("used ServerThread has returned to threadPool");
290
 
 
291
 
      client.disconnect();
292
 
      shutdownServer();
293
 
      log.info(getName() + " PASSES");
294
 
   }
295
 
   
296
 
   
297
 
   public void testServerCallbackWriteTimeout() throws Throwable
298
 
   {
299
 
      log.info("entering " + getName());
300
 
      
301
 
      // Start server.
302
 
      if (isBisocket(getTransport()))
303
 
      {
304
 
         callbackTest = true;
305
 
         setupServer(true, false, "1000", 5000, 1);
306
 
      }
307
 
      else
308
 
      {
309
 
         setupServer(false, true, "1000", 5000, 1);
310
 
      }
311
 
      
312
 
      // Create client.
313
 
      InvokerLocator clientLocator = new InvokerLocator(locatorURI);
314
 
      HashMap clientConfig = new HashMap();
315
 
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
316
 
      clientConfig.put("numberOfCallRetries", "1");
317
 
      addExtraClientConfig(clientConfig);
318
 
      Client client = new Client(clientLocator, clientConfig);
319
 
      client.connect();
320
 
      log.info("client is connected");
321
 
      
322
 
      // Test connection
323
 
      client.invoke("abc");
324
 
      log.info("connection is good");
325
 
      
326
 
      // Test server callback write timeout.
327
 
      log.info("registering callback handler");
328
 
      log.info("**************************************");
329
 
      log.info("*** WorkerThread error is expected ***");
330
 
      log.info("**************************************");
331
 
      HashMap metadata = new HashMap();
332
 
      if (isBisocket(getTransport()))
333
 
      {
334
 
         metadata.put(Bisocket.IS_CALLBACK_SERVER, "true");
335
 
      }
336
 
      TestCallbackHandler callbackHandler = new TestCallbackHandler();
337
 
      client.addListener(callbackHandler, metadata, null, true);
338
 
      log.info("added listener");
339
 
      
340
 
      // Test server invoker state.
341
 
      Thread.sleep(20000);
342
 
      log.info("waking up");
343
 
      Throwable t = invocationHandler.t;
344
 
      assertTrue(t instanceof HandleCallbackException);
345
 
      log.info("t.getCause:", t.getCause());
346
 
      if (t.getCause() instanceof InvocationFailureException)
347
 
      {
348
 
         InvocationFailureException e = (InvocationFailureException) t.getCause();
349
 
         assertNotNull(e.getMessage());
350
 
         assertTrue(e.getMessage().startsWith("Unable to perform invocation"));
351
 
         assertTrue(e.getCause() instanceof IOException);
352
 
         IOException ioe = (IOException) e.getCause();
353
 
         assertEquals("closed", ioe.getMessage());
354
 
      }
355
 
      else
356
 
      {
357
 
         assertTrue(t.getCause() instanceof CannotConnectException);
358
 
         log.info("t.getCause().getCause(): ", t.getCause().getCause());
359
 
         assertTrue(t.getCause().getCause() instanceof InvocationTargetException);
360
 
         log.info("t.getCause().getCause().getCause(): ", t.getCause().getCause().getCause());
361
 
//         assertTrue(t.getCause().getCause().getCause() instanceof SSLProtocolException);
362
 
         assertTrue(t.getCause().getCause().getCause() instanceof IOException);
363
 
         assertEquals("closed", t.getCause().getCause().getCause().getMessage());
364
 
      }
365
 
      log.info("got expected Exception");
366
 
      
367
 
      client.disconnect();
368
 
      shutdownServer();
369
 
      log.info(getName() + " PASSES");
370
 
   }
371
 
   
372
 
   
373
 
   protected abstract String getTransport();
374
 
   
375
 
   protected boolean isBisocket(String transport)
376
 
   {
377
 
      return transport.indexOf("bisocket") >= 0;
378
 
   }
379
 
   
380
 
   protected String getServerSocketFactoryClassName()
381
 
   {
382
 
      return TestServerSocketFactory.class.getName();
383
 
   }
384
 
   
385
 
   protected Constructor getServerSocketFactoryConstructor() throws NoSuchMethodException
386
 
   {
387
 
      return TestServerSocketFactory.class.getConstructor(new Class[]{int.class, int.class});
388
 
   }
389
 
   
390
 
   protected String getSocketFactoryClassName()
391
 
   {
392
 
      return TestSocketFactory.class.getName();
393
 
   }
394
 
   
395
 
   protected Constructor getSocketFactoryConstructor() throws NoSuchMethodException
396
 
   {
397
 
      return TestSocketFactory.class.getConstructor(new Class[]{int.class, int.class});
398
 
   }
399
 
   
400
 
   protected void addExtraClientConfig(Map config) {}
401
 
   protected void addExtraServerConfig(Map config) {}
402
 
   
403
 
 
404
 
   protected void setupServer(boolean setWriteTimeout, boolean setCallbackWriteTimeout,
405
 
                              String writeTimeout, int blockingTime, int initialWrites) throws Exception
406
 
   {
407
 
      host = InetAddress.getLocalHost().getHostAddress();
408
 
      port = PortUtil.findFreePort(host);
409
 
      locatorURI = getTransport() + "://" + host + ":" + port;
410
 
      String metadata = System.getProperty("remoting.metadata");
411
 
      if (metadata != null)
412
 
      {
413
 
         locatorURI += "/?" + metadata;
414
 
      }
415
 
      serverLocator = new InvokerLocator(locatorURI);
416
 
      log.info("Starting remoting server with locator uri of: " + locatorURI);
417
 
      HashMap config = new HashMap();
418
 
      config.put(InvokerLocator.FORCE_REMOTE, "true");
419
 
      if (isBisocket(getTransport()))
420
 
      {
421
 
         secondaryServerSocketPort = PortUtil.findFreePort(host);
422
 
         config.put(Bisocket.SECONDARY_BIND_PORT, Integer.toString(secondaryServerSocketPort));
423
 
         config.put(Bisocket.PING_FREQUENCY, "11111111");         
424
 
      }
425
 
      if (setWriteTimeout)
426
 
      {
427
 
         config.put(SocketWrapper.WRITE_TIMEOUT, writeTimeout);
428
 
         ServerSocketFactory ssf = (ServerSocketFactory) getServerSocketFactoryConstructor().newInstance(new Object[]{new Integer(blockingTime), new Integer(initialWrites)});
429
 
         config.put(Remoting.CUSTOM_SERVER_SOCKET_FACTORY, ssf);
430
 
      }
431
 
      if (setCallbackWriteTimeout)
432
 
      {
433
 
         config.put(SocketWrapper.WRITE_TIMEOUT, writeTimeout);
434
 
         SocketFactory sf = (SocketFactory) getSocketFactoryConstructor().newInstance(new Object[]{new Integer(blockingTime), new Integer(initialWrites)});
435
 
         config.put(Remoting.CUSTOM_SOCKET_FACTORY, sf);
436
 
      }
437
 
      if (callbackTest)
438
 
      {
439
 
         config.put("numberOfCallRetries", "1");
440
 
      }
441
 
      addExtraServerConfig(config);
442
 
      connector = new Connector(serverLocator, config);
443
 
      connector.create();
444
 
      invocationHandler = new TestInvocationHandler();
445
 
      connector.addInvocationHandler("test", invocationHandler);
446
 
      connector.start();
447
 
   }
448
 
   
449
 
   
450
 
   protected void shutdownServer() throws Exception
451
 
   {
452
 
      if (connector != null)
453
 
         connector.stop();
454
 
   }
455
 
   
456
 
   
457
 
   static class TestInvocationHandler implements ServerInvocationHandler
458
 
   {
459
 
      Throwable t;
460
 
 
461
 
      public void addListener(final InvokerCallbackHandler callbackHandler)
462
 
      {
463
 
         new Thread()
464
 
         {
465
 
            public void run()
466
 
            {
467
 
               try
468
 
               {
469
 
                  log.info("sending callback");
470
 
                  callbackHandler.handleCallback(new Callback("callback"));
471
 
               }
472
 
               catch (Throwable t)
473
 
               {
474
 
                  log.info("throwable: ", t);
475
 
                  TestInvocationHandler.this.t = t;
476
 
               }
477
 
            }
478
 
         }.start();
479
 
      }
480
 
      public Object invoke(final InvocationRequest invocation) throws Throwable
481
 
      {
482
 
         return invocation.getParameter();
483
 
      }
484
 
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
485
 
      public void setMBeanServer(MBeanServer server) {}
486
 
      public void setInvoker(ServerInvoker invoker) {}
487
 
   }
488
 
   
489
 
   
490
 
   static class TestCallbackHandler implements InvokerCallbackHandler
491
 
   {
492
 
      public void handleCallback(Callback callback) throws HandleCallbackException
493
 
      {
494
 
         log.info("received callback");
495
 
      }  
496
 
   }
497
 
   
498
 
   
499
 
   static public class TestServerSocketFactory extends ServerSocketFactory
500
 
   {
501
 
      int timeout;
502
 
      int initialWrites;
503
 
      
504
 
      public TestServerSocketFactory()
505
 
      {
506
 
         this.timeout = 5000;
507
 
         this.initialWrites = -1;
508
 
      }      
509
 
      public TestServerSocketFactory(int timeout, int initialWrites)
510
 
      {
511
 
         this.timeout = timeout;
512
 
         this.initialWrites = initialWrites;
513
 
      }
514
 
      public ServerSocket createServerSocket() throws IOException
515
 
      {
516
 
         ServerSocket ss = null;
517
 
         if (callbackTest)
518
 
         {
519
 
            ss = ServerSocketFactory.getDefault().createServerSocket();
520
 
         }
521
 
         else
522
 
         {
523
 
            ss = new TestServerSocket(timeout, initialWrites);
524
 
         }
525
 
         log.info("returning: " + ss);
526
 
         return ss;
527
 
      }
528
 
      public ServerSocket createServerSocket(int port) throws IOException
529
 
      {
530
 
         ServerSocket ss = null;
531
 
         if (callbackTest && port != secondaryServerSocketPort)
532
 
         {
533
 
            ss = ServerSocketFactory.getDefault().createServerSocket(port);
534
 
         }
535
 
         else
536
 
         {
537
 
            ss = new TestServerSocket(port, timeout, initialWrites);
538
 
         }
539
 
         log.info("returning: " + ss);
540
 
         return ss;
541
 
      }
542
 
 
543
 
      public ServerSocket createServerSocket(int port, int backlog) throws IOException
544
 
      {
545
 
         ServerSocket ss = null;
546
 
         if (callbackTest && port != secondaryServerSocketPort)
547
 
         {
548
 
            ss = ServerSocketFactory.getDefault().createServerSocket(port, backlog);
549
 
         }
550
 
         else
551
 
         {
552
 
            ss = new TestServerSocket(port, backlog, timeout, initialWrites);
553
 
         }
554
 
         log.info("returning: " + ss);
555
 
         return ss;
556
 
      }
557
 
 
558
 
      public ServerSocket createServerSocket(int port, int backlog, InetAddress ifAddress) throws IOException
559
 
      {
560
 
         ServerSocket ss = null;
561
 
         if (callbackTest && port != secondaryServerSocketPort)
562
 
         {
563
 
            ss = ServerSocketFactory.getDefault().createServerSocket(port, backlog, ifAddress);
564
 
         }
565
 
         else
566
 
         {
567
 
            ss = new TestServerSocket(port, backlog, ifAddress, timeout, initialWrites);
568
 
         }
569
 
         log.info("returning: " + ss);
570
 
         return ss;
571
 
      }
572
 
   }
573
 
   
574
 
   
575
 
   static class TestServerSocket extends ServerSocket
576
 
   {
577
 
      int timeout;
578
 
      int initialWrites;
579
 
 
580
 
      public TestServerSocket(int timeout, int initialWrites) throws IOException
581
 
      {
582
 
         super();
583
 
         this.timeout = timeout;
584
 
         this.initialWrites = initialWrites;
585
 
      }
586
 
      public TestServerSocket(int port, int timeout, int initialWrites) throws IOException
587
 
      {
588
 
         super(port);
589
 
         this.timeout = timeout;
590
 
         this.initialWrites = initialWrites;
591
 
      }
592
 
      public TestServerSocket(int port, int backlog, int timeout, int initialWrites) throws IOException
593
 
      {
594
 
         super(port, backlog);
595
 
         this.timeout = timeout;
596
 
         this.initialWrites = initialWrites;
597
 
      }
598
 
      public TestServerSocket(int port, int backlog, InetAddress bindAddr, int timeout, int initialWrites) throws IOException
599
 
      {
600
 
         super(port, backlog, bindAddr);
601
 
         this.timeout = timeout;
602
 
         this.initialWrites = initialWrites;
603
 
      }
604
 
      public Socket accept() throws IOException
605
 
      {
606
 
         Socket s = new TestSocket(timeout, initialWrites);
607
 
         implAccept(s);
608
 
         return s;
609
 
      }
610
 
      public String toString()
611
 
      {
612
 
         return "TestServerSocket[" + getLocalPort() + "]";
613
 
      }
614
 
   }
615
 
   
616
 
   
617
 
   public static class TestSocketFactory extends SocketFactory
618
 
   {
619
 
      int timeout;
620
 
      int initialWrites = -1;
621
 
      
622
 
      public TestSocketFactory()
623
 
      {
624
 
         timeout = 5000;
625
 
      }
626
 
      public TestSocketFactory(int timeout, int initialWrites)
627
 
      {
628
 
         this.timeout = timeout;
629
 
         this.initialWrites = initialWrites;
630
 
      }
631
 
      public Socket createSocket()
632
 
      {
633
 
         log.info("callbackTest: " + callbackTest);
634
 
         Socket s = null;
635
 
         if (callbackTest)
636
 
         {
637
 
            s = new Socket();
638
 
         }
639
 
         else
640
 
         {
641
 
            s = new TestSocket(timeout, initialWrites);
642
 
         }
643
 
         log.info(this + " returning " + s);
644
 
         return s;
645
 
      }
646
 
      public Socket createSocket(String arg0, int arg1) throws IOException, UnknownHostException
647
 
      {
648
 
         log.info("callbackTest: " + callbackTest + ", port: " + arg1);
649
 
         Socket s = null;
650
 
         if (callbackTest && arg1 != secondaryServerSocketPort)
651
 
         {
652
 
            s = new Socket(arg0, arg1);
653
 
         }
654
 
         else
655
 
         {
656
 
            s = new TestSocket(arg0, arg1, timeout, initialWrites);
657
 
         }
658
 
         log.info(this + " returning " + s);
659
 
         return s;
660
 
      }
661
 
 
662
 
      public Socket createSocket(InetAddress arg0, int arg1) throws IOException
663
 
      {
664
 
         log.info("callbackTest: " + callbackTest + ", port: " + arg1);
665
 
         Socket s = null;
666
 
         if (callbackTest && arg1 != secondaryServerSocketPort)
667
 
         {
668
 
            s = new Socket(arg0, arg1);
669
 
         }
670
 
         else
671
 
         {
672
 
            s = new TestSocket(arg0, arg1, timeout, initialWrites);
673
 
         }
674
 
         log.info(this + " returning " + s);
675
 
         return s;
676
 
      }
677
 
 
678
 
      public Socket createSocket(String arg0, int arg1, InetAddress arg2, int arg3) throws IOException, UnknownHostException
679
 
      {
680
 
         log.info("callbackTest: " + callbackTest + ", port: " + arg1);
681
 
         Socket s = null;
682
 
         if (callbackTest && arg1 != secondaryServerSocketPort)
683
 
         {
684
 
            s = new Socket(arg0, arg1);
685
 
         }
686
 
         else
687
 
         {
688
 
            s = new TestSocket(arg0, arg1, arg2, arg3, timeout, initialWrites);
689
 
         }
690
 
         log.info(this + " returning " + s);
691
 
         return s;
692
 
      }
693
 
 
694
 
      public Socket createSocket(InetAddress arg0, int arg1, InetAddress arg2, int arg3) throws IOException
695
 
      {
696
 
         log.info("callbackTest: " + callbackTest + ", port: " + arg1);
697
 
         Socket s = null;
698
 
         if (callbackTest && arg1 != secondaryServerSocketPort)
699
 
         {
700
 
            s = new Socket(arg0, arg1);
701
 
         }
702
 
         else
703
 
         {
704
 
            s = new TestSocket(arg0, arg1, arg2, arg3, timeout, initialWrites);
705
 
         }
706
 
         log.info(this + " returning " + s);
707
 
         return s;
708
 
      }
709
 
   }
710
 
   
711
 
   static class TestSocket extends Socket
712
 
   {
713
 
      int timeout;
714
 
      int initialWrites;
715
 
      
716
 
      public TestSocket(int timeout, int initialWrites)
717
 
      {
718
 
         this.timeout = timeout;
719
 
         this.initialWrites = initialWrites;
720
 
      }
721
 
      public TestSocket(String host, int port, int timeout, int initialWrites) throws UnknownHostException, IOException
722
 
      {
723
 
         super(host, port);
724
 
         this.timeout = timeout;
725
 
         this.initialWrites = initialWrites;
726
 
      }
727
 
      public TestSocket(InetAddress address, int port, int timeout, int initialWrites) throws IOException
728
 
      {
729
 
         super(address, port);
730
 
         this.timeout = timeout;
731
 
         this.initialWrites = initialWrites;
732
 
      }
733
 
      public TestSocket(String host, int port, InetAddress localAddr, int localPort, int timeout, int initialWrites) throws IOException
734
 
      {
735
 
         super(host, port, localAddr, localPort);
736
 
         this.timeout = timeout;
737
 
         this.initialWrites = initialWrites;
738
 
      }
739
 
      public TestSocket(InetAddress address, int port, InetAddress localAddr, int localPort, int timeout, int initialWrites) throws IOException
740
 
      {
741
 
         super(address, port, localAddr, localPort);
742
 
         this.timeout = timeout;
743
 
         this.initialWrites = initialWrites;
744
 
      }
745
 
      public OutputStream getOutputStream() throws IOException
746
 
      {
747
 
         return new TestOutputStream(super.getOutputStream(), timeout, initialWrites);
748
 
      }
749
 
      public String toString()
750
 
      {
751
 
         return "TestSocket[" + getLocalPort() + "->" + getPort() + "]";
752
 
      }
753
 
   }
754
 
 
755
 
   static class TestOutputStream extends OutputStream
756
 
   {
757
 
      OutputStream os;
758
 
      int timeout;
759
 
      boolean closed;
760
 
      int initialWrites;
761
 
      boolean doWait = true;
762
 
      public static int counter;
763
 
      
764
 
      public TestOutputStream(OutputStream os, int timeout, int initialWrites)
765
 
      {
766
 
         this.os = os;
767
 
         this.timeout = timeout;
768
 
         this.initialWrites = initialWrites;
769
 
      }
770
 
      public void close()throws IOException
771
 
      {
772
 
         closed = true;
773
 
         super.close();
774
 
         log.info(this + " closed");
775
 
      }
776
 
      public void write(int b) throws IOException
777
 
      {
778
 
         System.out.print("b: " + b);
779
 
         if (closed)
780
 
         {
781
 
            log.info("TestOutputStream closed, cannot write");
782
 
            throw new IOException("closed");
783
 
         }
784
 
         if (doWait && ++counter > initialWrites)
785
 
         {
786
 
            try
787
 
            {
788
 
               log.info("TestOutputStream.write() sleeping: " + timeout);
789
 
               Thread.sleep(timeout);
790
 
            }
791
 
            catch (InterruptedException e)
792
 
            {
793
 
               e.printStackTrace();
794
 
            }
795
 
         }
796
 
         os.write(b);
797
 
      }
798
 
      public void write(byte b[], int off, int len) throws IOException
799
 
      {
800
 
         System.out.print("b: ");
801
 
         for (int i = 0; i < len; i++)
802
 
         {
803
 
            System.out.print(b[i] + " ");
804
 
         }
805
 
         System.out.println("");
806
 
         if (closed)
807
 
         {
808
 
            log.info("TestOutputStream closed, cannot write");
809
 
            throw new IOException("closed");
810
 
         }
811
 
         log.info("TestOutputStream: counter = " + counter + ", initialWrites = " + initialWrites);
812
 
         if (++counter > initialWrites)
813
 
         {
814
 
            try
815
 
            {
816
 
               log.info("TestOutputStream.write() sleeping: " + timeout);
817
 
               Thread.sleep(timeout);
818
 
            }
819
 
            catch (InterruptedException e)
820
 
            {
821
 
               e.printStackTrace();
822
 
            }
823
 
         }
824
 
         if (closed)
825
 
         {
826
 
            log.info("TestOutputStream closed, cannot write");
827
 
            throw new IOException("closed");
828
 
         }
829
 
         try
830
 
         {
831
 
            log.info(this + " calling write()");
832
 
            doWait = false;
833
 
            os.write(b, off, len);
834
 
            os.flush();
835
 
            doWait = true;
836
 
            log.info(this + " back from write()");
837
 
         }
838
 
         catch (IOException e)
839
 
         {
840
 
            log.info("exception: ", e);
841
 
            throw e;
842
 
         }
843
 
      }
844
 
   }
845
 
}
 
 
b'\\ No newline at end of file'