Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3.x: Fix blockingIterable not unblocking when force-disposed #6626

Merged
merged 4 commits into from
Aug 21, 2019
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 @@ -21,7 +21,7 @@

import io.reactivex.rxjava3.core.*;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.exceptions.MissingBackpressureException;
import io.reactivex.rxjava3.exceptions.*;
import io.reactivex.rxjava3.internal.queue.SpscArrayQueue;
import io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper;
import io.reactivex.rxjava3.internal.util.*;
Expand Down Expand Up @@ -62,7 +62,7 @@ static final class BlockingFlowableIterator<T>
long produced;

volatile boolean done;
Throwable error;
volatile Throwable error;

BlockingFlowableIterator(int batchSize) {
this.queue = new SpscArrayQueue<T>(batchSize);
Expand All @@ -75,6 +75,13 @@ static final class BlockingFlowableIterator<T>
@Override
public boolean hasNext() {
for (;;) {
if (isDisposed()) {
Throwable e = error;
if (e != null) {
throw ExceptionHelper.wrapOrThrow(e);
}
return false;
}
boolean d = done;
boolean empty = queue.isEmpty();
if (d) {
Expand All @@ -90,7 +97,7 @@ public boolean hasNext() {
BlockingHelper.verifyNonBlocking();
lock.lock();
try {
while (!done && queue.isEmpty()) {
while (!done && queue.isEmpty() && !isDisposed()) {
condition.await();
}
} catch (InterruptedException ex) {
Expand Down Expand Up @@ -175,6 +182,7 @@ public void remove() {
@Override
public void dispose() {
SubscriptionHelper.cancel(this);
signalConsumer(); // Just in case it is currently blocking in hasNext.
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ static final class BlockingObservableIterator<T>
final Condition condition;

volatile boolean done;
Throwable error;
volatile Throwable error;

BlockingObservableIterator(int batchSize) {
this.queue = new SpscLinkedArrayQueue<T>(batchSize);
Expand All @@ -64,6 +64,13 @@ static final class BlockingObservableIterator<T>
@Override
public boolean hasNext() {
for (;;) {
if (isDisposed()) {
Throwable e = error;
if (e != null) {
throw ExceptionHelper.wrapOrThrow(e);
}
return false;
}
boolean d = done;
boolean empty = queue.isEmpty();
if (d) {
Expand All @@ -80,7 +87,7 @@ public boolean hasNext() {
BlockingHelper.verifyNonBlocking();
lock.lock();
try {
while (!done && queue.isEmpty()) {
while (!done && queue.isEmpty() && !isDisposed()) {
condition.await();
}
} finally {
Expand Down Expand Up @@ -146,6 +153,7 @@ public void remove() {
@Override
public void dispose() {
DisposableHelper.dispose(this);
signalConsumer(); // Just in case it is currently blocking in hasNext.
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,18 @@
import static org.junit.Assert.*;

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

import org.junit.Test;
import org.reactivestreams.*;

import io.reactivex.rxjava3.core.*;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.exceptions.*;
import io.reactivex.rxjava3.internal.operators.flowable.BlockingFlowableIterable.BlockingFlowableIterator;
import io.reactivex.rxjava3.internal.subscriptions.BooleanSubscription;
import io.reactivex.rxjava3.processors.PublishProcessor;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class BlockingFlowableToIteratorTest extends RxJavaTest {

Expand Down Expand Up @@ -163,4 +167,28 @@ protected void subscribeActual(Subscriber<? super Integer> s) {

it.next();
}

@Test(expected = NoSuchElementException.class)
public void disposedIteratorHasNextReturns() {
Iterator<Integer> it = PublishProcessor.<Integer>create()
.blockingIterable().iterator();
((Disposable)it).dispose();
akarnokd marked this conversation as resolved.
Show resolved Hide resolved
assertFalse(it.hasNext());
it.next();
}

@Test
public void asyncDisposeUnblocks() {
final Iterator<Integer> it = PublishProcessor.<Integer>create()
.blockingIterable().iterator();

Schedulers.single().scheduleDirect(new Runnable() {
@Override
public void run() {
((Disposable)it).dispose();
}
}, 1, TimeUnit.SECONDS);

assertFalse(it.hasNext());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import io.reactivex.rxjava3.exceptions.TestException;
import io.reactivex.rxjava3.internal.operators.observable.BlockingObservableNext.NextObserver;
import io.reactivex.rxjava3.plugins.RxJavaPlugins;
import io.reactivex.rxjava3.processors.BehaviorProcessor;
import io.reactivex.rxjava3.schedulers.Schedulers;
import io.reactivex.rxjava3.subjects.*;
import io.reactivex.rxjava3.testsupport.TestHelper;
Expand Down Expand Up @@ -333,9 +332,9 @@ public void singleSourceManyIterators() throws InterruptedException {

@Test
public void synchronousNext() {
assertEquals(1, BehaviorProcessor.createDefault(1).take(1).blockingSingle().intValue());
assertEquals(2, BehaviorProcessor.createDefault(2).blockingIterable().iterator().next().intValue());
assertEquals(3, BehaviorProcessor.createDefault(3).blockingNext().iterator().next().intValue());
assertEquals(1, BehaviorSubject.createDefault(1).take(1).blockingSingle().intValue());
assertEquals(2, BehaviorSubject.createDefault(2).blockingIterable().iterator().next().intValue());
assertEquals(3, BehaviorSubject.createDefault(3).blockingNext().iterator().next().intValue());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@
import static org.junit.Assert.*;

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

import org.junit.Test;

import io.reactivex.rxjava3.core.*;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposables;
import io.reactivex.rxjava3.disposables.*;
import io.reactivex.rxjava3.exceptions.TestException;
import io.reactivex.rxjava3.internal.operators.observable.BlockingObservableIterable.BlockingObservableIterator;
import io.reactivex.rxjava3.schedulers.Schedulers;
import io.reactivex.rxjava3.subjects.PublishSubject;

public class BlockingObservableToIteratorTest extends RxJavaTest {

Expand Down Expand Up @@ -104,4 +107,28 @@ public void remove() {
BlockingObservableIterator<Integer> it = new BlockingObservableIterator<Integer>(128);
it.remove();
}

@Test(expected = NoSuchElementException.class)
public void disposedIteratorHasNextReturns() {
Iterator<Integer> it = PublishSubject.<Integer>create()
.blockingIterable().iterator();
((Disposable)it).dispose();
assertFalse(it.hasNext());
it.next();
}

@Test
public void asyncDisposeUnblocks() {
final Iterator<Integer> it = PublishSubject.<Integer>create()
.blockingIterable().iterator();

Schedulers.single().scheduleDirect(new Runnable() {
@Override
public void run() {
((Disposable)it).dispose();
}
}, 1, TimeUnit.SECONDS);

assertFalse(it.hasNext());
}
}