use of com.alipay.sofa.rpc.test.generic.bean.People in project sofa-rpc by sofastack.
the class GenericTest method createMyComplexBean.
/**
* 构建自定义扩展对象构成bean
*/
private ComplexBean createMyComplexBean() {
ComplexBean complexBean = new ComplexBean();
complexBean.setStrs(new String[] { null, "123", null });
complexBean.setClazz(MyList.class);
complexBean.setJobs(new Job[] { null, new Job("coder"), null });
ArrayList list = new MyList();
list.add(null);
list.add(1);
list.add(null);
complexBean.setList(list);
MyMap map = new MyMap();
map.put("1", new Job("coder"));
map.put(1, new People("wang", null));
complexBean.setMap(map);
return complexBean;
}
use of com.alipay.sofa.rpc.test.generic.bean.People in project sofa-rpc by sofastack.
the class GenericTest method testAll.
@Test
public void testAll() throws SofaRpcException, InterruptedException {
// 发布服务
ServerConfig serverConfig2 = new ServerConfig().setPort(22222).setDaemon(false);
List<MethodConfig> methodConfigs = new ArrayList<MethodConfig>();
MethodConfig config1 = new MethodConfig().setName("helloFuture").setInvokeType("future");
methodConfigs.add(config1);
MethodConfig config2 = new MethodConfig().setName("helloCallback").setInvokeType("callback").setOnReturn(new TestCallback());
methodConfigs.add(config2);
MethodConfig config21 = new MethodConfig().setName("helloCallbackException").setInvokeType("callback").setOnReturn(new TestCallback());
methodConfigs.add(config21);
MethodConfig config3 = new MethodConfig().setName("helloOneway").setInvokeType("oneway");
methodConfigs.add(config3);
MethodConfig config4 = new MethodConfig().setName("helloTimeout").setInvokeType("sync");
methodConfigs.add(config4);
// C服务的服务端
ProviderConfig<TestInterface> CProvider = new ProviderConfig<TestInterface>().setInterfaceId(TestInterface.class.getName()).setRef(new TestClass()).setServer(serverConfig2);
CProvider.export();
// 引用服务
ConsumerConfig<GenericService> BConsumer = new ConsumerConfig<GenericService>().setInterfaceId(TestInterface.class.getName()).setGeneric(true).setMethods(methodConfigs).setDirectUrl("bolt://127.0.0.1:22222").setTimeout(3000).setRetries(2);
GenericService proxy = BConsumer.refer();
GenericObject genericObject = new GenericObject("com.alipay.sofa.rpc.test.generic.bean.People");
genericObject.putField("name", "Lilei");
genericObject.putField("job", new Job("coder"));
People people = new People();
people.setName("Lilei");
people.setJob(new Job("coder"));
// sync 调用
assertEquals(proxy.$invoke("hello", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { people }), new TestClass().hello(people));
People peopleResult = proxy.$genericInvoke("hello", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { genericObject }, People.class);
assertEquals(peopleResult, new TestClass().hello(people));
GenericObject result = (GenericObject) proxy.$genericInvoke("hello", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { genericObject });
isCorrect(result);
// future 调用
proxy.$invoke("helloFuture", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { people });
assertEquals(SofaResponseFuture.getResponse(1000, true), new TestClass().helloFuture(people));
proxy.$genericInvoke("helloFuture", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { genericObject }, People.class);
assertEquals(SofaResponseFuture.getResponse(1000, true), new TestClass().helloFuture(people));
proxy.$genericInvoke("helloFuture", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { genericObject });
result = (GenericObject) SofaResponseFuture.getResponse(1000, true);
isCorrect(result);
// callback调用
proxy.$invoke("helloCallback", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { people });
TestCallback.startLatach();
assertEquals(TestCallback.result, new TestClass().helloCallback(people));
proxy.$genericInvoke("helloCallback", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { genericObject }, People.class);
TestCallback.startLatach();
assertEquals(TestCallback.result, new TestClass().helloCallback(people));
proxy.$genericInvoke("helloCallback", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { genericObject });
TestCallback.startLatach();
isCorrect((GenericObject) TestCallback.result);
TestCallback.result = null;
// oneway调用
proxy.$invoke("helloOneway", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { people });
proxy.$genericInvoke("helloOneway", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { genericObject }, People.class);
proxy.$genericInvoke("helloOneway", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { genericObject });
// callback出现异常
proxy.$invoke("helloCallbackException", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { people });
TestCallback.startLatach();
Assert.assertEquals(((Throwable) TestCallback.result).getMessage(), "Hello~");
proxy.$genericInvoke("helloCallbackException", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { genericObject }, People.class);
TestCallback.startLatach();
Assert.assertEquals(((Throwable) TestCallback.result).getMessage(), "Hello~");
proxy.$genericInvoke("helloCallbackException", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { genericObject });
TestCallback.startLatach();
Assert.assertEquals(((Throwable) TestCallback.result).getMessage(), "Hello~");
testTimeout(proxy, genericObject, people);
testComplexBean(proxy);
testBasicBean(proxy);
}
use of com.alipay.sofa.rpc.test.generic.bean.People in project sofa-rpc by sofastack.
the class GenericTest method testTimeout.
private void testTimeout(final GenericService proxy, GenericObject genericObject, People people) {
// 1. 构造GenericContext 对象
GenericContext genericContext = new GenericContext();
genericContext.setClientTimeout(5000);
// 2. 未指定参数,发生超时
boolean isSuccess = true;
try {
proxy.$genericInvoke("helloTimeout", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { people });
} catch (Exception e) {
isSuccess = false;
}
Assert.assertFalse(isSuccess);
// 3. 指定超时,结果序列化为People 类
People peopleResult = proxy.$genericInvoke("helloTimeout", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { genericObject }, People.class, genericContext);
assertEquals(peopleResult, people);
// 4. 指定超时,结果序列化为GenericObject
GenericObject result = (GenericObject) proxy.$genericInvoke("helloTimeout", new String[] { "com.alipay.sofa.rpc.test.generic.bean.People" }, new Object[] { genericObject }, genericContext);
isCorrect(result);
}
Aggregations