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

« back to all changes in this revision

Viewing changes to src/tests/org/jboss/test/remoting/datatype/DataTypeRaceTestCase.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 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.datatype;
 
23
 
 
24
import java.net.InetAddress;
 
25
import java.util.HashMap;
 
26
import java.util.Map;
 
27
 
 
28
import javax.management.MBeanServer;
 
29
 
 
30
import junit.framework.TestCase;
 
31
 
 
32
import org.apache.log4j.ConsoleAppender;
 
33
import org.apache.log4j.Level;
 
34
import org.apache.log4j.Logger;
 
35
import org.apache.log4j.PatternLayout;
 
36
import org.jboss.remoting.Client;
 
37
import org.jboss.remoting.InvocationRequest;
 
38
import org.jboss.remoting.InvokerLocator;
 
39
import org.jboss.remoting.MicroRemoteClientInvoker;
 
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.ClientInvoker;
 
44
import org.jboss.remoting.transport.Connector;
 
45
import org.jboss.remoting.transport.PortUtil;
 
46
 
 
47
import EDU.oswego.cs.dl.util.concurrent.Rendezvous;
 
48
 
 
49
 
 
50
/**
 
51
 * Unit test for JBREM-1109.
 
52
 * 
 
53
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
 
54
 * @version 
 
55
 * <p>
 
56
 * Copyright Apr 8, 2009
 
57
 * </p>
 
58
 */
 
59
public class DataTypeRaceTestCase extends TestCase
 
60
{
 
61
   private static Logger log = Logger.getLogger(DataTypeRaceTestCase.class);
 
62
   
 
63
   private static boolean firstTime = true;
 
64
   protected static String dataType;
 
65
   
 
66
   protected String host;
 
67
   protected int port;
 
68
   protected String locatorURI;
 
69
   protected InvokerLocator serverLocator;
 
70
   protected Connector connector;
 
71
   protected TestInvocationHandler invocationHandler;
 
72
   protected Object lock = new Object();
 
73
 
 
74
   
 
75
   public void setUp() throws Exception
 
76
   {
 
77
      if (firstTime)
 
78
      {
 
79
         firstTime = false;
 
80
         Logger.getLogger("org.jboss.remoting").setLevel(Level.INFO);
 
81
         Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
 
82
         String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
 
83
         PatternLayout layout = new PatternLayout(pattern);
 
84
         ConsoleAppender consoleAppender = new ConsoleAppender(layout);
 
85
         Logger.getRootLogger().addAppender(consoleAppender);  
 
86
      }
 
87
   }
 
88
 
 
89
   
 
90
   public void tearDown()
 
91
   {
 
92
   }
 
93
   
 
94
   
 
95
   public void testDataTypeRace() throws Throwable
 
96
   {
 
97
      log.info("entering " + getName());
 
98
      
 
99
      // Start server.
 
100
      setupServer();
 
101
      
 
102
      // Create client.
 
103
      InvokerLocator clientLocator = new InvokerLocator(locatorURI);
 
104
      HashMap clientConfig = new HashMap();
 
105
      clientConfig.put(InvokerLocator.FORCE_REMOTE, "true");
 
106
      addExtraClientConfig(clientConfig);
 
107
      Client client = new Client(clientLocator, clientConfig);
 
108
      client.connect();
 
109
      log.info("client is connected");
 
110
      
 
111
      // Test datatype race.
 
112
      MicroRemoteClientInvoker clientInvoker = (MicroRemoteClientInvoker) client.getInvoker();
 
113
      
 
114
      int THREADS = 1000;
 
115
      TestThread[] threads = new TestThread[THREADS];
 
116
      Rendezvous startBarrier = new Rendezvous(THREADS);
 
117
      Rendezvous stopBarrier = new Rendezvous(THREADS + 1);
 
118
      
 
119
      log.info(getName() + " creating " + THREADS + " threads");
 
120
      for (int i = 0; i < THREADS; i++)
 
121
      {
 
122
         threads[i] = new TestThread(clientInvoker, startBarrier, stopBarrier, i);
 
123
         threads[i].start();
 
124
      }
 
125
      
 
126
      log.info(getName() + " waiting on stopBarrier");
 
127
      rendezvous(stopBarrier);
 
128
      log.info(getName() + " checking threads");
 
129
      
 
130
      for (int i = 0; i < THREADS; i++)
 
131
      {
 
132
         assertTrue("failure in " + threads[i], threads[i].ok);
 
133
      }
 
134
      
 
135
      client.disconnect();
 
136
      shutdownServer();
 
137
      log.info(getName() + " PASSES");
 
138
   }
 
139
   
 
140
   
 
141
   protected String getTransport()
 
142
   {
 
143
      return "socket";
 
144
   }
 
145
   
 
146
   
 
147
   protected void addExtraClientConfig(Map config) {}
 
148
   protected void addExtraServerConfig(Map config) {}
 
149
   
 
150
 
 
151
   protected void setupServer() throws Exception
 
152
   {
 
153
      host = InetAddress.getLocalHost().getHostAddress();
 
154
      port = PortUtil.findFreePort(host);
 
155
      locatorURI = getTransport() + "://" + host + ":" + port;
 
156
      String metadata = System.getProperty("remoting.metadata");
 
157
      if (metadata != null)
 
158
      {
 
159
         locatorURI += "/?" + metadata;
 
160
      }
 
161
      serverLocator = new InvokerLocator(locatorURI);
 
162
      log.info("Starting remoting server with locator uri of: " + locatorURI);
 
163
      HashMap config = new HashMap();
 
164
      config.put(InvokerLocator.FORCE_REMOTE, "true");
 
165
      addExtraServerConfig(config);
 
166
      connector = new Connector(serverLocator, config);
 
167
      connector.create();
 
168
      invocationHandler = new TestInvocationHandler();
 
169
      connector.addInvocationHandler("test", invocationHandler);
 
170
      connector.start();
 
171
   }
 
172
   
 
173
   
 
174
   protected void shutdownServer() throws Exception
 
175
   {
 
176
      if (connector != null)
 
177
         connector.stop();
 
178
   }
 
179
   
 
180
   
 
181
   protected static void rendezvous(Rendezvous barrier)
 
182
   {
 
183
      while (true)
 
184
      {
 
185
         try
 
186
         {
 
187
            barrier.rendezvous(null);
 
188
            break;
 
189
         }
 
190
         catch (InterruptedException e1)
 
191
         {
 
192
 
 
193
         }
 
194
      }
 
195
   }
 
196
   
 
197
   
 
198
   static class TestInvocationHandler implements ServerInvocationHandler
 
199
   {
 
200
      public void addListener(InvokerCallbackHandler callbackHandler) {}
 
201
      public Object invoke(final InvocationRequest invocation) throws Throwable
 
202
      {
 
203
         return invocation.getParameter();
 
204
      }
 
205
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
 
206
      public void setMBeanServer(MBeanServer server) {}
 
207
      public void setInvoker(ServerInvoker invoker) {}
 
208
   }
 
209
   
 
210
   static class TestThread extends Thread
 
211
   {
 
212
      String name;
 
213
      ClientInvoker clientInvoker;
 
214
      Rendezvous startBarrier;
 
215
      Rendezvous stopBarrier;
 
216
      InvocationRequest request = new InvocationRequest(null, null, "abc", null, null, null);
 
217
      boolean ok;
 
218
      
 
219
      public TestThread(ClientInvoker clientInvoker, Rendezvous startBarrier, Rendezvous stopBarrier, int number)
 
220
      {
 
221
         this.clientInvoker = clientInvoker;
 
222
         this.startBarrier = startBarrier;
 
223
         this.stopBarrier = stopBarrier;
 
224
         name = "TestThread[" + number + "]";
 
225
      }
 
226
      
 
227
      public void run()
 
228
      {
 
229
//         log.debug(this + " waiting on startBarrier");
 
230
         rendezvous(startBarrier);
 
231
//         log.debug(this + " executing");
 
232
         try
 
233
         {
 
234
               clientInvoker.invoke(request);
 
235
//            log.debug(this + " waiting on stopBarrier");
 
236
            ok = true;
 
237
            rendezvous(stopBarrier);
 
238
//            log.debug(this + " done");
 
239
         }
 
240
         catch (Throwable t)
 
241
         {
 
242
            t.printStackTrace();
 
243
            rendezvous(stopBarrier);
 
244
         }
 
245
      }
 
246
      
 
247
      public String toString()
 
248
      {
 
249
         return name;
 
250
      }
 
251
   }
 
252
}
 
 
b'\\ No newline at end of file'