/** * Generate a resource and a Nono based on that resource and then * dispose that resource eagerly when the Nono terminates or the * downstream cancels the sequence. * @param <R> the resource type * @param resourceSupplier the callback to get a resource for each subscriber * @param sourceSupplier the function that returns a Nono for the generated resource * @param disposer the consumer of the resource once the upstream terminates or the * downstream cancels * @return the new Nono instance */ public static <R> Nono using(Callable<R> resourceSupplier, Function<? super R, ? extends Nono> sourceSupplier, Consumer<? super R> disposer) { return using(resourceSupplier, sourceSupplier, disposer, true); }
/** * Generate a resource and a Nono based on that resource and then * dispose that resource eagerly when the Nono terminates or the * downstream cancels the sequence. * @param <R> the resource type * @param resourceSupplier the callback to get a resource for each subscriber * @param sourceSupplier the function that returns a Nono for the generated resource * @param disposer the consumer of the resource once the upstream terminates or the * downstream cancels * @return the new Nono instance */ public static <R> Nono using(Callable<R> resourceSupplier, Function<? super R, ? extends Nono> sourceSupplier, Consumer<? super R> disposer) { return using(resourceSupplier, sourceSupplier, disposer, true); }
@Test public void usingSourceThrows() { Nono.using(Functions.justCallable(0), new Function<Integer, Nono>() { @Override public Nono apply(Integer v) throws Exception { throw new IllegalArgumentException(); } }, this ) .test() .assertFailure(IllegalArgumentException.class); }
@Test public void usingResourceThrows() { Nono.using(new Callable<Object>() { @Override public Object call() throws Exception { throw new IllegalArgumentException(); } }, Functions.justFunction(Nono.complete()), this ) .test() .assertFailure(IllegalArgumentException.class); }
@Test public void usingError() { Nono.using( Functions.justCallable(1), Functions.justFunction(ioError), this ) .test() .assertFailure(IOException.class); Assert.assertEquals(1, count); }
@Test public void usingNonEagerError() { Nono.using( Functions.justCallable(1), Functions.justFunction(ioError), this, false ) .test() .assertFailure(IOException.class); Assert.assertEquals(1, count); }
@Test public void usingDisposerThrows4() { Nono.using(Functions.justCallable(0), new Function<Integer, Nono>() { @Override public Nono apply(Integer v) throws Exception { throw new IOException(); } }, new Consumer<Integer>() { @Override public void accept(Integer t) throws Exception { throw new IllegalArgumentException(); } } ) .test() .assertFailure(CompositeException.class) .assertOf(new Consumer<TestSubscriber<Void>>() { @SuppressWarnings("unchecked") @Override public void accept(TestSubscriber<Void> ts) throws Exception { TestHelper.assertCompositeExceptions(ts, IOException.class, IllegalArgumentException.class); } }); }
@Test public void usingDisposerThrows1() { Nono.using(Functions.justCallable(0), Functions.justFunction(Nono.complete()), new Consumer<Integer>() { @Override public void accept(Integer t) throws Exception { throw new IllegalArgumentException(); } } ) .test() .assertFailure(IllegalArgumentException.class); }
@Test public void using() { Nono.using( Functions.justCallable(1), Functions.justFunction(Nono.complete()), this ) .test() .assertResult(); Assert.assertEquals(1, count); }
@Test public void usingNonEager() { Nono.using( Functions.justCallable(1), Functions.justFunction(Nono.complete()), this, false ) .test() .assertResult(); Assert.assertEquals(1, count); }
@Test public void usingDisposerThrows2() { Nono.using(Functions.justCallable(0), Functions.justFunction(ioError), new Consumer<Integer>() { @Override public void accept(Integer t) throws Exception { throw new IllegalArgumentException(); } } ) .test() .assertFailure(CompositeException.class) .assertOf(new Consumer<TestSubscriber<Void>>() { @SuppressWarnings("unchecked") @Override public void accept(TestSubscriber<Void> ts) throws Exception { TestHelper.assertCompositeExceptions(ts, IOException.class, IllegalArgumentException.class); } }); }
@Test public void usingDisposerThrows5() { List<Throwable> errors = TestHelper.trackPluginErrors(); try { Nono.using(Functions.justCallable(0), new Function<Integer, Nono>() { @Override public Nono apply(Integer v) throws Exception { throw new IOException(); } }, new Consumer<Integer>() { @Override public void accept(Integer t) throws Exception { throw new IllegalArgumentException(); } }, false ) .test() .assertFailure(IOException.class); TestHelper.assertError(errors, 0, IllegalArgumentException.class); } finally { RxJavaPlugins.reset(); } }
@Test public void usingDisposerThrows3() { List<Throwable> errors = TestHelper.trackPluginErrors(); try { Nono.using(Functions.justCallable(0), Functions.justFunction(Nono.complete()), new Consumer<Integer>() { @Override public void accept(Integer t) throws Exception { throw new IllegalArgumentException(); } }, false ) .test() .assertResult(); TestHelper.assertError(errors, 0, IllegalArgumentException.class); } finally { RxJavaPlugins.reset(); } }