private static String writeThriftObjectAsTText(Consumer<TProtocol> writer) { final TMemoryBuffer buffer = new TMemoryBuffer(1024); final TProtocol protocol = new TTextProtocol.Factory().getProtocol(buffer); writer.accept(protocol); return new String(buffer.getArray(), 0, buffer.length()); }
public String toBinaryString() throws IOException { org.apache.hadoop.hive.ql.plan.api.Query q = getQueryPlan(); TMemoryBuffer tmb = new TMemoryBuffer(q.toString().length() * 5); TBinaryProtocol oprot = new TBinaryProtocol(tmb); try { q.write(oprot); } catch (TException e) { // TODO Auto-generated catch block e.printStackTrace(); return q.toString(); } byte[] buf = new byte[tmb.length()]; tmb.read(buf, 0, tmb.length()); return new String(buf); // return getQueryPlan().toString(); }
@Test public void testSync_OnewayHelloService_hello() throws Exception { final AtomicReference<String> actualName = new AtomicReference<>(); final OnewayHelloService.Client client = new OnewayHelloService.Client.Factory().getClient(inProto, outProto); client.send_hello(FOO); assertThat(out.length()).isGreaterThan(0); final THttpService service = THttpService.of( (OnewayHelloService.Iface) actualName::set, defaultSerializationFormat); invoke(service); assertThat(promise.get().isEmpty()).isTrue(); assertThat(actualName.get()).isEqualTo(FOO); }
@Test public void testAsync_OnewayHelloService_hello() throws Exception { final AtomicReference<String> actualName = new AtomicReference<>(); final OnewayHelloService.Client client = new OnewayHelloService.Client.Factory().getClient(inProto, outProto); client.send_hello(FOO); assertThat(out.length()).isGreaterThan(0); final THttpService service = THttpService.of((OnewayHelloService.AsyncIface) (name, resultHandler) -> { actualName.set(name); resultHandler.onComplete(null); }, defaultSerializationFormat); invoke(service); assertThat(promise.get().isEmpty()).isTrue(); assertThat(actualName.get()).isEqualTo(FOO); }
@Test public void testSync_NameSortService_sort() throws Exception { final NameSortService.Client client = new NameSortService.Client.Factory().getClient(inProto, outProto); client.send_sort(Arrays.asList(NAME_C, NAME_B, NAME_A)); assertThat(out.length()).isGreaterThan(0); final THttpService service = THttpService.of((NameSortService.Iface) names -> { final ArrayList<Name> sorted = new ArrayList<>(names); Collections.sort(sorted); return sorted; }, defaultSerializationFormat); invoke(service); assertThat(client.recv_sort()).containsExactly(NAME_A, NAME_B, NAME_C); }
@Test public void testSync_DevNullService_consume() throws Exception { final AtomicReference<String> consumed = new AtomicReference<>(); final DevNullService.Client client = new DevNullService.Client.Factory().getClient(inProto, outProto); client.send_consume(FOO); assertThat(out.length()).isGreaterThan(0); final THttpService service = THttpService.of( (DevNullService.Iface) consumed::set, defaultSerializationFormat); invoke(service); assertThat(consumed.get()).isEqualTo(FOO); client.recv_consume(); }
@Test public void testAsync_NameSortService_sort() throws Exception { final NameSortService.Client client = new NameSortService.Client.Factory().getClient(inProto, outProto); client.send_sort(Arrays.asList(NAME_C, NAME_B, NAME_A)); assertThat(out.length()).isGreaterThan(0); final THttpService service = THttpService.of((NameSortService.AsyncIface) (names, resultHandler) -> { final ArrayList<Name> sorted = new ArrayList<>(names); Collections.sort(sorted); resultHandler.onComplete(sorted); }, defaultSerializationFormat); invoke(service); assertThat(client.recv_sort()).containsExactly(NAME_A, NAME_B, NAME_C); }
@Test public void testAsync_DevNullService_consume() throws Exception { final AtomicReference<String> consumed = new AtomicReference<>(); final DevNullService.Client client = new DevNullService.Client.Factory().getClient(inProto, outProto); client.send_consume("bar"); assertThat(out.length()).isGreaterThan(0); final THttpService service = THttpService.of((DevNullService.AsyncIface) (value, resultHandler) -> { consumed.set(value); resultHandler.onComplete(null); }, defaultSerializationFormat); invoke(service); assertThat(consumed.get()).isEqualTo("bar"); client.recv_consume(); }
@Test public void testSync_HelloService_hello_with_null() throws Exception { final HelloService.Client client = new HelloService.Client.Factory().getClient(inProto, outProto); client.send_hello(null); assertThat(out.length()).isGreaterThan(0); final THttpService service = THttpService.of( (HelloService.Iface) name -> String.valueOf(name != null), defaultSerializationFormat); invoke(service); assertThat(client.recv_hello()).isEqualTo("false"); }
@Test public void testIdentity_HelloService_hello() throws Exception { final HelloService.Client client = new HelloService.Client.Factory().getClient(inProto, outProto); client.send_hello(FOO); assertThat(out.length()).isGreaterThan(0); final THttpService syncService = THttpService.of( (HelloService.Iface) name -> "Hello, " + name + '!', defaultSerializationFormat); final THttpService asyncService = THttpService.of( (HelloService.AsyncIface) (name, resultHandler) -> resultHandler.onComplete("Hello, " + name + '!'), defaultSerializationFormat); invokeTwice(syncService, asyncService); assertThat(promise.get()).isEqualTo(promise2.get()); }
@Test public void testIdentity_DevNullService_consume() throws Exception { final DevNullService.Client client = new DevNullService.Client.Factory().getClient(inProto, outProto); client.send_consume(FOO); assertThat(out.length()).isGreaterThan(0); final THttpService syncService = THttpService.of((DevNullService.Iface) value -> { // NOOP }, defaultSerializationFormat); final THttpService asyncService = THttpService.of( (DevNullService.AsyncIface) (value, resultHandler) -> resultHandler.onComplete(null), defaultSerializationFormat); invokeTwice(syncService, asyncService); assertThat(promise.get()).isEqualTo(promise2.get()); }
@Test public void testSync_HelloService_hello() throws Exception { final HelloService.Client client = new HelloService.Client.Factory().getClient(inProto, outProto); client.send_hello(FOO); assertThat(out.length()).isGreaterThan(0); final THttpService service = THttpService.of( (HelloService.Iface) name -> "Hello, " + name + '!', defaultSerializationFormat); invoke(service); assertThat(client.recv_hello()).isEqualTo("Hello, foo!"); }
@Test public void testAsync_HelloService_hello_with_null() throws Exception { final HelloService.Client client = new HelloService.Client.Factory().getClient(inProto, outProto); client.send_hello(null); assertThat(out.length()).isGreaterThan(0); final THttpService service = THttpService.of( (HelloService.AsyncIface) (name, resultHandler) -> resultHandler.onComplete(String.valueOf(name != null)), defaultSerializationFormat); invoke(service); assertThat(client.recv_hello()).isEqualTo("false"); }
private void invokeTwice(THttpService service1, THttpService service2) throws Exception { final HttpData content = HttpData.of(out.getArray(), 0, out.length()); invoke0(service1, content, promise); invoke0(service2, content, promise2); }
@Test public void testAsync_HelloService_hello() throws Exception { final HelloService.Client client = new HelloService.Client.Factory().getClient(inProto, outProto); client.send_hello(FOO); assertThat(out.length()).isGreaterThan(0); final THttpService service = THttpService.of( (HelloService.AsyncIface) (name, resultHandler) -> resultHandler.onComplete("Hello, " + name + '!'), defaultSerializationFormat); invoke(service); assertThat(client.recv_hello()).isEqualTo("Hello, foo!"); }
@Test public void testIdentity_FileService_create_reply() throws Exception { final FileService.Client client = new FileService.Client.Factory().getClient(inProto, outProto); client.send_create(BAR); assertThat(out.length()).isGreaterThan(0); final THttpService syncService = THttpService.of((FileService.Iface) path -> { throw newFileServiceException(); }, defaultSerializationFormat); final THttpService asyncService = THttpService.of( (FileService.AsyncIface) (path, resultHandler) -> resultHandler.onError(newFileServiceException()), defaultSerializationFormat); invokeTwice(syncService, asyncService); assertThat(promise.get()).isEqualTo(promise2.get()); }
@Test public void testIdentity_NameService_removeMiddle() throws Exception { final NameService.Client client = new NameService.Client.Factory().getClient(inProto, outProto); client.send_removeMiddle(new Name(FOO, BAZ, BAR)); assertThat(out.length()).isGreaterThan(0); final THttpService syncService = THttpService.of( (NameService.Iface) name -> new Name(name.first, null, name.last), defaultSerializationFormat); final THttpService asyncService = THttpService.of( (NameService.AsyncIface) (name, resultHandler) -> resultHandler.onComplete(new Name(name.first, null, name.last)), defaultSerializationFormat); invokeTwice(syncService, asyncService); assertThat(promise.get()).isEqualTo(promise2.get()); }
private void invoke(THttpService service) throws Exception { invoke0(service, HttpData.of(out.getArray(), 0, out.length()), promise); final HttpData res = promise.get(); in.reset(res.array(), res.offset(), res.length()); }
@Test public void testSync_NameService_removeMiddle() throws Exception { final NameService.Client client = new NameService.Client.Factory().getClient(inProto, outProto); client.send_removeMiddle(new Name(BAZ, BAR, FOO)); assertThat(out.length()).isGreaterThan(0); final THttpService service = THttpService.of( (NameService.Iface) name -> new Name(name.first, null, name.last), defaultSerializationFormat); invoke(service); assertThat(client.recv_removeMiddle()).isEqualTo(new Name(BAZ, null, FOO)); }
@Test public void testAsync_NameService_removeMiddle() throws Exception { final NameService.Client client = new NameService.Client.Factory().getClient(inProto, outProto); client.send_removeMiddle(new Name(BAZ, BAR, FOO)); assertThat(out.length()).isGreaterThan(0); final THttpService service = THttpService.of( (NameService.AsyncIface) (name, resultHandler) -> resultHandler.onComplete(new Name(name.first, null, name.last)), defaultSerializationFormat); invoke(service); assertThat(client.recv_removeMiddle()).isEqualTo(new Name(BAZ, null, FOO)); }