Search in sources :

Example 1 with DefaultProvider

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));
}
Also used : URL(com.weibo.api.motan.rpc.URL) DefaultProvider(com.weibo.api.motan.rpc.DefaultProvider) Test(org.junit.Test)

Example 2 with DefaultProvider

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);
    }
}
Also used : MockChannel(com.weibo.api.motan.mock.MockChannel) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) URL(com.weibo.api.motan.rpc.URL) Response(com.weibo.api.motan.rpc.Response) DefaultProvider(com.weibo.api.motan.rpc.DefaultProvider) Test(org.junit.Test)

Example 3 with DefaultProvider

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);
}
Also used : DefaultResponse(com.weibo.api.motan.rpc.DefaultResponse) YarRequest(com.weibo.yar.YarRequest) YarResponse(com.weibo.yar.YarResponse) DefaultProvider(com.weibo.api.motan.rpc.DefaultProvider) DefaultProvider(com.weibo.api.motan.rpc.DefaultProvider) Provider(com.weibo.api.motan.rpc.Provider) Test(org.junit.Test)

Example 4 with DefaultProvider

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"));
    }
}
Also used : ProviderMessageRouter(com.weibo.api.motan.transport.ProviderMessageRouter) DefaultProvider(com.weibo.api.motan.rpc.DefaultProvider) URL(com.weibo.api.motan.rpc.URL) DefaultProvider(com.weibo.api.motan.rpc.DefaultProvider) Provider(com.weibo.api.motan.rpc.Provider) Test(org.junit.Test)

Example 5 with DefaultProvider

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)));
}
Also used : DefaultProvider(com.weibo.api.motan.rpc.DefaultProvider) URL(com.weibo.api.motan.rpc.URL) DefaultProvider(com.weibo.api.motan.rpc.DefaultProvider) Provider(com.weibo.api.motan.rpc.Provider) Test(org.junit.Test)

Aggregations

DefaultProvider (com.weibo.api.motan.rpc.DefaultProvider)5 Test (org.junit.Test)5 URL (com.weibo.api.motan.rpc.URL)4 Provider (com.weibo.api.motan.rpc.Provider)3 MockChannel (com.weibo.api.motan.mock.MockChannel)1 DefaultRequest (com.weibo.api.motan.rpc.DefaultRequest)1 DefaultResponse (com.weibo.api.motan.rpc.DefaultResponse)1 Response (com.weibo.api.motan.rpc.Response)1 ProviderMessageRouter (com.weibo.api.motan.transport.ProviderMessageRouter)1 YarRequest (com.weibo.yar.YarRequest)1 YarResponse (com.weibo.yar.YarResponse)1