Last active
August 29, 2015 14:15
-
-
Save knutwalker/edc50f648d295cbb982f to your computer and use it in GitHub Desktop.
Usage of Rx TestScheduler
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
import rx.Observable; | |
import rx.Scheduler; | |
import rx.schedulers.Schedulers; | |
import rx.schedulers.TestScheduler; | |
import java.io.IOException; | |
import java.util.List; | |
import java.util.concurrent.TimeUnit; | |
public final class TimersTest { | |
static Observable<List<Long>> observe(final Scheduler scheduler) { | |
return Observable | |
.interval(100, TimeUnit.MILLISECONDS, scheduler) | |
.take(1337) | |
.buffer(1, TimeUnit.SECONDS, scheduler); | |
} | |
static void real() throws IOException { | |
final Scheduler scheduler = Schedulers.io(); | |
observe(scheduler).forEach(System.out::println); | |
System.in.read(); | |
} | |
static void test() throws IOException { | |
final TestScheduler scheduler = Schedulers.test(); | |
observe(scheduler).forEach(System.out::println); | |
while (true) { | |
final int read = System.in.read(); | |
if (read == 113) break; | |
System.out.println("Advance 500 millis"); | |
scheduler.advanceTimeBy(500, TimeUnit.MILLISECONDS); | |
} | |
} | |
public static void main(final String... args) throws IOException { | |
test(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment