Skip to content

Fix Concat Breaks with Double onCompleted #1819

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
Nov 2, 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: 12 additions & 4 deletions src/main/java/rx/internal/operators/OperatorConcat.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,10 @@ static class ConcatInnerSubscriber<T> extends Subscriber<T> {

private final Subscriber<T> child;
private final ConcatSubscriber<T> parent;
@SuppressWarnings("unused")
private volatile int once = 0;
@SuppressWarnings("rawtypes")
private final static AtomicIntegerFieldUpdater<ConcatInnerSubscriber> ONCE_UPDATER = AtomicIntegerFieldUpdater.newUpdater(ConcatInnerSubscriber.class, "once");

public ConcatInnerSubscriber(ConcatSubscriber<T> parent, Subscriber<T> child, long initialRequest) {
this.parent = parent;
Expand All @@ -195,14 +199,18 @@ public void onNext(T t) {

@Override
public void onError(Throwable e) {
// terminal error through parent so everything gets cleaned up, including this inner
parent.onError(e);
if (ONCE_UPDATER.compareAndSet(this, 0, 1)) {
// terminal error through parent so everything gets cleaned up, including this inner
parent.onError(e);
}
}

@Override
public void onCompleted() {
// terminal completion to parent so it continues to the next
parent.completeInner();
if (ONCE_UPDATER.compareAndSet(this, 0, 1)) {
// terminal completion to parent so it continues to the next
parent.completeInner();
}
}

};
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/rx/internal/operators/OperatorConcatTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -695,5 +695,27 @@ public void testInnerBackpressureWithoutAlignedBoundaries() {
ts.assertNoErrors();
assertEquals((RxRingBuffer.SIZE * 4) + 20, ts.getOnNextEvents().size());
}

// https://github.com/ReactiveX/RxJava/issues/1818
@Test
public void testConcatWithNonCompliantSourceDoubleOnComplete() {
Observable<String> o = Observable.create(new OnSubscribe<String>() {

@Override
public void call(Subscriber<? super String> s) {
s.onNext("hello");
s.onCompleted();
s.onCompleted();
}

});

TestSubscriber<String> ts = new TestSubscriber<String>();
Observable.concat(o, o).subscribe(ts);
ts.awaitTerminalEvent(500, TimeUnit.MILLISECONDS);
ts.assertTerminalEvent();
ts.assertNoErrors();
ts.assertReceivedOnNext(Arrays.asList("hello", "hello"));
}

}