Skip to content

Commit e9c5d48

Browse files
authored
4.x: Fix Streamable.intervalRange emitting nothing when start + count overflows (#8398)
intervalRange accepts start + count == Long.MAX_VALUE + 1, for example start = 1 with count = Long.MAX_VALUE, which the Javadoc suggests for an unlimited range. The streamer stored end = start + count, which wraps to Long.MIN_VALUE, so counterLocal >= end held right away and the sequence completed empty. Compare against the end through a subtraction so the wrapped end still works, as Flowable.intervalRange does with c == end.
1 parent ca68c46 commit e9c5d48

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

‎src/main/java/io/reactivex/rxjava4/internal/operators/streamable/StreamableIntervalRange.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ static final class IntervalStreamer extends AtomicReference<Disposable>
8787
public void run() {
8888
var c = counterTask++;
8989
available = c;
90-
if (c + 1 >= end) {
90+
if (c + 1 - end >= 0) {
9191
DisposableHelper.dispose(this);
9292
}
9393
for (;;) {
@@ -107,7 +107,7 @@ public void run() {
107107
public @NonNull CompletionStage<Boolean> next() {
108108
waiter.getAndSet(null);
109109
for (;;) {
110-
if (counterLocal >= end) {
110+
if (counterLocal - end >= 0) {
111111
return NEXT_FALSE;
112112
}
113113
var c = current;

‎src/test/java/io/reactivex/rxjava4/internal/operators/streamable/StreamableIntervalRangeTest.java‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,23 @@ public void underNoOverflowLong() throws Throwable {
316316
Streamable.intervalRange(-2, Long.MAX_VALUE, 20, 20, TimeUnit.MILLISECONDS, Schedulers.single());
317317
}
318318

319+
@Test
320+
public void maxValueEnd() {
321+
Streamable.intervalRange(Long.MAX_VALUE - 2, 3, 1, 1, TimeUnit.MILLISECONDS, Schedulers.single())
322+
.test()
323+
.awaitDone(5, TimeUnit.SECONDS)
324+
.assertResult(Long.MAX_VALUE - 2, Long.MAX_VALUE - 1, Long.MAX_VALUE);
325+
}
326+
327+
@Test
328+
public void unlimitedFromOne() {
329+
Streamable.intervalRange(1, Long.MAX_VALUE, 1, 1, TimeUnit.MILLISECONDS, Schedulers.single())
330+
.take(3)
331+
.test()
332+
.awaitDone(5, TimeUnit.SECONDS)
333+
.assertResult(1L, 2L, 3L);
334+
}
335+
319336
@Test
320337
public void underflowLongExec() throws Throwable {
321338
assertThrows(IllegalArgumentException.class, () -> {

0 commit comments

Comments
 (0)