~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/idle/SafeIdleTimeoutTestCase.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.transport.socket.timeout.idle;
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.remoting.Client;
38
 
import org.jboss.remoting.InvocationRequest;
39
 
import org.jboss.remoting.InvokerLocator;
40
 
import org.jboss.remoting.ServerInvocationHandler;
41
 
import org.jboss.remoting.ServerInvoker;
42
 
import org.jboss.remoting.callback.InvokerCallbackHandler;
43
 
import org.jboss.remoting.transport.Connector;
44
 
import org.jboss.remoting.transport.PortUtil;
45
 
import org.jboss.remoting.transport.socket.SocketServerInvoker;
46
 
 
47
 
 
48
 
/**
49
 
 * Unit test for JBREM-1107.
50
 
 * 
51
 
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
52
 
 * @version 
53
 
 * <p>
54
 
 * Copyright Sep 2, 2009
55
 
 * </p>
56
 
 */
57
 
public class SafeIdleTimeoutTestCase extends TestCase
58
 
{
59
 
   private static Logger log = Logger.getLogger(SafeIdleTimeoutTestCase.class);
60
 
   
61
 
   private static boolean firstTime = true;
62
 
   
63
 
   protected String host;
64
 
   protected int port;
65
 
   protected String locatorURI;
66
 
   protected InvokerLocator serverLocator;
67
 
   protected Connector connector;
68
 
   protected TestInvocationHandler invocationHandler;
69
 
 
70
 
   
71
 
   public void setUp() throws Exception
72
 
   {
73
 
      if (firstTime)
74
 
      {
75
 
         firstTime = false;
76
 
         Logger.getLogger("org.jboss.remoting").setLevel(Level.INFO);
77
 
         Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
78
 
         String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
79
 
         PatternLayout layout = new PatternLayout(pattern);
80
 
         ConsoleAppender consoleAppender = new ConsoleAppender(layout);
81
 
         Logger.getRootLogger().addAppender(consoleAppender);  
82
 
      }
83
 
   }
84
 
 
85
 
   
86
 
   public void tearDown()
87
 
   {
88
 
   }
89
 
   
90
 
   
91
 
   public void testSafeIdleTimeout() throws Throwable
92
 
   {
93
 
      log.info("entering " + getName());
94
 
      
95
 
      // Start server.
96
 
      setupServer();
97
 
      
98
 
      // Create client.
99
 
      InvokerLocator clientLocator = new InvokerLocator(locatorURI);
100
 
      HashMap clientConfig = new HashMap();
101
 
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
102
 
      clientConfig.put("numberOfCallRetries", "1");
103
 
      addExtraClientConfig(clientConfig);
104
 
      Client client = new Client(clientLocator, clientConfig);
105
 
      client.connect();
106
 
      log.info("client is connected");
107
 
      
108
 
      // Verify invocation can complete.
109
 
      assertEquals("abc", client.invoke("abc"));
110
 
      
111
 
      // Verify ServerThread was discarded.
112
 
      SocketServerInvoker serverInvoker = (SocketServerInvoker) connector.getServerInvoker();
113
 
      assertEquals(0, serverInvoker.getCurrentClientPoolSize());
114
 
      assertEquals(0, serverInvoker.getCurrentThreadPoolSize());
115
 
      
116
 
      client.disconnect();
117
 
      shutdownServer();
118
 
      log.info(getName() + " PASSES");
119
 
   }
120
 
   
121
 
   
122
 
   protected String getTransport()
123
 
   {
124
 
      return "socket";
125
 
   }
126
 
   
127
 
   
128
 
   protected void addExtraClientConfig(Map config) {}
129
 
   protected void addExtraServerConfig(Map config) {}
130
 
   
131
 
 
132
 
   protected void setupServer() throws Exception
133
 
   {
134
 
      host = InetAddress.getLocalHost().getHostAddress();
135
 
      port = PortUtil.findFreePort(host);
136
 
      locatorURI = getTransport() + "://" + host + ":" + port + "/?&idleTimeout=2";
137
 
      String metadata = System.getProperty("remoting.metadata");
138
 
      if (metadata != null)
139
 
      {
140
 
         locatorURI += "&" + metadata;
141
 
      }
142
 
      serverLocator = new InvokerLocator(locatorURI);
143
 
      log.info("Starting remoting server with locator uri of: " + locatorURI);
144
 
      HashMap config = new HashMap();
145
 
      config.put(InvokerLocator.FORCE_REMOTE, "true");
146
 
      addExtraServerConfig(config);
147
 
      connector = new Connector(serverLocator, config);
148
 
      connector.create();
149
 
      invocationHandler = new TestInvocationHandler();
150
 
      connector.addInvocationHandler("test", invocationHandler);
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
 
         log.info(this + " going to sleep");
168
 
         Thread.sleep(10000);
169
 
         log.info(this + " has woken up");
170
 
         return invocation.getParameter();
171
 
      }
172
 
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
173
 
      public void setMBeanServer(MBeanServer server) {}
174
 
      public void setInvoker(ServerInvoker invoker) {}
175
 
   }
176
 
}
 
 
b'\\ No newline at end of file'