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

2.x: UnicastSubject fix onTerminate #4592

Merged
merged 1 commit into from
Sep 23, 2016
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
20 changes: 18 additions & 2 deletions src/main/java/io/reactivex/subjects/UnicastSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,13 @@ protected void subscribeActual(Observer<? super T> observer) {
}
}

void doTerminate() {
Runnable r = onTerminate.get();
if (r != null && onTerminate.compareAndSet(r, null)) {
r.run();
}
}

void notifyOnCancelled() {
Runnable r = onCancelled;
onCancelled = null;
Expand Down Expand Up @@ -184,7 +191,7 @@ public void onNext(T t) {
return;
}
if (t == null) {
onError(new NullPointerException());
onError(new NullPointerException("onNext called with null. Null values are generally not allowed in 2.x operators and sources."));
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should not this throw immediately rather than trying to go through onError?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, the spec mandates throwing on null input and I think the Processor TCK also expects it. I thought signalling NPE is more graceful with Subjects and FlowableProcessors and shuts down the streams as well whereas a thrown NPE may leave everybody hanging.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright I'll leave the decision up to you. I'd prefer the fail early approach however delegating it down the stream also works. If you want me to change it I'll do so in a follow up PR.

return;
}
queue.offer(t);
Expand All @@ -198,10 +205,13 @@ public void onError(Throwable t) {
return;
}
if (t == null) {
t = new NullPointerException();
t = new NullPointerException("onError called with null. Null values are generally not allowed in 2.x operators and sources.");
}
error = t;
done = true;

doTerminate();

drain();
}

Expand All @@ -211,6 +221,9 @@ public void onComplete() {
return;
}
done = true;

doTerminate();

drain();
}

Expand Down Expand Up @@ -374,6 +387,9 @@ public void clear() {
public void dispose() {
if (!disposed) {
disposed = true;

doTerminate();

actual.lazySet(null);
if (wip.getAndIncrement() == 0) {
clearAndNotify(queue);
Expand Down
54 changes: 53 additions & 1 deletion src/test/java/io/reactivex/subjects/UnicastSubjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

package io.reactivex.subjects;

import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;
import java.util.concurrent.atomic.AtomicBoolean;
import static org.junit.Assert.assertEquals;
import org.junit.Test;

import io.reactivex.internal.fuseable.QueueDisposable;
Expand Down Expand Up @@ -57,4 +61,52 @@ public void fusionOfflie() {
.assertOf(ObserverFusion.<Integer>assertFuseable())
.assertOf(ObserverFusion.<Integer>assertFusionMode(QueueDisposable.ASYNC))
.assertResult(1);
}}
}

@Test
public void onTerminateCalledWhenOnError() {
final AtomicBoolean didRunOnTerminate = new AtomicBoolean();

UnicastSubject<Integer> us = UnicastSubject.create(Observable.bufferSize(), new Runnable() {
@Override public void run() {
didRunOnTerminate.set(true);
}
});

assertEquals(false, didRunOnTerminate.get());
us.onError(new RuntimeException("some error"));
assertEquals(true, didRunOnTerminate.get());
}

@Test
public void onTerminateCalledWhenOnComplete() {
final AtomicBoolean didRunOnTerminate = new AtomicBoolean();

UnicastSubject<Integer> us = UnicastSubject.create(Observable.bufferSize(), new Runnable() {
@Override public void run() {
didRunOnTerminate.set(true);
}
});

assertEquals(false, didRunOnTerminate.get());
us.onComplete();
assertEquals(true, didRunOnTerminate.get());
}

@Test
public void onTerminateCalledWhenCanceled() {
final AtomicBoolean didRunOnTerminate = new AtomicBoolean();

UnicastSubject<Integer> us = UnicastSubject.create(Observable.bufferSize(), new Runnable() {
@Override public void run() {
didRunOnTerminate.set(true);
}
});

final Disposable subscribe = us.subscribe();

assertEquals(false, didRunOnTerminate.get());
subscribe.dispose();
assertEquals(true, didRunOnTerminate.get());
}
}