/** * Creates a Goro implementation that binds to a worker service to schedule tasks. * This implementation binds to the backing service when one of {@code Goro} methods is invoked, * then delegates all the tasks to the service and unbinds asap. * @param context context that will bind to the service * @return Goro implementation that binds to {@link GoroService} */ public static Goro bindOnDemandWith(final Context context) { if (context == null) { throw new IllegalArgumentException("Context cannot be null"); } return new OnDemandBindingGoro(context); }
@Override public final void addTaskListener(final GoroListener listener) { super.addTaskListener(listener); bindIfRequired(); }
@Override public void onServiceConnected(final ComponentName name, final IBinder binder) { updateDelegate(Goro.from(binder)); unbindIfRequired(); }
public void testExecutor() throws Exception { final CountDownLatch sync = new CountDownLatch(1); Runnable task = new Runnable() { @Override public void run() { sync.countDown(); } }; goro.getExecutor("test").execute(task); assertThat(sync.await(2, TimeUnit.SECONDS)).describedAs("Operation timed out").isTrue(); waitMainThread(); assertThat(goro.delegate()).describedAs("Service must be unbound").isNull(); }
public void testSchedule() throws InterruptedException { final CountDownLatch sync = new CountDownLatch(1); Callable<String> task = new Callable<String>() { @Override public String call() { return "done"; } }; final Object[] result = new Object[1]; goro.schedule(task) .subscribe(new FutureObserver<String>() { @Override public void onSuccess(final String value) { result[0] = value; sync.countDown(); } @Override public void onError(final Throwable error) { result[0] = error; sync.countDown(); } }); assertThat(sync.await(2, TimeUnit.SECONDS)).describedAs("Operation timed out").isTrue(); assertThat(result).containsOnly("done"); waitMainThread(); assertThat(goro.delegate()).describedAs("Service must be unbound").isNull(); }
@Override public void onServiceDisconnected(final ComponentName name) { unbindIfRequired(); }
private synchronized void unbindIfRequired() { if (bindRequested && updateDelegate(null)) { bindRequested = false; GoroService.unbind(context, this); } }
@Test public void scheduleShouldBeDelegated() throws Exception { Callable<?> task = mock(Callable.class); goro.schedule("2", task); testingQueues.executeAll(); verify(task).call(); }
@Override public final <T> ObservableFuture<T> schedule(final String queueName, final Callable<T> task) { ObservableFuture<T> result = super.schedule(queueName, task); bindIfRequired(); return result; }
@Override protected final void removeTasksInQueue(final String queueName) { super.removeTasksInQueue(queueName); bindIfRequired(); }
@Override public final void removeTaskListener(final GoroListener listener) { super.removeTaskListener(listener); bindIfRequired(); }
@Override public final Executor getExecutor(final String queueName) { Executor executor = super.getExecutor(queueName); bindIfRequired(); return executor; }