use of net.morimekta.providence.client.HttpClientHandler in project providence by morimekta.
the class ProvidenceServletTest method testSimpleRequest.
@Test
public void testSimpleRequest() throws IOException, Failure {
AtomicBoolean called = new AtomicBoolean();
when(impl.test(any(Request.class))).thenAnswer(i -> {
called.set(true);
return new Response("response");
});
TestService.Iface client = new TestService.Client(new HttpClientHandler(this::endpoint, factory(), provider));
Response response = client.test(new Request("request"));
waitAtMost(Duration.ONE_HUNDRED_MILLISECONDS).untilTrue(called);
assertNotNull(response);
assertEquals("{text:\"response\"}", response.asString());
verify(instrumentation).onComplete(anyDouble(), any(PServiceCall.class), any(PServiceCall.class));
verifyNoMoreInteractions(instrumentation);
}
use of net.morimekta.providence.client.HttpClientHandler in project providence by morimekta.
the class ProvidenceServletTest method testSimpleRequest_exception.
@Test
public void testSimpleRequest_exception() throws IOException, Failure {
AtomicBoolean called = new AtomicBoolean();
when(impl.test(any(Request.class))).thenAnswer(i -> {
called.set(true);
throw Failure.builder().setText("failure").build();
});
TestService.Iface client = new TestService.Client(new HttpClientHandler(this::endpoint, factory(), provider));
try {
client.test(new Request("request"));
fail("No exception");
} catch (Failure ex) {
assertEquals("failure", ex.getText());
}
waitAtMost(Duration.ONE_HUNDRED_MILLISECONDS).untilTrue(called);
verify(instrumentation).onComplete(anyDouble(), any(PServiceCall.class), any(PServiceCall.class));
verifyNoMoreInteractions(instrumentation);
}
use of net.morimekta.providence.client.HttpClientHandler in project providence by morimekta.
the class DropWizardIT method testProvidenceServlet.
@Test
public void testProvidenceServlet() throws IOException, CalculateException {
// This test is just to prove that the providence servlet can be used in dropwizard too.
Calculator.Iface client = new Calculator.Client(new HttpClientHandler(() -> new GenericUrl(uri("test")), factory(), new DefaultSerializerProvider()));
Operand result = client.calculate(new Operation(Operator.ADD, list(withNumber(52d), withImaginary(new Imaginary(1d, -1d)), withNumber(15d))));
assertThat(debugString(result), is(equalTo("{\n" + " imaginary = {\n" + " v = 68\n" + " i = -1\n" + " }\n" + "}")));
}
use of net.morimekta.providence.client.HttpClientHandler in project providence by morimekta.
the class RPCOptions method getHandler.
public PServiceCallHandler getHandler() {
Serializer serializer = getSerializer(format);
URI uri = URI.create(endpoint);
if (uri.getScheme() == null || uri.getScheme().length() == 0) {
throw new ArgumentException("No protocol on URI: " + endpoint);
}
if (uri.getScheme().startsWith("thrift")) {
if (// Must have host and port.
(uri.getPort() < 1) || (uri.getHost() == null || uri.getHost().length() == 0) || // No path, query or fragment.
(uri.getFragment() != null && uri.getFragment().length() > 0) || (uri.getQuery() != null && uri.getQuery().length() > 0) || (uri.getPath() != null && uri.getPath().length() > 0)) {
throw new ArgumentException("Illegal thrift URI: " + endpoint);
}
InetSocketAddress address = new InetSocketAddress(uri.getHost(), uri.getPort());
switch(uri.getScheme()) {
case "thrift":
return new SocketClientHandler(serializer, address, connect_timeout, read_timeout);
case "thrift+nonblocking":
return new NonblockingSocketClientHandler(serializer, address, connect_timeout, read_timeout);
default:
throw new ArgumentException("Unknown thrift protocol " + uri.getScheme());
}
}
GenericUrl url = new GenericUrl(endpoint);
Map<String, String> hdrs = new HashMap<>();
for (String hdr : headers) {
String[] parts = hdr.split("[:]", 2);
if (parts.length != 2) {
throw new ArgumentException("Invalid headers param: " + hdr);
}
hdrs.put(parts[0].trim(), parts[1].trim());
}
HttpTransport transport = new NetHttpTransport();
HttpRequestFactory factory = transport.createRequestFactory(new SetHeadersInitializer(hdrs, connect_timeout, read_timeout));
SerializerProvider serializerProvider = new ThriftSerializerProvider(serializer.mediaType());
return new HttpClientHandler(() -> url, factory, serializerProvider);
}
Aggregations