-
Notifications
You must be signed in to change notification settings - Fork 7.6k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 { | ||
|
||
|
@@ -76,5 +73,55 @@ public void onNext(Integer t) { | |
}); | ||
assertEquals(Arrays.asList(3L,1L,2L,3L,4L,5L), requests); | ||
} | ||
|
||
@Test | ||
public void dontRequestIfDownstreamRequestsLate() { | ||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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
withouttest
prefix, oh, I'm so happy now 😸