KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > javax > management > timer > TimerTest


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.javax.management.timer;
10
11 import java.util.Date JavaDoc;
12 import java.util.Vector JavaDoc;
13 import javax.management.InstanceNotFoundException JavaDoc;
14 import javax.management.MBeanServer JavaDoc;
15 import javax.management.MBeanServerInvocationHandler JavaDoc;
16 import javax.management.Notification JavaDoc;
17 import javax.management.NotificationFilter JavaDoc;
18 import javax.management.NotificationListener JavaDoc;
19 import javax.management.ObjectName JavaDoc;
20 import javax.management.timer.Timer JavaDoc;
21 import javax.management.timer.TimerMBean JavaDoc;
22 import javax.management.timer.TimerNotification JavaDoc;
23
24 import test.MX4JTestCase;
25 import test.MutableBoolean;
26 import test.MutableInteger;
27 import test.MutableLong;
28
29 /**
30  * @version $Revision: 1.13 $
31  */

32 public class TimerTest extends MX4JTestCase
33 {
34    private MBeanServer JavaDoc m_server;
35    private ObjectName JavaDoc m_timerName;
36    private TimerMBean JavaDoc m_timer;
37
38    public TimerTest(String JavaDoc s)
39    {
40       super(s);
41    }
42
43    protected void setUp() throws Exception JavaDoc
44    {
45       m_server = newMBeanServer();
46       m_timerName = new ObjectName JavaDoc("Service:type=Timer");
47       m_server.createMBean("javax.management.timer.Timer", m_timerName, null);
48       m_timer = (TimerMBean JavaDoc)MBeanServerInvocationHandler.newProxyInstance(m_server, m_timerName, TimerMBean JavaDoc.class, false);
49    }
50
51    protected void tearDown() throws Exception JavaDoc
52    {
53       m_server.unregisterMBean(m_timerName);
54    }
55
56    public void testStartStop() throws Exception JavaDoc
57    {
58       m_timer.start();
59       assertTrue(m_timer.isActive());
60       m_timer.stop();
61       assertFalse(m_timer.isActive());
62    }
63
64    public void testStartStopStart() throws Exception JavaDoc
65    {
66       m_timer.start();
67       assertTrue(m_timer.isActive());
68       m_timer.stop();
69       assertFalse(m_timer.isActive());
70       m_timer.start();
71       assertTrue(m_timer.isActive());
72       // Will be stopped during unregistration
73
}
74
75    public void testOneShotNotification() throws Exception JavaDoc
76    {
77       m_timer.start();
78
79       final long now = System.currentTimeMillis();
80       final MutableInteger mid = new MutableInteger(-1);
81       final MutableInteger occurrencesCount = new MutableInteger(0);
82
83       final String JavaDoc notifType = "timer-test";
84       final long delay = 3 * Timer.ONE_SECOND;
85
86       NotificationListener JavaDoc listener = new NotificationListener JavaDoc()
87       {
88          public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
89          {
90             // Test that the listener has been called at the specified time
91
long elapsed = System.currentTimeMillis() - now;
92             assertTrue(elapsed >= delay);
93             assertFalse(elapsed - delay > 50);
94
95             assertTrue(notification instanceof TimerNotification JavaDoc);
96
97             Integer JavaDoc id = ((TimerNotification JavaDoc)notification).getNotificationID();
98             assertEquals(mid.get(), id.intValue());
99
100             occurrencesCount.set(occurrencesCount.get() + 1);
101          }
102       };
103
104       m_server.addNotificationListener(m_timerName, listener, new NotificationFilter JavaDoc()
105       {
106          public boolean isNotificationEnabled(Notification JavaDoc notification)
107          {
108             return notification.getType().equals(notifType);
109          }
110       }, null);
111
112       // Notify after a while
113
Date JavaDoc date = new Date JavaDoc(now + delay);
114       // One shot notification at the specified time
115
Integer JavaDoc id = m_timer.addNotification(notifType, "timer-message", "user-data", date);
116       mid.set(id.intValue());
117
118       // Sleep to wait for the notification to happen
119
sleep(delay * 2);
120
121       // Check notification arrived
122
assertTrue(occurrencesCount.get() == 1);
123
124       // Check that it won't be notified again
125
assertTrue(m_timer.getNbNotifications() == 0);
126    }
127
128    public void testPeriodicNotification() throws Exception JavaDoc
129    {
130       m_timer.start();
131
132       final String JavaDoc notifType = "timer-test";
133       final String JavaDoc periodicNotifType = "timer-test-periodic";
134
135       final MutableInteger occurrencesCount = new MutableInteger(0);
136
137       NotificationListener JavaDoc listener = new NotificationListener JavaDoc()
138       {
139          public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
140          {
141             occurrencesCount.set(occurrencesCount.get() + 1);
142          }
143       };
144
145       final MutableInteger periodicOccurrences = new MutableInteger(0);
146       NotificationListener JavaDoc periodicListener = new NotificationListener JavaDoc()
147       {
148          public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
149          {
150             periodicOccurrences.set(periodicOccurrences.get() + 1);
151          }
152       };
153
154       m_server.addNotificationListener(m_timerName, listener, new NotificationFilter JavaDoc()
155       {
156          public boolean isNotificationEnabled(Notification JavaDoc notification)
157          {
158             return notification.getType().equals(notifType);
159          }
160       }, null);
161       m_server.addNotificationListener(m_timerName, periodicListener, new NotificationFilter JavaDoc()
162       {
163          public boolean isNotificationEnabled(Notification JavaDoc notification)
164          {
165             return notification.getType().equals(periodicNotifType);
166          }
167       }, null);
168
169       // Register to happen 3 times on the first listener
170
long now = System.currentTimeMillis();
171       // Notify in one second
172
Date JavaDoc date = new Date JavaDoc(now + Timer.ONE_SECOND);
173       String JavaDoc message = "timer-message";
174       Integer JavaDoc id = m_timer.addNotification(notifType, message, "user-data", date, Timer.ONE_SECOND, 3L);
175
176       // Register to happen periodically
177
// Notify in one second
178
date = new Date JavaDoc(now + Timer.ONE_SECOND);
179       String JavaDoc userDataPeriodic = "user-data-periodic";
180       Integer JavaDoc periodicID = m_timer.addNotification(periodicNotifType, "timer-message-periodic", userDataPeriodic, date, Timer.ONE_SECOND);
181
182       // Sleep some time
183
sleep(Timer.ONE_SECOND);
184
185       Vector JavaDoc v = m_timer.getAllNotificationIDs();
186       assertEquals(v.size(), 2);
187       assertTrue(v.contains(id));
188       assertTrue(v.contains(periodicID));
189
190       v = m_timer.getNotificationIDs(periodicNotifType);
191       assertEquals(v.size(), 1);
192       assertTrue(v.contains(periodicID));
193
194       assertEquals(m_timer.getNotificationMessage(id), message);
195
196       assertEquals(m_timer.getNotificationUserData(periodicID), userDataPeriodic);
197
198       // Sleep till the end of the three-time notification
199
sleep(Timer.ONE_SECOND * 6);
200
201       // Check that was called the right number of times
202
assertEquals(occurrencesCount.get(), 3);
203
204       // The three-time notification is expired now
205
v = m_timer.getAllNotificationIDs();
206       assertEquals(v.size(), 1);
207       assertTrue(v.contains(periodicID));
208
209       Long JavaDoc p = m_timer.getPeriod(periodicID);
210       assertEquals(p.longValue(), Timer.ONE_SECOND);
211
212       assertEquals(m_timer.getNotificationType(periodicID), periodicNotifType);
213
214       // Removing non existing notification
215
try
216       {
217          m_timer.removeNotifications("dummy");
218          fail("Removed non-existing notification");
219       }
220       catch (InstanceNotFoundException JavaDoc ignored)
221       {
222       }
223
224       // Should have already been removed, was the three-shot notification
225
try
226       {
227          m_timer.removeNotification(id);
228          fail("Removed non-existing notification");
229       }
230       catch (InstanceNotFoundException JavaDoc ignored)
231       {
232       }
233
234       // Some more wait
235
sleep(Timer.ONE_SECOND * 3);
236
237       // Removing existing notification
238
m_timer.removeNotification(periodicID);
239
240       // Check that none are still present
241
assertTrue(m_timer.isEmpty());
242
243       // Wait some more to be sure the periodic listener is not notified anymore
244
int periodTimes = periodicOccurrences.get();
245       assertTrue(periodTimes > 0);
246
247       sleep(Timer.ONE_SECOND * 5);
248
249       assertEquals(periodicOccurrences.get(), periodTimes);
250    }
251
252    public void testTimerNotStarted() throws Exception JavaDoc
253    {
254       // Don't start the Timer. Notification should not be emitted
255
// m_timer.start();
256

257       final MutableBoolean bool = new MutableBoolean(false);
258       NotificationListener JavaDoc listener = new NotificationListener JavaDoc()
259       {
260          public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
261          {
262             bool.set(true);
263          }
264       };
265       m_server.addNotificationListener(m_timerName, listener, null, null);
266
267       long now = System.currentTimeMillis();
268       m_timer.addNotification("timer-notif", "Must not be emitted", null, new Date JavaDoc(now + Timer.ONE_SECOND));
269
270       // Sleep to wait for the notification to happen
271
sleep(Timer.ONE_SECOND * 2);
272
273       assertFalse(bool.get());
274    }
275
276    public void testAddStopRemoveNotification() throws Exception JavaDoc
277    {
278       // Check that add + stop + remove behaves correctly
279

280       final MutableInteger count = new MutableInteger(0);
281       NotificationListener JavaDoc listener = new NotificationListener JavaDoc()
282       {
283          public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
284          {
285             count.set(count.get() + 1);
286          }
287       };
288       m_server.addNotificationListener(m_timerName, listener, null, null);
289
290       long now = System.currentTimeMillis();
291       Date JavaDoc date = new Date JavaDoc(now + Timer.ONE_SECOND);
292
293       // Periodic notification
294
Integer JavaDoc id = m_timer.addNotification("notif-type", "notif-message", "notif-data", date, Timer.ONE_SECOND);
295       m_timer.start();
296
297       // Wait for the notifications to arrive...
298
sleep(Timer.ONE_SECOND * 2);
299
300       m_timer.stop();
301
302       int counted = count.get();
303
304       assertEquals(m_timer.getNbNotifications(), 1);
305
306       m_timer.removeNotification(id);
307       assertTrue(m_timer.isEmpty());
308
309       // Wait some more to be sure that there are no more notifications
310
Thread.sleep(Timer.ONE_SECOND * 5);
311
312       assertEquals(counted, count.get());
313    }
314
315    public void testSendPastNotifications1() throws Exception JavaDoc
316    {
317       final MutableBoolean bool = new MutableBoolean(false);
318       NotificationListener JavaDoc listener = new NotificationListener JavaDoc()
319       {
320          public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
321          {
322             bool.set(true);
323          }
324       };
325       m_server.addNotificationListener(m_timerName, listener, null, null);
326
327       long now = System.currentTimeMillis();
328
329       // This one-shot notification is already passed, sendPastNotifications is false
330
// so the notification must not be emitted
331
Date JavaDoc date = new Date JavaDoc(now - Timer.ONE_SECOND);
332       m_timer.setSendPastNotifications(false);
333       m_timer.addNotification("notif-type", "notif-message", "notif-data", date);
334       m_timer.start();
335
336       // Wait that the notification arrives
337
sleep(Timer.ONE_SECOND);
338
339       assertFalse(bool.get());
340       assertTrue(m_timer.isEmpty());
341    }
342
343    public void testNotificationsWithOldDate() throws Exception JavaDoc
344    {
345       final MutableBoolean bool = new MutableBoolean(false);
346       NotificationListener JavaDoc listener = new NotificationListener JavaDoc()
347       {
348          public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
349          {
350             bool.set(true);
351          }
352       };
353       m_server.addNotificationListener(m_timerName, listener, null, null);
354
355       long now = System.currentTimeMillis();
356
357       m_timer.start();
358       Date JavaDoc date = new Date JavaDoc(now - Timer.ONE_SECOND);
359       m_timer.addNotification("notif-type", "notif-message", "notif-data", date);
360
361       // Wait that the notification arrives
362
sleep(Timer.ONE_SECOND);
363
364       assertTrue(bool.get());
365       assertTrue(m_timer.isEmpty());
366    }
367
368    public void testSendPastNotifications2() throws Exception JavaDoc
369    {
370       final MutableBoolean bool = new MutableBoolean(false);
371       NotificationListener JavaDoc listener = new NotificationListener JavaDoc()
372       {
373          public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
374          {
375             bool.set(true);
376          }
377       };
378       m_server.addNotificationListener(m_timerName, listener, null, null);
379
380       long now = System.currentTimeMillis();
381
382       // This one-shot notification is already passed, sendPastNotifications is true
383
// so the notification must be emitted
384
Date JavaDoc date = new Date JavaDoc(now - Timer.ONE_SECOND);
385       m_timer.setSendPastNotifications(true);
386       m_timer.addNotification("notif-type", "notif-message", "notif-data", date);
387       m_timer.start();
388
389       // Wait that the notification arrives
390
sleep(Timer.ONE_SECOND);
391
392       assertTrue(bool.get());
393       assertTrue(m_timer.isEmpty());
394    }
395
396    public void testSendPastNotifications3() throws Exception JavaDoc
397    {
398       final MutableInteger count = new MutableInteger(0);
399       NotificationListener JavaDoc listener = new NotificationListener JavaDoc()
400       {
401          public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
402          {
403             count.set(count.get() + 1);
404          }
405       };
406       m_server.addNotificationListener(m_timerName, listener, null, null);
407
408       long now = System.currentTimeMillis();
409
410       // This periodic notification started in the past, sendPastNotifications is false
411
// so only some notification must be emitted
412
long occurrences = 10;
413       long skip = 4;
414       Date JavaDoc date = new Date JavaDoc(now - Timer.ONE_SECOND * skip);
415       m_timer.setSendPastNotifications(false);
416       m_timer.addNotification("notif-type", "notif-message", "notif-data", date, Timer.ONE_SECOND, occurrences);
417       m_timer.start();
418
419       // Wait for the notifications to happen
420
sleep(Timer.ONE_SECOND * (occurrences + 1));
421
422       // Sometimes we loose one notification because we're not that fast, it's ok.
423
long expected = occurrences - skip;
424       if (count.get() != expected && count.get() != expected - 1)
425          fail("Expected notifications not emitted: expecting " + expected + " got " + count.get());
426       assertTrue(m_timer.isEmpty());
427    }
428
429    public void testSendPastNotifications4() throws Exception JavaDoc
430    {
431       final MutableInteger count = new MutableInteger(0);
432       NotificationListener JavaDoc listener = new NotificationListener JavaDoc()
433       {
434          public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
435          {
436             count.set(count.get() + 1);
437          }
438       };
439       m_server.addNotificationListener(m_timerName, listener, null, null);
440
441       long now = System.currentTimeMillis();
442
443       // This periodic notification started in the past, sendPastNotifications is true
444
// so all notifications must be emitted
445
long occurrences = 10;
446       long skip = 4;
447       Date JavaDoc date = new Date JavaDoc(now - Timer.ONE_SECOND * skip);
448       m_timer.setSendPastNotifications(true);
449       m_timer.addNotification("notif-type", "notif-message", "notif-data", date, Timer.ONE_SECOND, occurrences);
450       m_timer.start();
451
452       // Wait for the notifications to happen
453
sleep(Timer.ONE_SECOND * (occurrences + 1));
454
455       assertEquals(count.get(), occurrences);
456       assertTrue(m_timer.isEmpty());
457    }
458
459    public void testSendPastNotifications5() throws Exception JavaDoc
460    {
461       final MutableInteger count = new MutableInteger(0);
462       NotificationListener JavaDoc listener = new NotificationListener JavaDoc()
463       {
464          public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
465          {
466             count.set(count.get() + 1);
467          }
468       };
469       m_server.addNotificationListener(m_timerName, listener, null, null);
470
471       long now = System.currentTimeMillis();
472
473       // This periodic notification is started, sendPastNotifications is false
474
// the Timer is started, then stopped, then restarted
475
long occurrences = 10;
476       long pre = 2;
477       long skip = 4;
478       Date JavaDoc date = new Date JavaDoc(now + Timer.ONE_SECOND);
479       m_timer.setSendPastNotifications(false);
480       m_timer.addNotification("notif-type", "notif-message", "notif-data", date, Timer.ONE_SECOND, occurrences);
481       m_timer.start();
482
483       // Wait for the notifications to happen
484
sleep(Timer.ONE_SECOND * pre);
485       m_timer.stop();
486
487       // Sometimes we loose one notification because we're not that fast, it's ok.
488
if (count.get() != pre && count.get() != pre - 1)
489          fail("Expected notifications not emitted: expecting " + pre + " got " + count.get());
490       assertEquals(m_timer.getNbNotifications(), 1);
491
492       // Wait to skip some notification
493
sleep(Timer.ONE_SECOND * skip);
494
495       // Restart the Timer
496
m_timer.start();
497
498       // Wait for the remaining notifications to happen
499
sleep(Timer.ONE_SECOND * (occurrences - pre - skip + 1));
500
501       m_timer.stop();
502
503       // Sometimes we loose one notification because we're not that fast, it's ok.
504
long expected = occurrences - skip;
505       if (count.get() != expected && count.get() != expected - 1)
506          fail("Expected notifications not emitted. Expected " + expected + " or " + (expected - 1) + ". got " + count.get());
507       assertTrue(m_timer.isEmpty());
508    }
509
510    public void testSendPastNotifications6() throws Exception JavaDoc
511    {
512       final MutableInteger count = new MutableInteger(0);
513       NotificationListener JavaDoc listener = new NotificationListener JavaDoc()
514       {
515          public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
516          {
517             count.set(count.get() + 1);
518          }
519       };
520       m_server.addNotificationListener(m_timerName, listener, null, null);
521
522       long now = System.currentTimeMillis();
523
524       // This periodic notification is started, sendPastNotifications is true
525
// the Timer is started, then stopped, then restarted
526
long occurrences = 10;
527       long pre = 2;
528       long skip = 4;
529       Date JavaDoc date = new Date JavaDoc(now + Timer.ONE_SECOND);
530       m_timer.setSendPastNotifications(true);
531       m_timer.addNotification("notif-type", "notif-message", "notif-data", date, Timer.ONE_SECOND, occurrences, true);
532       m_timer.start();
533
534       // Wait for the notifications to happen
535
sleep(Timer.ONE_SECOND * pre);
536       m_timer.stop();
537
538       // Sometimes we loose one notification because we're not that fast, it's ok.
539
if (count.get() != pre && count.get() != pre - 1)
540          fail("Expected notifications not emitted: expecting " + pre + " got " + count.get());
541       assertFalse(m_timer.isEmpty());
542
543       // Wait to skip some notification
544
sleep(Timer.ONE_SECOND * skip);
545
546       // Restart the Timer
547
m_timer.start();
548
549       // Wait for the remaining notifications to happen
550
sleep(Timer.ONE_SECOND * (occurrences - pre - skip + 1));
551
552       m_timer.stop();
553       assertEquals(count.get(), occurrences);
554       assertTrue(m_timer.isEmpty());
555    }
556
557    public void testFixedDelay() throws Exception JavaDoc
558    {
559       m_timer.start();
560
561       final int occurrences = 100;
562       final String JavaDoc fdNotifType = "timer-test-fixed-delay";
563       final String JavaDoc frNotifType = "timer-test-fixed-rate";
564
565       final MutableInteger frOccurrences = new MutableInteger(0);
566       final MutableLong frElapsedTime = new MutableLong(0);
567       final MutableLong frLastTime = new MutableLong(System.currentTimeMillis());
568
569       NotificationListener JavaDoc frListener = new NotificationListener JavaDoc()
570       {
571          public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
572          {
573             if (frOccurrences.get() < occurrences)
574             {
575                long now = System.currentTimeMillis();
576                frElapsedTime.set(frElapsedTime.get() + (now - frLastTime.get()));
577                frLastTime.set(now);
578                frOccurrences.set(frOccurrences.get() + 1);
579             }
580          }
581       };
582
583       final MutableInteger fdOccurrences = new MutableInteger(0);
584       final MutableLong fdElapsedTime = new MutableLong(0);
585       final MutableLong fdLastTime = new MutableLong(System.currentTimeMillis());
586
587       NotificationListener JavaDoc fdListener = new NotificationListener JavaDoc()
588       {
589          public void handleNotification(Notification JavaDoc notification, Object JavaDoc handback)
590          {
591             if (fdOccurrences.get() < occurrences)
592             {
593                long now = System.currentTimeMillis();
594                fdElapsedTime.set(fdElapsedTime.get() + (now - fdLastTime.get()));
595                fdLastTime.set(now);
596                fdOccurrences.set(fdOccurrences.get() + 1);
597             }
598          }
599       };
600
601       m_server.addNotificationListener(m_timerName, fdListener, new NotificationFilter JavaDoc()
602       {
603          public boolean isNotificationEnabled(Notification JavaDoc notification)
604          {
605             return notification.getType().equals(fdNotifType);
606          }
607       }, null);
608
609       m_server.addNotificationListener(m_timerName, frListener, new NotificationFilter JavaDoc()
610       {
611          public boolean isNotificationEnabled(Notification JavaDoc notification)
612          {
613             return notification.getType().equals(frNotifType);
614          }
615       }, null);
616
617       // Testing fixed delay/fixed rate
618
long now = System.currentTimeMillis();
619       // Notify in one second
620
Date JavaDoc date = new Date JavaDoc(now + Timer.ONE_SECOND);
621       // Register to happen 10 times
622
m_timer.addNotification(fdNotifType, "timer-message", "user-data", date, 10, occurrences, false);
623       m_timer.addNotification(frNotifType, "timer-message", "user-data", date, 10, occurrences, true);
624
625       // Sleep some time
626
while (frOccurrences.get() < occurrences || fdOccurrences.get() < occurrences)
627       {
628          sleep(10);
629          System.gc();
630       }
631
632       assertEquals(frOccurrences.get(), occurrences);
633       assertEquals(fdOccurrences.get(), occurrences);
634
635       if (((1.0f * frElapsedTime.get()) / fdElapsedTime.get()) > 0.95)
636          fail("Fixed rate and fixed delay exhibit no execution rate differences");
637    }
638
639    public void testRemoveNotifications() throws Exception JavaDoc
640    {
641       m_timer.addNotification("mx4j.timer.test", "test notification", null, new Date JavaDoc(System.currentTimeMillis()), 8);
642       m_timer.addNotification("mx4j.timer.ignore", "ignore me", null, new Date JavaDoc(System.currentTimeMillis()), 4);
643       m_timer.addNotification("mx4j.timer.test", "another test", null, new Date JavaDoc(System.currentTimeMillis()), 8);
644       assertEquals(m_timer.getNbNotifications(), 3);
645       m_timer.start();
646       m_timer.removeNotifications("mx4j.timer.ignore");
647       assertEquals(m_timer.getNbNotifications(), 2);
648    }
649
650    public void testRemoveNonexistentNotifications() throws Exception JavaDoc
651    {
652       m_timer.addNotification("mx4j.timer.test", "test notification", null, new Date JavaDoc(System.currentTimeMillis()), 4);
653       m_timer.start();
654       try
655       {
656          m_timer.removeNotifications("mx4j.timer.bogus");
657          fail("Expecting InstanceNotFoundException");
658       }
659       catch (InstanceNotFoundException JavaDoc x)
660       {
661       }
662    }
663 }
664
Popular Tags