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

« back to all changes in this revision

Viewing changes to tests/org/jboss/test/remoting/callback/pull/memory/callbackstore/CallbackTestClient.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
 
 
23
 
package org.jboss.test.remoting.callback.pull.memory.callbackstore;
24
 
 
25
 
import junit.framework.TestCase;
26
 
import org.jboss.logging.Logger;
27
 
import org.jboss.remoting.Client;
28
 
import org.jboss.remoting.InvokerLocator;
29
 
import org.jboss.remoting.callback.Callback;
30
 
import org.jboss.remoting.callback.HandleCallbackException;
31
 
import org.jboss.remoting.callback.InvokerCallbackHandler;
32
 
 
33
 
import java.util.List;
34
 
 
35
 
/**
36
 
 * @author <a href="mailto:telrod@e2technologies.net">Tom Elrod</a>
37
 
 */
38
 
public class CallbackTestClient extends TestCase
39
 
{
40
 
   // Default locator values
41
 
   private static String transport = "socket";
42
 
   private static String host = "localhost";
43
 
   private static int port = 5412;
44
 
 
45
 
   private String locatorURI = transport + "://" + host + ":" + port;
46
 
 
47
 
   private Client remotingClient;
48
 
   private CallbackHandler pullCallbackHandler;
49
 
 
50
 
   private boolean isCallbackDone = false;
51
 
 
52
 
   private int numberOfCallbacks = 520;
53
 
 
54
 
   private static final Logger log = Logger.getLogger(CallbackTestClient.class);
55
 
 
56
 
   public void createRemotingClient() throws Exception
57
 
   {
58
 
      InvokerLocator locator = new InvokerLocator(getInvokerLocatorURI());
59
 
      System.out.println("Calling remoting server with locator uri of: " + getInvokerLocatorURI());
60
 
 
61
 
      // This could have been new Client(locator), but want to show that subsystem param is null
62
 
      // Could have also been new Client(locator, "sample");
63
 
      remotingClient = new Client(locator);
64
 
      remotingClient.connect();
65
 
 
66
 
   }
67
 
   
68
 
   protected String getInvokerLocatorURI()
69
 
   {
70
 
      return locatorURI;
71
 
   }
72
 
 
73
 
   public void makeInvocation(String param) throws Throwable
74
 
   {
75
 
      Object response = remotingClient.invoke(param, null);
76
 
      System.out.println("Invocation response: " + response);
77
 
   }
78
 
 
79
 
   public void setUp() throws Exception
80
 
   {
81
 
      createRemotingClient();
82
 
   }
83
 
 
84
 
   public void tearDown() throws Exception
85
 
   {
86
 
      if(remotingClient != null)
87
 
      {
88
 
         if(pullCallbackHandler != null)
89
 
         {
90
 
            try
91
 
            {
92
 
               remotingClient.removeListener(pullCallbackHandler);
93
 
            }
94
 
            catch(Throwable throwable)
95
 
            {
96
 
               throw new Exception(throwable);
97
 
            }
98
 
         }
99
 
         remotingClient.disconnect();
100
 
      }
101
 
   }
102
 
 
103
 
   public void testPullCallback() throws Throwable
104
 
   {
105
 
      numberOfCallbacks = calculateNumberOfCallbacks();
106
 
      System.out.println("Number of callbacks need to activate persitence: " + numberOfCallbacks);
107
 
      pullCallbackHandler = new CallbackHandler();
108
 
      // by passing only the callback handler, will indicate pull callbacks
109
 
      remotingClient.addListener(pullCallbackHandler);
110
 
 
111
 
      // need to tell server handler how many
112
 
      makeInvocation("" + numberOfCallbacks);
113
 
 
114
 
      // now make invocation on server, which should cause a callback to happen
115
 
      makeInvocation("Do something");
116
 
 
117
 
      boolean didItWork = checkForCallback();
118
 
 
119
 
      System.out.println("Did it work = " + didItWork);
120
 
      log.debug("Did it work = " + didItWork);
121
 
 
122
 
      int totalCallbacks = 0;
123
 
      if(didItWork)
124
 
      {
125
 
         // now need to go get the callbacks until none left.
126
 
         int callbacksReceived = getAllCallbacks(pullCallbackHandler);
127
 
 
128
 
         System.out.println("callbacks received = " + callbacksReceived);
129
 
         log.debug("callbacks received = " + callbacksReceived);
130
 
         totalCallbacks = totalCallbacks + callbacksReceived;
131
 
      }
132
 
 
133
 
      System.out.println("total callbacks received: " + totalCallbacks);
134
 
      log.debug("total callbacks received: " + totalCallbacks);
135
 
      System.out.println("total callbacks expected: " + numberOfCallbacks);
136
 
      log.debug("total callbacks expected: " + numberOfCallbacks);
137
 
 
138
 
      assertEquals(numberOfCallbacks, totalCallbacks);
139
 
   }
140
 
 
141
 
   /**
142
 
    * calculate how many 102400 byte callback messages it will take to consume 30%
143
 
    * of the vm's memory.  The CallbackInvocationHandler will take care of consuming 70%
144
 
    * so need to make sure we have enough callbacks to trigger persistence.
145
 
    */
146
 
   private int calculateNumberOfCallbacks()
147
 
   {
148
 
      long max = Runtime.getRuntime().maxMemory();
149
 
      int targetMem = (int) (max * 0.1);
150
 
      int num = targetMem / 102400;
151
 
      return num;
152
 
   }
153
 
 
154
 
   private int getAllCallbacks(CallbackHandler pullCallbackHandler) throws Throwable
155
 
   {
156
 
      int counter = 0;
157
 
      List callbacks = null;
158
 
 
159
 
      callbacks = remotingClient.getCallbacks(pullCallbackHandler);
160
 
      while(callbacks.size() > 0)
161
 
      {
162
 
         System.out.println("callbacks.size() = " + callbacks.size());
163
 
         counter = counter + callbacks.size();
164
 
         for(int i = 0; i < callbacks.size(); i++)
165
 
         {
166
 
            ((Callback) callbacks.get(i)).getCallbackObject();
167
 
         }
168
 
 
169
 
         // need to give time for server to clean up mem
170
 
         Thread.currentThread().sleep(2000);
171
 
         callbacks = remotingClient.getCallbacks(pullCallbackHandler);
172
 
      }
173
 
      return counter;
174
 
   }
175
 
 
176
 
   private boolean checkForCallback() throws Throwable
177
 
   {
178
 
      boolean isComplete = false;
179
 
 
180
 
      int waitPeriod = 1000;
181
 
      int numOfWaits = 600000;
182
 
      for(int x = 0; x < numOfWaits; x++)
183
 
      {
184
 
         //isComplete = pushCallbackHandler.isComplete();
185
 
         isComplete = ((Boolean) remotingClient.invoke("getdone")).booleanValue();
186
 
         if(!isComplete)
187
 
         {
188
 
            try
189
 
            {
190
 
               Thread.currentThread().sleep(waitPeriod);
191
 
            }
192
 
            catch(InterruptedException e)
193
 
            {
194
 
               e.printStackTrace();
195
 
            }
196
 
         }
197
 
         else
198
 
         {
199
 
            break;
200
 
         }
201
 
      }
202
 
      return isComplete;
203
 
   }
204
 
 
205
 
   public static void main(String[] args)
206
 
   {
207
 
      CallbackTestClient client = new CallbackTestClient();
208
 
      try
209
 
      {
210
 
         client.setUp();
211
 
         client.testPullCallback();
212
 
         client.tearDown();
213
 
      }
214
 
      catch(Throwable throwable)
215
 
      {
216
 
         throwable.printStackTrace();
217
 
      }
218
 
   }
219
 
 
220
 
 
221
 
   public class PushCallbackHandler extends CallbackHandler
222
 
   {
223
 
 
224
 
   }
225
 
 
226
 
   public class CallbackHandler implements InvokerCallbackHandler
227
 
   {
228
 
      boolean isComplete = false;
229
 
 
230
 
      /**
231
 
       * Will take the callback message and send back to client.
232
 
       * If client locator is null, will store them till client polls to get them.
233
 
       *
234
 
       * @param callback
235
 
       * @throws org.jboss.remoting.callback.HandleCallbackException
236
 
       *
237
 
       */
238
 
      public void handleCallback(Callback callback) throws HandleCallbackException
239
 
      {
240
 
         System.out.println("Received callback value of: " + callback.getCallbackObject());
241
 
         System.out.println("Received callback handle object of: " + callback.getCallbackHandleObject());
242
 
         System.out.println("Received callback server invoker of: " + callback.getServerLocator());
243
 
         isComplete = true;
244
 
      }
245
 
 
246
 
      public boolean isComplete()
247
 
      {
248
 
         return isComplete;
249
 
      }
250
 
   }
251
 
 
252
 
}
 
 
b'\\ No newline at end of file'