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 the bug that Scan may request 0 when n is 1 #1904

Merged
merged 1 commit into from
Nov 29, 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
17 changes: 15 additions & 2 deletions src/main/java/rx/internal/operators/OperatorScan.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,30 @@ public void setProducer(final Producer producer) {

final AtomicBoolean once = new AtomicBoolean();

final AtomicBoolean excessive = new AtomicBoolean();

@Override
public void request(long n) {
if (once.compareAndSet(false, true)) {
if (initialValue == NO_INITIAL_VALUE || n == Long.MAX_VALUE) {
producer.request(n);
} else {
producer.request(n - 1);
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);
}
}
} else {
// pass-thru after first time
producer.request(n);
if (excessive.compareAndSet(true, false) && n != Long.MAX_VALUE) {
producer.request(n - 1);
} else {
producer.request(n);
}
}
}
});
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/rx/internal/operators/OperatorScanTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -295,4 +295,21 @@ public void call(List<Integer> list, Integer t2) {
assertEquals(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), o.toBlocking().single());
assertEquals(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), o.toBlocking().single());
}

@Test
public void testScanWithRequestOne() {
Observable<Integer> o = Observable.just(1, 2).scan(0, new Func2<Integer, Integer, Integer>() {

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

}).take(1);
TestSubscriber<Integer> subscriber = new TestSubscriber<Integer>();
o.subscribe(subscriber);
subscriber.assertReceivedOnNext(Arrays.asList(0));
subscriber.assertTerminalEvent();
subscriber.assertNoErrors();
}
}