use of net.morimekta.providence.thrift.client.NonblockingSocketClientHandler in project providence by morimekta.
the class NonblockingSocketServerTest method testWithProvidenceClient.
@Test
public void testWithProvidenceClient() throws IOException, ExecutionException, InterruptedException, TimeoutException {
try (NonblockingSocketClientHandler handler = new NonblockingSocketClientHandler(serializer, new InetSocketAddress("localhost", port))) {
RemoteMap.Client client = new RemoteMap.Client(handler);
Future<Boolean> a = executor.submit(() -> client.put("a", "1234"));
Thread.sleep(3);
Future<Boolean> b = executor.submit(() -> client.put("b", "2345"));
Thread.sleep(3);
Future<Boolean> c = executor.submit(() -> client.put("c", "3456"));
Future<String> f = executor.submit(() -> client.get("f"));
Thread.sleep(3);
Future<Boolean> d = executor.submit(() -> client.put("d", "4567"));
Thread.sleep(3);
Future<Boolean> e = executor.submit(() -> client.put("e", "5678"));
assertThat(a.get(1, TimeUnit.SECONDS), is(false));
assertThat(b.get(1, TimeUnit.SECONDS), is(false));
assertThat(c.get(1, TimeUnit.SECONDS), is(false));
assertThat(d.get(1, TimeUnit.SECONDS), is(false));
assertThat(e.get(1, TimeUnit.SECONDS), is(false));
try {
f.get(1, TimeUnit.SECONDS);
fail("no exception");
} catch (ExecutionException ee) {
assertThat(ee.getCause(), is(instanceOf(NotFound.class)));
}
assertThat(remoteMap, is(ImmutableMap.of("a", "1234", "b", "2345", "c", "3456", "d", "4567", "e", "5678")));
}
}
use of net.morimekta.providence.thrift.client.NonblockingSocketClientHandler 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