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

« back to all changes in this revision

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