Skip to content

Instantly share code, notes, and snippets.

@knutwalker
Last active August 29, 2015 14:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save knutwalker/edc50f648d295cbb982f to your computer and use it in GitHub Desktop.
Save knutwalker/edc50f648d295cbb982f to your computer and use it in GitHub Desktop.
Usage of Rx TestScheduler
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