use of com.alibaba.dubbo.config.ApplicationConfig 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.ApplicationConfig 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.ApplicationConfig in project dubbo by alibaba.
the class ConfigTest method testDubboProtocolPortOverride.
@Test
public void testDubboProtocolPortOverride() throws Exception {
String dubboPort = System.getProperty("dubbo.protocol.dubbo.port");
int port = 55555;
System.setProperty("dubbo.protocol.dubbo.port", String.valueOf(port));
ServiceConfig<DemoService> service = null;
try {
ApplicationConfig application = new ApplicationConfig();
application.setName("dubbo-protocol-port-override");
RegistryConfig registry = new RegistryConfig();
registry.setAddress("N/A");
ProtocolConfig protocol = new ProtocolConfig();
service = new ServiceConfig<DemoService>();
service.setInterface(DemoService.class);
service.setRef(new DemoServiceImpl());
service.setApplication(application);
service.setRegistry(registry);
service.setProtocol(protocol);
service.export();
Assert.assertEquals(port, service.getExportedUrls().get(0).getPort());
} finally {
if (StringUtils.isNotEmpty(dubboPort)) {
System.setProperty("dubbo.protocol.dubbo.port", dubboPort);
}
if (service != null) {
service.unexport();
}
}
}
use of com.alibaba.dubbo.config.ApplicationConfig 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.ApplicationConfig 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);
}
}
}
Aggregations