-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Closed
Milestone
Description
Recently I notices a problem when using Single.timeout with a value really small. So in the snippet bellow you will see 0 nanoseconds, but it also happens with 10 nanoseconds. So also values > 0.
The problem is, that in such a case, the error is no always emitted.
for (int i = 0; i < 10000000; i++) {
final int y = i;
final CountDownLatch latch = new CountDownLatch(1);
Single.never()
.timeout(0, TimeUnit.NANOSECONDS, Schedulers.computation())
.subscribe(v -> {}, e -> {
System.out.println("timeout " + y);
latch.countDown();
});
if (!latch.await(1, TimeUnit.SECONDS)) {
throw new IllegalStateException("Timeout was not happening!");
}
}
The workaround so far for me is to use a Single.timer instead.
for (int i = 0; i < 10000000; i++) {
final int y = i;
final CountDownLatch latch = new CountDownLatch(1);
Single.amb(Arrays.asList(Single.never(), Single.timer(0, TimeUnit.NANOSECONDS, Schedulers.computation()).doOnSuccess(l -> {
throw new TimeoutException();
})))
.subscribe(v -> {}, e -> {
System.out.println("timeout " + y);
latch.countDown();
});
if (!latch.await(1, TimeUnit.SECONDS)) {
throw new IllegalStateException("Timeout was not happening!");
}
}
The above snippets take like 2-3 minute to run in the success case. Most of the time the first one fails during that time, but sometimes also succeeds.
I tested it also with Observable, but could not notice the problematic behavior.
Do you have an idea where this comes from?