use of com.alipay.sofa.rpc.config.ApplicationConfig in project sofa-rpc by sofastack.
the class RestProviderTest method testProvider.
@Test
public void testProvider() {
JAXRSProviderManager.registerCustomProviderInstance(new CustomerInjectorFactory());
JAXRSProviderManager.registerCustomProviderInstance(new ClientRequestTestFilter());
JAXRSProviderManager.registerCustomProviderInstance(new ClientRequestTestFilter2());
JAXRSProviderManager.registerCustomProviderInstance(new ContainerRequestTestFilter());
JAXRSProviderManager.registerCustomProviderInstance(new ContainerResponseTestFilter());
JAXRSProviderManager.registerCustomProviderInstance(new ClientResponseTestFilter());
ServerConfig serverConfig = new ServerConfig().setStopTimeout(60000).setPort(8803).setProtocol(RpcConstants.PROTOCOL_TYPE_REST);
ProviderConfig<RestService> providerConfig = new ProviderConfig<RestService>().setInterfaceId(RestService.class.getName()).setRef(new RestServiceImpl()).setServer(serverConfig).setBootstrap("rest").setRegister(false);
providerConfig.export();
ConsumerConfig<RestService> consumerConfig = new ConsumerConfig<RestService>().setInterfaceId(RestService.class.getName()).setDirectUrl("rest://127.0.0.1:8803").setProtocol("rest").setBootstrap("rest").setTimeout(30000).setRegister(false).setFilter(Arrays.asList("-*")).setApplication(new ApplicationConfig().setAppName("TestClient"));
RestService restService = consumerConfig.refer();
Assert.assertEquals("serverok", restService.get("ok"));
String nameA = ClientRequestTestFilter.getName();
String nameA2 = ClientRequestTestFilter2.getName();
String nameB = ContainerRequestTestFilter.getName();
String nameC = ContainerResponseTestFilter.getName();
String nameD = ClientResponseTestFilter.getName();
Assert.assertEquals("AtestInjecttestInject", nameA);
Assert.assertEquals("A2", nameA2);
Assert.assertEquals("B", nameB);
Assert.assertEquals("C", nameC);
Assert.assertEquals("D", nameD);
}
use of com.alipay.sofa.rpc.config.ApplicationConfig in project sofa-rpc by sofastack.
the class RestTracerTest method testRestTracer.
@Test
public void testRestTracer() throws InterruptedException, IOException {
Reporter clientReporter = reflectToTracer();
memoryReporter = (MemoryReporterImpl) clientReporter;
ServerConfig restServer = new ServerConfig().setPort(8583).setProtocol(RpcConstants.PROTOCOL_TYPE_REST);
List<ServerConfig> servers = new ArrayList<ServerConfig>(2);
servers.add(restServer);
ProviderConfig<RestService> providerConfig = new ProviderConfig<RestService>().setInterfaceId(RestService.class.getName()).setRef(new RestServiceImpl()).setRegister(false).setServer(servers);
providerConfig.export();
// rest服务
ConsumerConfig<RestService> consumerConfigRest = new ConsumerConfig<RestService>().setInterfaceId(RestService.class.getName()).setProtocol(RpcConstants.PROTOCOL_TYPE_REST).setDirectUrl("rest://127.0.0.1:8583").setTimeout(1000).setApplication(new ApplicationConfig().setAppName("TestClientRest"));
final RestService restServiceRest = consumerConfigRest.refer();
restServiceRest.get("test");
final int times = 10;
final CountDownLatch latch = new CountDownLatch(times);
final AtomicInteger success = new AtomicInteger(0);
for (int i = 0; i < times; i++) {
new Thread(new Runnable() {
@Override
public void run() {
try {
for (int j = 0; j < times; j++) {
final String ok_rest = restServiceRest.get("ok_rest");
Assert.assertEquals("serverok_rest", ok_rest);
success.incrementAndGet();
}
} catch (Throwable e) {
e.printStackTrace();
} finally {
latch.countDown();
}
}
}).start();
}
latch.await();
Assert.assertEquals(times * times, success.get());
TimeUnit.SECONDS.sleep(5);
List<String> clientDigestContents = memoryReporter.getClientDigestHolder();
List<String> serverDigestContents = memoryReporter.getServerDigestHolder();
List<JSONObject> clientDigest = TracerChecker.convertContents2Json(clientDigestContents);
List<String> clientTraceIds = readTraceId(clientDigest);
List<JSONObject> serverDigest = TracerChecker.convertContents2Json(serverDigestContents);
List<String> serverTraceIds = readTraceId(serverDigest);
Assert.assertTrue(CommonUtils.isNotEmpty(clientTraceIds));
Assert.assertTrue(CommonUtils.isNotEmpty(serverTraceIds));
HashSet<String> hashSet = new HashSet<String>(200);
for (String clientTraceId : clientTraceIds) {
// will not duplicate
Assert.assertTrue(!hashSet.contains(clientTraceId));
hashSet.add(clientTraceId);
Assert.assertTrue(serverTraceIds.contains(clientTraceId));
}
// validate one rpc server and rpc client field
boolean result = TracerChecker.validateTracerDigest(clientDigest.get(0), "client", RpcConstants.PROTOCOL_TYPE_REST);
Assert.assertTrue(result);
result = TracerChecker.validateTracerDigest(serverDigest.get(0), "server", RpcConstants.PROTOCOL_TYPE_REST);
Assert.assertTrue(result);
}
use of com.alipay.sofa.rpc.config.ApplicationConfig in project sofa-rpc by sofastack.
the class BoltDirectUrlTest method testAll.
// @Test
// FIXME 目前bolt的IO线程关闭时未释放,暂不支持本测试用例
public void testAll() {
// 只有2个线程 执行
ServerConfig serverConfig = new ServerConfig().setPort(12300).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setDaemon(true);
// 发布一个服务,每个请求要执行1秒
ProviderConfig<HelloService> providerConfig = new ProviderConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setRef(new HelloServiceImpl()).setBootstrap("bolt").setApplication(new ApplicationConfig().setAppName("serverApp")).setServer(serverConfig).setRegister(false);
providerConfig.export();
final ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setDirectUrl("bolt://127.0.0.1:12300").setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setBootstrap("bolt").setApplication(new ApplicationConfig().setAppName("clientApp")).setReconnectPeriod(1000);
HelloService helloService = consumerConfig.refer();
Assert.assertNotNull(helloService.sayHello("xx", 22));
serverConfig.getServer().stop();
// 关闭后再调用一个抛异常
try {
helloService.sayHello("xx", 22);
} catch (Exception e) {
// 应该抛出异常
Assert.assertTrue(e instanceof SofaRpcException);
}
Assert.assertTrue(TestUtils.delayGet(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return CommonUtils.isEmpty(consumerConfig.getConsumerBootstrap().getCluster().getConnectionHolder().getAvailableConnections());
}
}, true, 50, 40));
serverConfig.getServer().start();
// 等待客户端重连服务端
Assert.assertTrue(TestUtils.delayGet(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return CommonUtils.isNotEmpty(consumerConfig.getConsumerBootstrap().getCluster().getConnectionHolder().getAvailableConnections());
}
}, true, 50, 60));
Assert.assertNotNull(helloService.sayHello("xx", 22));
}
use of com.alipay.sofa.rpc.config.ApplicationConfig in project sofa-rpc by sofastack.
the class BoltMockTest method testRemote.
@Test
public void testRemote() {
HttpMockServer.initSever(1235);
HttpMockServer.addMockPath("/", JSON.toJSONString("mockJson"));
HttpMockServer.start();
final ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setBootstrap("bolt").setApplication(new ApplicationConfig().setAppName("clientApp")).setReconnectPeriod(1000).setMockMode(MockMode.REMOTE).setParameter("mockUrl", "http://127.0.0.1:1235/");
HelloService helloService = consumerConfig.refer();
Assert.assertEquals("mockJson", helloService.sayHello("xx", 22));
HttpMockServer.stop();
}
use of com.alipay.sofa.rpc.config.ApplicationConfig in project sofa-rpc by sofastack.
the class BoltMockTest method testLocal.
@Test
public void testLocal() {
final ConsumerConfig<HelloService> consumerConfig = new ConsumerConfig<HelloService>().setInterfaceId(HelloService.class.getName()).setProtocol(RpcConstants.PROTOCOL_TYPE_BOLT).setBootstrap("bolt").setApplication(new ApplicationConfig().setAppName("clientApp")).setReconnectPeriod(1000).setMockMode("local").setMockRef(new HelloService() {
@Override
public String sayHello(String name, int age) {
return "mock";
}
});
HelloService helloService = consumerConfig.refer();
Assert.assertEquals("mock", helloService.sayHello("xx", 22));
}
Aggregations