Skip to content

Commit

Permalink
Schedule tasks with negative delays immediately as per Scheduler spec
Browse files Browse the repository at this point in the history
This fixes the issue in #346.
  • Loading branch information
samueltardieu committed Nov 8, 2016
1 parent 8c65ad8 commit 3aa9588
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@ final class HandlerScheduler extends Scheduler {
@Override
public Disposable scheduleDirect(Runnable run, long delay, TimeUnit unit) {
if (run == null) throw new NullPointerException("run == null");
if (delay < 0) throw new IllegalArgumentException("delay < 0: " + delay);
if (unit == null) throw new NullPointerException("unit == null");

run = RxJavaPlugins.onSchedule(run);
ScheduledRunnable scheduled = new ScheduledRunnable(handler, run);
handler.postDelayed(scheduled, unit.toMillis(delay));
handler.postDelayed(scheduled, Math.max(0L, unit.toMillis(delay)));
return scheduled;
}

Expand All @@ -57,7 +56,6 @@ private static final class HandlerWorker extends Worker {
@Override
public Disposable schedule(Runnable run, long delay, TimeUnit unit) {
if (run == null) throw new NullPointerException("run == null");
if (delay < 0) throw new IllegalArgumentException("delay < 0: " + delay);
if (unit == null) throw new NullPointerException("unit == null");

if (disposed) {
Expand All @@ -71,7 +69,7 @@ public Disposable schedule(Runnable run, long delay, TimeUnit unit) {
Message message = Message.obtain(handler, scheduled);
message.obj = this; // Used as token for batch disposal of this worker's runnables.

handler.sendMessageDelayed(message, unit.toMillis(delay));
handler.sendMessageDelayed(message, Math.max(0L, unit.toMillis(delay)));

// Re-check disposed state for removing in case we were racing a call to dispose().
if (disposed) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ public void directScheduleOncePostsImmediately() {
assertEquals(1, counter.get());
}

@Test
public void directScheduleOnceWithNegativeDelayPostsImmediately() {
CountingRunnable counter = new CountingRunnable();
scheduler.scheduleDirect(counter, -1, TimeUnit.MINUTES);

runUiThreadTasks();
assertEquals(1, counter.get());
}

@Test
public void directScheduleOnceUsesHook() {
final CountingRunnable newCounter = new CountingRunnable();
Expand Down Expand Up @@ -300,6 +309,17 @@ public void workerScheduleOncePostsImmediately() {
assertEquals(1, counter.get());
}

@Test
public void workerScheduleOnceWithNegativeDelayPostsImmediately() {
Worker worker = scheduler.createWorker();

CountingRunnable counter = new CountingRunnable();
worker.schedule(counter, -1, TimeUnit.MINUTES);

runUiThreadTasks();
assertEquals(1, counter.get());
}

@Test
public void workerScheduleOnceUsesHook() {
final CountingRunnable newCounter = new CountingRunnable();
Expand Down Expand Up @@ -665,12 +685,6 @@ public void directScheduleOnceInputValidation() {
} catch (NullPointerException e) {
assertEquals("run == null", e.getMessage());
}
try {
scheduler.scheduleDirect(new CountingRunnable(), -1, MINUTES);
fail();
} catch (IllegalArgumentException e) {
assertEquals("delay < 0: -1", e.getMessage());
}
try {
scheduler.scheduleDirect(new CountingRunnable(), 1, null);
fail();
Expand All @@ -687,12 +701,6 @@ public void directSchedulePeriodicallyInputValidation() {
} catch (NullPointerException e) {
assertEquals("run == null", e.getMessage());
}
try {
scheduler.schedulePeriodicallyDirect(new CountingRunnable(), -1, 1, MINUTES);
fail();
} catch (IllegalArgumentException e) {
assertEquals("delay < 0: -1", e.getMessage());
}
try {
scheduler.schedulePeriodicallyDirect(new CountingRunnable(), 1, -1, MINUTES);
fail();
Expand Down Expand Up @@ -722,12 +730,6 @@ public void workerScheduleOnceInputValidation() {
} catch (NullPointerException e) {
assertEquals("run == null", e.getMessage());
}
try {
worker.schedule(new CountingRunnable(), -1, MINUTES);
fail();
} catch (IllegalArgumentException e) {
assertEquals("delay < 0: -1", e.getMessage());
}
try {
worker.schedule(new CountingRunnable(), 1, null);
fail();
Expand All @@ -745,12 +747,6 @@ public void workerSchedulePeriodicallyInputValidation() {
} catch (NullPointerException e) {
assertEquals("run == null", e.getMessage());
}
try {
worker.schedulePeriodically(new CountingRunnable(), -1, 1, MINUTES);
fail();
} catch (IllegalArgumentException e) {
assertEquals("delay < 0: -1", e.getMessage());
}
try {
worker.schedulePeriodically(new CountingRunnable(), 1, -1, MINUTES);
fail();
Expand Down

0 comments on commit 3aa9588

Please sign in to comment.