Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue #55 #56

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/org/jmock/lib/concurrent/DeterministicScheduler.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void runNextPendingCommand() {

scheduledTask.run();

if (scheduledTask.repeats()) {
if (!scheduledTask.isCancelled() && scheduledTask.repeats()) {
deltaQueue.add(scheduledTask.repeatDelay, scheduledTask);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package org.jmock.test.unit.lib.concurrent;


import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.sameInstance;
import static org.junit.Assert.assertThat;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

import org.jmock.Expectations;
import org.jmock.Sequence;
Expand Down Expand Up @@ -208,7 +206,37 @@ public void testCanCancelScheduledCommands() {

scheduler.tick(2, TimeUnit.SECONDS);
}


public void testCancellingARunningCommandStopsItFromRunningAgain() {
DeterministicScheduler deterministicScheduler = new DeterministicScheduler();
ObjectThatCancelsARepeatingTask objectThatCancelsARepeatingTask = new ObjectThatCancelsARepeatingTask(deterministicScheduler);
objectThatCancelsARepeatingTask.scheduleATaskThatWillCancelItself(1, TimeUnit.SECONDS);
deterministicScheduler.tick(2,TimeUnit.SECONDS);
assertThat("cancelling runnable run count", objectThatCancelsARepeatingTask.runCount(), is(1));
}
public static class ObjectThatCancelsARepeatingTask {

private final ScheduledExecutorService scheduler;
private ScheduledFuture<?> scheduledFuture;
private final AtomicInteger counter = new AtomicInteger();
public ObjectThatCancelsARepeatingTask(ScheduledExecutorService scheduler) {
this.scheduler = scheduler;
}
public void scheduleATaskThatWillCancelItself(int interval, TimeUnit unit){
scheduledFuture = scheduler.scheduleAtFixedRate(
new CancellingRunnable(), interval,interval,unit);
}
public int runCount(){
return counter.get();
}
private class CancellingRunnable implements Runnable{
@Override
public void run() {
scheduledFuture.cancel(true);
counter.incrementAndGet();
}
}
}
static final int TIMEOUT_IGNORED = 1000;

public void testCanScheduleCallablesAndGetTheirResultAfterTheyHaveBeenExecuted() throws Exception {
Expand Down