forked from jbossejb3/jboss-ejb3-timerservice
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JBAS-8232 Fixed timer cancellation - The Future wasn't being made ava…
…ilable to the timer when the timer was created out of a persisted timer
- Loading branch information
Showing
7 changed files
with
406 additions
and
60 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
testsuite/src/test/java/org/jboss/ejb3/timerservice/integration/test/cancel/SimpleTimer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source. | ||
* Copyright 2009, Red Hat Middleware LLC, and individual contributors | ||
* as indicated by the @author tags. See the copyright.txt file in the | ||
* distribution for a full listing of individual contributors. | ||
* | ||
* This is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation; either version 2.1 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this software; if not, write to the Free | ||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. | ||
*/ | ||
package org.jboss.ejb3.timerservice.integration.test.cancel; | ||
|
||
/** | ||
* SimpleTimer | ||
* | ||
* @author Jaikiran Pai | ||
* @version $Revision: $ | ||
*/ | ||
public interface SimpleTimer | ||
{ | ||
void createTimer(long intialDuration, long intervalDurationMillis); | ||
|
||
void stopTimers(); | ||
|
||
boolean timersCreated(); | ||
|
||
int getTimeoutCount(); | ||
} |
109 changes: 109 additions & 0 deletions
109
...te/src/test/java/org/jboss/ejb3/timerservice/integration/test/cancel/SimpleTimerSLSB.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source. | ||
* Copyright 2009, Red Hat Middleware LLC, and individual contributors | ||
* as indicated by the @author tags. See the copyright.txt file in the | ||
* distribution for a full listing of individual contributors. | ||
* | ||
* This is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation; either version 2.1 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this software; if not, write to the Free | ||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. | ||
*/ | ||
package org.jboss.ejb3.timerservice.integration.test.cancel; | ||
|
||
import java.util.Collection; | ||
|
||
import javax.annotation.Resource; | ||
import javax.ejb.EJB; | ||
import javax.ejb.Remote; | ||
import javax.ejb.Stateless; | ||
import javax.ejb.Timeout; | ||
import javax.ejb.Timer; | ||
import javax.ejb.TimerService; | ||
|
||
import org.jboss.ejb3.annotation.RemoteBinding; | ||
import org.jboss.logging.Logger; | ||
|
||
/** | ||
* SimpleTimerSLSB | ||
* | ||
* @author Jaikiran Pai | ||
* @version $Revision: $ | ||
*/ | ||
@Stateless | ||
@Remote(SimpleTimer.class) | ||
@RemoteBinding(jndiBinding = SimpleTimerSLSB.JNDI_NAME) | ||
public class SimpleTimerSLSB implements SimpleTimer | ||
{ | ||
|
||
private static Logger logger = Logger.getLogger(SimpleTimerSLSB.class); | ||
|
||
public static final String JNDI_NAME = "CancelTimerTestCaseBean"; | ||
|
||
@Resource | ||
private TimerService timerService; | ||
|
||
@EJB | ||
private TimeoutTracker timeoutTracker; | ||
|
||
@Override | ||
public void createTimer(long intialDuration, long intervalDurationMillis) | ||
{ | ||
// cancel existing timers | ||
cancelTimers(); | ||
logger.info("Creating timer starting at " + intialDuration | ||
+ " milli. sec from now and with a recurring duration of " + intervalDurationMillis + " milli. sec"); | ||
timerService.createTimer(intialDuration, intervalDurationMillis, "Test Timer"); | ||
|
||
} | ||
|
||
@Override | ||
public void stopTimers() | ||
{ | ||
cancelTimers(); | ||
} | ||
|
||
@Override | ||
public boolean timersCreated() | ||
{ | ||
Collection<Timer> timers = timerService.getTimers(); | ||
logger.info("Number of timers for bean: " + this.getClass().getSimpleName() + " = " + timers.size()); | ||
return (timers.size() > 0); | ||
} | ||
|
||
@Override | ||
public int getTimeoutCount() | ||
{ | ||
return this.timeoutTracker.getTimeoutCount(); | ||
} | ||
|
||
private void cancelTimers() | ||
{ | ||
logger.info("Canceling all existing timers: "); | ||
Collection<Timer> timers = timerService.getTimers(); | ||
for (Timer timer : timers) | ||
{ | ||
logger.info("Canceling timer: " + timer); | ||
timer.cancel(); | ||
|
||
} | ||
} | ||
|
||
@Timeout | ||
public void handleTimeout(Timer timer) | ||
{ | ||
logger.info("Timeout called on bean: " + this.getClass().getSimpleName() + " for timer " + timer); | ||
this.timeoutTracker.trackTimeout(timer); | ||
} | ||
|
||
} |
48 changes: 48 additions & 0 deletions
48
...ite/src/test/java/org/jboss/ejb3/timerservice/integration/test/cancel/TimeoutTracker.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source. | ||
* Copyright 2009, Red Hat Middleware LLC, and individual contributors | ||
* as indicated by the @author tags. See the copyright.txt file in the | ||
* distribution for a full listing of individual contributors. | ||
* | ||
* This is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation; either version 2.1 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this software; if not, write to the Free | ||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. | ||
*/ | ||
package org.jboss.ejb3.timerservice.integration.test.cancel; | ||
|
||
import javax.ejb.Singleton; | ||
import javax.ejb.Timer; | ||
|
||
/** | ||
* TimeoutTracker | ||
* | ||
* @author Jaikiran Pai | ||
* @version $Revision: $ | ||
*/ | ||
@Singleton | ||
public class TimeoutTracker | ||
{ | ||
|
||
private int count; | ||
|
||
public void trackTimeout(Timer timer) | ||
{ | ||
this.count++; | ||
} | ||
|
||
public int getTimeoutCount() | ||
{ | ||
return this.count; | ||
} | ||
} |
126 changes: 126 additions & 0 deletions
126
...st/java/org/jboss/ejb3/timerservice/integration/test/cancel/unit/CancelTimerTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/* | ||
* JBoss, Home of Professional Open Source. | ||
* Copyright 2009, Red Hat Middleware LLC, and individual contributors | ||
* as indicated by the @author tags. See the copyright.txt file in the | ||
* distribution for a full listing of individual contributors. | ||
* | ||
* This is free software; you can redistribute it and/or modify it | ||
* under the terms of the GNU Lesser General Public License as | ||
* published by the Free Software Foundation; either version 2.1 of | ||
* the License, or (at your option) any later version. | ||
* | ||
* This software is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public | ||
* License along with this software; if not, write to the Free | ||
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | ||
* 02110-1301 USA, or see the FSF site: http://www.fsf.org. | ||
*/ | ||
package org.jboss.ejb3.timerservice.integration.test.cancel.unit; | ||
|
||
import java.io.File; | ||
import java.net.URL; | ||
import java.util.Date; | ||
|
||
import junit.framework.Assert; | ||
|
||
import org.jboss.ejb3.timerservice.integration.test.cancel.SimpleTimer; | ||
import org.jboss.ejb3.timerservice.integration.test.cancel.SimpleTimerSLSB; | ||
import org.jboss.ejb3.timerservice.integration.test.common.AbstractTimerServiceTestCase; | ||
import org.jboss.logging.Logger; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
/** | ||
* Tests that timers which have been cancelled will no longer fire timeouts | ||
* | ||
* @see https://jira.jboss.org/browse/JBAS-8232 | ||
* | ||
* @author Jaikiran Pai | ||
* @version $Revision: $ | ||
*/ | ||
public class CancelTimerTestCase extends AbstractTimerServiceTestCase | ||
{ | ||
private static Logger logger = Logger.getLogger(CancelTimerTestCase.class); | ||
|
||
private URL deployment; | ||
|
||
/** | ||
* | ||
* @return | ||
* @throws Exception | ||
*/ | ||
@Before | ||
public void before() throws Exception | ||
{ | ||
String jarName = "cancel-timer-test.jar"; | ||
File jar = buildSimpleJar(jarName, SimpleTimerSLSB.class.getPackage()); | ||
this.deployment = jar.toURI().toURL(); | ||
this.redeploy(deployment); | ||
} | ||
|
||
@After | ||
public void after() throws Exception | ||
{ | ||
if (this.deployment != null) | ||
{ | ||
this.undeploy(deployment); | ||
} | ||
} | ||
|
||
/** | ||
* Tests that a non-calendar expression based timer, when cancelled, will no longer | ||
* fire timeouts | ||
* @see JBAS-8232 https://jira.jboss.org/browse/JBAS-8232 | ||
* @throws Exception | ||
*/ | ||
@Test | ||
public void testCancelOfSimpleTimer() throws Exception | ||
{ | ||
SimpleTimer bean = (SimpleTimer) this.getInitialContext().lookup(SimpleTimerSLSB.JNDI_NAME); | ||
|
||
long twoSeconds = 2000; | ||
long everySecond = 1000; | ||
|
||
bean.createTimer(twoSeconds, everySecond); | ||
logger.debug("Created timer to fire every second starting at " + new Date(System.currentTimeMillis() + twoSeconds)); | ||
|
||
// check that the timers were created | ||
Assert.assertTrue("No timers were created for bean " + SimpleTimerSLSB.class.getSimpleName(), bean | ||
.timersCreated()); | ||
|
||
final long THREE_SECONDS = 3000; | ||
|
||
// now wait for atleast 1 timeout to happen | ||
logger.info("Sleeping for 3 seconds for timeout to happen"); | ||
Thread.sleep(THREE_SECONDS); | ||
|
||
// check that atleast one timeout occured | ||
int timeoutCount = bean.getTimeoutCount(); | ||
Assert.assertTrue("Not even 1 timeout occured", timeoutCount > 0); | ||
|
||
// now cancel the timer | ||
bean.stopTimers(); | ||
|
||
int timeoutCountImmidiatelyAfterCancel = bean.getTimeoutCount(); | ||
|
||
// check that there are no more active timers for the bean | ||
Assert.assertFalse("Active timers found, even after cancelling the timers, on bean " | ||
+ SimpleTimerSLSB.class.getSimpleName(), bean.timersCreated()); | ||
|
||
// wait for a few more seconds and check whether the timer was indeed cancelled | ||
// or whether it is still firing timeouts | ||
logger | ||
.info("Sleeping for 3 more seconds after cancelling the timer, to make sure the timers were really cancelled"); | ||
Thread.sleep(THREE_SECONDS); | ||
|
||
int finalTimeoutCount = bean.getTimeoutCount(); | ||
// make sure that the timeout count immidiately after cancellation of timers is the same as the latest timeout count | ||
Assert.assertEquals("Timers wasn't really cancelled. Timeouts are still happening", | ||
timeoutCountImmidiatelyAfterCancel, finalTimeoutCount); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.