@Override public <E extends T> Completable delete(final E entity) { return Completable.fromCallable(new Callable<Void>() { @Override public Void call() throws Exception { delegate.delete(entity); return null; } }); }
@Override public <E extends T> Completable delete(final Iterable<E> entities) { return Completable.fromCallable(new Callable<Void>() { @Override public Void call() throws Exception { delegate.delete(entities); return null; } }); }
@Test(expected = NullPointerException.class) public void fromCallableNull() { Completable.fromCallable(null); }
@Test(expected = NullPointerException.class) public void fromCallableNull() { Completable.fromCallable(null); }
@Test(timeout = 5000) public void fromCallableNormal() { final AtomicInteger calls = new AtomicInteger(); Completable c = Completable.fromCallable(new Callable<Object>() { @Override public Object call() throws Exception { return calls.getAndIncrement(); } }); c.blockingAwait(); Assert.assertEquals(1, calls.get()); }
@Test(timeout = 5000, expected = TestException.class) public void fromCallableThrows() { Completable c = Completable.fromCallable(new Callable<Object>() { @Override public Object call() throws Exception { throw new TestException(); } }); c.blockingAwait(); }
@Test(timeout = 5000) public void repeatUntilNormal() { final AtomicInteger calls = new AtomicInteger(); final AtomicInteger times = new AtomicInteger(5); Completable c = Completable.fromCallable(new Callable<Object>() { @Override public Object call() throws Exception { calls.getAndIncrement(); return null; } }).repeatUntil(new BooleanSupplier() { @Override public boolean getAsBoolean() { return times.decrementAndGet() == 0; } }); c.blockingAwait(); Assert.assertEquals(5, calls.get()); }
@Test(timeout = 5000) public void repeat5Times() { final AtomicInteger calls = new AtomicInteger(); Completable c = Completable.fromCallable(new Callable<Object>() { @Override public Object call() throws Exception { calls.getAndIncrement(); return null; } }).repeat(5); c.blockingAwait(); Assert.assertEquals(5, calls.get()); }
@Test(timeout = 5000) public void repeat1Time() { final AtomicInteger calls = new AtomicInteger(); Completable c = Completable.fromCallable(new Callable<Object>() { @Override public Object call() throws Exception { calls.getAndIncrement(); return null; } }).repeat(1); c.blockingAwait(); Assert.assertEquals(1, calls.get()); }
@Test(timeout = 5000) public void repeat0Time() { final AtomicInteger calls = new AtomicInteger(); Completable c = Completable.fromCallable(new Callable<Object>() { @Override public Object call() throws Exception { calls.getAndIncrement(); return null; } }).repeat(0); c.blockingAwait(); Assert.assertEquals(0, calls.get()); }
@Test public void fromCallable() { final AtomicInteger atomicInteger = new AtomicInteger(); Completable.fromCallable(new Callable<Object>() { @Override public Object call() throws Exception { atomicInteger.incrementAndGet(); return null; } }) .test() .assertResult(); assertEquals(1, atomicInteger.get()); }
@Test public void fromActionErrorsDisposed() { final AtomicInteger calls = new AtomicInteger(); Completable.fromCallable(new Callable<Object>() { @Override public Object call() throws Exception { calls.incrementAndGet(); throw new TestException(); } }) .test(true) .assertEmpty(); assertEquals(1, calls.get()); } }
@Test public void fromCallableThrows() { Completable.fromCallable(new Callable<Object>() { @Override public Object call() throws Exception { throw new UnsupportedOperationException(); } }) .test() .assertFailure(UnsupportedOperationException.class); }
@Test public void fromCallableInvokesLazy() { final AtomicInteger atomicInteger = new AtomicInteger(); Completable completable = Completable.fromCallable(new Callable<Object>() { @Override public Object call() throws Exception { atomicInteger.incrementAndGet(); return null; } }); assertEquals(0, atomicInteger.get()); completable .test() .assertResult(); assertEquals(1, atomicInteger.get()); }
@Test(timeout = 5000) public void timeoutTimerCancelled() throws InterruptedException { Completable c = Completable.fromCallable(new Callable<Object>() { @Override public Object call() throws Exception { Thread.sleep(50); return null; } }).timeout(100, TimeUnit.MILLISECONDS, normal.completable); c.blockingAwait(); Thread.sleep(100); normal.assertSubscriptions(0); }
@Test public void fromCallableTwice() { final AtomicInteger atomicInteger = new AtomicInteger(); Callable<Object> callable = new Callable<Object>() { @Override public Object call() throws Exception { atomicInteger.incrementAndGet(); return null; } }; Completable.fromCallable(callable) .test() .assertResult(); assertEquals(1, atomicInteger.get()); Completable.fromCallable(callable) .test() .assertResult(); assertEquals(2, atomicInteger.get()); }
@Test(timeout = 5000) public void startWithCompletableNormal() { final AtomicBoolean run = new AtomicBoolean(); Completable c = normal.completable .startWith(Completable.fromCallable(new Callable<Object>() { @Override public Object call() throws Exception { run.set(normal.get() == 0); return null; } })); c.blockingAwait(); Assert.assertTrue("Did not start with other", run.get()); normal.assertSubscriptions(1); }
@Test(timeout = 5000) public void andThenCompletableNormal() { final AtomicBoolean run = new AtomicBoolean(); Completable c = normal.completable .andThen(Completable.fromCallable(new Callable<Object>() { @Override public Object call() throws Exception { run.set(normal.get() == 0); return null; } })); c.blockingAwait(); Assert.assertFalse("Start with other", run.get()); normal.assertSubscriptions(1); }
Completable fromCallableObservable = Completable.fromCallable(func);
final AtomicInteger calls = new AtomicInteger(); Completable c = Completable.fromCallable(new Callable<Object>() { @Override public Object call() throws Exception {