Skip to content

Instantly share code, notes, and snippets.

@ahmedre
Last active January 27, 2017 09:34
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 ahmedre/b7267d020b284faa5552c090b79707ad to your computer and use it in GitHub Desktop.
Save ahmedre/b7267d020b284faa5552c090b79707ad to your computer and use it in GitHub Desktop.
Contrived RxJava2 Interrupted Thread Example
package com.quran.labs.androidquran.model.bookmark;
import org.junit.Test;
import io.reactivex.Single;
import io.reactivex.observers.TestObserver;
import io.reactivex.schedulers.Schedulers;
public class ContrivedTest {
private Single<Integer> fakeDataPiece() {
return Single.fromCallable(() -> 1)
.subscribeOn(Schedulers.io());
}
private Single<String> handleDataObservable() {
return Single.zip(fakeDataPiece(), fakeDataPiece(), fakeDataPiece(),
(one, two, three) -> one + two + three)
.flatMap(result -> Single.just(handleResult(result)))
.subscribeOn(Schedulers.io());
}
private String handleResult(Integer data) throws InterruptedException {
if (Thread.currentThread().isInterrupted()) {
throw new InterruptedException("interrupted thread");
}
return "result is: " + data;
}
@Test
public void testInterruption() {
TestObserver<String> testObserver = new TestObserver<>();
handleDataObservable()
.subscribe(testObserver);
testObserver.awaitTerminalEvent();
testObserver.assertNoErrors();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment