ClientProxyImpl implements ClientProxy<C> { @Override @SuppressWarnings("unchecked") public synchronized C get() { return (C) this.enhancer.create( new Class[]{TProtocolFactory.class, TAsyncSSLClientManager.class, TNonblockingSSLTransport.class}, new Object[]{null, null, null}); } @Inject  ClientProxyImpl(@Named("ClientProxyExecutor") ExecutorService executor,
                         TypeLiteral<C> type,
                         @Assisted final ClientPool<C> clientPool); @Override @SuppressWarnings("unchecked") synchronized C get();  }
@Test public void testSuccessfulProxyMethodCall() throws Exception { when(clientPool.acquire()).thenReturn(Futures.immediateFuture(client)); mockCallSuccess(client); ClientProxyImpl<AsyncSSLClient> proxy = new ClientProxyImpl<>(executor, typeLiteral, clientPool); assertThat(performEchoCall(proxy.get(), "foobar"), is("foobar")); verify(clientPool).acquire(); verify(clientPool).release(client, true); verifyNoMoreInteractions(clientPool); }
@Test public void testFailedProxyMethodCall() throws Exception { when(clientPool.acquire()).thenReturn(Futures.immediateFuture(client)); mockCallError(client, new TException("Something happened")); ClientProxyImpl<AsyncSSLClient> proxy = new ClientProxyImpl<>(executor, typeLiteral, clientPool); try { performEchoCall(proxy.get(), "foobar"); fail(); } catch (TException e) { assertThat(e.getMessage(), is("Something happened")); } verify(clientPool).acquire(); verify(clientPool).release(client, false); verifyNoMoreInteractions(clientPool); }
@Test public void testFailedProxyMethodCallWithApplicationException() throws Exception { when(clientPool.acquire()).thenReturn(Futures.immediateFuture(client)); mockCallError(client, new TApplicationException("Something happened")); ClientProxyImpl<AsyncSSLClient> proxy = new ClientProxyImpl<>(executor, typeLiteral, clientPool); try { performEchoCall(proxy.get(), "foobar"); fail(); } catch (TException e) { assertThat(e.getMessage(), is("Something happened")); } verify(clientPool).acquire(); verify(clientPool).release(client, true); verifyNoMoreInteractions(clientPool); }
@Test public void testCannotAcquireClient() throws Exception { when(clientPool.acquire()).thenReturn(Futures.<AsyncSSLClient>immediateFailedFuture(new Exception("foo"))); ClientProxyImpl<AsyncSSLClient> proxy = new ClientProxyImpl<>(executor, typeLiteral, clientPool); try { performEchoCall(proxy.get(), "foobar"); fail(); } catch (Exception e) { assertThat(e.getMessage(), is("foo")); } verify(clientPool).acquire(); verifyNoMoreInteractions(clientPool); }
@Test public void testCallMethodWithoutCallback() throws Exception { ClientProxyImpl<AsyncSSLClient> proxy = new ClientProxyImpl<>(executor, typeLiteral, clientPool); try { proxy.get().getProtocolFactory(); fail(); } catch (Exception e) { assertTrue(e instanceof IllegalArgumentException); } verifyNoMoreInteractions(clientPool); }
@SuppressWarnings("unchecked") @Test public void testSetTimeoutMultipleClients() throws Exception { AsyncSSLClient client2 = mock(AsyncSSLClient.class); when(clientPool.acquire()).thenReturn(Futures.immediateFuture(client), Futures.immediateFuture(client2)); mockCallSuccess(client); mockCallSuccess(client2); ClientProxy<AsyncSSLClient> proxy = new ClientProxyImpl<>(executor, typeLiteral, clientPool); AsyncSSLClient clientProxy = proxy.get(); clientProxy.setTimeout(10); assertThat(performEchoCall(clientProxy, "foo"), is("foo")); assertThat(performEchoCall(clientProxy, "bar"), is("bar")); verify(clientPool, times(2)).acquire(); verify(client).setTimeout(10); verify(client2).setTimeout(10); verify(clientPool).release(client, true); verify(clientPool).release(client2, true); verifyNoMoreInteractions(clientPool); }