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

« back to all changes in this revision

Viewing changes to src/tests/org/jboss/test/remoting/lease/socket/multiple/SocketLeaseTestClient.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
package org.jboss.test.remoting.lease.socket.multiple;
 
2
 
 
3
import junit.framework.TestCase;
 
4
 
 
5
import org.jboss.logging.Logger;
 
6
import org.jboss.remoting.Client;
 
7
import org.jboss.remoting.InvokerLocator;
 
8
import org.jboss.remoting.callback.Callback;
 
9
import org.jboss.remoting.callback.HandleCallbackException;
 
10
import org.jboss.remoting.callback.InvokerCallbackHandler;
 
11
 
 
12
import java.util.HashMap;
 
13
import java.util.Map;
 
14
 
 
15
/**
 
16
 * @author <a href="mailto:tom.elrod@jboss.com">Tom Elrod</a>
 
17
 */
 
18
public class SocketLeaseTestClient extends TestCase
 
19
{
 
20
   private static Logger log = Logger.getLogger(SocketLeaseTestClient.class);
 
21
   // Default locator values
 
22
   private static String transport = "socket";
 
23
   private static String host = "localhost";
 
24
   private static int port = 5400;
 
25
   
 
26
   private String locatorURI = transport + "://" + host + ":" + port + "/?" + InvokerLocator.CLIENT_LEASE + "=" + "true";
 
27
   private String callbackLocatorURI = transport + "://" + host + ":" + (port + 1);
 
28
   
 
29
   private int COUNT = 50;
 
30
   private boolean[] success = new boolean[COUNT];
 
31
   private boolean[] done = new boolean[COUNT];
 
32
   
 
33
// public void setUp()
 
34
// {
 
35
// org.apache.log4j.BasicConfigurator.configure();
 
36
// org.apache.log4j.Category.getRoot().setLevel(Level.INFO);
 
37
// org.apache.log4j.Category.getInstance("org.jboss.remoting").setLevel(XLevel.TRACE);
 
38
// org.apache.log4j.Category.getInstance("org.jgroups").setLevel(Level.FATAL);
 
39
// }
 
40
   
 
41
   public void setUp()
 
42
   {
 
43
      log.info("entering setUp()");
 
44
   }
 
45
   protected String getLocatorUri()
 
46
   {
 
47
      return locatorURI;
 
48
   }
 
49
   
 
50
   public void testMultipleLeases() throws Throwable
 
51
   {
 
52
      log.info("entering " + getName());
 
53
 
 
54
      class CallerThread extends Thread
 
55
      {
 
56
         int id;
 
57
         
 
58
         CallerThread(int id)
 
59
         {
 
60
            this.id = id;
 
61
         }
 
62
         
 
63
         public void run()
 
64
         {
 
65
            try
 
66
            {
 
67
               log.info("calling runClient(" + id + ")");
 
68
               runClient(id);
 
69
               log.info("runClient(" + id + ") returns");
 
70
               done[id] = true;
 
71
            }
 
72
            catch (Throwable e)
 
73
            {
 
74
               e.printStackTrace();
 
75
            }
 
76
         }
 
77
      }
 
78
      
 
79
      for (int i = 0; i < COUNT; i++)
 
80
      {  
 
81
         Thread t = new CallerThread(i);
 
82
         t.setName("client: " + i);
 
83
         t.start();
 
84
         log.info("started client thread " + i);
 
85
      }
 
86
      
 
87
      Thread.sleep(35000);
 
88
      
 
89
      boolean allDone = false;
 
90
      while (!allDone)
 
91
      {
 
92
         allDone = true;
 
93
         for (int i = 0; i < COUNT; i++)
 
94
         {
 
95
            allDone &= done[i];
 
96
            log.info("done[" + i + "]: " + done[i]);
 
97
         }
 
98
         Thread.sleep(1000);
 
99
      }
 
100
      
 
101
      boolean allSuccess = true;
 
102
      for (int i = 0; i < COUNT; i++)
 
103
      {
 
104
         allSuccess &= success[i];
 
105
      }
 
106
      
 
107
      log.info("success: " + allSuccess);
 
108
      assertTrue(allSuccess);
 
109
      log.info(getName() + " PASSES");
 
110
   }
 
111
   
 
112
   
 
113
   public void runClient(int id) throws Throwable
 
114
   {
 
115
      InvokerLocator locator = new InvokerLocator(getLocatorUri());
 
116
      System.out.println("Calling remoting server with locator uri of: " + getLocatorUri());
 
117
      
 
118
      //InvokerLocator callbackLocator = new InvokerLocator(callbackLocatorURI);
 
119
      //Connector callbackConnector = new Connector(callbackLocator);
 
120
      //callbackConnector.create();
 
121
      //callbackConnector.start();
 
122
      
 
123
      //TestCallbackHandler callbackHandler = new TestCallbackHandler();
 
124
      
 
125
      Map metadata = new HashMap();
 
126
      metadata.put("clientName", "test1");
 
127
      Client remotingClient1 = new Client(locator, metadata);
 
128
      remotingClient1.connect();
 
129
      
 
130
      //remotingClient1.addListener(callbackHandler, callbackLocator);
 
131
      
 
132
      Object ret = remotingClient1.invoke("test1");
 
133
      System.out.println("Response was: " + ret);
 
134
      
 
135
      Thread.currentThread().sleep(1000);
 
136
      
 
137
      // now create second client
 
138
      Map metadata2 = new HashMap();
 
139
      metadata2.put("clientName", "test1");
 
140
      Client remotingClient2 =new Client(locator, metadata2);
 
141
      remotingClient2.connect();
 
142
      //remotingClient2.addListener(callbackHandler, callbackLocator);
 
143
      
 
144
      ret = remotingClient2.invoke("test2");
 
145
      System.out.println("Response was: " + ret);
 
146
      
 
147
      ret = remotingClient1.invoke("test1");
 
148
      System.out.println("Response was: " + ret);
 
149
      
 
150
      Thread.currentThread().sleep(1000);
 
151
      
 
152
      if(remotingClient1 != null)
 
153
      {
 
154
         //remotingClient1.removeListener(callbackHandler);
 
155
         remotingClient1.disconnect();
 
156
      }
 
157
      
 
158
      System.out.println("remoting client 1 (thread " + id + ") disconnected");
 
159
      
 
160
      //Thread.currentThread().sleep(10000);
 
161
      Thread.currentThread().sleep(30000);
 
162
      
 
163
      ret = remotingClient2.invoke("test2");
 
164
      System.out.println("Response was: " + ret);
 
165
      
 
166
      if(remotingClient2 != null)
 
167
      {
 
168
         //remotingClient2.removeListener(callbackHandler);
 
169
         remotingClient2.disconnect();
 
170
      }
 
171
      
 
172
      success[id] = true;
 
173
      log.info("thread " + id + " successful");
 
174
   }
 
175
 
 
176
   public class TestCallbackHandler implements InvokerCallbackHandler
 
177
   {
 
178
 
 
179
      public void handleCallback(Callback callback) throws HandleCallbackException
 
180
      {
 
181
         System.out.println("callback: " + callback);
 
182
      }
 
183
   }
 
184
 
 
185
 
 
186
}