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

« back to all changes in this revision

Viewing changes to tests/org/jboss/test/remoting/lease/UniquePingerTaskTestCase.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 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.lease;
23
 
 
24
 
import java.lang.reflect.Field;
25
 
import java.lang.reflect.Method;
26
 
import java.net.InetAddress;
27
 
import java.util.HashMap;
28
 
import java.util.TimerTask;
29
 
 
30
 
import javax.management.MBeanServer;
31
 
 
32
 
import junit.framework.TestCase;
33
 
 
34
 
import org.apache.log4j.ConsoleAppender;
35
 
import org.apache.log4j.Level;
36
 
import org.apache.log4j.Logger;
37
 
import org.apache.log4j.PatternLayout;
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.ServerInvocationHandler;
44
 
import org.jboss.remoting.ServerInvoker;
45
 
import org.jboss.remoting.callback.InvokerCallbackHandler;
46
 
import org.jboss.remoting.transport.Connector;
47
 
import org.jboss.remoting.transport.PortUtil;
48
 
import org.jboss.remoting.transport.socket.SocketClientInvoker;
49
 
 
50
 
 
51
 
/**
52
 
 * Unit test for JBREM-794.
53
 
 * 
54
 
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
55
 
 * @version $Revision: 5467 $
56
 
 * <p>
57
 
 * Copyright Aug 22, 2007
58
 
 * </p>
59
 
 */
60
 
public class UniquePingerTaskTestCase extends TestCase
61
 
{
62
 
   private static Logger log = Logger.getLogger(UniquePingerTaskTestCase.class);
63
 
   private static boolean firstTime = true;
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
 
   /**
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
 
   /**
97
 
    * Verifies that only one LeasePinger.LeaseTimerTask gets created.
98
 
    */
99
 
   public void testUniqueLeaseTimerTask() throws Throwable
100
 
   {
101
 
      log.info("entering " + getName());
102
 
      
103
 
      setupServer();
104
 
      SocketClientInvoker clientInvoker = new SocketClientInvoker(serverLocator);
105
 
      clientInvoker.connect();
106
 
      LeasePinger leasePinger = new LeasePinger(clientInvoker, "abc", 2000);
107
 
      log.info("expect WARN \"failed to ping to server\"");
108
 
      Method method = LeasePinger.class.getDeclaredMethod("setLeasePingerId", new Class[]{String.class});
109
 
      method.setAccessible(true);
110
 
      method.invoke(leasePinger, new Object[]{"dummyID"});
111
 
      leasePinger.addClient("def", null, 1000);
112
 
      
113
 
      // Verify LeasePingerTimerTask has not been created.
114
 
      Field field = LeasePinger.class.getDeclaredField("timerTask");
115
 
      field.setAccessible(true);
116
 
      TimerTask timerTask = (TimerTask) field.get(leasePinger);
117
 
      assertNull(timerTask);
118
 
      leasePinger.startPing();
119
 
      timerTask = (TimerTask) field.get(leasePinger);
120
 
      assertNotNull(timerTask);
121
 
      
122
 
      // Verify new LeasePingerTimerTask gets created when adding new Client
123
 
      // with shorter lease period.
124
 
      leasePinger.addClient("ghi", null, 500);
125
 
      TimerTask newTimerTask = (TimerTask) field.get(leasePinger);
126
 
      assertNotSame(newTimerTask, timerTask);
127
 
      
128
 
      log.info(getName() + " PASSES");
129
 
   }
130
 
   
131
 
   protected void setupServer() throws Exception
132
 
   {
133
 
      host = InetAddress.getLocalHost().getHostAddress();
134
 
      port = PortUtil.findFreePort(host);
135
 
      locatorURI = "socket://" + host + ":" + port;
136
 
      String metadata = System.getProperty("remoting.metadata");
137
 
      if (metadata != null)
138
 
      {
139
 
         locatorURI += "/?" + metadata;
140
 
      }
141
 
      serverLocator = new InvokerLocator(locatorURI);
142
 
      log.info("Starting remoting server with locator uri of: " + locatorURI);
143
 
      HashMap config = new HashMap();
144
 
      config.put(InvokerLocator.FORCE_REMOTE, "true");
145
 
      config.put("leasePeriod", "1000");
146
 
      connector = new Connector(serverLocator, config);
147
 
      connector.create();
148
 
      invocationHandler = new TestInvocationHandler();
149
 
      connector.addInvocationHandler("test", invocationHandler);
150
 
      connector.addConnectionListener(new TestConnectionListener());
151
 
      connector.start();
152
 
   }
153
 
   
154
 
   
155
 
   protected void shutdownServer() throws Exception
156
 
   {
157
 
      if (connector != null)
158
 
         connector.stop();
159
 
   }
160
 
   
161
 
   
162
 
   static class TestInvocationHandler implements ServerInvocationHandler
163
 
   {
164
 
      public void addListener(InvokerCallbackHandler callbackHandler) {}
165
 
      public Object invoke(final InvocationRequest invocation) throws Throwable
166
 
      {
167
 
         return invocation.getParameter();
168
 
      }
169
 
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
170
 
      public void setMBeanServer(MBeanServer server) {}
171
 
      public void setInvoker(ServerInvoker invoker) {}
172
 
   }
173
 
   
174
 
   
175
 
   static class TestConnectionListener implements ConnectionListener
176
 
   {
177
 
      public void handleConnectionException(Throwable throwable, Client client)
178
 
      {
179
 
      }  
180
 
   }
181
 
}
 
 
b'\\ No newline at end of file'