Permalink
Browse files

Added a poll method to TaskScheduler to help in writing tests without…

… the use of Thread.sleep()
  • Loading branch information...
orfjackal committed Nov 30, 2008
1 parent 78b7da7 commit e2c1af2ee61d41a1b1614a82bdbe4900661d28c1
@@ -31,11 +31,16 @@
package net.orfjackal.dimdwarf.scheduler;
+import javax.annotation.Nullable;
+
/**
* @author Esko Luontola
* @since 26.11.2008
*/
public interface TaskProducer {
TaskBootstrap takeNextTask() throws InterruptedException;
+
+ @Nullable
+ TaskBootstrap pollNextTask();
}
@@ -137,6 +137,10 @@ public TaskBootstrap takeNextTask() throws InterruptedException {
return scheduledTasks.take();
}
+ public TaskBootstrap pollNextTask() {
+ return scheduledTasks.poll();
+ }
+
@Nullable
private Runnable getTaskInsideTransaction0(ScheduledTaskHolder holder) {
cancelTakeOnRollback(holder);
@@ -55,8 +55,6 @@
@Group({"fast"})
public class TaskSchedulerSpec extends Specification<Object> {
- private static final int THREAD_SYNC_DELAY = 5;
-
private TaskSchedulerImpl scheduler;
private Provider<BindingStorage> bindings;
private Provider<EntityInfo> info;
@@ -140,20 +138,14 @@ public static TaskBootstrap takeNextTaskFrom(TaskSchedulerImpl scheduler) {
}
private boolean thereAreNoExecutableTasksRightNow() {
- interruptTestThreadAfter(THREAD_SYNC_DELAY); // TODO: figure out a more reliable thread synchronization method than sleeping
- specify(new Block() {
- public void run() throws Throwable {
- TaskBootstrap bootstrap;
- do {
- // FIXME: Sometimes blocks here when stress testing. Maybe the interrupt disappears somewhere?
- // At least these tests are affected:
- // WhenATaskIsScheduledWithFixedDelay.theFollowingExecutionsAreAfterTheFixedDelay
- // WhenATaskIsScheduledAtFixedRate.theFirstExecutionIsAfterTheInitialDelay
- bootstrap = scheduler.takeNextTask();
- } while (isNullTask(bootstrap));
+ while (true) {
+ TaskBootstrap bootstrap = scheduler.pollNextTask();
+ if (bootstrap == null) {
+ return true; // no expired tasks in queue
+ } else {
+ specify(isNullTask(bootstrap)); // current task is cancelled, but there might still be more
}
- }, should.raise(InterruptedException.class));
- return true;
+ }
}
private boolean isNullTask(final TaskBootstrap bootstrap) {
@@ -174,20 +166,6 @@ private boolean taskCanBeTakenExactlyAfterDelay(final int delay) {
return true;
}
- private static void interruptTestThreadAfter(final int delay) {
- final Thread testThread = Thread.currentThread();
- new Thread(new Runnable() {
- public void run() {
- try {
- Thread.sleep(delay); // TODO: figure out a more reliable thread synchronization method than sleeping
- } catch (InterruptedException e) {
- throw new RuntimeException(e);
- }
- testThread.interrupt();
- }
- }).start();
- }
-
private boolean _cancelsSuccessfully(Future<?> f) {
specify(!f.isDone());
specify(!f.isCancelled());
@@ -68,9 +68,14 @@ public FilterChain get() {
taskQueue = new LinkedBlockingDeque<TaskBootstrap>();
TaskProducer producer = new TaskProducer() {
+
public TaskBootstrap takeNextTask() throws InterruptedException {
return taskQueue.take();
}
+
+ public TaskBootstrap pollNextTask() {
+ return taskQueue.poll();
+ }
};
logger = mock(Logger.class);

0 comments on commit e2c1af2

Please sign in to comment.