Search in sources :

Example 11 with ServiceConfig

use of com.alibaba.dubbo.config.ServiceConfig in project dubbo by alibaba.

the class CacheTest method testCache.

@Test
public void testCache() throws Exception {
    ServiceConfig<CacheService> service = new ServiceConfig<CacheService>();
    service.setApplication(new ApplicationConfig("cache-provider"));
    service.setRegistry(new RegistryConfig("N/A"));
    service.setProtocol(new ProtocolConfig("dubbo", 29582));
    service.setInterface(CacheService.class.getName());
    service.setRef(new CacheServiceImpl());
    service.export();
    try {
        ReferenceConfig<CacheService> reference = new ReferenceConfig<CacheService>();
        reference.setApplication(new ApplicationConfig("cache-consumer"));
        reference.setInterface(CacheService.class);
        reference.setUrl("dubbo://127.0.0.1:29582?scope=remote&cache=true");
        CacheService cacheService = reference.get();
        try {
            // 测试缓存生效,多次调用返回同样的结果。(服务器端自增长返回值)
            String fix = null;
            for (int i = 0; i < 3; i++) {
                String result = cacheService.findCache("0");
                Assert.assertTrue(fix == null || fix.equals(result));
                fix = result;
                Thread.sleep(100);
            }
            // LRU的缺省cache.size为1000,执行1001次,应有溢出
            for (int n = 0; n < 1001; n++) {
                String pre = null;
                for (int i = 0; i < 10; i++) {
                    String result = cacheService.findCache(String.valueOf(n));
                    Assert.assertTrue(pre == null || pre.equals(result));
                    pre = result;
                }
            }
            // 测试LRU有移除最开始的一个缓存项
            String result = cacheService.findCache("0");
            Assert.assertFalse(fix == null || fix.equals(result));
        } finally {
            reference.destroy();
        }
    } finally {
        service.unexport();
    }
}
Also used : RegistryConfig(com.alibaba.dubbo.config.RegistryConfig) ServiceConfig(com.alibaba.dubbo.config.ServiceConfig) ApplicationConfig(com.alibaba.dubbo.config.ApplicationConfig) ReferenceConfig(com.alibaba.dubbo.config.ReferenceConfig) ProtocolConfig(com.alibaba.dubbo.config.ProtocolConfig) Test(org.junit.Test)

Example 12 with ServiceConfig

use of com.alibaba.dubbo.config.ServiceConfig in project dubbo by alibaba.

the class ConfigTest method testGenericServiceConfig.

@Test
public void testGenericServiceConfig() throws Exception {
    ServiceConfig<GenericService> service = new ServiceConfig<GenericService>();
    service.setApplication(new ApplicationConfig("test"));
    service.setRegistry(new RegistryConfig("mock://localhost"));
    service.setInterface(DemoService.class.getName());
    service.setGeneric(Constants.GENERIC_SERIALIZATION_BEAN);
    service.setRef(new GenericService() {

        public Object $invoke(String method, String[] parameterTypes, Object[] args) throws GenericException {
            return null;
        }
    });
    try {
        service.export();
        Collection<Registry> collection = MockRegistryFactory.getCachedRegistry();
        MockRegistry registry = (MockRegistry) collection.iterator().next();
        URL url = registry.getRegistered().get(0);
        Assert.assertEquals(Constants.GENERIC_SERIALIZATION_BEAN, url.getParameter(Constants.GENERIC_KEY));
    } finally {
        MockRegistryFactory.cleanCachedRegistry();
        service.unexport();
    }
}
Also used : RegistryConfig(com.alibaba.dubbo.config.RegistryConfig) GenericService(com.alibaba.dubbo.rpc.service.GenericService) MockRegistry(com.alibaba.dubbo.config.spring.registry.MockRegistry) DemoService(com.alibaba.dubbo.config.spring.api.DemoService) JUnitMatchers.containsString(org.junit.matchers.JUnitMatchers.containsString) Registry(com.alibaba.dubbo.registry.Registry) MockRegistry(com.alibaba.dubbo.config.spring.registry.MockRegistry) GenericException(com.alibaba.dubbo.rpc.service.GenericException) URL(com.alibaba.dubbo.common.URL) ServiceConfig(com.alibaba.dubbo.config.ServiceConfig) ApplicationConfig(com.alibaba.dubbo.config.ApplicationConfig) Test(org.junit.Test)

Example 13 with ServiceConfig

use of com.alibaba.dubbo.config.ServiceConfig in project dubbo by alibaba.

the class ConfigTest method testReferGenericExport.

@Test
public void testReferGenericExport() throws Exception {
    ApplicationConfig ac = new ApplicationConfig("test-refer-generic-export");
    RegistryConfig rc = new RegistryConfig();
    rc.setAddress(RegistryConfig.NO_AVAILABLE);
    ServiceConfig<GenericService> sc = new ServiceConfig<GenericService>();
    sc.setApplication(ac);
    sc.setRegistry(rc);
    sc.setInterface(DemoService.class.getName());
    sc.setRef(new GenericService() {

        public Object $invoke(String method, String[] parameterTypes, Object[] args) throws GenericException {
            return null;
        }
    });
    ReferenceConfig<DemoService> ref = new ReferenceConfig<DemoService>();
    ref.setApplication(ac);
    ref.setRegistry(rc);
    ref.setInterface(DemoService.class.getName());
    try {
        sc.export();
        ref.get();
        Assert.fail();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        sc.unexport();
        ref.destroy();
    }
}
Also used : RegistryConfig(com.alibaba.dubbo.config.RegistryConfig) GenericService(com.alibaba.dubbo.rpc.service.GenericService) DemoService(com.alibaba.dubbo.config.spring.api.DemoService) JUnitMatchers.containsString(org.junit.matchers.JUnitMatchers.containsString) GenericException(com.alibaba.dubbo.rpc.service.GenericException) BeanCreationException(org.springframework.beans.factory.BeanCreationException) GenericException(com.alibaba.dubbo.rpc.service.GenericException) BeansException(org.springframework.beans.BeansException) RpcException(com.alibaba.dubbo.rpc.RpcException) ApplicationConfig(com.alibaba.dubbo.config.ApplicationConfig) ServiceConfig(com.alibaba.dubbo.config.ServiceConfig) ReferenceConfig(com.alibaba.dubbo.config.ReferenceConfig) Test(org.junit.Test)

Example 14 with ServiceConfig

use of com.alibaba.dubbo.config.ServiceConfig in project dubbo by alibaba.

the class ConfigTest method testSystemPropertyOverrideProperties.

@Test
public void testSystemPropertyOverrideProperties() throws Exception {
    String portString = System.getProperty("dubbo.protocol.port");
    System.clearProperty("dubbo.protocol.port");
    try {
        int port = 1234;
        System.setProperty("dubbo.protocol.port", String.valueOf(port));
        ApplicationConfig application = new ApplicationConfig();
        application.setName("aaa");
        RegistryConfig registry = new RegistryConfig();
        registry.setAddress("N/A");
        ProtocolConfig protocol = new ProtocolConfig();
        protocol.setName("rmi");
        ServiceConfig<DemoService> service = new ServiceConfig<DemoService>();
        service.setInterface(DemoService.class);
        service.setRef(new DemoServiceImpl());
        service.setApplication(application);
        service.setRegistry(registry);
        service.setProtocol(protocol);
        service.export();
        try {
            URL url = service.toUrls().get(0);
            // from api
            assertEquals("aaa", url.getParameter("application"));
            // from dubbo.properties
            assertEquals("world", url.getParameter("owner"));
            // from system property
            assertEquals(1234, url.getPort());
        } finally {
            service.unexport();
        }
    } finally {
        if (portString != null) {
            System.setProperty("dubbo.protocol.port", portString);
        }
    }
}
Also used : RegistryConfig(com.alibaba.dubbo.config.RegistryConfig) ApplicationConfig(com.alibaba.dubbo.config.ApplicationConfig) ServiceConfig(com.alibaba.dubbo.config.ServiceConfig) DemoService(com.alibaba.dubbo.config.spring.api.DemoService) JUnitMatchers.containsString(org.junit.matchers.JUnitMatchers.containsString) ProtocolConfig(com.alibaba.dubbo.config.ProtocolConfig) URL(com.alibaba.dubbo.common.URL) DemoServiceImpl(com.alibaba.dubbo.config.spring.impl.DemoServiceImpl) Test(org.junit.Test)

Example 15 with ServiceConfig

use of com.alibaba.dubbo.config.ServiceConfig in project dubbo by alibaba.

the class ConfigTest method testSystemPropertyOverrideApiDefault.

@Test
public void testSystemPropertyOverrideApiDefault() throws Exception {
    System.setProperty("dubbo.application.name", "sysover");
    System.setProperty("dubbo.application.owner", "sysowner");
    System.setProperty("dubbo.registry.address", "N/A");
    System.setProperty("dubbo.protocol.name", "dubbo");
    System.setProperty("dubbo.protocol.port", "20834");
    try {
        ServiceConfig<DemoService> serviceConfig = new ServiceConfig<DemoService>();
        serviceConfig.setInterface(DemoService.class);
        serviceConfig.setRef(new DemoServiceImpl());
        serviceConfig.export();
        try {
            assertEquals("sysover", serviceConfig.getApplication().getName());
            assertEquals("sysowner", serviceConfig.getApplication().getOwner());
            assertEquals("N/A", serviceConfig.getRegistry().getAddress());
            assertEquals("dubbo", serviceConfig.getProtocol().getName());
            assertEquals(20834, serviceConfig.getProtocol().getPort().intValue());
        } finally {
            serviceConfig.unexport();
        }
    } finally {
        System.setProperty("dubbo.application.name", "");
        System.setProperty("dubbo.application.owner", "");
        System.setProperty("dubbo.registry.address", "");
        System.setProperty("dubbo.protocol.name", "");
        System.setProperty("dubbo.protocol.port", "");
    }
}
Also used : ServiceConfig(com.alibaba.dubbo.config.ServiceConfig) DemoService(com.alibaba.dubbo.config.spring.api.DemoService) DemoServiceImpl(com.alibaba.dubbo.config.spring.impl.DemoServiceImpl) Test(org.junit.Test)

Aggregations

ServiceConfig (com.alibaba.dubbo.config.ServiceConfig)16 Test (org.junit.Test)16 RegistryConfig (com.alibaba.dubbo.config.RegistryConfig)11 DemoService (com.alibaba.dubbo.config.spring.api.DemoService)11 ApplicationConfig (com.alibaba.dubbo.config.ApplicationConfig)10 ProtocolConfig (com.alibaba.dubbo.config.ProtocolConfig)9 ReferenceConfig (com.alibaba.dubbo.config.ReferenceConfig)8 URL (com.alibaba.dubbo.common.URL)7 DemoServiceImpl (com.alibaba.dubbo.config.spring.impl.DemoServiceImpl)6 RpcException (com.alibaba.dubbo.rpc.RpcException)4 JUnitMatchers.containsString (org.junit.matchers.JUnitMatchers.containsString)4 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)4 GenericService (com.alibaba.dubbo.rpc.service.GenericService)3 Date (java.util.Date)3 GenericException (com.alibaba.dubbo.rpc.service.GenericException)2 ConsumerConfig (com.alibaba.dubbo.config.ConsumerConfig)1 ProviderConfig (com.alibaba.dubbo.config.ProviderConfig)1 MockRegistry (com.alibaba.dubbo.config.spring.registry.MockRegistry)1 Registry (com.alibaba.dubbo.registry.Registry)1 HashMap (java.util.HashMap)1