Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ZakTaccardi
Last active December 15, 2015 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ZakTaccardi/570cd32308aa9690d182 to your computer and use it in GitHub Desktop.
Save ZakTaccardi/570cd32308aa9690d182 to your computer and use it in GitHub Desktop.
/**
* A repository to store someone's favorite color.
*/
public interface FavoriteColorRepo {
/**
* Loads the user's favorite color.
* @return ex- "blue"
*/
public Observable<String> getFavoriteColor();
/**
* Will asynchronously save the users favorite color.
*/
public void saveFavoriteColorAsync(String favoriteColor);
}
/**
* This is a fake database class that we will use to simulate storing and retrieving data from the
* disk with.
*/
public class FavoriteColorRepoImpl implements FavoriteColorRepo {
private final Scheduler main;
private final Scheduler io;
private final Scheduler comp;
private AtomicReference<String> favoriteColor;
public FavoriteColorRepoImpl(Scheduler main, Scheduler io, Scheduler comp) {
this.main = main;
this.io = io;
this.comp = comp;
favoriteColor = new AtomicReference<>("blue");
}
public Observable<String> getFavoriteColor() {
return Observable.defer(() -> Observable.just(favoriteColor.get()));
}
@Override
public void saveFavoriteColorAsync(final String color) {
Observable.just(color)
.map(s -> {
simulateLongRunningOperation();
favoriteColor.set(s); //"saves" the favorite color
return null; //nothing needs to be returned here
})
.subscribeOn(io) //io thread
.subscribe();
}
private void simulateLongRunningOperation() {
try {
//Let's pretend it took a while for us to save our favorite color.
Thread.sleep(1000L);
} catch (InterruptedException e) {
Exceptions.propagate(e);
}
}
}
/**
* Tests for the {@link FavoriteColorRepoImpl} class.
*/
@RunWith(RobolectricGradleTestRunner.class)
@Config(sdk = 21, constants = BuildConfig.class)
public class FavoriteColorRepoImplTest {
FavoriteColorRepoImpl favoriteColorRepoImpl;
@Before
public void setUp() throws Exception {
//let's set a new component for our application
final ApplicationComponent applicationComponent = DaggerApplicationComponent.builder()
.applicationModule(
new ApplicationModule((MyApplication) RuntimeEnvironment.application)
)
//and supply a different threading module.
.threadingModule(new MockThreadingModule())
.build();
MyApplication.setComponent(applicationComponent);
favoriteColorRepoImpl = (FavoriteColorRepoImpl) MyApplication.getComponent().getFavoriteColorRepo();
}
@Test
public void testSaveFavoriteColorAsync() throws Exception {
final String expected = "green"; //1
favoriteColorRepoImpl.saveFavoriteColorAsync(expected); //2
final String actual = favoriteColorRepoImpl.getFavoriteColor().toBlocking().first(); //3
Assert.assertThat("Favorite color should now be green after saving",
actual, Matchers.equalTo(expected)); //4
}
}
/**
* A mock threading module where all schedulers run on the main thread. This is useful for testing.
*/
@Module
public static class MockThreadingModule extends ThreadingModule {
@Override
public Scheduler provideMainThreadScheduler() {
return AndroidSchedulers.mainThread();
}
@Override
public Scheduler provideIoScheduler() {
return AndroidSchedulers.mainThread();
}
@Override
public Scheduler provideCompScheduler() {
return AndroidSchedulers.mainThread();
}
}
/**
* A dagger module to decouple threading even further!
*/
@Module
public class ThreadingModule {
public static final String MAIN_THREAD = "main";
public static final String IO_THREAD = "io";
public static final String COMPUTATION_THREAD = "comp";
@Provides
@Named(MAIN_THREAD)
@Singleton
public Scheduler provideMainThreadScheduler() {
return AndroidSchedulers.mainThread();
}
@Provides
@Named(IO_THREAD)
@Singleton
public Scheduler provideIoScheduler() {
return Schedulers.io();
}
@Provides
@Named(COMPUTATION_THREAD)
@Singleton
public Scheduler provideCompScheduler() {
return Schedulers.computation();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment