Skip to content
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

Fix 'request(0)' issue in Scan #1957

Merged
merged 1 commit into from
Dec 13, 2014
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
16 changes: 7 additions & 9 deletions src/main/java/rx/internal/operators/OperatorScan.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,19 +147,17 @@ public void request(long n) {
if (once.compareAndSet(false, true)) {
if (initialValue == NO_INITIAL_VALUE || n == Long.MAX_VALUE) {
producer.request(n);
} else if (n == 1) {
excessive.set(true);
Copy link
Member

Choose a reason for hiding this comment

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

What does excessive mean? I'm re-reading this code and the intent of that variable doesn't jump out at me.

Copy link
Member Author

Choose a reason for hiding this comment

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

It means we request more than what we should.

If a user requests 1 from scan in onStart, scan need to request 1 to the upstream. However, when scan receives an onNext, it will emit 2 onNext to the downstream: the initial value and accumulator.call(this.value, initialvalue). So the downstream only requests 1, but scan emits 2 values. I use excessive to indicate we are in an excessive state.

So the next time when the downstream requests, I will try to request n-1.

Copy link
Member Author

Choose a reason for hiding this comment

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

If a user requests X (X > 1) from scan in onStart, scan can simply request (X-1). In such case, excessive will be false.

Copy link
Member

Choose a reason for hiding this comment

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

Got it, thanks for the explanation.

producer.request(1); // request at least 1
} else {
if (n == Long.MAX_VALUE) {
producer.request(Long.MAX_VALUE);
} else if (n == 1) {
excessive.set(true);
producer.request(1); // request at least 1
} else {
producer.request(n - 1);
}
// n != Long.MAX_VALUE && n != 1
producer.request(n - 1);
}
} else {
// pass-thru after first time
if (excessive.compareAndSet(true, false) && n != Long.MAX_VALUE) {
if (n > 1 // avoid to request 0
&& excessive.compareAndSet(true, false) && n != Long.MAX_VALUE) {
producer.request(n - 1);
} else {
producer.request(n);
Expand Down
56 changes: 52 additions & 4 deletions src/test/java/rx/internal/operators/OperatorScanTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

import org.junit.Before;
import org.junit.Test;
import org.mockito.MockitoAnnotations;

import rx.Observable;
import rx.Observer;
import rx.Producer;
import rx.Subscriber;
import rx.functions.Action2;
import rx.functions.Func0;
Expand Down Expand Up @@ -312,4 +312,52 @@ public Integer call(Integer t1, Integer t2) {
subscriber.assertTerminalEvent();
subscriber.assertNoErrors();
}

@Test
public void testScanShouldNotRequestZero() {
final AtomicReference<Producer> producer = new AtomicReference<Producer>();
Observable<Integer> o = Observable.create(new Observable.OnSubscribe<Integer>() {
@Override
public void call(final Subscriber subscriber) {
Producer p = spy(new Producer() {

private AtomicBoolean requested = new AtomicBoolean(false);

@Override
public void request(long n) {
if (requested.compareAndSet(false, true)) {
subscriber.onNext(1);
} else {
subscriber.onCompleted();
}
}
});
producer.set(p);
subscriber.setProducer(p);
}
}).scan(100, new Func2<Integer, Integer, Integer>() {

@Override
public Integer call(Integer t1, Integer t2) {
return t1 + t2;
}

});

o.subscribe(new TestSubscriber<Integer>() {

@Override
public void onStart() {
request(1);
}

@Override
public void onNext(Integer integer) {
request(1);
}
});

verify(producer.get(), never()).request(0);
verify(producer.get(), times(2)).request(1);
}
}