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

« back to all changes in this revision

Viewing changes to tests/org/jboss/test/remoting/shutdown/AbstractClient.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.shutdown;
23
 
 
24
 
import java.net.InetAddress;
25
 
import java.util.HashMap;
26
 
import java.util.Map;
27
 
 
28
 
import junit.framework.TestCase;
29
 
 
30
 
import org.apache.log4j.Logger;
31
 
import org.jboss.remoting.Client;
32
 
import org.jboss.remoting.ConnectionListener;
33
 
import org.jboss.remoting.InvokerLocator;
34
 
import org.jboss.remoting.ServerInvoker;
35
 
import org.jboss.remoting.callback.Callback;
36
 
import org.jboss.remoting.callback.CallbackPoller;
37
 
import org.jboss.remoting.callback.HandleCallbackException;
38
 
import org.jboss.remoting.callback.InvokerCallbackHandler;
39
 
 
40
 
/** 
41
 
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
42
 
 * @version $Revision: 3011 $
43
 
 * <p>
44
 
 * Copyright Jan 19, 2007
45
 
 * </p>
46
 
 */
47
 
public abstract class AbstractClient extends TestCase
48
 
{    
49
 
   private static Logger log = Logger.getLogger(AbstractClient.class);
50
 
   private String transport;
51
 
   private Map extraConfig;
52
 
   
53
 
   
54
 
   public AbstractClient(String transport, Map config)
55
 
   {
56
 
      this.transport = transport;
57
 
      this.extraConfig = new HashMap(config);
58
 
      log.info("client transport: " + transport);
59
 
      log.info("log4j.configuration: " + System.getProperty("log4j.configuration"));
60
 
      Runtime.getRuntime().traceMethodCalls(true);
61
 
   }
62
 
   
63
 
   
64
 
   /**
65
 
    * This test is used to verify that a JVM with a client connected to a server will shut
66
 
    * down.  To exercise as many threads as possible, it enables leasing, registers a
67
 
    * connection listener, and registers a callback handler for blocking polled callbacks
68
 
    * and another callback handler for nonblocking polled callbacks.
69
 
    * 
70
 
    * At the end of the method, it creates a Thread which runs longer that this test is
71
 
    * supposed to last.  According to the value returned by the overridden abstract
72
 
    * method daemon(), it the Thread will be a daemon or non-daemon thread.
73
 
    */
74
 
   public void testShutdown() throws Throwable
75
 
   {
76
 
      try
77
 
      {
78
 
         String host = InetAddress.getLocalHost().getHostAddress();
79
 
         String portString = System.getProperty("port");
80
 
         int port = Integer.parseInt(portString);
81
 
         String locatorURI = transport + "://" + host + ":" + port;
82
 
         InvokerLocator locator = new InvokerLocator(locatorURI);
83
 
         HashMap clientConfig = new HashMap(extraConfig);
84
 
         clientConfig.put(Client.ENABLE_LEASE, "true");
85
 
         clientConfig.put(InvokerLocator.CLIENT_LEASE_PERIOD, "1000");
86
 
         Client client = new Client(locator, clientConfig);
87
 
         client.connect();
88
 
         log.info("client connected");
89
 
         log.info("READY");
90
 
         ConnectionListener listener = new ShutdownTestServer.TestListener();
91
 
         client.addConnectionListener(listener, 1000);
92
 
         Integer i = (Integer) client.invoke(new Integer(17));
93
 
         if (18 != i.intValue())
94
 
            throw new Exception("invocation failed");
95
 
         log.info("invocation successful");
96
 
         TestCallbackHandler callbackHandler1 = new TestCallbackHandler();
97
 
         HashMap metadata = new HashMap();
98
 
         metadata.put(ServerInvoker.BLOCKING_TIMEOUT, "2000");
99
 
         metadata.put(ServerInvoker.BLOCKING_MODE, ServerInvoker.BLOCKING);
100
 
         addCallbackArgs(metadata);
101
 
         log.info("metadata: " + metadata);
102
 
         client.addListener(callbackHandler1, metadata, null, false);
103
 
         log.info("added blocking listener 1");
104
 
         TestCallbackHandler callbackHandler2 = new TestCallbackHandler();
105
 
         metadata.clear();
106
 
         metadata.put(CallbackPoller.CALLBACK_POLL_PERIOD, "500");
107
 
         metadata.put(ServerInvoker.BLOCKING_MODE, ServerInvoker.NONBLOCKING);
108
 
         addCallbackArgs(metadata);
109
 
         log.info("metadata: " + metadata);
110
 
         client.addListener(callbackHandler2, metadata, null, false);
111
 
         log.info("added nonblocking listener 2");
112
 
         Thread.sleep(4000);
113
 
         if (!callbackHandler1.receivedCallback)
114
 
         {
115
 
            log.info("callback 1 failed");
116
 
            throw new Exception("callback 1 failed");
117
 
         }
118
 
         if (!callbackHandler2.receivedCallback)
119
 
         {
120
 
            log.info("callback 2 failed");
121
 
            throw new Exception("callback 2 failed");
122
 
         }
123
 
         log.info("callback successful");
124
 
         client.removeConnectionListener(listener);
125
 
         log.info("calling removeListener(): 1");
126
 
         client.removeListener(callbackHandler1);
127
 
         log.info("calling removeListener(): 2");
128
 
         client.removeListener(callbackHandler2);
129
 
         log.info("calling disconnect()");
130
 
         client.disconnect();
131
 
         Thread t = new Thread()
132
 
         {
133
 
            public void run()
134
 
            {
135
 
               try
136
 
               {
137
 
                  Thread.sleep(20000);
138
 
               }
139
 
               catch (InterruptedException e)
140
 
               {
141
 
                  log.info("interrupted");
142
 
               }
143
 
            }
144
 
         };
145
 
         t.setDaemon(daemon());
146
 
         t.start();
147
 
         log.info("client disconnected");
148
 
      }
149
 
      catch (Exception e)
150
 
      {
151
 
         log.info("exception in client: " + e);
152
 
         System.exit(1);
153
 
      }
154
 
   }
155
 
   
156
 
   
157
 
   abstract protected boolean daemon();
158
 
   
159
 
   
160
 
   protected void addCallbackArgs(Map map)
161
 
   {
162
 
      return;
163
 
   }
164
 
   
165
 
   
166
 
   protected static void getConfig(Map config, String configs)
167
 
   {
168
 
      int start = 0;
169
 
      int ampersand = configs.indexOf('&');
170
 
      while (ampersand > 0)
171
 
      {
172
 
         String s = configs.substring(start, ampersand);
173
 
         int equals = s.indexOf('=');
174
 
         String param = s.substring(0, equals);
175
 
         String value = s.substring(equals + 1);
176
 
         config.put(param, value);
177
 
         start = ampersand + 1;
178
 
         ampersand = configs.indexOf('&', start);
179
 
      }
180
 
   }
181
 
   
182
 
   
183
 
   public class TestCallbackHandler implements InvokerCallbackHandler
184
 
   {  
185
 
      public boolean receivedCallback;
186
 
      
187
 
      public void handleCallback(Callback callback) throws HandleCallbackException
188
 
      {
189
 
         receivedCallback = true;
190
 
         log.info("received callback: " + callback);
191
 
      }  
192
 
   }
193
 
}