@Test public void shouldWriteUnion_thatThriftRead() throws Exception { TMemoryBuffer transport = new TMemoryBuffer(1024); TBinaryProtocol protocol = new TBinaryProtocol(transport); UnionB fireflyUnionB = new UnionB(); OrderedStruct fireflyOrderedStruct = new OrderedStruct(); fireflyOrderedStruct.id = 99; fireflyUnionB.os = fireflyOrderedStruct; StructTypeAdapterFactory.StructTypeAdapter structTypeAdapter = new StructTypeAdapterFactory.StructTypeAdapter(UnionB.class, thrift); structTypeAdapter.write(fireflyUnionB, protocol); com.meituan.firefly.testthrift.UnionB thriftUnionB = new com.meituan.firefly.testthrift.UnionB(); thriftUnionB.read(protocol); assertThat(thriftUnionB.getSetField()).isEqualTo(com.meituan.firefly.testthrift.UnionB._Fields.OS); assertThat(thriftUnionB.getOs()).isNotNull(); assertThat(thriftUnionB.getOs().getId()).isEqualTo(99); }
public static void main(String[] args) throws IOException, TTransportException, ClassNotFoundException { TMemoryBuffer transport = new TMemoryBuffer(4096); Trade trade = new Trade(); trade.symbol = "F"; trade.price = 13.10; trade.size = 2500; ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(trade); transport.write(baos.toByteArray()); byte[] buf = new byte[128]; int bytes_read = transport.read(buf, 0, buf.length); ByteArrayInputStream bais = new ByteArrayInputStream(buf); ObjectInputStream ois = new ObjectInputStream(bais); Trade trade_read = (Trade) ois.readObject(); System.out.println("Trade(" + bytes_read + "): " + trade_read.symbol + " " + trade_read.size + " @ " + trade_read.price); }
private String thriftRequest(byte[] input){ try{ //Input TMemoryBuffer inbuffer = new TMemoryBuffer(input.length); inbuffer.write(input); TProtocol inprotocol = new TJSONProtocol(inbuffer); //Output TMemoryBuffer outbuffer = new TMemoryBuffer(100); TProtocol outprotocol = new TJSONProtocol(outbuffer); TProcessor processor = new Calculator.Processor(new CalculatorHandler()); processor.process(inprotocol, outprotocol); byte[] output = new byte[outbuffer.length()]; outbuffer.readAll(output, 0, output.length); return new String(output,"UTF-8"); }catch(Throwable t){ return "Error:"+t.getMessage(); } }
public void testBinary() throws Exception { for (byte[] b : Arrays.asList(new byte[0], new byte[]{0,1,2,3,4,5,6,7,8,9,10}, new byte[]{0,1,2,3,4,5,6,7,8,9,10,11,12,13,14}, new byte[128])) { if (canBeUsedNaked()) { internalTestNakedBinary(b); } internalTestBinaryField(b); } if (canBeUsedNaked()) { byte[] data = {1, 2, 3, 4, 5, 6}; TMemoryBuffer buf = new TMemoryBuffer(0); TProtocol proto = getFactory().getProtocol(buf); ByteBuffer bb = ByteBuffer.wrap(data); bb.get(); proto.writeBinary(bb.slice()); assertEquals(ByteBuffer.wrap(data, 1, 5), proto.readBinary()); } }
public void testMessage() throws Exception { List<TMessage> msgs = Arrays.asList(new TMessage[]{ new TMessage("short message name", TMessageType.CALL, 0), new TMessage("1", TMessageType.REPLY, 12345), new TMessage("loooooooooooooooooooooooooooooooooong", TMessageType.EXCEPTION, 1 << 16), new TMessage("Janky", TMessageType.CALL, 0), }); for (TMessage msg : msgs) { TMemoryBuffer buf = new TMemoryBuffer(0); TProtocol proto = getFactory().getProtocol(buf); TMessage output = null; proto.writeMessageBegin(msg); proto.writeMessageEnd(); output = proto.readMessageBegin(); assertEquals(msg, output); } }
private void internalTestStructField(StructFieldTestCase testCase) throws Exception { TMemoryBuffer buf = new TMemoryBuffer(0); TProtocol proto = getFactory().getProtocol(buf); TField field = new TField("test_field", testCase.type_, testCase.id_); proto.writeStructBegin(new TStruct("test_struct")); proto.writeFieldBegin(field); testCase.writeMethod(proto); proto.writeFieldEnd(); proto.writeStructEnd(); proto.readStructBegin(); TField readField = proto.readFieldBegin(); assertEquals(testCase.id_, readField.id); assertEquals(testCase.type_, readField.type); testCase.readMethod(proto); proto.readStructEnd(); }
private String thriftRequest(byte[] input){ try{ //Input TMemoryBuffer inbuffer = new TMemoryBuffer(input.length); inbuffer.write(input); TProtocol inprotocol = new TJSONProtocol(inbuffer); //Output TMemoryBuffer outbuffer = new TMemoryBuffer(100); TProtocol outprotocol = new TJSONProtocol(outbuffer); TProcessor processor = new ThriftTest.Processor(new TestHandler()); processor.process(inprotocol, outprotocol); byte[] output = new byte[outbuffer.length()]; outbuffer.readAll(output, 0, output.length); return new String(output,"UTF-8"); }catch(Throwable t){ return "Error:"+t.getMessage(); } }
/** This doesn't use thrift sequence ids because scrooge doesn't */ @Override protected ThriftClientRequest makeRequest(List<byte[]> spans) throws TException { int encodedSize = InternalScribeCodec.messageSizeInBytes(category, spans); TMemoryBuffer mem = new TMemoryBuffer(encodedSize); TBinaryProtocol prot = new TBinaryProtocol(mem); InternalScribeCodec.writeLogRequest(category, spans, 0, prot); return new ThriftClientRequest(mem.getArray(), false); }
@Test public void testThriftBaseReadWrite() { TBinaryProtocol protocol = new TBinaryProtocol(new TMemoryBuffer(16)); ArgsThriftBase args = new ArgsThriftBase(testString); ResultThriftBase result = new ResultThriftBase(); try { args.write(protocol); result.read(protocol); assertEquals(args.getStr(), result.getStr(), testString); } catch (TException e) { fail(); } }
static byte[] serialize(AccessControlEntry ace) throws IOException { TMemoryBuffer transport = new TMemoryBuffer(BUFFER_SIZE); TJSONProtocol protocol = new TJSONProtocol(transport); try { ace.write(protocol); transport.flush(); return transport.toString(UTF_8.name()).getBytes(UTF_8); } catch (TException e) { throw new IOException("Failed to serialize access control entry : ", e); } catch (UnsupportedEncodingException uee) { throw new IOException("Failed to serialize acesss control entry : ", uee); } }
private void translator_test_base(TProtocolFactory protocolFactory, final String token, MessageTransalator translator) throws TException, InstantiationException, IllegalAccessException { TMemoryBuffer externalServiceBuffer = new TMemoryBufferWithLength(1024); ExternalTestService.Client externalServiceClient = new ExternalTestService.Client(protocolFactory.getProtocol(externalServiceBuffer)); externalServiceClient.send_getSomeData( new AuthToken().setToken(token).setChecksum(128), new RequestData().setSomeStringField("somevalue").setSomeIntField(8) ); TMemoryBuffer internalServiceBuffer = new TMemoryBufferWithLength(1024); InternalTestService.Client internalServiceClient = new InternalTestService.Client(protocolFactory.getProtocol(internalServiceBuffer)); internalServiceClient.send_getSomeData( new UserData().setId("user1"), new RequestData().setSomeStringField("somevalue").setSomeIntField(8) ); byte[] externalServiceMessage = Arrays.copyOf(externalServiceBuffer.getArray(), externalServiceBuffer.length()); byte[] internalServiceMessage = Arrays.copyOf(internalServiceBuffer.getArray(), internalServiceBuffer.length()); Assert.assertTrue( "Translated external message must be the same as internal message", Arrays.equals( translator.process(externalServiceMessage), internalServiceMessage ) ); }
@Test public void shouldReadStruct_thatThriftWrite() throws Exception { TMemoryBuffer transport = new TMemoryBuffer(1024); TBinaryProtocol protocol = new TBinaryProtocol(transport); com.meituan.firefly.testthrift.OrderedStruct thriftOrderedStruct = new com.meituan.firefly.testthrift.OrderedStruct(99); thriftOrderedStruct.write(protocol); StructTypeAdapterFactory.StructTypeAdapter structTypeAdapter = new StructTypeAdapterFactory.StructTypeAdapter(OrderedStruct.class, thrift); OrderedStruct fireflyOrderedStruct = (OrderedStruct) structTypeAdapter.read(protocol); assertThat(fireflyOrderedStruct).isNotNull(); assertThat(fireflyOrderedStruct.id).isEqualTo(99); }
@Test public void shouldWriteStruct_thatThriftRead() throws Exception { TMemoryBuffer transport = new TMemoryBuffer(1024); TBinaryProtocol protocol = new TBinaryProtocol(transport); OrderedStruct fireflyOrderedStruct = new OrderedStruct(); fireflyOrderedStruct.id = 99; StructTypeAdapterFactory.StructTypeAdapter structTypeAdapter = new StructTypeAdapterFactory.StructTypeAdapter(OrderedStruct.class, thrift); structTypeAdapter.write(fireflyOrderedStruct, protocol); com.meituan.firefly.testthrift.OrderedStruct thriftOrderedStruct = new com.meituan.firefly.testthrift.OrderedStruct(); thriftOrderedStruct.read(protocol); assertThat(thriftOrderedStruct.id).isEqualTo(99); }
@Test public void shouldReadMixStruct_thatThriftWrite() throws Exception { TMemoryBuffer transport = new TMemoryBuffer(1024); TBinaryProtocol protocol = new TBinaryProtocol(transport); com.meituan.firefly.testthrift.MixStruct thriftMixStruct = new com.meituan.firefly.testthrift.MixStruct(1, 2); thriftMixStruct.write(protocol); StructTypeAdapterFactory.StructTypeAdapter structTypeAdapter = new StructTypeAdapterFactory.StructTypeAdapter(MixStruct.class, thrift); MixStruct fireflyMixStruct = (MixStruct) structTypeAdapter.read(protocol); assertThat(fireflyMixStruct).isNotNull(); assertThat(fireflyMixStruct.id).isEqualTo(1); assertThat(fireflyMixStruct.uid).isEqualTo(2); }
@Test public void shouldWriteMixStruct_thatThriftRead() throws Exception { TMemoryBuffer transport = new TMemoryBuffer(1024); TBinaryProtocol protocol = new TBinaryProtocol(transport); MixStruct fireflyMixStruct = new MixStruct(); fireflyMixStruct.id = 1; fireflyMixStruct.uid = 2; StructTypeAdapterFactory.StructTypeAdapter structTypeAdapter = new StructTypeAdapterFactory.StructTypeAdapter(MixStruct.class, thrift); structTypeAdapter.write(fireflyMixStruct, protocol); com.meituan.firefly.testthrift.MixStruct thriftMixStruct = new com.meituan.firefly.testthrift.MixStruct(); thriftMixStruct.read(protocol); assertThat(thriftMixStruct.id).isEqualTo(1); assertThat(thriftMixStruct.uid).isEqualTo(2); }
@Test public void shouldReadUnion_thatThriftWrite() throws Exception { TMemoryBuffer transport = new TMemoryBuffer(1024); TBinaryProtocol protocol = new TBinaryProtocol(transport); com.meituan.firefly.testthrift.OrderedStruct thriftOrderedStruct = new com.meituan.firefly.testthrift.OrderedStruct(99); com.meituan.firefly.testthrift.UnionB thriftUnionB = new com.meituan.firefly.testthrift.UnionB(com.meituan.firefly.testthrift.UnionB._Fields.OS, thriftOrderedStruct); thriftUnionB.write(protocol); StructTypeAdapterFactory.StructTypeAdapter structTypeAdapter = new StructTypeAdapterFactory.StructTypeAdapter(UnionB.class, thrift); UnionB fireflyUnionB = (UnionB) structTypeAdapter.read(protocol); assertThat(fireflyUnionB).isNotNull(); assertThat(fireflyUnionB.os).isNotNull(); assertThat(fireflyUnionB.os.id).isEqualTo(99); }
@Nonnull public static byte[] save(@Nonnull TBase<?, ?> object) throws TException { // AutoExpandingBufferWriteTransport transport = new AutoExpandingBufferWriteTransport(4096, 1.4); TMemoryBuffer transport = new TMemoryBuffer(4096); TProtocol protocol = new TCompactProtocol(transport); object.write(protocol); // return new ByteArray(transport.getBuf().array(), 0, transport.getPos()); return Arrays.copyOf(transport.getArray(), transport.length()); }
/** * Initialize buffers. * @throws TException if buffer initialization fails */ protected void prepareMethodCall() throws TException { TMemoryBuffer memoryBuffer = new TMemoryBuffer(INITIAL_MEMORY_BUFFER_SIZE); TProtocol protocol = protocolFactory.getProtocol(memoryBuffer); write_args(protocol); int length = memoryBuffer.length(); frameBuffer = ByteBuffer.wrap(memoryBuffer.getArray(), 0, length); TFramedTransport.encodeFrameSize(length, sizeBufferArray); sizeBuffer = ByteBuffer.wrap(sizeBufferArray); }
@Override public void send(List<byte[]> spans) throws IOException { for (byte[] message : spans) { TMemoryBuffer transport = new TMemoryBuffer(message.length); try { transport.write(message); com.twitter.zipkin.gen.Span zSpan = new com.twitter.zipkin.gen.Span(); zSpan.read(new TBinaryProtocol(transport)); receivedSpans.add(zSpan); } catch (TException e) { throw new IOException(e); } } }
@Before public void before() { in = new TMemoryInputTransport(); out = new TMemoryBuffer(128); inProto = ThriftProtocolFactories.get(defaultSerializationFormat).getProtocol(in); outProto = ThriftProtocolFactories.get(defaultSerializationFormat).getProtocol(out); promise = new CompletableFuture<>(); promise2 = new CompletableFuture<>(); }
@BeforeMethod public void before() { outputMemoryBuf = new TMemoryBuffer(0); TBinaryProtocol outputBinaryProtocol = new TBinaryProtocol(new RememberingTransport(outputMemoryBuf)); outputIntegrityValidatingProtocol = new IntegrityCheckingProtocol(outputBinaryProtocol, MAC_KEYS); inputMemoryTrans = new TMemoryInputTransport(); TBinaryProtocol inputBinaryProtocol = new TBinaryProtocol(new RememberingTransport(inputMemoryTrans)); inputIntegrityValidatingProtocol = new IntegrityCheckingProtocol(inputBinaryProtocol, MAC_KEYS); }
@Test public void compactProtocolVint() throws TException { TMemoryBuffer tMemoryBuffer = writeVInt32(BytesUtils.zigzagToInt(64)); logger.debug("length:{}", tMemoryBuffer.length()); TMemoryBuffer tMemoryBuffer2 = writeVInt32(64); logger.debug("length:{}", tMemoryBuffer2.length()); }
public static void main(String[] args) throws TException { TMemoryBuffer trans = new TMemoryBuffer(4096); TProtocol proto = new TBinaryProtocol(trans); proto.writeString("Hello Thrift Serialization"); System.out.println("Wrote " + trans.length() + " bytes to the TMemoryBuffer"); String strMsg = proto.readString(); System.out.println("Recovered string: " + strMsg); }
public static void main(String[] argv) throws java.io.IOException, java.lang.InterruptedException, java.util.concurrent.TimeoutException, TException, TTransportException { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.queueDeclare(QUEUE_NAME, false, false, false, null); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(QUEUE_NAME, true, consumer); System.out.println("Waiting for trade reports..."); while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); byte[] data = delivery.getBody(); TMemoryBuffer trans = new TMemoryBuffer(data.length); trans.write(data, 0, data.length); TCompactProtocol proto = new TCompactProtocol(trans); TradeReport tr = new TradeReport(); tr.read(proto); System.out.println("[" + tr.seq_num + "] " + tr.symbol + " @ " + tr.price + " x " + tr.size); } }
@Test public void compactProtocolVint() throws TException { TMemoryBuffer tMemoryBuffer = writeVInt32(BytesUtils.zigzagToInt(64)); logger.trace("length:{}", tMemoryBuffer.length()); TMemoryBuffer tMemoryBuffer2 = writeVInt32(64); logger.trace("length:{}", tMemoryBuffer2.length()); }
@Test public void testWriteWithValue() throws Exception { TTransport tTransport = new TMemoryBuffer(100); TProtocol tProtocol = new TJSONProtocol(tTransport); Foo foo = new Foo("foo", new ArrayList<String>(), new HashSet<String>(), new HashMap<String, String>()); foo.addToOptionalList("FOO-LIST"); foo.addToOptionalSet("FOO-SET"); foo.putToOptionalMap("FOO-MAP-KEY", "FOO-MAP-VALUE"); Bar bar = new Bar(); bar.setName("bar"); bar.addToBarList("BAR-LIST"); bar.addToBarSet("BAR-SET"); bar.putToBarMap("BAR-MAP-KEY", "BAR-MAP-VALUE"); foo.setBar(bar); Foo wrapped = makeNullSafe(foo); wrapped.write(tProtocol); Foo newFoo = new Foo("new-foo", new ArrayList<String>(), new HashSet<String>(), new HashMap<String, String>()); // read from message newFoo.read(tProtocol); assertThat(newFoo.getOptionalList(), hasItem("FOO-LIST")); assertThat(newFoo.getOptionalSet(), hasItem("FOO-SET")); assertThat(newFoo.getOptionalMap(), hasEntry("FOO-MAP-KEY", "FOO-MAP-VALUE")); assertThat(newFoo.getBar(), is(notNullValue())); assertThat(newFoo.getBar().getName(), is("bar")); assertThat(newFoo.getBar().getBarList(), hasItem("BAR-LIST")); assertThat(newFoo.getBar().getBarSet(), hasItem("BAR-SET")); assertThat(newFoo.getBar().getBarMap(), hasEntry("BAR-MAP-KEY", "BAR-MAP-VALUE")); assertThat(newFoo.getBar().getBaz(), is(nullValue())); }
@Test public void testWriteSetCollectionsToEmpty() throws Exception { TTransport tTransport = new TMemoryBuffer(100); TProtocol tProtocol = new TJSONProtocol(tTransport); // original object has values in collections Foo foo = new Foo("foo", new ArrayList<String>(), new HashSet<String>(), new HashMap<String, String>()); foo.addToOptionalList("FOO-LIST"); foo.addToOptionalSet("FOO-SET"); foo.putToOptionalMap("FOO-MAP-KEY", "FOO-MAP-VALUE"); // wrap it Foo wrapped = makeNullSafe(foo); // make collections empty wrapped.getOptionalList().clear(); wrapped.getOptionalSet().clear(); wrapped.getOptionalMap().clear(); // write wrapped.write(tProtocol); Foo newFoo = new Foo("new-foo", new ArrayList<String>(), new HashSet<String>(), new HashMap<String, String>()); // read from message newFoo.read(tProtocol); // now new object should have empty collections assertThat(newFoo.getName(), is("foo")); assertThat(newFoo.getOptionalList(), is(emptyCollectionOf(String.class))); assertThat(newFoo.getOptionalSet(), is(emptyCollectionOf(String.class))); assertThat(newFoo.getOptionalMap(), is(notNullValue())); assertThat(newFoo.getOptionalMap().size(), is(0)); }
public void testServerRequest() throws Exception { Srv.Iface handler = new Srv.Iface() { public int Janky(int i32arg) throws TException { return i32arg * 2; } public int primitiveMethod() throws TException { return 0; } public CompactProtoTestStruct structMethod() throws TException { return null; } public void voidMethod() throws TException { } public void methodWithDefaultArgs(int something) throws TException { } @Override public void onewayMethod() throws TException { } }; Srv.Processor testProcessor = new Srv.Processor(handler); TMemoryBuffer clientOutTrans = new TMemoryBuffer(0); TProtocol clientOutProto = getFactory().getProtocol(clientOutTrans); TMemoryBuffer clientInTrans = new TMemoryBuffer(0); TProtocol clientInProto = getFactory().getProtocol(clientInTrans); Srv.Client testClient = new Srv.Client(clientInProto, clientOutProto); testClient.send_Janky(1); // System.out.println(clientOutTrans.inspect()); testProcessor.process(clientOutProto, clientInProto); // System.out.println(clientInTrans.inspect()); assertEquals(2, testClient.recv_Janky()); }
public void readAndCompare(TBase struct, TBase fixture, byte[] inputBytes) throws TException { TTransport trans = new TMemoryBuffer(0); trans.write(inputBytes, 0, inputBytes.length); TProtocol iprot = new TBinaryProtocol(trans); struct.read(iprot); assertEquals(fixture, struct); }
public static <T extends TBase> void testDeserialization(TProtocolFactory factory, T object, Class<T> klass) throws Exception { TMemoryBuffer buf = new TMemoryBuffer(0); object.write(factory.getProtocol(buf)); byte[] serialized = new byte[100*1024]; buf.read(serialized, 0, 100*1024); long startTime = System.currentTimeMillis(); for (int i = 0; i < HOW_MANY; i++) { T o2 = klass.newInstance(); o2.read(factory.getProtocol(new TMemoryInputTransport(serialized))); } long endTime = System.currentTimeMillis(); System.out.println("Deserialization test time: " + (endTime - startTime) + " ms"); }
@Before public void setUp() { transport = new TMemoryBuffer(1024 * 1024); protocol = new TCompactProtocol(transport); }