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

« back to all changes in this revision

Viewing changes to tests/org/jboss/test/remoting/lease/synchronization/MultipleClientSynchronizationTestCase.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.lease.synchronization;
23
 
 
24
 
import java.net.InetAddress;
25
 
import java.util.Date;
26
 
import java.util.HashMap;
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.logging.XLevel;
37
 
import org.jboss.remoting.Client;
38
 
import org.jboss.remoting.ConnectionListener;
39
 
import org.jboss.remoting.InvocationRequest;
40
 
import org.jboss.remoting.InvokerLocator;
41
 
import org.jboss.remoting.ServerInvocationHandler;
42
 
import org.jboss.remoting.ServerInvoker;
43
 
import org.jboss.remoting.callback.InvokerCallbackHandler;
44
 
import org.jboss.remoting.transport.Connector;
45
 
import org.jboss.remoting.transport.PortUtil;
46
 
 
47
 
 
48
 
/**
49
 
 * 
50
 
 * @author <a href="ron.sigal@jboss.com">Ron Sigal</a>
51
 
 * @version $Revision: 2913 $
52
 
 * <p>
53
 
 * Copyright Jul 31, 2007
54
 
 * </p>
55
 
 */
56
 
public class MultipleClientSynchronizationTestCase extends TestCase
57
 
{
58
 
   protected static Logger log = Logger.getLogger(MultipleClientSynchronizationTestCase.class);
59
 
   protected static boolean firstTime = true;
60
 
   protected static BooleanHolder go1 = new BooleanHolder();
61
 
   protected static BooleanHolder go2 = new BooleanHolder();
62
 
   protected static BooleanHolder stop1 = new BooleanHolder();
63
 
   protected static BooleanHolder stop2 = new BooleanHolder();
64
 
   protected static InvokerLocator locator;
65
 
   protected static int counter;
66
 
   protected static Object lock = new Object();
67
 
   
68
 
   public void setUp() throws Exception
69
 
   {
70
 
      if (firstTime)
71
 
      {
72
 
         firstTime = false;
73
 
         Logger.getLogger("org.jboss.remoting").setLevel(XLevel.INFO);
74
 
         Logger.getLogger("org.jboss.test.remoting").setLevel(Level.INFO);
75
 
         String pattern = "[%d{ABSOLUTE}] [%t] %5p (%F:%L) - %m%n";
76
 
         PatternLayout layout = new PatternLayout(pattern);
77
 
         ConsoleAppender consoleAppender = new ConsoleAppender(layout);
78
 
         Logger.getRootLogger().addAppender(consoleAppender);  
79
 
      }
80
 
      
81
 
      counter = 0;
82
 
   }
83
 
   
84
 
   
85
 
   public void testMultipleClientsStoppingAndStartingSimultaneously() throws Throwable
86
 
   {
87
 
      log.info("entering " + getName());
88
 
      String host = InetAddress.getLocalHost().getHostAddress();
89
 
      int port = PortUtil.findFreePort(host);
90
 
      String locatorURI = "socket://" + host + ":" + port;
91
 
      locator = new InvokerLocator(locatorURI);
92
 
      Connector connector = new Connector(locator);
93
 
      connector.create();
94
 
      connector.setLeasePeriod(1000);
95
 
      connector.addConnectionListener(new TestListener());
96
 
      TestHandler handler = new TestHandler();
97
 
      connector.addInvocationHandler("test", handler);
98
 
      connector.start();
99
 
      
100
 
      int INVOCATIONS = 1000;
101
 
      ClientThread[] threads = new ClientThread[INVOCATIONS];
102
 
      
103
 
      // Start clients 0..499.
104
 
      for (int i = 0; i < INVOCATIONS / 2; i++)
105
 
      {
106
 
         threads[i] = new ClientThread(i, go1, stop1);
107
 
         threads[i].start();
108
 
      }
109
 
      
110
 
      synchronized (go1)
111
 
      {
112
 
         go1.value = true;
113
 
         go1.notifyAll();
114
 
      }
115
 
      Thread.sleep(5000);
116
 
      
117
 
      // Stop clients 0..499 and start clients 500..999.
118
 
      for (int i = INVOCATIONS / 2; i < INVOCATIONS; i++)
119
 
      {
120
 
         threads[i] = new ClientThread(i, go2, stop2);
121
 
         threads[i].start();
122
 
      }
123
 
      
124
 
      synchronized (stop1)
125
 
      {
126
 
         stop1.value = true;
127
 
         stop1.notifyAll();
128
 
      }
129
 
      synchronized (go2)
130
 
      {
131
 
         go2.value = true;
132
 
         go2.notifyAll();
133
 
      }
134
 
      
135
 
      Thread.sleep(5000);
136
 
      
137
 
      // Stop clients 500..999.
138
 
      synchronized (stop2)
139
 
      {
140
 
         stop2.value = true;
141
 
         stop2.notifyAll();
142
 
      }
143
 
      
144
 
      // Wait for clients to disconnect.
145
 
      for (int i = 0; i < 60; i++)
146
 
      {
147
 
         synchronized (lock)
148
 
         {
149
 
            log.info("counter: " + counter);
150
 
            if (counter == INVOCATIONS)
151
 
               break;
152
 
         }
153
 
         
154
 
         try
155
 
         {
156
 
            Thread.sleep(2000);
157
 
         }
158
 
         catch (Exception e)
159
 
         {
160
 
         }
161
 
      }
162
 
      
163
 
      // Verify that all clients started and stopped successfully.
164
 
      for (int i = 0; i < INVOCATIONS; i++)
165
 
      {
166
 
         assertTrue(new Date() + ": failure in thread: " + i, threads[i].ok);
167
 
      }
168
 
      
169
 
      connector.stop();
170
 
      log.info(getName() + " PASSES");
171
 
   }
172
 
   
173
 
   public void testMultipleClientsStartingStoppingStarting() throws Throwable
174
 
   {
175
 
      log.info("entering " + getName());
176
 
      String host = InetAddress.getLocalHost().getHostAddress();
177
 
      int port = PortUtil.findFreePort(host);
178
 
      String locatorURI = "socket://" + host + ":" + port;
179
 
      locator = new InvokerLocator(locatorURI);
180
 
      Connector connector = new Connector(locator);
181
 
      connector.create();
182
 
      connector.setLeasePeriod(1000);
183
 
      connector.addConnectionListener(new TestListener());
184
 
      TestHandler handler = new TestHandler();
185
 
      connector.addInvocationHandler("test", handler);
186
 
      connector.start();
187
 
      
188
 
      int INVOCATIONS = 1000;
189
 
      ClientThread[] threads = new ClientThread[INVOCATIONS];
190
 
      
191
 
      // Start clients 0..499.
192
 
      for (int i = 0; i < INVOCATIONS / 2; i++)
193
 
      {
194
 
         threads[i] = new ClientThread(i, go1, stop1);
195
 
         threads[i].start();
196
 
      }
197
 
      
198
 
      synchronized (go1)
199
 
      {
200
 
         go1.value = true;
201
 
         go1.notifyAll();
202
 
      }
203
 
      Thread.sleep(5000);
204
 
      
205
 
      // Stop clients 0..499.
206
 
      synchronized (stop1)
207
 
      {
208
 
         stop1.value = true;
209
 
         stop1.notifyAll();
210
 
      }
211
 
      
212
 
      // Start clients 500..999.
213
 
      for (int i = INVOCATIONS / 2; i < INVOCATIONS; i++)
214
 
      {
215
 
         threads[i] = new ClientThread(i, go2, stop2);
216
 
         threads[i].start();
217
 
      }
218
 
      
219
 
      synchronized (go2)
220
 
      {
221
 
         go2.value = true;
222
 
         go2.notifyAll();
223
 
      }
224
 
      
225
 
      Thread.sleep(5000);
226
 
      
227
 
      // Stop clients 500..999.
228
 
      synchronized (stop2)
229
 
      {
230
 
         stop2.value = true;
231
 
         stop2.notifyAll();
232
 
      }
233
 
      
234
 
      // Wait for clients to disconnect.
235
 
      for (int i = 0; i < 60; i++)
236
 
      {
237
 
         synchronized (lock)
238
 
         {
239
 
            log.info("counter: " + counter);
240
 
            if (counter == INVOCATIONS)
241
 
               break;
242
 
         }
243
 
         
244
 
         try
245
 
         {
246
 
            Thread.sleep(2000);
247
 
         }
248
 
         catch (Exception e)
249
 
         {
250
 
         }
251
 
      }
252
 
 
253
 
      // Verify that all clients started and stopped successfully.
254
 
      for (int i = 0; i < INVOCATIONS; i++)
255
 
      {
256
 
         assertTrue(new Date() + ": failure in thread: " + threads[i], threads[i].ok);
257
 
      }
258
 
      
259
 
      connector.stop();
260
 
      log.info(getName() + " PASSES");
261
 
   }
262
 
   
263
 
   
264
 
   public class ClientThread extends Thread
265
 
   {
266
 
      boolean ok;
267
 
      int id;
268
 
      BooleanHolder startFlag;
269
 
      BooleanHolder stopFlag;
270
 
      Client client;
271
 
      
272
 
      public ClientThread(int id, BooleanHolder startFlag, BooleanHolder stopFlag) throws Exception
273
 
      {
274
 
         this.id = id;
275
 
         this.startFlag = startFlag;
276
 
         this.stopFlag = stopFlag;
277
 
         
278
 
         HashMap config = new HashMap();
279
 
         config.put(InvokerLocator.FORCE_REMOTE, "true");
280
 
         config.put(Client.ENABLE_LEASE, "true");
281
 
         config.put(InvokerLocator.CLIENT_LEASE_PERIOD, "1000");
282
 
         client = new Client(locator, config);
283
 
         setName("ClientThread-" + id);
284
 
         log.debug("client created (" + id + "): " + client.getSessionId());
285
 
      }
286
 
      
287
 
      public void run()
288
 
      {
289
 
         try
290
 
         {
291
 
            synchronized (startFlag)
292
 
            {
293
 
               while (!startFlag.value)
294
 
               {
295
 
                  try {startFlag.wait();} catch (InterruptedException e) {}
296
 
               }
297
 
            }
298
 
            log.debug("client got start flag (" + id + "): " + client.getSessionId());
299
 
            client.connect();
300
 
            log.debug("client connected (" + id + "): " + client.getSessionId());
301
 
         }
302
 
         catch (Throwable e)
303
 
         {
304
 
            e.printStackTrace();
305
 
         }
306
 
         
307
 
         try
308
 
         {
309
 
            log.debug("client waiting for stop flag (" + id + "): " + client.getSessionId());
310
 
           
311
 
            synchronized (stopFlag)
312
 
            {
313
 
               while (!stopFlag.value)
314
 
               {
315
 
                  try {stopFlag.wait();} catch (InterruptedException e) {}
316
 
               }
317
 
            }
318
 
 
319
 
            log.debug("client got stop flag (" + id + "): " + client.getSessionId());
320
 
            client.disconnect();
321
 
//            log.info("client disconnected (" + id + "): " + client.getSessionId());
322
 
            ok = true;
323
 
            log.debug("client ok (" + id + "):" + client.getSessionId());
324
 
            
325
 
            synchronized (lock)
326
 
            {
327
 
               counter++;
328
 
            }
329
 
         }
330
 
         catch (Throwable e)
331
 
         {
332
 
            e.printStackTrace();
333
 
         }
334
 
      }
335
 
   }
336
 
   
337
 
   
338
 
   static class TestHandler implements ServerInvocationHandler
339
 
   {
340
 
      public void setMBeanServer(MBeanServer server) {}
341
 
      public void setInvoker(ServerInvoker invoker) {}
342
 
      public Object invoke(InvocationRequest invocation) throws Throwable {return null;}
343
 
      public void addListener(InvokerCallbackHandler callbackHandler) {}
344
 
      public void removeListener(InvokerCallbackHandler callbackHandler) {}
345
 
   }
346
 
   
347
 
   
348
 
   public static class TestListener implements ConnectionListener
349
 
   {
350
 
      public void handleConnectionException(Throwable throwable, Client client)
351
 
      {
352
 
         log.debug("got connection exception: " + throwable);
353
 
      }
354
 
   }
355
 
   
356
 
   
357
 
   static class BooleanHolder
358
 
   {
359
 
      public boolean value;
360
 
   }
361
 
}
 
 
b'\\ No newline at end of file'