Skip to content

Instantly share code, notes, and snippets.

@dallasgutauckis
Last active August 29, 2015 14:14
Show Gist options
  • Save dallasgutauckis/d3f6d7052ab57528885e to your computer and use it in GitHub Desktop.
Save dallasgutauckis/d3f6d7052ab57528885e to your computer and use it in GitHub Desktop.
Schedulers for automated testing
// So, we needed to be able to change all of our scheduler usage to run in the immediate thread, but couldn't change out the Scheduler implementations. Instead, we interfaced out a new way of getting our schedulers:
public interface RxSchedulerFactory {
public rx.Scheduler io();
public rx.Scheduler computation();
public rx.Scheduler main();
public rx.Scheduler newThread();
}
public class RxSchedulerFactoryImpl implements RxSchedulerFactory {
@Override
public rx.Scheduler io() {
return Schedulers.io();
}
@Override
public rx.Scheduler computation() {
return Schedulers.computation();
}
@Override
public rx.Scheduler main() {
return AndroidSchedulers.mainThread();
}
@Override
public rx.Scheduler newThread() {
return Schedulers.newThread();
}
}
// Then, in other classes, we inject the RxSchedulerFactory, which the implementation uses from instead of directly from the Schedulers and AndroidSchedulers static classes
mTrackerService.sendEvents(tmpEvents)
.subscribeOn(mSchedulerFactory.io())
.observeOn(mSchedulerFactory.io())
.subscribe(new Observer<Response>() { … });
// This allows us to replace with a Test implementation
public final class TestRxSchedulerFactory implements RxSchedulerFactory {
@Override
public Scheduler io() {
return Schedulers.immediate();
}
@Override
public Scheduler computation() {
return Schedulers.immediate();
}
@Override
public Scheduler main() {
return Schedulers.immediate();
}
@Override
public Scheduler newThread() {
return Schedulers.immediate();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment