Last active
August 15, 2018 04:05
-
-
Save mzakyalvan/7fe5f0cebea3a9038a2326dc7d4b7e2d to your computer and use it in GitHub Desktop.
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 sample; | |
| import io.reactivex.Single; | |
| import java.util.Random; | |
| import java.util.concurrent.CountDownLatch; | |
| import java.util.concurrent.TimeUnit; | |
| public class SimulateTimeout { | |
| public static void main(String[] args) throws Exception { | |
| SimulateTimeout sample = new SimulateTimeout(); | |
| CountDownLatch latch = new CountDownLatch(10); | |
| for(int i = 0; i < 10; i++) { | |
| sample.process("input#"+i) | |
| .doOnSuccess(output -> latch.countDown()) | |
| .subscribe(output -> System.out.println(output), Throwable::printStackTrace); | |
| } | |
| latch.await(); | |
| } | |
| final Random random = new Random(); | |
| public Single<String> process(String input) { | |
| return Single.just(input) | |
| .flatMap(request -> Single.just(request + " : Processed") | |
| // Simulate long running process. | |
| .delay(random.nextInt(2_000), TimeUnit.MILLISECONDS) | |
| .timeout(1_000, TimeUnit.MILLISECONDS, Single.just(request + " : TimedOut"))); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment