Skip to content

Commit

Permalink
[Java] Return a boolean to indicate if cancelling a timer was success…
Browse files Browse the repository at this point in the history
…ful or not.
  • Loading branch information
mjpt777 committed Jan 1, 2018
1 parent 03e2a27 commit 8793abc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
9 changes: 7 additions & 2 deletions agrona/src/main/java/org/agrona/DeadlineTimerWheel.java
Original file line number Diff line number Diff line change
Expand Up @@ -213,19 +213,24 @@ public long scheduleTimer(final long deadline)
* Cancel a previously scheduled timer.
*
* @param timerId of the timer to cancel.
* @return true if successful otherwise false if the timerId did not exist.
*/
public void cancelTimer(final long timerId)
public boolean cancelTimer(final long timerId)
{
final int wheelIndex = tickForTimerId(timerId);
final int arrayIndex = indexInTickArray(timerId);

final long[] array = wheel[wheelIndex];

if (array[arrayIndex] != NULL_TIMER)
if (NULL_TIMER != array[arrayIndex])
{
array[arrayIndex] = NULL_TIMER;
timerCount--;

return true;
}

return false;
}

/**
Expand Down
5 changes: 4 additions & 1 deletion agrona/src/test/java/org/agrona/DeadlineTimerWheelTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static junit.framework.TestCase.assertFalse;
import static junit.framework.TestCase.assertTrue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertNotNull;
Expand Down Expand Up @@ -186,7 +188,8 @@ public void shouldBeAbleToCancelTimer()
}
while (-1 == firedTimestamp.value && controlTimestamp < (16 * wheel.tickResolution()));

wheel.cancelTimer(id);
assertTrue(wheel.cancelTimer(id));
assertFalse(wheel.cancelTimer(id));

do
{
Expand Down

0 comments on commit 8793abc

Please sign in to comment.