Last active
January 27, 2017 09:34
-
-
Save ahmedre/b7267d020b284faa5552c090b79707ad to your computer and use it in GitHub Desktop.
Contrived RxJava2 Interrupted Thread Example
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
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