Search in sources :

Example 1 with MockChannel

use of com.weibo.api.motan.mock.MockChannel 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 2 with MockChannel

use of com.weibo.api.motan.mock.MockChannel 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 MockChannel

use of com.weibo.api.motan.mock.MockChannel in project motan by weibocom.

the class DefaultRpcCodecTest method main.

public static void main(String[] args) throws Exception {
    DefaultRpcCodec codec = new DefaultRpcCodec();
    MockChannel channel = new MockChannel(new URL("motan", "localhost", 18080, IHello.class.getName()));
    System.out.println("requestSize: " + requestSize(codec, channel, null).length);
    System.out.println("responseSize: " + responseSize(codec, channel, null).length);
    System.out.println("responseSize: " + exceptionResponseSize(codec, channel).length);
    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < 200; i++) {
        builder.append("1");
    }
    String value = builder.toString();
    String[] arr = new String[] { value, value, value };
    Long[] sets = new Long[20];
    for (int i = 0; i < 20; i++) {
        sets[i] = 1000000000L + i;
    }
    System.out.println("requestSize 1k: " + requestSize(codec, channel, arr).length);
    System.out.println("responseSize 1k: " + responseSize(codec, channel, arr).length);
    byte[] data = null;
    for (int i = 0; i < loop; i++) {
        data = requestSize(codec, channel, sets);
        codec.decode(channel, "", data);
    }
    long start = System.nanoTime();
    for (int i = 0; i < loop; i++) {
        data = requestSize(codec, channel, sets);
    }
    System.out.println("request encode performance: " + (System.nanoTime() - start) / loop + " ns");
    start = System.nanoTime();
    for (int i = 0; i < loop; i++) {
        codec.decode(channel, "", data);
    }
    System.out.println("request decode performance: " + (System.nanoTime() - start) / loop + " ns");
}
Also used : MockChannel(com.weibo.api.motan.mock.MockChannel) DefaultRpcCodec(com.weibo.api.motan.protocol.rpc.DefaultRpcCodec) URL(com.weibo.api.motan.rpc.URL)

Aggregations

MockChannel (com.weibo.api.motan.mock.MockChannel)3 URL (com.weibo.api.motan.rpc.URL)3 DefaultRequest (com.weibo.api.motan.rpc.DefaultRequest)2 Test (org.junit.Test)2 DefaultRpcCodec (com.weibo.api.motan.protocol.rpc.DefaultRpcCodec)1 DefaultProvider (com.weibo.api.motan.rpc.DefaultProvider)1 Response (com.weibo.api.motan.rpc.Response)1 Channel (com.weibo.api.motan.transport.Channel)1