Skip to content

1.x: fix Completable.using not disposing the resource if the factory crashes during the subscription phase. #3585

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
Jan 12, 2016
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
25 changes: 23 additions & 2 deletions src/main/java/rx/Completable.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@

package rx;

import java.util.Iterator;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicBoolean;

import rx.Observable.OnSubscribe;
import rx.annotations.Experimental;
import rx.exceptions.Exceptions;
import rx.exceptions.*;
import rx.functions.*;
import rx.internal.operators.*;
import rx.internal.util.*;
Expand Down Expand Up @@ -864,12 +864,33 @@ public void call(final CompletableSubscriber s) {
try {
cs = completableFunc1.call(resource);
} catch (Throwable e) {
try {
disposer.call(resource);
} catch (Throwable ex) {
Exceptions.throwIfFatal(e);
Exceptions.throwIfFatal(ex);

s.onSubscribe(Subscriptions.unsubscribed());
s.onError(new CompositeException(Arrays.asList(e, ex)));
return;
}
Exceptions.throwIfFatal(e);

s.onSubscribe(Subscriptions.unsubscribed());
s.onError(e);
return;
}

if (cs == null) {
try {
disposer.call(resource);
} catch (Throwable ex) {
Exceptions.throwIfFatal(ex);

s.onSubscribe(Subscriptions.unsubscribed());
s.onError(new CompositeException(Arrays.asList(new NullPointerException("The completable supplied is null"), ex)));
return;
}
s.onSubscribe(Subscriptions.unsubscribed());
s.onError(new NullPointerException("The completable supplied is null"));
return;
Expand Down
134 changes: 134 additions & 0 deletions src/test/java/rx/CompletableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
import rx.subjects.PublishSubject;
import rx.subscriptions.*;

import static org.mockito.Mockito.*;
import static org.junit.Assert.*;

/**
* Test Completable methods and operators.
*/
Expand Down Expand Up @@ -3410,4 +3413,135 @@ public void endWithFlowableError() {
ts.assertError(TestException.class);
ts.assertNotCompleted();
}

@Test
public void usingFactoryThrows() {
@SuppressWarnings("unchecked")
Action1<Integer> onDispose = mock(Action1.class);

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

Completable.using(new Func0<Integer>() {
@Override
public Integer call() {
return 1;
}
},
new Func1<Integer, Completable>() {
@Override
public Completable call(Integer t) {
throw new TestException();
}
}, onDispose).subscribe(ts);

verify(onDispose).call(1);

ts.assertNoValues();
ts.assertNotCompleted();
ts.assertError(TestException.class);
}

@Test
public void usingFactoryAndDisposerThrow() {
Action1<Integer> onDispose = new Action1<Integer>() {
@Override
public void call(Integer t) {
throw new TestException();
}
};

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

Completable.using(new Func0<Integer>() {
@Override
public Integer call() {
return 1;
}
},
new Func1<Integer, Completable>() {
@Override
public Completable call(Integer t) {
throw new TestException();
}
}, onDispose).subscribe(ts);

ts.assertNoValues();
ts.assertNotCompleted();
ts.assertError(CompositeException.class);

CompositeException ex = (CompositeException)ts.getOnErrorEvents().get(0);

List<Throwable> listEx = ex.getExceptions();

assertEquals(2, listEx.size());

assertTrue(listEx.get(0).toString(), listEx.get(0) instanceof TestException);
assertTrue(listEx.get(1).toString(), listEx.get(1) instanceof TestException);
}

@Test
public void usingFactoryReturnsNull() {
@SuppressWarnings("unchecked")
Action1<Integer> onDispose = mock(Action1.class);

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

Completable.using(new Func0<Integer>() {
@Override
public Integer call() {
return 1;
}
},
new Func1<Integer, Completable>() {
@Override
public Completable call(Integer t) {
return null;
}
}, onDispose).subscribe(ts);

verify(onDispose).call(1);

ts.assertNoValues();
ts.assertNotCompleted();
ts.assertError(NullPointerException.class);
}

@Test
public void usingFactoryReturnsNullAndDisposerThrows() {
Action1<Integer> onDispose = new Action1<Integer>() {
@Override
public void call(Integer t) {
throw new TestException();
}
};

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

Completable.using(new Func0<Integer>() {
@Override
public Integer call() {
return 1;
}
},
new Func1<Integer, Completable>() {
@Override
public Completable call(Integer t) {
return null;
}
}, onDispose).subscribe(ts);

ts.assertNoValues();
ts.assertNotCompleted();
ts.assertError(CompositeException.class);

CompositeException ex = (CompositeException)ts.getOnErrorEvents().get(0);

List<Throwable> listEx = ex.getExceptions();

assertEquals(2, listEx.size());

assertTrue(listEx.get(0).toString(), listEx.get(0) instanceof NullPointerException);
assertTrue(listEx.get(1).toString(), listEx.get(1) instanceof TestException);
}

}