Skip to content

PublishSubject Add Before onSubscribe #3345

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

Closed
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
43 changes: 23 additions & 20 deletions src/main/java/io/reactivex/subjects/PublishSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,26 +183,29 @@ static final class State<T> extends AtomicReference<Object> implements Publisher
AtomicReferenceFieldUpdater.newUpdater(State.class, PublishSubscriber[].class, "subscribers");

@Override
public void subscribe(Subscriber<? super T> t) {
PublishSubscriber<T> ps = new PublishSubscriber<>(t, this);
t.onSubscribe(ps);
if (ps.cancelled == 0) {
if (add(ps)) {
// if cancellation happened while a successful add, the remove() didn't work
// so we need to do it again
if (ps.cancelled != 0) {
remove(ps);
}
} else {
Object o = get();
if (o == COMPLETE) {
ps.onComplete();
} else {
ps.onError((Throwable)o);
}
}
}
}
public void subscribe(Subscriber<? super T> t) {
Copy link
Member

Choose a reason for hiding this comment

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

I think we still want to avoid tabs in the source, right?

Copy link
Member Author

Choose a reason for hiding this comment

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

Really? We care about that?

Copy link
Member

Choose a reason for hiding this comment

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

https://github.com/ReactiveX/RxJava/blob/1.x/CONTRIBUTING.md

When submitting code, please make every effort to follow existing conventions and style in order to keep the code as readable as possible.

PublishSubscriber<T> ps = new PublishSubscriber<>(t, this);
if (ps.cancelled == 0) {
boolean added = add(ps);
if (added) {
// if cancellation happened while a successful add, the remove() didn't work
// so we need to do it again
if (ps.cancelled != 0) {
remove(ps);
}
}
t.onSubscribe(ps);
if (!added) {
Object o = get();
if (o == COMPLETE) {
ps.onComplete();
} else {
ps.onError((Throwable) o);
}
return;
}
}
}

/**
* @return the array of currently subscribed subscribers
Expand Down
48 changes: 48 additions & 0 deletions src/test/java/io/reactivex/subjects/PublishSubjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
import static org.mockito.Mockito.*;

import java.util.ArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.*;

import org.junit.Test;
Expand Down Expand Up @@ -426,4 +429,49 @@ public void testCurrentStateMethodsError() {
assertFalse(as.hasComplete());
assertTrue(as.getThrowable() instanceof TestException);
}

@Test
public void testRegisteredBeforeOnSubscribe() throws InterruptedException {
PublishSubject<Object> as = PublishSubject.create();

Runnable sideEffect = () -> {
as.onNext(1);
as.onComplete();
};

final AtomicReference<Object> next = new AtomicReference<Object>();
final CountDownLatch latch = new CountDownLatch(1);

as.subscribe(new Subscriber<Object>() {

@Override
public void onSubscribe(Subscription s) {
s.request(Long.MAX_VALUE);
// we are subscribed, so expect that we can now trigger side-effects that cause data to flow
sideEffect.run();
}

@Override
public void onNext(Object t) {
next.set(t);
}

@Override
public void onError(Throwable t) {
latch.countDown();
}

@Override
public void onComplete() {
latch.countDown();
}

});

if(!latch.await(500, TimeUnit.MILLISECONDS)) {
fail("Did not receive events");
}

assertEquals(1, next.get());
}
}