KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > mx4j > remote > RemoteNotificationClientHandlerTest


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package test.mx4j.remote;
10
11 import java.io.IOException JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.Map JavaDoc;
14 import javax.management.Notification JavaDoc;
15 import javax.management.NotificationListener JavaDoc;
16 import javax.management.ObjectName JavaDoc;
17 import javax.management.remote.NotificationResult JavaDoc;
18 import javax.management.remote.TargetedNotification JavaDoc;
19
20 import mx4j.remote.AbstractRemoteNotificationClientHandler;
21 import mx4j.remote.MX4JRemoteConstants;
22 import mx4j.remote.NotificationTuple;
23 import mx4j.remote.RemoteNotificationClientHandler;
24 import test.MX4JTestCase;
25 import test.MutableBoolean;
26 import test.MutableLong;
27 import test.MutableObject;
28
29 /**
30  * @version $Revision: 1.10 $
31  */

32 public class RemoteNotificationClientHandlerTest extends MX4JTestCase
33 {
34    public RemoteNotificationClientHandlerTest(String JavaDoc s)
35    {
36       super(s);
37    }
38
39    protected void tearDown() throws Exception JavaDoc
40    {
41       sleep(2000);
42    }
43
44    public void testListenerEquality() throws Exception JavaDoc
45    {
46       AbstractRemoteNotificationClientHandler handler = new AbstractRemoteNotificationClientHandler(null, null, null)
47       {
48          protected NotificationResult JavaDoc fetchNotifications(long sequence, int maxNumber, long timeout)
49          {
50             sleep(timeout);
51             return null;
52          }
53
54          protected void sendConnectionNotificationLost(long number)
55          {
56          }
57
58          protected long getRetryPeriod()
59          {
60             return 1000;
61          }
62
63          protected int getMaxRetries()
64          {
65             return 2;
66          }
67       };
68
69       try
70       {
71          handler.start();
72
73          NotificationListener JavaDoc listener = new NotificationListener JavaDoc()
74          {
75             public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
76             {
77             }
78          };
79
80          ObjectName JavaDoc name = ObjectName.getInstance(":name=emitter");
81          handler.addNotificationListener(new Integer JavaDoc(1), new NotificationTuple(name, listener, null, null));
82          handler.addNotificationListener(new Integer JavaDoc(2), new NotificationTuple(name, listener, null, new Object JavaDoc()));
83
84          assertTrue(handler.isActive());
85
86          Integer JavaDoc[] ids = handler.getNotificationListeners(new NotificationTuple(name, listener));
87          assertEquals(ids.length, 2);
88
89          handler.removeNotificationListeners(new Integer JavaDoc[]{new Integer JavaDoc(1), new Integer JavaDoc(2)});
90          assertTrue(handler.isActive());
91
92          handler.stop();
93          assertTrue(!handler.isActive());
94       }
95       finally
96       {
97          handler.stop();
98       }
99    }
100
101    public void testAddRemove() throws Exception JavaDoc
102    {
103       RemoteNotificationClientHandler handler = new AbstractRemoteNotificationClientHandler(null, null, null)
104       {
105          public NotificationResult JavaDoc fetchNotifications(long sequenceNumber, int maxNumber, long timeout)
106          {
107             sleep(timeout);
108             return null;
109          }
110
111          protected void sendConnectionNotificationLost(long number)
112          {
113          }
114
115          protected long getRetryPeriod()
116          {
117             return 1000;
118          }
119
120          protected int getMaxRetries()
121          {
122             return 2;
123          }
124       };
125
126       NotificationListener JavaDoc listener = new NotificationListener JavaDoc()
127       {
128          public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
129          {
130          }
131       };
132
133       try
134       {
135          handler.start();
136
137          ObjectName JavaDoc name = ObjectName.getInstance(":name=emitter");
138          handler.addNotificationListener(new Integer JavaDoc(1), new NotificationTuple(name, listener, null, null));
139          Object JavaDoc handback = new Object JavaDoc();
140          handler.addNotificationListener(new Integer JavaDoc(2), new NotificationTuple(name, listener, null, handback));
141          handler.removeNotificationListeners(new Integer JavaDoc[]{new Integer JavaDoc(2)});
142
143          assertFalse(handler.contains(new NotificationTuple(name, listener, null, handback)));
144          assertTrue(handler.contains(new NotificationTuple(name, listener, null, null)));
145
146          Integer JavaDoc id = handler.getNotificationListener(new NotificationTuple(name, listener, null, null));
147          assertEquals(id.intValue(), 1);
148
149          handler.removeNotificationListeners(new Integer JavaDoc[]{new Integer JavaDoc(1)});
150          assertFalse(handler.contains(new NotificationTuple(name, listener, null, null)));
151       }
152       finally
153       {
154          handler.stop();
155       }
156    }
157
158    public void testNotificationDelivery() throws Exception JavaDoc
159    {
160       final MutableObject holder = new MutableObject(null);
161       final ObjectName JavaDoc name = ObjectName.getInstance(":name=emitter");
162       final int nextSequence = 1;
163       RemoteNotificationClientHandler handler = new AbstractRemoteNotificationClientHandler(null, null, null)
164       {
165          public NotificationResult JavaDoc fetchNotifications(long sequenceNumber, int maxNumber, long timeout)
166          {
167             synchronized (holder)
168             {
169                Notification JavaDoc notification = new Notification JavaDoc("type", name, 0);
170                TargetedNotification JavaDoc targeted = new TargetedNotification JavaDoc(notification, new Integer JavaDoc(1));
171                if (sequenceNumber < 0)
172                {
173                   // Return nextSequence as next sequence number: next call must have this sequence number
174
NotificationResult JavaDoc result1 = new NotificationResult JavaDoc(0, nextSequence, new TargetedNotification JavaDoc[]{targeted});
175                   holder.set(result1);
176                   return result1;
177                }
178
179                if (sequenceNumber == nextSequence)
180                {
181                   NotificationResult JavaDoc result2 = new NotificationResult JavaDoc(1, nextSequence + 1, new TargetedNotification JavaDoc[]{targeted});
182                   holder.set(result2);
183                   return result2;
184                }
185
186                try
187                {
188                   holder.wait(timeout);
189                }
190                catch (InterruptedException JavaDoc x)
191                {
192                   Thread.currentThread().interrupt();
193                }
194                holder.set(null);
195                return null;
196             }
197          }
198
199          protected void sendConnectionNotificationLost(long number)
200          {
201          }
202
203          protected int getMaxRetries()
204          {
205             return 2;
206          }
207
208          protected long getRetryPeriod()
209          {
210             return 1000;
211          }
212       };
213
214       final MutableObject notifHolder = new MutableObject(null);
215       NotificationListener JavaDoc listener = new NotificationListener JavaDoc()
216       {
217          public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
218          {
219             notifHolder.set(notification);
220          }
221       };
222
223       try
224       {
225          handler.start();
226
227          handler.addNotificationListener(new Integer JavaDoc(1), new NotificationTuple(name, listener, null, null));
228
229          synchronized (holder)
230          {
231             // This wait time is much less than the sleep time for the fetcher thread
232
while (holder.get() == null) holder.wait(10);
233             NotificationResult JavaDoc result = (NotificationResult JavaDoc)holder.get();
234             assertEquals(result.getNextSequenceNumber(), nextSequence);
235             holder.set(null);
236
237             // Wait for the notification to arrive
238
while (notifHolder.get() == null) holder.wait(10);
239             Notification JavaDoc notif = (Notification JavaDoc)notifHolder.get();
240             assertEquals(notif.getSource(), name);
241
242             // Wait for the second fetchNotification call
243
while (holder.get() == null) holder.wait(10);
244             result = (NotificationResult JavaDoc)holder.get();
245             assertEquals(result.getNextSequenceNumber(), nextSequence + 1);
246          }
247
248          handler.removeNotificationListeners(new Integer JavaDoc[]{new Integer JavaDoc(1)});
249          sleep(2000);
250       }
251       finally
252       {
253          handler.stop();
254       }
255    }
256
257    public void testNotificationsLost() throws Exception JavaDoc
258    {
259       final MutableObject holder = new MutableObject(null);
260       final MutableLong lost = new MutableLong(0);
261       final ObjectName JavaDoc name = ObjectName.getInstance(":name=emitter");
262       final long losts = 1;
263       RemoteNotificationClientHandler handler = new AbstractRemoteNotificationClientHandler(null, null, null)
264       {
265          public NotificationResult JavaDoc fetchNotifications(long sequenceNumber, int maxNumber, long timeout)
266          {
267             synchronized (holder)
268             {
269                if (sequenceNumber < 0) return new NotificationResult JavaDoc(0, 0, new TargetedNotification JavaDoc[0]);
270
271                // Avoid to spin loop the fetcher thread
272
sleep(1000);
273
274                // Return a earliest sequence greater than the requested, to test notification lost behavior
275
return new NotificationResult JavaDoc(losts, losts, new TargetedNotification JavaDoc[0]);
276             }
277          }
278
279          protected void sendConnectionNotificationLost(long number)
280          {
281             synchronized (holder)
282             {
283                lost.set(number);
284             }
285          }
286
287          protected int getMaxRetries()
288          {
289             return 2;
290          }
291
292          protected long getRetryPeriod()
293          {
294             return 1000;
295          }
296       };
297
298       Integer JavaDoc id = new Integer JavaDoc(1);
299       NotificationListener JavaDoc listener = new NotificationListener JavaDoc()
300       {
301          public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
302          {
303          }
304       };
305
306       try
307       {
308          handler.start();
309
310          handler.addNotificationListener(id, new NotificationTuple(name, listener, null, null));
311
312          synchronized (holder)
313          {
314             while (lost.get() == 0) holder.wait(10);
315             assertEquals(lost.get(), losts);
316          }
317
318          handler.removeNotificationListeners(new Integer JavaDoc[]{id});
319       }
320       finally
321       {
322          handler.stop();
323       }
324    }
325
326    public void testConnectionFailure() throws Exception JavaDoc
327    {
328       final int retries = 2;
329       final long period = 1000;
330       AbstractRemoteNotificationClientHandler handler = new AbstractRemoteNotificationClientHandler(null, null, null)
331       {
332          protected NotificationResult JavaDoc fetchNotifications(long sequence, int maxNumber, long timeout) throws IOException JavaDoc
333          {
334             throw new IOException JavaDoc();
335          }
336
337          protected void sendConnectionNotificationLost(long number)
338          {
339          }
340
341          protected int getMaxRetries()
342          {
343             return retries;
344          }
345
346          protected long getRetryPeriod()
347          {
348             return period;
349          }
350       };
351
352       NotificationListener JavaDoc listener = new NotificationListener JavaDoc()
353       {
354          public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
355          {
356          }
357       };
358
359       try
360       {
361          handler.start();
362
363          ObjectName JavaDoc name = ObjectName.getInstance(":name=emitter");
364          Integer JavaDoc id = new Integer JavaDoc(1);
365          handler.addNotificationListener(id, new NotificationTuple(name, listener, null, null));
366
367          sleep(5000 + period * retries);
368
369          assertTrue(!handler.isActive());
370       }
371       finally
372       {
373          handler.stop();
374       }
375    }
376
377    public void testQueueOverflow() throws Exception JavaDoc
378    {
379       final Object JavaDoc lock = new Object JavaDoc();
380       int queueCapacity = 10;
381       final int count = 4;
382       final long sleep = 500;
383       final Integer JavaDoc id = new Integer JavaDoc(1);
384       final ObjectName JavaDoc name = ObjectName.getInstance(":name=emitter");
385       final MutableBoolean notify = new MutableBoolean(true);
386       final MutableLong queued = new MutableLong(0);
387       final MutableLong delivered = new MutableLong(0);
388
389       Map JavaDoc environment = new HashMap JavaDoc();
390       environment.put(MX4JRemoteConstants.NOTIFICATION_QUEUE_CAPACITY, new Integer JavaDoc(queueCapacity));
391       RemoteNotificationClientHandler handler = new AbstractRemoteNotificationClientHandler(null, null, environment)
392       {
393          protected NotificationResult JavaDoc fetchNotifications(long sequenceNumber, int maxNumber, long timeout)
394          {
395             if (sequenceNumber < 0) return new NotificationResult JavaDoc(0, 0, new TargetedNotification JavaDoc[0]);
396
397             boolean doNotify = false;
398             synchronized (lock)
399             {
400                doNotify = notify.get();
401             }
402
403             if (doNotify)
404             {
405                // Avoid spin looping the fetcher thread, but don't sleep too much, we have to fill the client's queue
406
sleep(sleep);
407                TargetedNotification JavaDoc[] notifications = new TargetedNotification JavaDoc[count];
408                for (int i = 0; i < count; ++i) notifications[i] = new TargetedNotification JavaDoc(new Notification JavaDoc("type", name, sequenceNumber + i), id);
409                long nextSequence = sequenceNumber + count;
410                NotificationResult JavaDoc result = new NotificationResult JavaDoc(0, nextSequence, notifications);
411                synchronized (lock)
412                {
413                   queued.set(getNotificationsCount());
414                }
415                return result;
416             }
417             else
418             {
419                sleep(timeout);
420                return new NotificationResult JavaDoc(0, sequenceNumber, new TargetedNotification JavaDoc[0]);
421             }
422          }
423
424          protected long getRetryPeriod()
425          {
426             return 1000;
427          }
428
429          protected int getMaxRetries()
430          {
431             return 5;
432          }
433
434          protected void sendConnectionNotificationLost(long number)
435          {
436             System.out.println("Lost notifications: " + number);
437             // Stop sending notifications
438
synchronized (lock)
439             {
440                notify.set(false);
441                // Deliver notifications until the last we queued on the client
442
queued.set(getNotificationsCount());
443             }
444          }
445       };
446
447       NotificationListener JavaDoc listener = new NotificationListener JavaDoc()
448       {
449          public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
450          {
451             long sequence = notification.getSequenceNumber();
452             synchronized (lock)
453             {
454                delivered.set(sequence);
455             }
456             System.out.println("Received notification, sequence is " + sequence);
457             // Sleep longer than notification emission, to fill the client's queue
458
sleep(sleep * 2);
459             System.out.println("Handled notification, sequence is " + sequence);
460          }
461       };
462
463       try
464       {
465          handler.start();
466          handler.addNotificationListener(id, new NotificationTuple(name, listener, null, null));
467          // Wait until we empty the client's queue
468
synchronized (lock)
469          {
470             while (notify.get())
471             {
472                lock.wait(50);
473                if (queued.get() > queueCapacity) fail("Queued notifications " + queued.get() + " must not pass max capacity " + queueCapacity);
474             }
475
476             // Test timeouts if we don't deliver everything
477
while (delivered.get() < queued.get()) lock.wait(10);
478          }
479       }
480       finally
481       {
482          handler.stop();
483       }
484    }
485 }
486
Popular Tags