Skip to content

1.x: AsyncSubject now supports backpressure #3828

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

Merged
merged 1 commit into from
Apr 4, 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
12 changes: 8 additions & 4 deletions src/main/java/rx/subjects/AsyncSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import rx.exceptions.Exceptions;
import rx.functions.Action1;
import rx.internal.operators.NotificationLite;
import rx.internal.producers.SingleProducer;
import rx.subjects.SubjectSubscriptionManager.SubjectObserver;

/**
Expand Down Expand Up @@ -68,9 +69,13 @@ public static <T> AsyncSubject<T> create() {
public void call(SubjectObserver<T> o) {
Object v = state.getLatest();
NotificationLite<T> nl = state.nl;
o.accept(v, nl);
if (v == null || (!nl.isCompleted(v) && !nl.isError(v))) {
if (v == null || nl.isCompleted(v)) {
o.onCompleted();
} else
if (nl.isError(v)) {
o.onError(nl.getError(v));
} else {
o.actual.setProducer(new SingleProducer<T>(o.actual, nl.getValue(v)));
}
}
};
Expand All @@ -97,8 +102,7 @@ public void onCompleted() {
if (last == nl.completed()) {
bo.onCompleted();
} else {
bo.onNext(nl.getValue(last));
bo.onCompleted();
bo.actual.setProducer(new SingleProducer<T>(bo.actual, nl.getValue(last)));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/rx/subjects/SubjectSubscriptionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public State remove(SubjectObserver o) {
*/
protected static final class SubjectObserver<T> implements Observer<T> {
/** The actual Observer. */
final Observer<? super T> actual;
final Subscriber<? super T> actual;
/** Was the emitFirst run? Guarded by this. */
boolean first = true;
/** Guarded by this. */
Expand All @@ -215,7 +215,7 @@ protected static final class SubjectObserver<T> implements Observer<T> {
protected volatile boolean caughtUp;
/** Indicate where the observer is at replaying. */
private volatile Object index;
public SubjectObserver(Observer<? super T> actual) {
public SubjectObserver(Subscriber<? super T> actual) {
this.actual = actual;
}
@Override
Expand Down
43 changes: 43 additions & 0 deletions src/test/java/rx/subjects/AsyncSubjectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -430,4 +430,47 @@ public void testAsyncSubjectValueError() {
assertNull(async.getValue());
assertFalse(async.hasValue());
}

@Test
public void backpressureOnline() {
TestSubscriber<Integer> ts = TestSubscriber.create(0);

AsyncSubject<Integer> subject = AsyncSubject.create();

subject.subscribe(ts);

subject.onNext(1);
subject.onCompleted();

ts.assertNoValues();
ts.assertNoErrors();
ts.assertNotCompleted();

ts.requestMore(1);

ts.assertValue(1);
ts.assertCompleted();
ts.assertNoErrors();
}

@Test
public void backpressureOffline() {
TestSubscriber<Integer> ts = TestSubscriber.create(0);

AsyncSubject<Integer> subject = AsyncSubject.create();
subject.onNext(1);
subject.onCompleted();

subject.subscribe(ts);

ts.assertNoValues();
ts.assertNoErrors();
ts.assertNotCompleted();

ts.requestMore(1);

ts.assertValue(1);
ts.assertCompleted();
ts.assertNoErrors();
}
}