Search in sources :

Example 11 with DefaultRequest

use of com.weibo.api.motan.rpc.DefaultRequest in project motan by weibocom.

the class CompressRpcCodecTest method testGzip.

// 测试gz压缩
@Test
public void testGzip() throws IOException {
    DefaultRequest request = getRequest("int[]", new Object[] { new int[] { 1, 2 } });
    byte[] bytes = rpcCodec.encode(channel, request);
    assertFalse(isGzip(bytes));
    // 小于阈值
    int bodyLength = ByteUtil.bytes2int(bytes, 12);
    URL url = new URL("motan", "localhost", 18080, "com.weibo.api.motan.procotol.example.IHello");
    Map<String, String> params = url.getParameters();
    params.put(URLParamType.usegz.name(), "true");
    params.put(URLParamType.mingzSize.name(), String.valueOf(bodyLength - 1));
    Channel tempChannel = new MockChannel(url);
    bytes = rpcCodec.encode(tempChannel, request);
    assertTrue(isGzip(bytes));
    // 等于、大于阈值.url内部对数字类型参数有缓存,且无法动态更新,需要重新生产url及channel
    url = new URL("motan", "localhost", 18080, "com.weibo.api.motan.procotol.example.IHello");
    params = url.getParameters();
    params.put(URLParamType.usegz.name(), "true");
    params.put(URLParamType.mingzSize.name(), String.valueOf(bodyLength));
    tempChannel = new MockChannel(url);
    bytes = rpcCodec.encode(tempChannel, request);
    assertFalse(isGzip(bytes));
}
Also used : MockChannel(com.weibo.api.motan.mock.MockChannel) DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) MockChannel(com.weibo.api.motan.mock.MockChannel) Channel(com.weibo.api.motan.transport.Channel) URL(com.weibo.api.motan.rpc.URL) Test(org.junit.Test)

Example 12 with DefaultRequest

use of com.weibo.api.motan.rpc.DefaultRequest in project motan by weibocom.

the class CompressRpcCodecTest method testSwitcher.

// 测试开关有效性
@Test
public void testSwitcher() throws IOException {
    DefaultRequest request = getRequest("void", null);
    byte[] bytes = rpcCodec.encode(channel, request);
    assertTrue(isCompressVersion(bytes));
    // 整体开关测试
    MotanSwitcherUtil.setSwitcherValue(CompressRpcCodec.CODEC_VERSION_SWITCHER, true);
    bytes = rpcCodec.encode(channel, request);
    assertTrue(isV1Version(bytes));
    // 分组开关测试
    MotanSwitcherUtil.setSwitcherValue(CompressRpcCodec.CODEC_VERSION_SWITCHER, false);
    MotanSwitcherUtil.setSwitcherValue(CompressRpcCodec.GROUP_CODEC_VERSION_SWITCHER + URLParamType.group.getValue(), true);
    bytes = rpcCodec.encode(channel, request);
    assertTrue(isV1Version(bytes));
}
Also used : DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) Test(org.junit.Test)

Example 13 with DefaultRequest

use of com.weibo.api.motan.rpc.DefaultRequest in project motan by weibocom.

the class CompressRpcCodecTest method getRequest.

protected DefaultRequest getRequest(String paramtersDesc, Object[] params) {
    DefaultRequest request = new DefaultRequest();
    request.setInterfaceName(basicInterface);
    request.setMethodName(basicMethod);
    request.setParamtersDesc(paramtersDesc);
    if (params != null) {
        request.setArguments(params);
    }
    Map<String, String> attachmentsMap = new HashMap<String, String>();
    attachmentsMap.put(URLParamType.group.name(), URLParamType.group.getValue());
    attachmentsMap.put(URLParamType.version.name(), URLParamType.version.getValue());
    attachmentsMap.put(URLParamType.module.name(), URLParamType.module.getValue());
    attachmentsMap.put(URLParamType.application.name(), URLParamType.application.getValue());
    request.setAttachments(attachmentsMap);
    addMethodSign(request, (CompressRpcCodec) rpcCodec);
    return request;
}
Also used : DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest) HashMap(java.util.HashMap)

Example 14 with DefaultRequest

use of com.weibo.api.motan.rpc.DefaultRequest 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 15 with DefaultRequest

use of com.weibo.api.motan.rpc.DefaultRequest in project motan by weibocom.

the class DefaultRpcCodecTest method getRequest.

// 获取基础request,不包括请求方法和参数描述,使用默认接口类和分组
protected DefaultRequest getRequest(String paramtersDesc, Object[] params) {
    DefaultRequest request = new DefaultRequest();
    request.setInterfaceName(basicInterface);
    request.setMethodName(basicMethod);
    request.setParamtersDesc(paramtersDesc);
    if (params != null) {
        request.setArguments(params);
    }
    return request;
}
Also used : DefaultRequest(com.weibo.api.motan.rpc.DefaultRequest)

Aggregations

DefaultRequest (com.weibo.api.motan.rpc.DefaultRequest)45 Test (org.junit.Test)26 DefaultResponse (com.weibo.api.motan.rpc.DefaultResponse)8 Request (com.weibo.api.motan.rpc.Request)7 Model (com.weibo.api.motan.protocol.example.Model)5 Response (com.weibo.api.motan.rpc.Response)5 MotanServiceException (com.weibo.api.motan.exception.MotanServiceException)4 Channel (com.weibo.api.motan.transport.Channel)4 MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)3 IHello (com.weibo.api.motan.protocol.example.IHello)3 URL (com.weibo.api.motan.rpc.URL)3 MessageHandler (com.weibo.api.motan.transport.MessageHandler)3 HashMap (java.util.HashMap)3 MockChannel (com.weibo.api.motan.mock.MockChannel)2 MockReferer (com.weibo.api.motan.mock.MockReferer)2 Referer (com.weibo.api.motan.rpc.Referer)2 Listener (io.grpc.ServerCall.Listener)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Codec (com.weibo.api.motan.codec.Codec)1 Serialization (com.weibo.api.motan.codec.Serialization)1