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_TimerEntity.java 8456 2006-06-13 08:28:03Z durieuxp $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.clients.timer;
027:
028: import org.objectweb.jonas.jtests.beans.transacted.Simple;
029: import org.objectweb.jonas.jtests.beans.transacted.SimpleEHome;
030:
031: /**
032: * Tests of the TimerService, specific to entity beans.
033: * @author Philippe Durieux (jonas team)
034: */
035: public abstract class A_TimerEntity extends A_Timer {
036:
037: /**
038: * constructor
039: * @param name name of the test suite.
040: */
041: public A_TimerEntity(String name) {
042: super (name);
043: }
044:
045: /**
046: * return SimpleHome, that can be either BMP, CMP1, or CMP2 bean.
047: */
048: protected abstract SimpleEHome getHome();
049:
050: /**
051: * Test interval timer
052: */
053: public void testTimer2() throws Exception {
054: int duration = 4;
055: int intervalduration = 3;
056: Simple s = getSimple(random(500000));
057: int expected = s.getTimerCount();
058: int id = s.setTimer(duration, intervalduration);
059: try {
060: assertEquals("timer expired too quickly", expected, s
061: .getTimerCount());
062: sleep(1000 * duration + 1500);
063: expected++;
064: assertEquals("timer did not expire first", expected, s
065: .getTimerCount());
066: for (int i = 0; i < 3; i++) {
067: sleep(1000 * intervalduration);
068: expected++;
069: assertEquals("timer did not expired at " + i, expected,
070: s.getTimerCount());
071: }
072: } finally {
073: s.remove(); // should remove the timer too.
074: }
075: }
076:
077: /**
078: * Test for bug #305384
079: * Create a timer for an entity bean
080: * Remove this bean instance before timer expire
081: * Check that the timer is removed.
082: */
083: public void test305384() throws Exception {
084: int duration = 7;
085: Simple s = getSimple(random(500000));
086: for (int i = 0; i < 10; i++) {
087: sleep(100);
088: s.startInfoTimer(duration, "Lock");
089: }
090: sleep(1000);
091: s.remove(); // should remove the timers too.
092: System.out
093: .println("Check that no exception is raised on server within 5 sec");
094: }
095:
096: /**
097: * Test this with a jonas restart to see if the timer
098: * is correctly restarted.
099: * @throws Exception
100: */
101: public void testPersistentTimer() throws Exception {
102: int duration = 4;
103: int intervalduration = 3;
104: Simple s = getSimple(random(500000));
105: int expected = s.getTimerCount();
106: s.setTimer(duration, intervalduration);
107: assertEquals("timer expired too quickly", expected, s
108: .getTimerCount());
109: sleep(1000 * duration + 1500);
110: expected++;
111: assertEquals("timer did not expire first", expected, s
112: .getTimerCount());
113: for (int i = 0; i < 3; i++) {
114: sleep(1000 * intervalduration);
115: expected++;
116: assertEquals("timer did not expired at " + i, expected, s
117: .getTimerCount());
118: }
119: }
120:
121: /**
122: * test timer in ejbPostCreate
123: */
124: public void testTimerInEjbPostCreate() throws Exception {
125: Simple s = getHome().createWithTimer(10, 2000);
126: sleep(4000);
127: s.remove();
128: }
129:
130: /**
131: * test immediate timer in ejbPostCreate (bug #300502)
132: */
133: public void testTimerInEjbPostCreate2() throws Exception {
134: Simple s = getHome().createWithTimer(10, 2);
135: sleep(1000);
136: s.remove();
137: }
138:
139: /**
140: * test cancel in ejbTimeout (bug #300306)
141: */
142: public void testCancelInTimeout() throws Exception {
143: int duration = 2;
144: int intervalduration = -1;
145: Simple s = getSimple(random(500000));
146: int id = s.setTimer(duration, intervalduration);
147: // timer should be cancelled at first expiration.
148: // TODO: test this.
149: sleep(6000);
150: s.remove();
151: }
152: }
|