Skip to content

2.x: make withLatestFrom conditional subscriber, test cold consumption #5495

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 2 commits into from
Jul 18, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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,6 +21,7 @@
import io.reactivex.exceptions.Exceptions;
import io.reactivex.functions.BiFunction;
import io.reactivex.internal.functions.ObjectHelper;
import io.reactivex.internal.fuseable.ConditionalSubscriber;
import io.reactivex.internal.subscriptions.SubscriptionHelper;
import io.reactivex.subscribers.SerializedSubscriber;

Expand All @@ -45,7 +46,8 @@ protected void subscribeActual(Subscriber<? super R> s) {
source.subscribe(wlf);
}

static final class WithLatestFromSubscriber<T, U, R> extends AtomicReference<U> implements FlowableSubscriber<T>, Subscription {
static final class WithLatestFromSubscriber<T, U, R> extends AtomicReference<U>
implements ConditionalSubscriber<T>, Subscription {

private static final long serialVersionUID = -312246233408980075L;

Expand All @@ -69,6 +71,13 @@ public void onSubscribe(Subscription s) {

@Override
public void onNext(T t) {
if (!tryOnNext(t)) {
s.get().request(1);
}
}

@Override
public boolean tryOnNext(T t) {
U u = get();
if (u != null) {
R r;
Expand All @@ -78,11 +87,12 @@ public void onNext(T t) {
Exceptions.throwIfFatal(e);
cancel();
actual.onError(e);
return;
return false;
}
actual.onNext(r);
} else{
request(1);
return true;
} else {
return false;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import io.reactivex.exceptions.Exceptions;
import io.reactivex.functions.Function;
import io.reactivex.internal.functions.ObjectHelper;
import io.reactivex.internal.fuseable.ConditionalSubscriber;
import io.reactivex.internal.subscriptions.*;
import io.reactivex.internal.util.*;
import io.reactivex.plugins.RxJavaPlugins;
Expand Down Expand Up @@ -95,7 +96,7 @@ protected void subscribeActual(Subscriber<? super R> s) {

static final class WithLatestFromSubscriber<T, R>
extends AtomicInteger
implements FlowableSubscriber<T>, Subscription {
implements ConditionalSubscriber<T>, Subscription {

private static final long serialVersionUID = 1577321883966341961L;

Expand Down Expand Up @@ -147,8 +148,15 @@ public void onSubscribe(Subscription s) {

@Override
public void onNext(T t) {
if (!tryOnNext(t) && !done) {
s.get().request(1);
}
}

@Override
public boolean tryOnNext(T t) {
if (done) {
return;
return false;
}
AtomicReferenceArray<Object> ara = values;
int n = ara.length();
Expand All @@ -159,8 +167,7 @@ public void onNext(T t) {
Object o = ara.get(i);
if (o == null) {
// somebody hasn't signalled yet, skip this T
s.get().request(1);
return;
return false;
}
objects[i + 1] = o;
}
Expand All @@ -173,10 +180,11 @@ public void onNext(T t) {
Exceptions.throwIfFatal(ex);
cancel();
onError(ex);
return;
return false;
}

HalfSerializer.onNext(actual, v, this, error);
return true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ public void zeroOtherCombinerReturnsNull() {
}

@Test
public void testSingleRequestNotForgottenWhenNoData() {
public void singleRequestNotForgottenWhenNoData() {
PublishProcessor<Integer> source = PublishProcessor.create();
PublishProcessor<Integer> other = PublishProcessor.create();

Expand All @@ -750,4 +750,30 @@ public void testSingleRequestNotForgottenWhenNoData() {

ts.assertValue((2 << 8) + 1);
}

@Test
public void coldSourceConsumedWithoutOther() {
Flowable.range(1, 10).withLatestFrom(Flowable.never(),
new BiFunction<Integer, Object, Object>() {
@Override
public Object apply(Integer a, Object b) throws Exception {
return a;
}
})
.test(1)
.assertResult();
}

@Test
public void coldSourceConsumedWithoutManyOthers() {
Flowable.range(1, 10).withLatestFrom(Flowable.never(), Flowable.never(), Flowable.never(),
new Function4<Integer, Object, Object, Object, Object>() {
@Override
public Object apply(Integer a, Object b, Object c, Object d) throws Exception {
return a;
}
})
.test(1)
.assertResult();
}
}