Skip to content

1.x: fix doOnRequest premature requesting. #3662

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
Feb 3, 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
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ private static final class ParentSubscriber<T> extends Subscriber<T> {

ParentSubscriber(Subscriber<? super T> child) {
this.child = child;
this.request(0);
}

private void requestMore(long n) {
Expand Down
67 changes: 57 additions & 10 deletions src/test/java/rx/internal/operators/OperatorDoOnRequestTest.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
package rx.internal.operators;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.*;
import java.util.concurrent.atomic.*;

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

import rx.*;
import rx.Observable;
import rx.Subscriber;
import rx.functions.Action0;
import rx.functions.Action1;
import rx.Observable.OnSubscribe;
import rx.functions.*;

public class OperatorDoOnRequestTest {

Expand Down Expand Up @@ -76,5 +73,55 @@ public void onNext(Integer t) {
});
assertEquals(Arrays.asList(3L,1L,2L,3L,4L,5L), requests);
}

@Test
public void dontRequestIfDownstreamRequestsLate() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Yay! You did it! @Test without test prefix, oh, I'm so happy now 😸

final List<Long> requested = new ArrayList<Long>();

Action1<Long> empty = Actions.empty();

final AtomicReference<Producer> producer = new AtomicReference<Producer>();

Observable.create(new OnSubscribe<Integer>() {
@Override
public void call(Subscriber<? super Integer> t) {
t.setProducer(new Producer() {
@Override
public void request(long n) {
requested.add(n);
}
});
}
}).doOnRequest(empty).subscribe(new Subscriber<Object>() {
@Override
public void onNext(Object t) {

}

@Override
public void onError(Throwable e) {

}

@Override
public void onCompleted() {

}

@Override
public void setProducer(Producer p) {
producer.set(p);
}
});

producer.get().request(1);

int s = requested.size();
if (s == 1) {
Copy link
Contributor

Choose a reason for hiding this comment

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

? this may work differently from time to time or you want to support changes in future?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is for a future case where doOnRequest doesn't request anything on its own. Didn't want to mess with that right now.

Copy link
Contributor

Choose a reason for hiding this comment

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

well…ok

// this allows for an implementation that itself doesn't request
Assert.assertEquals(Arrays.asList(1L), requested);
} else {
Assert.assertEquals(Arrays.asList(0L, 1L), requested);
}
}
}