PromiseProxy extends Promise<R> implements Message<T, R>, ActionHandle, ResultListener<R> { @Override public void onResult(@Nullable R result) { final ResultListener<R> listener = mListener; mListener = null; if (listener == null) { throw new ResultListenerIsNotSpecifiedException(); } listener.onResult(result); } PromiseProxy(@NonNull MessageSender sender, @NonNull Message<T, R> message); @Override void thenNothing(); @NonNull @Override ActionHandle then(@NonNull ResultListener<R> listener); @NonNull @Override ActionHandle then(@NonNull ActorThread thread, @NonNull ResultListener<R> listener); @Nullable @Override R get(); @Override void onResult(@Nullable R result); @Override void forget(); @Override Promise<R> invoke(@NonNull T impl); @NonNull @Override ActorInvokeException exception(); @Override String toString();  }
@Test(timeout = 2000) public void get_deliverResult_nonNullResult() throws Exception { final Object result = new Object(); final CountDownLatch latch = new CountDownLatch(1); PromiseProxy promise = new PromiseProxy(mSender, mMessage); ResultDeliveryThread thread = new ResultDeliveryThread(promise, latch, null); Mockito.doAnswer(thread).when(mSender).deliver(Mockito.<Message>any()); thread.start(); latch.await(); promise.onResult(result); thread.join(); Assert.assertSame(result, thread.delivered()); }
@Test(timeout = 2000) public void get_deliverResult_nullResult() throws Exception { final CountDownLatch latch = new CountDownLatch(1); PromiseProxy promise = new PromiseProxy(mSender, mMessage); ResultDeliveryThread thread = new ResultDeliveryThread(promise, latch, new Object()); Mockito.doAnswer(thread).when(mSender).deliver(Mockito.<Message>any()); thread.start(); latch.await(); Thread.sleep(200); promise.onResult(null); thread.join(); Assert.assertNull(thread.delivered()); }
@Test(expected = ResultListenerIsNotSpecifiedException.class) public void onResult_throw_withoutListener() { PromiseProxy promise = new PromiseProxy(mSender, mMessage); promise.onResult(new Object()); }