@Test public void testPassFutureThrough() throws ExecutionException, InterruptedException, TimeoutException, EntityException, MessageCodecException { final SerializationCodec codec = new SerializationCodec() { @Override public <T> T decode(final Class<T> type, final byte[] buffer) { assertThat(type == Integer.class, is(true)); return super.decode(type, buffer); } }; final EntityClientEndpoint endpoint = mock(EntityClientEndpoint.class); final InvocationBuilder builder = mock(InvocationBuilder.class); when(endpoint.beginInvoke()).thenReturn(builder); when(builder.message(ArgumentMatchers.<EntityMessage>any())).thenReturn(builder); when(builder.withExecutor(ArgumentMatchers.<Executor>any())).thenReturn(builder); final InvokeFuture future = mock(InvokeFuture.class); when(builder.invoke()).thenReturn(future); when(future.get()).thenReturn(messageResponse(Integer.class, 42)); when(future.getWithTimeout(1, TimeUnit.SECONDS)).thenReturn(messageResponse(Integer.class, 43)) .thenThrow(new TimeoutException("Blah!")); final PassThrough proxy = ClientProxyFactory.createProxy(PassThrough.class, PassThrough.class, endpoint, null, codec); assertThat(proxy.aSync().get(), is(42)); assertThat(proxy.aSync().get(1, TimeUnit.SECONDS), is(43)); try { proxy.aSync().get(1, TimeUnit.SECONDS); fail(); } catch (TimeoutException e) { assertThat(e.getMessage(), is("Blah!")); } }
public static <T> T createProxy(Class<T> clientType, Class<? super T> type, EntityClientEndpoint<ProxyEntityMessage, ProxyEntityResponse> entityClientEndpoint, Class<?>[] messageTypes, Codec codec) { if (entityClientEndpoint == null) { throw new NullPointerException("EntityClientEndpoint has to be provided!"); } if (!type.isInterface()) { throw new IllegalArgumentException("We only proxy interfaces!"); } final Class<?>[] interfaces; if (messageTypes == null || messageTypes.length == 0) { interfaces = new Class<?>[]{clientType, Entity.class}; } else { interfaces = new Class<?>[]{clientType, Entity.class, ServerMessageAware.class}; } return clientType.cast(Proxy.newProxyInstance( Entity.class.getClassLoader(), interfaces, new VoltronProxyInvocationHandler( entityClientEndpoint, CommonProxyFactory.invert(CommonProxyFactory.createResponseTypeMappings(type, messageTypes)).values(), codec) )); }
ClientProxyFactory { public static <T> T createProxy(Class<T> clientType, Class<? super T> type, EntityClientEndpoint<ProxyEntityMessage, ProxyEntityResponse> entityClientEndpoint, Class<?>[] messageTypes, Codec codec) { if (entityClientEndpoint == null) { throw new NullPointerException("EntityClientEndpoint has to be provided!"); } if (!type.isInterface()) { throw new IllegalArgumentException("We only proxy interfaces!"); } final Class<?>[] interfaces; if (messageTypes == null || messageTypes.length == 0) { interfaces = new Class<?>[]{clientType, Entity.class}; } else { interfaces = new Class<?>[]{clientType, Entity.class, ServerMessageAware.class}; } return clientType.cast(Proxy.newProxyInstance( Entity.class.getClassLoader(), interfaces, new VoltronProxyInvocationHandler( entityClientEndpoint, CommonProxyFactory.invert(CommonProxyFactory.createResponseTypeMappings(type, messageTypes)).values(), codec) )); } }
ClientProxyFactory { public static <T> T createProxy(Class<T> clientType, Class<? super T> type, EntityClientEndpoint<ProxyEntityMessage, ProxyEntityResponse> entityClientEndpoint, Class<?>[] messageTypes, Codec codec) { if (entityClientEndpoint == null) { throw new NullPointerException("EntityClientEndpoint has to be provided!"); } if (!type.isInterface()) { throw new IllegalArgumentException("We only proxy interfaces!"); } final Class<?>[] interfaces; if (messageTypes == null || messageTypes.length == 0) { interfaces = new Class<?>[]{clientType, Entity.class}; } else { interfaces = new Class<?>[]{clientType, Entity.class, ServerMessageAware.class}; } return clientType.cast(Proxy.newProxyInstance( Entity.class.getClassLoader(), interfaces, new VoltronProxyInvocationHandler( entityClientEndpoint, CommonProxyFactory.invert(CommonProxyFactory.createResponseTypeMappings(type, messageTypes)).values(), codec) )); }  }
ClientProxyFactory { public static <T> T createProxy(Class<T> clientType, Class<? super T> type, EntityClientEndpoint<ProxyEntityMessage, ProxyEntityResponse> entityClientEndpoint, Class<?>[] messageTypes, Codec codec) { if (entityClientEndpoint == null) { throw new NullPointerException("EntityClientEndpoint has to be provided!"); } if (!type.isInterface()) { throw new IllegalArgumentException("We only proxy interfaces!"); } final Class<?>[] interfaces; if (messageTypes == null || messageTypes.length == 0) { interfaces = new Class<?>[]{clientType, Entity.class}; } else { interfaces = new Class<?>[]{clientType, Entity.class, ServerMessageAware.class}; } return clientType.cast(Proxy.newProxyInstance( Entity.class.getClassLoader(), interfaces, new VoltronProxyInvocationHandler( entityClientEndpoint, CommonProxyFactory.invert(CommonProxyFactory.createResponseTypeMappings(type, messageTypes)).values(), codec) )); }  static T createEntityProxy(Class<T> clientType,
                                                                            Class<? super T> type,
                                                                            EntityClientEndpoint<ProxyEntityMessage, ProxyEntityResponse> entityClientEndpoint,
                                                                            Class<?>[] messageTypes,
                                                                            Codec codec); static T createProxy(Class<T> clientType,
                                  Class<? super T> type,
                                  EntityClientEndpoint<ProxyEntityMessage, ProxyEntityResponse> entityClientEndpoint,
                                  Class<?>[] messageTypes,
                                  Codec codec); }
ClientProxyFactory { public static <T> T createProxy(Class<T> clientType, Class<? super T> type, EntityClientEndpoint<ProxyEntityMessage, ProxyEntityResponse> entityClientEndpoint, Class<?>[] messageTypes, Codec codec) { if (entityClientEndpoint == null) { throw new NullPointerException("EntityClientEndpoint has to be provided!"); } if (!type.isInterface()) { throw new IllegalArgumentException("We only proxy interfaces!"); } final Class<?>[] interfaces; if (messageTypes == null || messageTypes.length == 0) { interfaces = new Class<?>[]{clientType, Entity.class}; } else { interfaces = new Class<?>[]{clientType, Entity.class, ServerMessageAware.class}; } return clientType.cast(Proxy.newProxyInstance( Entity.class.getClassLoader(), interfaces, new VoltronProxyInvocationHandler( entityClientEndpoint, CommonProxyFactory.invert(CommonProxyFactory.createResponseTypeMappings(type, messageTypes)).values(), codec) )); }  static T createEntityProxy(Class<T> clientType,
                                                                            Class<? super T> type,
                                                                            EntityClientEndpoint<ProxyEntityMessage, ProxyEntityResponse> entityClientEndpoint,
                                                                            Class<?>[] messageTypes,
                                                                            Codec codec); static T createProxy(Class<T> clientType,
                                  Class<? super T> type,
                                  EntityClientEndpoint<ProxyEntityMessage, ProxyEntityResponse> entityClientEndpoint,
                                  Class<?>[] messageTypes,
                                  Codec codec);  }