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

« back to all changes in this revision

Viewing changes to src/tests/org/jboss/test/remoting/connection/identity/LeaseIdentityTestCase.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, Red Hat Middleware LLC, and individual contributors
 
4
 * as indicated by the @author tags. See the copyright.txt file in the
 
5
 * distribution for a 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
 
 
23
package org.jboss.test.remoting.connection.identity;
 
24
 
 
25
import java.net.InetAddress;
 
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.logging.XLevel;
 
38
import org.jboss.remoting.Client;
 
39
import org.jboss.remoting.ConnectionListener;
 
40
import org.jboss.remoting.InvocationRequest;
 
41
import org.jboss.remoting.InvokerLocator;
 
42
import org.jboss.remoting.LeasePinger;
 
43
import org.jboss.remoting.MicroRemoteClientInvoker;
 
44
import org.jboss.remoting.Remoting;
 
45
import org.jboss.remoting.ServerInvocationHandler;
 
46
import org.jboss.remoting.ServerInvoker;
 
47
import org.jboss.remoting.callback.InvokerCallbackHandler;
 
48
import org.jboss.remoting.transport.ClientInvoker;
 
49
import org.jboss.remoting.transport.Connector;
 
50
import org.jboss.remoting.transport.PortUtil;
 
51
import org.jboss.util.id.GUID;
 
52
 
 
53
 
 
54
/**
 
55
 * Unit test for JBREM-1133.
 
56
 * 
 
57
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
 
58
 * @version $Revision: 1.1 $
 
59
 * <p>
 
60
 * Copyright May 08, 2009
 
61
 * </p>
 
62
 */
 
63
public class LeaseIdentityTestCase extends TestCase
 
64
{
 
65
   private static Logger log = Logger.getLogger(LeaseIdentityTestCase.class);
 
66
   
 
67
   protected static long LEASE_PERIOD = 2000;
 
68
   protected static String LEASE_PERIOD_STRING = "2000";
 
69
   
 
70
   private static boolean firstTime = true;
 
71
   
 
72
   protected String host;
 
73
   protected int port;
 
74
   protected String locatorURI;
 
75
   protected InvokerLocator serverLocator;
 
76
   protected Connector connector;
 
77
   protected TestInvocationHandler invocationHandler;
 
78
   protected TestConnectionListener listener;
 
79
 
 
80
   
 
81
   public void setUp() throws Exception
 
82
   {
 
83
      if (firstTime)
 
84
      {
 
85
         firstTime = false;
 
86
         Logger.getLogger("org.jboss.remoting").setLevel(XLevel.INFO);
 
87
         Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
 
88
         String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
 
89
         PatternLayout layout = new PatternLayout(pattern);
 
90
         ConsoleAppender consoleAppender = new ConsoleAppender(layout);
 
91
         Logger.getRootLogger().addAppender(consoleAppender);  
 
92
      }
 
93
   }
 
94
 
 
95
   
 
96
   public void tearDown()
 
97
   {
 
98
   }
 
99
   
 
100
   
 
101
   public void testDefault() throws Throwable
 
102
   {
 
103
      log.info("entering " + getName());
 
104
      
 
105
      // Start server.
 
106
      setupServer(false, null);
 
107
      
 
108
      // Create client.
 
109
      InvokerLocator clientLocator = new InvokerLocator(locatorURI);
 
110
      HashMap clientConfig = new HashMap();
 
111
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
112
      clientConfig.put(Client.ENABLE_LEASE, "true");
 
113
      addExtraClientConfig(clientConfig);
 
114
      Client client = new Client(clientLocator, clientConfig);
 
115
      client.connect();
 
116
      log.info("client is connected");
 
117
      
 
118
      // Test connection.
 
119
      assertEquals("abc", client.invoke("abc"));
 
120
      log.info("connection is good");
 
121
      
 
122
      // Test lease behavior.
 
123
      MicroRemoteClientInvoker clientInvoker = (MicroRemoteClientInvoker) client.getInvoker();
 
124
      clientInvoker.terminateLease(client.getSessionId(), 0);
 
125
      TestLeasePinger leasePinger = new TestLeasePinger(clientInvoker, clientInvoker.getSessionId(), LEASE_PERIOD);
 
126
      leasePinger.setLeasePingerId(new GUID().toString());
 
127
      leasePinger.addClient(client.getSessionId(), client.getConfiguration(), LEASE_PERIOD);
 
128
      leasePinger.startPing();
 
129
      Thread.sleep(LEASE_PERIOD * 4);
 
130
      assertFalse(listener.called);
 
131
      
 
132
      leasePinger.stopPing();
 
133
      client.disconnect();
 
134
      shutdownServer();
 
135
      log.info(getName() + " PASSES");
 
136
   }
 
137
   
 
138
   
 
139
   public void testClientConnectionIdentityFalse() throws Throwable
 
140
   {
 
141
      log.info("entering " + getName());
 
142
      
 
143
      // Start server.
 
144
      setupServer(true, "false");
 
145
      
 
146
      // Create client.
 
147
      InvokerLocator clientLocator = new InvokerLocator(locatorURI);
 
148
      HashMap clientConfig = new HashMap();
 
149
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
150
      clientConfig.put(Client.ENABLE_LEASE, "true");
 
151
      addExtraClientConfig(clientConfig);
 
152
      Client client = new Client(clientLocator, clientConfig);
 
153
      client.connect();
 
154
      log.info("client is connected");
 
155
      
 
156
      // Test connection.
 
157
      assertEquals("abc", client.invoke("abc"));
 
158
      log.info("connection is good");
 
159
      
 
160
      // Test lease behavior.
 
161
      MicroRemoteClientInvoker clientInvoker = (MicroRemoteClientInvoker) client.getInvoker();
 
162
      clientInvoker.terminateLease(client.getSessionId(), 0);
 
163
      TestLeasePinger leasePinger = new TestLeasePinger(clientInvoker, clientInvoker.getSessionId(), LEASE_PERIOD);
 
164
      leasePinger.setLeasePingerId(new GUID().toString());
 
165
      leasePinger.addClient(client.getSessionId(), client.getConfiguration(), LEASE_PERIOD);
 
166
      leasePinger.startPing();
 
167
      Thread.sleep(LEASE_PERIOD * 4);
 
168
      assertFalse(listener.called);
 
169
      
 
170
      leasePinger.stopPing();
 
171
      client.disconnect();
 
172
      shutdownServer();
 
173
      log.info(getName() + " PASSES");
 
174
   }
 
175
   
 
176
   
 
177
   public void testClientConnectionIdentityTrue() throws Throwable
 
178
   {
 
179
      log.info("entering " + getName());
 
180
      
 
181
      // Start server.
 
182
      setupServer(true, "true");
 
183
      
 
184
      // Create client.
 
185
      InvokerLocator clientLocator = new InvokerLocator(locatorURI);
 
186
      HashMap clientConfig = new HashMap();
 
187
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
188
      clientConfig.put(Client.ENABLE_LEASE, "true");
 
189
      addExtraClientConfig(clientConfig);
 
190
      Client client = new Client(clientLocator, clientConfig);
 
191
      client.connect();
 
192
      log.info("client is connected");
 
193
      
 
194
      // Test connection.
 
195
      assertEquals("abc", client.invoke("abc"));
 
196
      log.info("connection is good");
 
197
      
 
198
      // Test lease behavior.
 
199
      MicroRemoteClientInvoker clientInvoker = (MicroRemoteClientInvoker) client.getInvoker();
 
200
      clientInvoker.terminateLease(client.getSessionId(), 0);
 
201
      TestLeasePinger leasePinger = new TestLeasePinger(clientInvoker, clientInvoker.getSessionId(), LEASE_PERIOD);
 
202
      leasePinger.setLeasePingerId(new GUID().toString());
 
203
      leasePinger.addClient(client.getSessionId(), client.getConfiguration(), LEASE_PERIOD);
 
204
      leasePinger.startPing();
 
205
      Thread.sleep(LEASE_PERIOD * 4);
 
206
      assertTrue(listener.called);
 
207
      assertNull(listener.throwable);
 
208
      
 
209
      leasePinger.stopPing();
 
210
      client.disconnect();
 
211
      shutdownServer();
 
212
      log.info(getName() + " PASSES");
 
213
   }
 
214
   
 
215
   
 
216
   protected String getTransport()
 
217
   {
 
218
      return "socket";
 
219
   }
 
220
   
 
221
   
 
222
   protected void addExtraClientConfig(Map config) {}
 
223
   protected void addExtraServerConfig(Map config) {}
 
224
   
 
225
 
 
226
   protected void setupServer(boolean setUseClientIdentity, String useClientIdentity) throws Exception
 
227
   {
 
228
      host = InetAddress.getLocalHost().getHostAddress();
 
229
      port = PortUtil.findFreePort(host);
 
230
      locatorURI = getTransport() + "://" + host + ":" + port;
 
231
      String metadata = System.getProperty("remoting.metadata");
 
232
      if (metadata != null)
 
233
      {
 
234
         locatorURI += "/?" + metadata;
 
235
      }
 
236
      serverLocator = new InvokerLocator(locatorURI);
 
237
      log.info("Starting remoting server with locator uri of: " + locatorURI);
 
238
      HashMap config = new HashMap();
 
239
      config.put(InvokerLocator.FORCE_REMOTE, "true");
 
240
      config.put("leasePeriod", LEASE_PERIOD_STRING);
 
241
      if (setUseClientIdentity)
 
242
      {
 
243
         config.put(Remoting.USE_CLIENT_CONNECTION_IDENTITY, useClientIdentity);
 
244
      }
 
245
      addExtraServerConfig(config);
 
246
      connector = new Connector(serverLocator, config);
 
247
      connector.create();
 
248
      invocationHandler = new TestInvocationHandler();
 
249
      connector.addInvocationHandler("test", invocationHandler);
 
250
      connector.start();
 
251
      listener = new TestConnectionListener();
 
252
      connector.addConnectionListener(listener);
 
253
   }
 
254
   
 
255
   
 
256
   protected void shutdownServer() throws Exception
 
257
   {
 
258
      if (connector != null)
 
259
         connector.stop();
 
260
   }
 
261
   
 
262
   
 
263
   static class TestInvocationHandler implements ServerInvocationHandler
 
264
   {
 
265
      public void addListener(InvokerCallbackHandler callbackHandler) {}
 
266
      public Object invoke(final InvocationRequest invocation) throws Throwable
 
267
      {
 
268
         return invocation.getParameter();
 
269
      }
 
270
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
 
271
      public void setMBeanServer(MBeanServer server) {}
 
272
      public void setInvoker(ServerInvoker invoker) {}
 
273
   }
 
274
   
 
275
 
 
276
   static class TestConnectionListener implements ConnectionListener
 
277
   {
 
278
      public boolean called;
 
279
      public Throwable throwable;
 
280
      
 
281
      public void handleConnectionException(Throwable throwable, Client client)
 
282
      {
 
283
         called = true;
 
284
         this.throwable = throwable;
 
285
         log.info("called: throwable = " + throwable);
 
286
      }  
 
287
   }
 
288
   
 
289
   
 
290
   static class TestLeasePinger extends LeasePinger
 
291
   {
 
292
      public TestLeasePinger(ClientInvoker invoker, String invokerSessionID, long defaultLeasePeriod)
 
293
      {
 
294
         super(invoker, invokerSessionID, defaultLeasePeriod);
 
295
      }
 
296
 
 
297
      public void setLeasePingerId(String leasePingerId)
 
298
      {
 
299
         super.setLeasePingerId(leasePingerId);
 
300
      }
 
301
   }
 
302
}
 
 
b'\\ No newline at end of file'