Thrift { public <T> T create(Class<T> service, TProtocolFactory protocolFactory, Interceptor... interceptors) { return create(service, protocolFactory, null, interceptors); } private  Thrift(); void setDefaultValue(boolean defaultValue); boolean hasDefaultValue(); TypeAdapter getAdapter(Type type); T create(Class<T> service, TProtocolFactory protocolFactory, Interceptor... interceptors); T create(Class<T> service, TProtocolFactory protocolFactory, Scheduler subscribScheduler, Interceptor... interceptors); static final Thrift instance; }
@Test public void shouldCreateService() { TestService service = thrift.create(TestService.class, new Thrift.SimpleTProtocolFactory() { @Override public TProtocol get() { return new TBinaryProtocol(new TMemoryInputTransport(new byte[]{})); } }); assertThat(service).isNotNull(); }
@Test public void testInterceptorOrder() throws Exception { AtomicInteger ai = new AtomicInteger(); OrderCheckInterceptor interceptor1 = new OrderCheckInterceptor(ai); OrderCheckInterceptor interceptor2 = new OrderCheckInterceptor(ai); final TTransport transport = new FlushableMemoryBuffer(4096); com.meituan.firefly.testfirefly.TestService testService = thrift.create(com.meituan.firefly.testfirefly.TestService.class, new Thrift.SimpleTProtocolFactory() { @Override public TProtocol get() { return new TBinaryProtocol(transport); } }, interceptor1, interceptor2); NotifyCheck notifyCheck = new NotifyCheck(100); com.meituan.firefly.testthrift.TestService.Processor<Iface> processor = new com.meituan.firefly.testthrift.TestService.Processor<Iface>(notifyCheck); testService.notify(100); processor.process(new TBinaryProtocol(transport), new TBinaryProtocol(transport)); Assert.assertTrue(notifyCheck.notified); Assertions.assertThat(new int[]{interceptor2.inorder, interceptor1.inorder, interceptor1.outorder, interceptor2.outorder}).isEqualTo(new int[]{0, 1, 2, 3}); }
@Test public void testInterceptorAbort() throws Exception { final TTransport transport = new FlushableMemoryBuffer(4096); final com.meituan.firefly.testfirefly.TestService testService = thrift.create(com.meituan.firefly.testfirefly.TestService.class, new Thrift.SimpleTProtocolFactory() { @Override public TProtocol get() { return new TBinaryProtocol(transport); } }, new Interceptor() { @Override public Object intercept(Method method, Object[] args, TProtocol protocol, int seqId, Processor processor) throws Throwable { return null; } }); NotifyCheck notifyCheck = new NotifyCheck(100); com.meituan.firefly.testthrift.TestService.Processor<Iface> processor = new com.meituan.firefly.testthrift.TestService.Processor<Iface>(notifyCheck); testService.notify(100); try { processor.process(new TBinaryProtocol(transport), new TBinaryProtocol(transport)); Assert.assertFalse(notifyCheck.notified); } catch (TException e) { } Assertions.assertThat(notifyCheck.notified).isFalse(); }