Skip to content

1.x: observeOn - fix in-sequence termination/unsubscription #3768

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
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
22 changes: 11 additions & 11 deletions src/main/java/rx/internal/operators/OperatorObserveOn.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,10 @@ public void call() {
// less frequently (usually after each RxRingBuffer.SIZE elements)

for (;;) {
if (checkTerminated(finished, q.isEmpty(), localChild, q)) {
return;
}

long requestAmount = requested.get();
boolean unbounded = requestAmount == Long.MAX_VALUE;
long currentEmission = 0L;

while (requestAmount != 0L) {
while (requestAmount != currentEmission) {
boolean done = finished;
Object v = q.poll();
boolean empty = v == null;
Expand All @@ -205,14 +200,19 @@ public void call() {
}

localChild.onNext(localOn.getValue(v));

requestAmount--;
currentEmission--;

currentEmission++;
emitted++;
}

if (currentEmission != 0L && !unbounded) {
requested.addAndGet(currentEmission);
if (requestAmount == currentEmission) {
if (checkTerminated(finished, q.isEmpty(), localChild, q)) {
return;
}
}

if (currentEmission != 0L) {
BackpressureUtils.produced(requested, currentEmission);
}

missed = counter.addAndGet(-missed);
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/rx/internal/operators/OperatorObserveOnTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -834,4 +834,27 @@ public void testErrorDelayedAsync() {
ts.assertError(TestException.class);
ts.assertNotCompleted();
}

@Test
public void requestExactCompletesImmediately() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure if it tests "immediately", this test passes without changes in this PR.

Maybe you can avoid going to another thread and awaitTerminalEvent()?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, should request 0 upfront and wait till the range completes before requesting 10. I'll change this to use the TestScheduler.

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated the test which now clearly fails on master.

TestSubscriber<Integer> ts = TestSubscriber.create(0);

TestScheduler test = Schedulers.test();

Observable.range(1, 10).observeOn(test).subscribe(ts);

test.advanceTimeBy(1, TimeUnit.SECONDS);

ts.assertNoValues();
ts.assertNoErrors();
ts.assertNotCompleted();

ts.requestMore(10);

test.advanceTimeBy(1, TimeUnit.SECONDS);

ts.assertValueCount(10);
ts.assertNoErrors();
ts.assertCompleted();
}
}