Search in sources :

Example 31 with ApplicationConfig

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);
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest) Test(org.junit.Test)

Example 32 with ApplicationConfig

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);
}
Also used : ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) Reporter(com.alipay.common.tracer.core.reporter.facade.Reporter) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) JSONObject(com.alibaba.fastjson.JSONObject) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HashSet(java.util.HashSet) Test(org.junit.Test) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest)

Example 33 with ApplicationConfig

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));
}
Also used : ServerConfig(com.alipay.sofa.rpc.config.ServerConfig) ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) ProviderConfig(com.alipay.sofa.rpc.config.ProviderConfig) HelloService(com.alipay.sofa.rpc.test.HelloService) HelloServiceImpl(com.alipay.sofa.rpc.test.HelloServiceImpl) ConsumerConfig(com.alipay.sofa.rpc.config.ConsumerConfig) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) SofaRpcException(com.alipay.sofa.rpc.core.exception.SofaRpcException) Callable(java.util.concurrent.Callable)

Example 34 with ApplicationConfig

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();
}
Also used : ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) HelloService(com.alipay.sofa.rpc.test.HelloService) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest) Test(org.junit.Test)

Example 35 with ApplicationConfig

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));
}
Also used : ApplicationConfig(com.alipay.sofa.rpc.config.ApplicationConfig) HelloService(com.alipay.sofa.rpc.test.HelloService) ActivelyDestroyTest(com.alipay.sofa.rpc.test.ActivelyDestroyTest) Test(org.junit.Test)

Aggregations

ApplicationConfig (com.alipay.sofa.rpc.config.ApplicationConfig)113 ServerConfig (com.alipay.sofa.rpc.config.ServerConfig)77 ConsumerConfig (com.alipay.sofa.rpc.config.ConsumerConfig)64 Test (org.junit.Test)53 ProviderConfig (com.alipay.sofa.rpc.config.ProviderConfig)52 HelloService (com.alipay.sofa.rpc.test.HelloService)30 ActivelyDestroyTest (com.alipay.sofa.rpc.test.ActivelyDestroyTest)21 CountDownLatch (java.util.concurrent.CountDownLatch)20 HelloServiceImpl (com.alipay.sofa.rpc.test.HelloServiceImpl)15 MethodConfig (com.alipay.sofa.rpc.config.MethodConfig)14 SofaRpcException (com.alipay.sofa.rpc.core.exception.SofaRpcException)14 SofaResponseCallback (com.alipay.sofa.rpc.core.invoke.SofaResponseCallback)13 ArrayList (java.util.ArrayList)12 RegistryConfig (com.alipay.sofa.rpc.config.RegistryConfig)11 RequestBase (com.alipay.sofa.rpc.core.request.RequestBase)11 ProviderGroup (com.alipay.sofa.rpc.client.ProviderGroup)10 EchoService (com.alipay.sofa.rpc.test.EchoService)10 EchoRequest (com.alipay.sofa.rpc.server.bolt.pb.EchoRequest)8 DemoServiceImpl (com.alipay.sofa.rpc.bootstrap.dubbo.demo.DemoServiceImpl)7 Filter (com.alipay.sofa.rpc.filter.Filter)7