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();
}
}
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();
}
}
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();
}
}
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);
}
}
}
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", "");
}
}
Aggregations