From 445d93c8801b88aaff64414bd6f6ac40892c0196 Mon Sep 17 00:00:00 2001 From: akarnokd Date: Mon, 10 Apr 2017 14:23:01 +0200 Subject: [PATCH] 2.x: Single.subscribe(BiConsumer) consistent isDisposed --- .../observers/BiConsumerSingleObserver.java | 2 + .../reactivex/single/SingleSubscribeTest.java | 37 +++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/main/java/io/reactivex/internal/observers/BiConsumerSingleObserver.java b/src/main/java/io/reactivex/internal/observers/BiConsumerSingleObserver.java index fddeb2445f..e14425f70e 100644 --- a/src/main/java/io/reactivex/internal/observers/BiConsumerSingleObserver.java +++ b/src/main/java/io/reactivex/internal/observers/BiConsumerSingleObserver.java @@ -37,6 +37,7 @@ public BiConsumerSingleObserver(BiConsumer onCallb @Override public void onError(Throwable e) { try { + lazySet(DisposableHelper.DISPOSED); onCallback.accept(null, e); } catch (Throwable ex) { Exceptions.throwIfFatal(ex); @@ -52,6 +53,7 @@ public void onSubscribe(Disposable d) { @Override public void onSuccess(T value) { try { + lazySet(DisposableHelper.DISPOSED); onCallback.accept(value, null); } catch (Throwable ex) { Exceptions.throwIfFatal(ex); diff --git a/src/test/java/io/reactivex/single/SingleSubscribeTest.java b/src/test/java/io/reactivex/single/SingleSubscribeTest.java index aac913f6e5..9e095db8d0 100644 --- a/src/test/java/io/reactivex/single/SingleSubscribeTest.java +++ b/src/test/java/io/reactivex/single/SingleSubscribeTest.java @@ -15,6 +15,7 @@ import static org.junit.Assert.*; +import java.io.IOException; import java.util.List; import org.junit.Test; @@ -227,4 +228,40 @@ public void successIsDisposed() { public void errorIsDisposed() { assertTrue(Single.error(new TestException()).subscribe(Functions.emptyConsumer(), Functions.emptyConsumer()).isDisposed()); } + + @Test + public void biConsumerIsDisposedOnSuccess() { + final Object[] result = { null, null }; + + Disposable d = Single.just(1) + .subscribe(new BiConsumer() { + @Override + public void accept(Integer t1, Throwable t2) throws Exception { + result[0] = t1; + result[1] = t2; + } + }); + + assertTrue("Not disposed?!", d.isDisposed()); + assertEquals(1, result[0]); + assertNull(result[1]); + } + + @Test + public void biConsumerIsDisposedOnError() { + final Object[] result = { null, null }; + + Disposable d = Single.error(new IOException()) + .subscribe(new BiConsumer() { + @Override + public void accept(Integer t1, Throwable t2) throws Exception { + result[0] = t1; + result[1] = t2; + } + }); + + assertTrue("Not disposed?!", d.isDisposed()); + assertNull(result[0]); + assertTrue("" + result[1], result[1] instanceof IOException); + } }