001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: A_Timer.java 8456 2006-06-13 08:28:03Z durieuxp $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.clients.timer;
027:
028: import java.util.Date;
029:
030: import javax.ejb.TimerHandle;
031: import org.objectweb.jonas.jtests.beans.transacted.Simple;
032: import org.objectweb.jonas.jtests.util.JTestCase;
033:
034: /**
035: * This is test of the TimerService.
036: * tests here are common to entity and session beans.
037: * beans used : transacted
038: * @author Philippe Durieux (jonas team)
039: */
040: public abstract class A_Timer extends JTestCase {
041:
042: /**
043: * constructor
044: * @param name name of the test suite.
045: */
046: public A_Timer(String name) {
047: super (name);
048: }
049:
050: /**
051: * Sets up the fixture, here load the beans if not loaded yet.
052: * This method is called before a test is executed.
053: */
054: protected void setUp() {
055: super .setUp();
056: useBeans("transacted", true);
057: }
058:
059: /**
060: * Get an instance of the bean.
061: * This method depends on the home used to get it.
062: * For entity bean, the arg is used to get a particular instance.
063: * For session beans, we get any session bean from the pool.
064: */
065: public abstract Simple getSimple(int i) throws Exception;
066:
067: // --------------------------------------------------------------------
068: // test cases
069: // --------------------------------------------------------------------
070:
071: /**
072: * Test single-event timer
073: */
074: public void testTimer1() throws Exception {
075: int duration = 5;
076: Simple s = getSimple(random(500000));
077: try {
078: int oldval = s.getTimerCount();
079: int id = s.setTimer(duration, 0);
080: sleep(2000);
081: assertEquals("timer expired too quickly", oldval, s
082: .getTimerCount());
083: sleep(4000);
084: assertEquals("timer did not expired", oldval + 1, s
085: .getTimerCount());
086: } finally {
087: s.remove();
088: }
089: }
090:
091: /**
092: * Test with absolute time
093: */
094: public void testTimerA1() throws Exception {
095: Date d = new Date(System.currentTimeMillis() + 1000);
096: Simple s = getSimple(random(500000));
097: try {
098: int oldval = s.getTimerCount();
099: int id = s.setTimer(d, 0);
100: sleep(2000);
101: assertEquals("timer did not expired", oldval + 1, s
102: .getTimerCount());
103: } finally {
104: s.remove();
105: }
106: }
107:
108: /**
109: * Test with absolute time (for persistent timer test)
110: */
111: public void testTimerAin4mn() throws Exception {
112: Date d = new Date(System.currentTimeMillis() + 4000 * 60);
113: Simple s = getSimple(random(500000));
114: try {
115: int oldval = s.getTimerCount();
116: int id = s.setTimer(d, 0);
117: } finally {
118: // Remove this line for persistent timers tests
119: s.remove();
120: }
121: }
122:
123: /**
124: * Test periodic timer
125: * Can be used also to test persistent timers.
126: */
127: public void testTimer2() throws Exception {
128: int duration = 5;
129: int period = 7;
130: Simple s = getSimple(random(500000));
131: try {
132: int oldval = s.getTimerCount();
133: int id = s.setTimer(duration, period);
134: sleep(2000);
135: assertEquals("timer expired too quickly", oldval, s
136: .getTimerCount());
137: sleep(4000);
138: assertEquals("timer did not expired", oldval + 1, s
139: .getTimerCount());
140: sleep(period * 1000);
141: assertEquals("timer did not expired twice", oldval + 2, s
142: .getTimerCount());
143: } finally {
144: s.remove();
145: }
146: }
147:
148: /**
149: * Test single-event timer Handle
150: */
151: public void testTimerHandle1() throws Exception {
152: int duration = 5;
153: Simple s = getSimple(random(500000));
154: try {
155: int oldval = s.getTimerCount();
156: int id = s.setTimerGetHandle(duration, 0);
157: sleep(2000);
158: assertEquals("timer expired too quickly", oldval, s
159: .getTimerCount());
160: sleep(4000);
161: assertEquals("timer did not expired", oldval + 1, s
162: .getTimerCount());
163: } finally {
164: s.remove();
165: }
166: }
167:
168: /**
169: * Test periodic timer Handle
170: */
171: public void testPeriodicTimerHandle1() throws Exception {
172: int duration = 5;
173: int period = 200;
174: Simple s = getSimple(random(500000));
175: try {
176: int oldval = s.getTimerCount();
177: int id = s.setTimerGetHandle(duration, period);
178: sleep(2000);
179: assertEquals("timer expired too quickly", oldval, s
180: .getTimerCount());
181: sleep(4000);
182: assertEquals("timer did not expired", oldval + 1, s
183: .getTimerCount());
184: } finally {
185: s.remove();
186: }
187: }
188:
189: /**
190: * test getTimeRemaining on a Timer
191: */
192: public void testTimeRemaining() throws Exception {
193: int duration1 = 5000;
194: int duration2 = 2000;
195: Simple s = getSimple(random(500000));
196: try {
197: int id1 = s.setTimer(duration1, 0);
198: int id2 = s.setTimer(duration2, 0);
199: long t1 = s.getTimeRemaining(id1) / 1000;
200: long t2 = s.getTimeRemaining(id2) / 1000;
201: sleep(1000);
202: assertTrue(t1 < duration1);
203: assertTrue(t2 < duration2);
204: assertTrue(t1 > duration1 - 2);
205: assertTrue(t2 > duration2 - 2);
206: } finally {
207: s.remove();
208: }
209: }
210:
211: /**
212: * Test du cancel Timer
213: */
214: public void testCancel1() throws Exception {
215: int duration = 2;
216: Simple s = getSimple(random(500000));
217: try {
218: int oldval = s.getTimerCount();
219: int id = s.setTimer(duration, 0);
220: assertEquals("timer expired too quickly", oldval, s
221: .getTimerCount());
222: s.cancelTimer(id);
223: sleep(1000 * (duration + 1));
224: assertEquals("timer did expired", oldval, s.getTimerCount());
225: } finally {
226: s.remove();
227: }
228: }
229:
230: /**
231: * Test of getTimers
232: */
233: public void testGetTimers() throws Exception {
234: Simple s = getSimple(random(500000));
235: int[] ids = new int[10];
236: int before = s.getTimerNumber();
237: try {
238: for (int i = 0; i < 10; i++) {
239: ids[i] = s.setTimer(i + 1, 100);
240: }
241: sleep(1000);
242: assertEquals("Bad number of timers", 10, s.getTimerNumber()
243: - before);
244: for (int i = 0; i < 10; i++) {
245: s.cancelTimer(ids[i]);
246: }
247: } finally {
248: s.remove();
249: }
250: }
251:
252: /**
253: * Test getTimers with cancel outside tx
254: * The cancelled timer should not be returned
255: */
256: public void testGetTimerCancelled() throws Exception {
257: Simple s = getSimple(random(500000));
258: int[] ids = new int[10];
259: int before = s.getTimerNumber();
260: try {
261: for (int i = 0; i < 10; i++) {
262: ids[i] = s.setTimer(i + 1, 100);
263: }
264: sleep(1000);
265: s.cancelTimer(ids[0]);
266: assertEquals("Bad number of timers", 9, s.getTimerNumber()
267: - before);
268: for (int i = 1; i < 10; i++) {
269: s.cancelTimer(ids[i]);
270: }
271: } finally {
272: s.remove();
273: }
274: }
275:
276: /**
277: * Test getTimers with cancel in the transaction.
278: * The cancelled timer should not be returned
279: */
280: public void testGetTimerCancelledInTx() throws Exception {
281: Simple s = getSimple(random(500000));
282: int[] ids = new int[10];
283: int before = s.getTimerNumber();
284: // start transaction
285: utx.begin();
286: try {
287: for (int i = 0; i < 10; i++) {
288: ids[i] = s.setTimer(i + 1, 100);
289: }
290: sleep(1000);
291: s.cancelTimer(ids[0]);
292: assertEquals("Bad number of timers", 9, s.getTimerNumber()
293: - before);
294: for (int i = 1; i < 10; i++) {
295: s.cancelTimer(ids[i]);
296: }
297: } finally {
298: utx.commit();
299: s.remove();
300: }
301: }
302:
303: /**
304: * Test getHandle on one-shot timer
305: */
306: public void testGetHandle1() throws Exception {
307: int duration = 2;
308: Simple s = getSimple(random(500000));
309: try {
310: int id = s.setTimer(duration, 0);
311: TimerHandle th = s.getTimerHandle(id);
312: assertTrue(th != null);
313: } finally {
314: s.remove();
315: }
316: }
317:
318: /**
319: * Test getHandle on one-shot timer that has expired
320: */
321: public void testGetExpiredHandle1() throws Exception {
322: int duration = 1;
323: Simple s = getSimple(random(500000));
324: try {
325: int id = s.setTimer(duration, 0);
326: sleep(3000);
327: TimerHandle th = s.getTimerHandle(id);
328: assertTrue(th == null);
329: } finally {
330: s.remove();
331: }
332: }
333: }
|