DefaultActorThread implements ActorThread { @VisibleForTesting void sendTransaction(@NonNull Transaction transaction) { ActorHandlerBase handler = mHandler; if (handler != null && handler.sendTransaction(transaction)) { return; } synchronized (this) { if ((handler = mHandler) != null && handler.sendTransaction(transaction)) { return; } HandlerThread thread = new HandlerThread(mThreadName); thread.start(); mHandler = mThreadFactory.createThread(thread.getLooper(), mStopDelay); mHandler.sendTransaction(transaction); } } DefaultActorThread(@NonNull ProxyFactory proxyFactory, @NonNull FailureHandler failureHandler,
                                     @NonNull String threadName);   DefaultActorThread(@NonNull ProxyFactory proxyFactory,
                                     @NonNull FailureHandler failureHandler, @NonNull String threadName, long stopDelay); @VisibleForTesting  DefaultActorThread(@NonNull ThreadFactory threadFactory, @NonNull ProxyFactory proxyFactory,
                                     @NonNull FailureHandler failureHandler, @NonNull String threadName, long stopDelay); @NonNull @Override ActorRef<T> bind(@NonNull Class<T> cls, @NonNull T impl);  }
@SuppressWarnings("unchecked") @Test public void sendTransaction_sendToHandler_validHandlerExists() { DefaultActorThread thread = createThread(); ActorHandlerBase handler = createHandler(-1, null); Mockito.doReturn(true).when(handler).sendTransaction(Mockito.<Transaction>any()); thread.mHandler = handler; Transaction transaction = Transaction.<Object>obtain(mImpl, mMessage, mFailureHandler); thread.sendTransaction(transaction); Mockito.verify(handler).sendTransaction(transaction); Assert.assertSame(handler, thread.mHandler); }
@SuppressWarnings("unchecked") @Test public void sendTransaction_sendToHandler_anotherThreadCreatedNewHandler() { DefaultActorThread thread = createThread(); ActorHandlerBase handler = createHandler(-1, null); Mockito.doReturn(false, true).when(handler).sendTransaction(Mockito.<Transaction>any()); thread.mHandler = handler; Transaction transaction = Transaction.<Object>obtain(mImpl, mMessage, mFailureHandler); thread.sendTransaction(transaction); Mockito.verify(handler, Mockito.times(2)).sendTransaction(transaction); Assert.assertSame(handler, thread.mHandler); }
@SuppressWarnings("unchecked") @Test public void sendTransaction_sendToNewHandler_invalidHandlerExists() { DefaultActorThread thread = createThread(); ActorHandlerBase handler = createHandler(-1, null); Mockito.doReturn(false).when(handler).sendTransaction(Mockito.<Transaction>any()); thread.mHandler = handler; Transaction transaction = Transaction.<Object>obtain(mImpl, mMessage, mFailureHandler); thread.sendTransaction(transaction); Mockito.verify(thread.mHandler).sendTransaction(transaction); Assert.assertNotSame(handler, thread.mHandler); }
@SuppressWarnings("unchecked") @Test public void sendTransaction_sendToHandler_initialState() { DefaultActorThread thread = createThread(); Transaction transaction = Transaction.<Object>obtain(mImpl, mMessage, mFailureHandler); thread.sendTransaction(transaction); Assert.assertNotNull(thread.mHandler); Mockito.verify(thread.mHandler).sendTransaction(transaction); }