Skip to content

Commit 7996633

Browse files
satoshunJakeWharton
authored andcommitted
Use async messages for Scheduler.scheduleDirect
1 parent 7a4a113 commit 7996633

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

rxandroid/src/main/java/io/reactivex/android/schedulers/HandlerScheduler.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,18 @@ final class HandlerScheduler extends Scheduler {
3232
}
3333

3434
@Override
35+
@SuppressLint("NewApi") // Async will only be true when the API is available to call.
3536
public Disposable scheduleDirect(Runnable run, long delay, TimeUnit unit) {
3637
if (run == null) throw new NullPointerException("run == null");
3738
if (unit == null) throw new NullPointerException("unit == null");
3839

3940
run = RxJavaPlugins.onSchedule(run);
4041
ScheduledRunnable scheduled = new ScheduledRunnable(handler, run);
41-
handler.postDelayed(scheduled, unit.toMillis(delay));
42+
Message message = Message.obtain(handler, scheduled);
43+
if (async) {
44+
message.setAsynchronous(true);
45+
}
46+
handler.sendMessageDelayed(message, unit.toMillis(delay));
4247
return scheduled;
4348
}
4449

rxandroid/src/test/java/io/reactivex/android/schedulers/HandlerSchedulerTest.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import android.os.Handler;
1717
import android.os.Looper;
18+
import android.os.Message;
1819
import io.reactivex.Scheduler;
1920
import io.reactivex.Scheduler.Worker;
2021
import io.reactivex.android.testutil.CountingRunnable;
@@ -34,6 +35,7 @@
3435
import org.robolectric.ParameterizedRobolectricTestRunner;
3536
import org.robolectric.annotation.Config;
3637
import org.robolectric.shadows.ShadowLooper;
38+
import org.robolectric.shadows.ShadowMessageQueue;
3739

3840
import static java.util.concurrent.TimeUnit.MINUTES;
3941
import static java.util.concurrent.TimeUnit.SECONDS;
@@ -43,6 +45,7 @@
4345
import static org.junit.Assert.assertSame;
4446
import static org.junit.Assert.assertTrue;
4547
import static org.junit.Assert.fail;
48+
import static org.robolectric.Shadows.shadowOf;
4649
import static org.robolectric.shadows.ShadowLooper.pauseMainLooper;
4750
import static org.robolectric.shadows.ShadowLooper.runUiThreadTasks;
4851
import static org.robolectric.shadows.ShadowLooper.runUiThreadTasksIncludingDelayedTasks;
@@ -61,9 +64,11 @@ public static Collection<Object[]> data() {
6164
}
6265

6366
private Scheduler scheduler;
67+
private boolean async;
6468

6569
public HandlerSchedulerTest(boolean async) {
6670
this.scheduler = new HandlerScheduler(new Handler(Looper.getMainLooper()), async);
71+
this.async = async;
6772
}
6873

6974
@Before
@@ -774,6 +779,47 @@ public void workerSchedulePeriodicallyInputValidation() {
774779
}
775780
}
776781

782+
@Test
783+
public void directScheduleSetAsync() {
784+
ShadowMessageQueue mainMessageQueue = shadowOf(Looper.getMainLooper().getQueue());
785+
786+
scheduler.scheduleDirect(new Runnable() {
787+
@Override public void run() {
788+
}
789+
});
790+
791+
Message message = mainMessageQueue.getHead();
792+
assertEquals(async, message.isAsynchronous());
793+
}
794+
795+
@Test
796+
public void workerScheduleSetAsync() {
797+
ShadowMessageQueue mainMessageQueue = shadowOf(Looper.getMainLooper().getQueue());
798+
799+
Worker worker = scheduler.createWorker();
800+
worker.schedule(new Runnable() {
801+
@Override public void run() {
802+
}
803+
});
804+
805+
Message message = mainMessageQueue.getHead();
806+
assertEquals(async, message.isAsynchronous());
807+
}
808+
809+
@Test
810+
public void workerSchedulePeriodicallySetAsync() {
811+
ShadowMessageQueue mainMessageQueue = shadowOf(Looper.getMainLooper().getQueue());
812+
813+
Worker worker = scheduler.createWorker();
814+
worker.schedulePeriodically(new Runnable() {
815+
@Override public void run() {
816+
}
817+
}, 1, 1, MINUTES);
818+
819+
Message message = mainMessageQueue.getHead();
820+
assertEquals(async, message.isAsynchronous());
821+
}
822+
777823
private static void idleMainLooper(long amount, TimeUnit unit) {
778824
// TODO delete this when https://github.com/robolectric/robolectric/pull/2592 is released.
779825
ShadowLooper.idleMainLooper(unit.toMillis(amount));

0 commit comments

Comments
 (0)