ThriftEventHandler implements TServerEventHandler { @Override public void preServe() { ServiceNodeUtils.joinService(serviceNode, retryIntervalMsec, serviceNodeEventHandler); } @Inject  ThriftEventHandler(@Assisted ServiceNodeEventHandler serviceNodeEventHandler,
                            @Assisted ServiceNode serviceNode); @Override void preServe(); @Override ServerContext createContext(TProtocol input, TProtocol output); @Override void deleteContext(ServerContext serverContext, TProtocol input, TProtocol output); @Override void processContext(ServerContext serverContext, TTransport inputTransport, TTransport outputTransport);  }
@Test public void testPreServeTriggersOnJoin() { when(serviceNode.join()).thenReturn(Futures.immediateFuture(lease)); when(lease.getExpirationFuture()).thenReturn(SettableFuture.<Void>create()); ThriftEventHandler handler = new ThriftEventHandler(serviceHandler, serviceNode); handler.preServe(); verify(serviceNode).join(); verify(lease).getExpirationFuture(); verify(serviceHandler).onJoin(); verifyNoMoreInteractions(serviceHandler, serviceNode, lease); }
@Test public void testExpirationTriggersRejoin() { when(serviceNode.join()).thenReturn( Futures.immediateFuture(lease), SettableFuture.<ServiceNode.Lease>create() ); when(lease.getExpirationFuture()).thenReturn(Futures.<Void>immediateFuture(null)); ThriftEventHandler handler = new ThriftEventHandler(serviceHandler, serviceNode); handler.preServe(); InOrder inOrder = inOrder(serviceNode, lease, serviceHandler); inOrder.verify(serviceNode).join(); inOrder.verify(serviceHandler).onJoin(); inOrder.verify(lease).getExpirationFuture(); inOrder.verify(serviceHandler).onLeave(); inOrder.verify(serviceNode).join(); verifyNoMoreInteractions(serviceHandler, serviceNode, lease); }
@Test public void testErrorJoiningTriggersRejoin() { when(serviceNode.join()).thenReturn( Futures.<ServiceNode.Lease>immediateFailedFuture(new Exception("can't join")), SettableFuture.<ServiceNode.Lease>create()); when(lease.getExpirationFuture()).thenReturn(SettableFuture.<Void>create()); ThriftEventHandler handler = new ThriftEventHandler(serviceHandler, serviceNode); handler.preServe(); InOrder inOrder = inOrder(serviceNode, lease, serviceHandler); inOrder.verify(serviceNode, times(2)).join(); verifyNoMoreInteractions(serviceHandler, serviceNode, lease); }
@Test public void testErrorOnExpirationFutureTriggersRejoin() throws Exception { when(serviceNode.join()).thenReturn( Futures.<ServiceNode.Lease>immediateFuture(lease), SettableFuture.<ServiceNode.Lease>create()); when(lease.getExpirationFuture()).thenReturn(Futures.<Void>immediateFailedFuture(new Exception("boom"))); ThriftEventHandler handler = new ThriftEventHandler(serviceHandler, serviceNode); handler.preServe(); InOrder inOrder = inOrder(serviceNode, lease, serviceHandler); inOrder.verify(serviceNode).join(); inOrder.verify(serviceHandler).onJoin(); inOrder.verify(lease).getExpirationFuture(); inOrder.verify(serviceHandler).onLeave(); inOrder.verify(serviceNode).join(); verifyNoMoreInteractions(serviceHandler, serviceNode, lease); }