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

2.x: Fix refCount eager disconnect not resetting the connection #6297

Merged
merged 2 commits into from
Nov 12, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,11 @@ void timeout(RefConnection rc) {
if (source instanceof Disposable) {
((Disposable)source).dispose();
} else if (source instanceof ResettableConnectable) {
((ResettableConnectable)source).resetIf(connectionObject);
if (connectionObject == null) {
rc.disconnectedEarly = true;
} else {
((ResettableConnectable)source).resetIf(connectionObject);
}
}
}
}
Expand All @@ -160,6 +164,8 @@ static final class RefConnection extends AtomicReference<Disposable>

boolean connected;

boolean disconnectedEarly;

RefConnection(FlowableRefCount<?> parent) {
this.parent = parent;
}
Expand All @@ -172,6 +178,11 @@ public void run() {
@Override
public void accept(Disposable t) throws Exception {
DisposableHelper.replace(this, t);
synchronized (parent) {
if (disconnectedEarly) {
((ResettableConnectable)parent.source).resetIf(t);
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,15 @@ void timeout(RefConnection rc) {
connection = null;
Disposable connectionObject = rc.get();
DisposableHelper.dispose(rc);

if (source instanceof Disposable) {
((Disposable)source).dispose();
} else if (source instanceof ResettableConnectable) {
((ResettableConnectable)source).resetIf(connectionObject);
if (connectionObject == null) {
rc.disconnectedEarly = true;
} else {
((ResettableConnectable)source).resetIf(connectionObject);
}
}
}
}
Expand All @@ -157,6 +162,8 @@ static final class RefConnection extends AtomicReference<Disposable>

boolean connected;

boolean disconnectedEarly;

RefConnection(ObservableRefCount<?> parent) {
this.parent = parent;
}
Expand All @@ -169,6 +176,11 @@ public void run() {
@Override
public void accept(Disposable t) throws Exception {
DisposableHelper.replace(this, t);
synchronized (parent) {
if (disconnectedEarly) {
((ResettableConnectable)parent.source).resetIf(t);
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1394,4 +1394,19 @@ public void timeoutDisposesSource() {

assertTrue(((Disposable)o.source).isDisposed());
}

@Test
public void disconnectBeforeConnect() {
BehaviorProcessor<Integer> processor = BehaviorProcessor.create();

Flowable<Integer> flowable = processor
.replay(1)
.refCount();

flowable.takeUntil(Flowable.just(1)).test();

processor.onNext(2);

flowable.take(1).test().assertResult(2);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1345,4 +1345,19 @@ public void timeoutDisposesSource() {

assertTrue(((Disposable)o.source).isDisposed());
}

@Test
public void disconnectBeforeConnect() {
BehaviorSubject<Integer> subject = BehaviorSubject.create();

Observable<Integer> observable = subject
.replay(1)
.refCount();

observable.takeUntil(Observable.just(1)).test();
Copy link
Contributor

Choose a reason for hiding this comment

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

For consistency, this line either needs same comment as flowable test, or it should be removed from flowable test

Copy link
Member Author

Choose a reason for hiding this comment

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

Removed from the other place.


subject.onNext(2);

observable.take(1).test().assertResult(2);
}
}