KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > stress > timer > TimerTestCase


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 package test.stress.timer;
23
24 import java.util.Date JavaDoc;
25 import java.util.Random JavaDoc;
26
27 import javax.management.MBeanServer JavaDoc;
28 import javax.management.MBeanServerFactory JavaDoc;
29 import javax.management.Notification JavaDoc;
30 import javax.management.NotificationListener JavaDoc;
31 import javax.management.ObjectName JavaDoc;
32 import javax.management.timer.Timer JavaDoc;
33
34 import junit.framework.TestCase;
35
36 /**
37  * Timer Stress Tests
38  *
39  * This test works by starting a lot of notifications at the start and
40  * checks the concurrency by performing lots of operations.<p>
41  *
42  * It then waits for the slow notifications to complete allowing any lag
43  * due to slower computers to be caught up with.<p>
44  *
45  * Any concurrency problem or dropped notifications should show up
46  * when the test times out and the target notifications are not reached.
47  *
48  * @author <a HREF="mailto:Adrian.Brock@HappeningTimes.com">Adrian Brock</a>.
49  */

50 public class TimerTestCase
51    extends TestCase
52    implements NotificationListener JavaDoc
53 {
54    // Constants ---------------------------------------------------------------
55

56    String JavaDoc TIMER_TYPE = "TimerType";
57    String JavaDoc MESSAGE = "Message";
58    String JavaDoc USER_DATA = "UserData";
59
60    // Attributes --------------------------------------------------------------
61

62    /**
63     * The MBeanServer
64     */

65    MBeanServer JavaDoc server;
66
67    /**
68     * The object name of the timer
69     */

70    ObjectName JavaDoc timerName;
71
72    /**
73     * The timer
74     */

75    Timer JavaDoc timer;
76
77    /**
78     * The number of notifications
79     */

80    int notifications = 0;
81
82    /**
83     * The target notifications
84     */

85    int target = 0;
86
87    /**
88     * The percentage done
89     */

90    int nextPercentage = 10;
91
92    // Constructor -------------------------------------------------------------
93

94    /**
95     * Construct the test
96     */

97    public TimerTestCase(String JavaDoc s)
98    {
99       super(s);
100    }
101
102    // Tests -------------------------------------------------------------------
103

104    /**
105     * Test the timer under stress conditions
106     */

107    public void testTortureOne()
108       throws Exception JavaDoc
109    {
110       target = TimerSUITE.TIMERS * TimerSUITE.NOTIFICATIONS;
111       System.err.println("Timer Torture One: target=" + target);
112
113       initTest();
114       try
115       {
116          initTimer();
117          startTimer();
118
119          // Start lots of timer notifications
120
nextPercentage = 10;
121          Random JavaDoc random = new Random JavaDoc();
122          for (int i = 0; i < TimerSUITE.TIMERS; i++)
123          {
124             addNotification(TimerSUITE.OFFSET,
125                             random.nextInt(TimerSUITE.PERIOD),
126                              TimerSUITE.NOTIFICATIONS);
127          }
128
129          // Perform some operations
130
for (int k = 0; k < target; k++)
131          {
132             Integer JavaDoc id = addNotification(Timer.ONE_HOUR, Timer.ONE_HOUR, 1);
133             timer.getAllNotificationIDs();
134             timer.getDate(id);
135             timer.getNbNotifications();;
136             timer.getNbOccurences(id);
137             timer.getNotificationIDs(TIMER_TYPE);
138             timer.getNotificationUserData(id);
139             timer.getPeriod(id);
140             timer.getSendPastNotifications();
141             timer.isActive();
142             timer.isEmpty();
143             timer.setSendPastNotifications(true);
144             removeNotification(id);
145          }
146          
147          // Give it time to complete but check for stalled
148
for (int j = 0; j < TimerSUITE.NOTIFICATIONS; j++)
149          {
150            if (notifications >= target)
151               break;
152            int lastNotifications = notifications;
153            sleep(TimerSUITE.PERIOD * 10);
154            if (lastNotifications == notifications)
155            {
156               sleep(TimerSUITE.PERIOD * 10);
157               if (lastNotifications == notifications)
158                  break;
159            }
160          }
161
162          // Test the number of notifications
163
assertTrue(notifications == target);
164       }
165       finally
166       {
167          endTest();
168       }
169    }
170
171    // Support -----------------------------------------------------------------
172

173    /**
174     * Start a new test
175     */

176    private void initTest()
177    {
178       notifications = 0;
179       server = MBeanServerFactory.createMBeanServer();
180    }
181
182    /**
183     * End the test
184     */

185    private void endTest()
186       throws Exception JavaDoc
187    {
188       server.removeNotificationListener(timerName, this);
189       stopTimer();
190       MBeanServerFactory.releaseMBeanServer(server);
191    }
192
193    /**
194     * Create a timer and register ourselves as a listener
195     */

196    private void initTimer()
197    {
198       try
199       {
200          timer = new Timer JavaDoc();
201          timerName = new ObjectName JavaDoc("test:type=timer");
202          server.registerMBean(timer, timerName);
203          server.addNotificationListener(timerName, this, null, null);
204       }
205       catch (Exception JavaDoc e)
206       {
207          fail(e.toString());
208       }
209    }
210
211    /**
212     * Start the timer
213     */

214    private void startTimer()
215    {
216       timer.start();
217    }
218
219    /**
220     * Stop the timer, does a small wait to avoid problems with the RI
221     */

222    private void stopTimer()
223    {
224       timer.removeAllNotifications();
225       timer.stop();
226    }
227
228    /**
229     * Add a notification
230     */

231    private Integer JavaDoc addNotification(long offset, long period, long occurs)
232    {
233       return timer.addNotification(TIMER_TYPE, MESSAGE, USER_DATA,
234                                    timeOffset(offset), period, occurs);
235    }
236
237    /**
238     * Remove a notification
239     */

240    private void removeNotification(Integer JavaDoc id)
241       throws Exception JavaDoc
242    {
243       timer.removeNotification(id);
244    }
245
246    /**
247     * Handle the notification, just add it to the list
248     */

249    public synchronized void handleNotification(Notification JavaDoc n, Object JavaDoc ignored)
250    {
251       notifications++;
252       float percentage = 100 * notifications / target;
253       if (percentage >= nextPercentage)
254       {
255          System.err.println("Done " + nextPercentage + "%");
256          nextPercentage += 10;
257       }
258    }
259
260    /**
261     * Get the time using and offset
262     */

263    private Date JavaDoc timeOffset(long offset)
264    {
265       return new Date JavaDoc(System.currentTimeMillis() + offset);
266    }
267
268    /**
269     * Sleep for a bit
270     */

271    private void sleep(long time)
272    {
273       try
274       {
275          Thread.sleep(time);
276       }
277       catch (InterruptedException JavaDoc ignored)
278       {
279       }
280    }
281 }
282
Popular Tags