Last active
July 28, 2019 04:16
-
-
Save jaredsburrows/addcb1cee85d313992255dc22bcf16c9 to your computer and use it in GitHub Desktop.
RxAndroid overriding schedulers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.junit.After; | |
| import org.junit.Before; | |
| import rx.Scheduler; | |
| import rx.android.plugins.RxAndroidPlugins; | |
| import rx.android.plugins.RxAndroidSchedulersHook; | |
| import rx.schedulers.Schedulers; | |
| /** | |
| * JUnit Tests. | |
| * | |
| * @author <a href="mailto:jaredsburrows@gmail.com">Jared Burrows</a> | |
| */ | |
| public abstract class Rx1TestBase { | |
| @Before public void setUp() throws Exception { | |
| RxAndroidPlugins.getInstance().registerSchedulersHook(new RxAndroidSchedulersHook() { | |
| @Override public Scheduler getMainThreadScheduler() { | |
| return Schedulers.immediate(); | |
| } | |
| }); | |
| } | |
| @After public void tearDown() throws Exception { | |
| RxAndroidPlugins.getInstance().reset(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import io.reactivex.Scheduler; | |
| import io.reactivex.android.plugins.RxAndroidPlugins; | |
| import io.reactivex.functions.Function; | |
| import io.reactivex.schedulers.Schedulers; | |
| import org.junit.After; | |
| import org.junit.Before; | |
| import java.util.concurrent.Callable; | |
| /** | |
| * JUnit Tests. | |
| * | |
| * @author <a href="mailto:jaredsburrows@gmail.com">Jared Burrows</a> | |
| */ | |
| public abstract class Rx2TestBase { | |
| @Before public void setUp() throws Exception { | |
| RxAndroidPlugins.setInitMainThreadSchedulerHandler(new Function<Callable<Scheduler>, Scheduler>() { | |
| @Override public Scheduler apply(Callable<Scheduler> schedulerCallable) throws Exception { | |
| return Schedulers.trampoline(); | |
| } | |
| }); | |
| } | |
| @After public void tearDown() throws Exception { | |
| RxAndroidPlugins.reset(); | |
| } | |
| } |
Author
I still think that there's an issue, if you only call setInitMainThreadSchedulerHandler in your @Before you still risk of AndroidSchedulers being statically initialized with the Looper dependent Scheduler, which if you are running on the JVM, will thrown an Exception. It's best to call setInitMainThreadSchedulerHandler as early as you possibly can, so in @Before is typically too late, best to move it to either a test runner or in the static @BeforeClass.
Once the baseline Scheduler defined by setInitMainThreadSchedulerHandler has been evaluated, it can't be changed, so there's no benefit/need to call it in each test's @Before hook.
These gotchas are what's lead me to believe that using the plugins should only be done as a last resort.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@peter-tackage Thanks! Updated.