use of com.weibo.api.motan.rpc.DefaultProvider in project motan by weibocom.
the class ProviderProtectedMessageRouterTest method testIsAllowRequest.
@Test
public void testIsAllowRequest() {
ProviderProtectedMessageRouter providerMessageRouter = new ProviderProtectedMessageRouter();
Provider<ProviderA> providerA = new DefaultProvider<ProviderA>(new A(), new URL("injvm", "localhost", 0, ProviderA.class.getName()), ProviderA.class);
Provider<ProviderB> providerB = new DefaultProvider<ProviderB>(new B(), new URL("injvm", "localhost", 0, ProviderB.class.getName()), ProviderB.class);
Provider<ProviderC> providerC = new DefaultProvider<ProviderC>(new C(), new URL("injvm", "localhost", 0, ProviderC.class.getName()), ProviderC.class);
providerMessageRouter.addProvider(providerA);
int maxThread = 40;
for (int i = 1; i <= maxThread; i++) {
Assert.assertTrue(providerMessageRouter.isAllowRequest(i, i, maxThread, null));
}
providerMessageRouter.addProvider(providerB);
for (int i = 1; i <= (maxThread * 3 / 4); i++) {
Assert.assertTrue(providerMessageRouter.isAllowRequest(i, i, maxThread, null));
}
Assert.assertTrue(providerMessageRouter.isAllowRequest(maxThread / 2, maxThread * 3 / 4 + 1, maxThread, null));
Assert.assertFalse(providerMessageRouter.isAllowRequest(maxThread / 2 + 1, maxThread * 3 / 4 + 1, maxThread, null));
providerMessageRouter.removeProvider(providerB);
providerMessageRouter.addProvider(providerC);
for (int i = 1; i <= (maxThread * 3 / 4); i++) {
Assert.assertTrue(providerMessageRouter.isAllowRequest(i, i, maxThread, null));
}
Assert.assertTrue(providerMessageRouter.isAllowRequest(maxThread / 4, maxThread * 3 / 4 + 1, maxThread, null));
Assert.assertFalse(providerMessageRouter.isAllowRequest(maxThread / 4 + 1, maxThread * 3 / 4 + 1, maxThread, null));
}
use of com.weibo.api.motan.rpc.DefaultProvider in project motan by weibocom.
the class C method testCall.
@Test
public void testCall() {
ProviderMessageRouter providerMessageRouter = new ProviderMessageRouter();
Provider<ProviderA> providerA = new DefaultProvider<ProviderA>(new A(), new URL("injvm", "localhost", 0, ProviderA.class.getName()), ProviderA.class);
Provider<ProviderB> providerB = new DefaultProvider<ProviderB>(new B(), new URL("injvm", "localhost", 0, ProviderB.class.getName()), ProviderB.class);
providerMessageRouter.addProvider(providerA);
providerMessageRouter.addProvider(providerB);
Assert.assertEquals(providerMessageRouter.getPublicMethodCount(), PUBLIC_METHOD_COUNT_ALL);
DefaultRequest requestA = new DefaultRequest();
requestA.setInterfaceName(com.weibo.api.motan.transport.ProviderA.class.getName());
requestA.setMethodName("providerA");
requestA.setParamtersDesc(ReflectUtil.EMPTY_PARAM);
Response response = (Response) providerMessageRouter.handle(new MockChannel(TestConstants.EMPTY_URL), requestA);
Assert.assertEquals("A", response.getValue());
DefaultRequest requestB = new DefaultRequest();
requestB.setInterfaceName(com.weibo.api.motan.transport.ProviderB.class.getName());
requestB.setMethodName("providerB");
requestB.setParamtersDesc(ReflectUtil.EMPTY_PARAM);
response = (Response) providerMessageRouter.handle(new MockChannel(TestConstants.EMPTY_URL), requestB);
Assert.assertEquals("B", response.getValue());
providerMessageRouter.removeProvider(providerA);
Assert.assertEquals(providerMessageRouter.getPublicMethodCount(), PUBLIC_METHOD_COUNT_B);
try {
Response result = (Response) providerMessageRouter.handle(new MockChannel(TestConstants.EMPTY_URL), requestA);
result.getValue();
Assert.assertTrue(false);
} catch (Exception e) {
Assert.assertTrue(true);
}
}
use of com.weibo.api.motan.rpc.DefaultProvider in project motan by weibocom.
the class YarMessageRouterTest method testHandle.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testHandle() {
response = new DefaultResponse();
response.setValue("test");
response.setProcessTime(1);
Provider provider = new DefaultProvider(null, null, AnnoService.class);
router.addProvider(provider);
YarRequest yarRequest = new YarRequest(1, "JSON", "hello", new Object[] { "params" });
yarRequest.setRequestPath(requestPath);
YarResponse yarResponse = (YarResponse) router.handle(null, yarRequest);
assertEquals(YarProtocolUtil.convert(response, "JSON"), yarResponse);
}
use of com.weibo.api.motan.rpc.DefaultProvider in project motan by weibocom.
the class YarRpcProtocolTest method testInitRequestRouter.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testInitRequestRouter() {
YarRpcProtocol protocol = new YarRpcProtocol();
URL url = new URL("motan", "localhost", 8002, "urlpath");
Provider provider = new DefaultProvider(null, url, MessageHandler.class);
ProviderMessageRouter router = protocol.initRequestRouter(url, provider);
assertNotNull(router);
URL url2 = new URL("motan", "localhost", 8003, "urlpath2");
Provider provider2 = new DefaultProvider(null, url2, MessageHandler.class);
ProviderMessageRouter router2 = protocol.initRequestRouter(url2, provider2);
assertNotNull(router2);
assertFalse(router2.equals(router));
URL url3 = new URL("motan", "localhost", 8002, "urlpath3");
Provider provider3 = new DefaultProvider(null, url3, MessageHandler.class);
ProviderMessageRouter router3 = protocol.initRequestRouter(url3, provider3);
assertNotNull(router3);
assertTrue(router3.equals(router));
try {
protocol.initRequestRouter(url, provider);
assertTrue(false);
} catch (Exception e) {
assertTrue(e.getMessage().contains("duplicate yar provider"));
}
}
use of com.weibo.api.motan.rpc.DefaultProvider in project motan by weibocom.
the class YarMessageRouterTest method testAddProvider.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testAddProvider() {
Provider provider = new DefaultProvider(null, null, AnnoService.class);
router.addProvider(provider);
assertTrue(router.checkProvider(requestPath));
router.removeProvider(provider);
assertFalse(router.checkProvider(requestPath));
URL url = new URL("motan", "localhost", 8002, "urlpath");
provider = new DefaultProvider(null, url, normalService.class);
router.addProvider(provider);
assertTrue(router.checkProvider(YarProtocolUtil.getYarPath(normalService.class, url)));
router.removeProvider(provider);
assertFalse(router.checkProvider(YarProtocolUtil.getYarPath(normalService.class, url)));
}
Aggregations