Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import static io.reactivex.rxjava4.internal.operators.streamable.StreamableHelper.suppressValueAndCancel;
import static io.reactivex.rxjava4.internal.operators.streamable.StreamableHelper.whenBoth;

import java.util.concurrent.CompletionStage;
import java.util.concurrent.*;

import io.reactivex.rxjava4.annotations.NonNull;
import io.reactivex.rxjava4.core.Streamable;
Expand Down Expand Up @@ -57,16 +57,16 @@ static final class TakeUntilMainStreamer<T, U> implements Streamer<T> {

final DisposableStreamerCancellation otherCancellation;

final CompletionStage<Boolean> otherNext;
final CompletableFuture<Boolean> otherNext;

CompletionStage<Boolean> mainNext;
CompletionStage<Boolean> mainNext = NEXT_FALSE;

public TakeUntilMainStreamer(
Streamer<? extends T> upstream,
Streamer<? extends U> otherStreamer,
DisposableStreamerCancellation mainCancellation,
DisposableStreamerCancellation otherCancellation,
CompletionStage<Boolean> otherNext
CompletableFuture<Boolean> otherNext
) {
this.upstream = upstream;
this.otherStreamer = otherStreamer;
Expand All @@ -77,12 +77,17 @@ public TakeUntilMainStreamer(

@Override
public @NonNull CompletionStage<Boolean> next() {
mainNext = suppressCancel(upstream.next(), mainCancellation, false);
return StreamableHelper.race(mainNext, otherNext, w -> {
if (w == 2) {
var mainResult = new CompletableFuture<Boolean>();
var result = StreamableHelper.race(otherNext, mainResult, w -> {
if (w == 1) {
mainCancellation.dispose();
}
});
if (!otherNext.isDone()) {
mainNext = suppressCancel(upstream.next(), mainCancellation, false);
StreamableHelper.forward(mainNext, mainResult);
}
return result;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@

package io.reactivex.rxjava4.internal.operators.streamable;

import static org.junit.jupiter.api.Assertions.*;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.*;

import org.junit.jupiter.api.Test;

import io.reactivex.rxjava4.core.Streamable;
import io.reactivex.rxjava4.disposables.Disposable;
import io.reactivex.rxjava4.exceptions.TestException;
import io.reactivex.rxjava4.schedulers.Schedulers;
import io.reactivex.rxjava4.schedulers.TestScheduler;

public class StreamableTakeUntilTest extends StreamableBaseTest {

Expand Down Expand Up @@ -59,6 +64,43 @@ public void empty() throws Throwable {
.assertResult();
}

@Test
public void emptyOtherBeforeSynchronousMain() throws Throwable {
var reads = new AtomicInteger();

Streamable.range(1, 5)
.doOnNext(_ -> reads.incrementAndGet())
.takeUntil(Streamable.empty())
.test()
.awaitDone(5, TimeUnit.SECONDS)
.assertResult();

assertEquals(0, reads.get());
}

@Test
public void timerFiresDuringSynchronousPull() throws Throwable {
var scheduler = new TestScheduler();
var cancelled = Disposable.empty();
var cancelledDuringPull = new AtomicBoolean();

Streamable.range(1, 5)
.withCancellation(cancellation -> {
cancellation.add(cancelled);
return cancellation;
})
.doOnNext(_ -> {
scheduler.advanceTimeBy(1, TimeUnit.SECONDS);
cancelledDuringPull.set(cancelled.isDisposed());
})
.takeUntil(Streamable.timer(1, TimeUnit.SECONDS, scheduler))
.test()
.awaitDone(5, TimeUnit.SECONDS)
.assertResult();

assertTrue(cancelledDuringPull.get());
}

@Test
public void otherError() throws Throwable {
Streamable.never()
Expand Down