Search in sources :

Example 31 with Channel

use of org.apache.dubbo.remoting.Channel in project dubbo by alibaba.

the class ThriftCodecTest method testEncodeReplyResponse.

@Test
public void testEncodeReplyResponse() throws Exception {
    int port = NetUtils.getAvailablePort();
    URL url = URL.valueOf(ThriftProtocol.NAME + "://127.0.0.1:" + port + "/" + Demo.Iface.class.getName());
    Channel channel = new MockedChannel(url);
    Request request = createRequest();
    AppResponse appResponse = new AppResponse();
    appResponse.setValue("Hello, World!");
    Response response = new Response();
    response.setResult(appResponse);
    response.setId(request.getId());
    ChannelBuffer bos = ChannelBuffers.dynamicBuffer(1024);
    ThriftCodec.RequestData rd = ThriftCodec.RequestData.create(ThriftCodec.getSeqId(), Demo.Iface.class.getName(), "echoString");
    ThriftCodec.CACHED_REQUEST.putIfAbsent(request.getId(), rd);
    codec.encode(channel, bos, response);
    byte[] buf = new byte[bos.writerIndex() - 4];
    System.arraycopy(bos.array(), 4, buf, 0, bos.writerIndex() - 4);
    ByteArrayInputStream bis = new ByteArrayInputStream(buf);
    if (bis.markSupported()) {
        bis.mark(0);
    }
    TIOStreamTransport transport = new TIOStreamTransport(bis);
    TBinaryProtocol protocol = new TBinaryProtocol(transport);
    Assertions.assertEquals(ThriftCodec.MAGIC, protocol.readI16());
    Assertions.assertEquals(protocol.readI32() + 4, bos.writerIndex());
    int headerLength = protocol.readI16();
    Assertions.assertEquals(ThriftCodec.VERSION, protocol.readByte());
    Assertions.assertEquals(Demo.Iface.class.getName(), protocol.readString());
    Assertions.assertEquals(request.getId(), protocol.readI64());
    if (bis.markSupported()) {
        bis.reset();
        bis.skip(headerLength);
    }
    TMessage message = protocol.readMessageBegin();
    Assertions.assertEquals("echoString", message.name);
    Assertions.assertEquals(TMessageType.REPLY, message.type);
    // Assertions.assertEquals(ThriftCodec.getSeqId(), message.seqid);
    Demo.echoString_result result = new Demo.echoString_result();
    result.read(protocol);
    protocol.readMessageEnd();
    Assertions.assertEquals(appResponse.getValue(), result.getSuccess());
}
Also used : Demo(org.apache.dubbo.rpc.gen.thrift.Demo) Channel(org.apache.dubbo.remoting.Channel) Request(org.apache.dubbo.remoting.exchange.Request) TIOStreamTransport(org.apache.thrift.transport.TIOStreamTransport) URL(org.apache.dubbo.common.URL) ChannelBuffer(org.apache.dubbo.remoting.buffer.ChannelBuffer) AppResponse(org.apache.dubbo.rpc.AppResponse) Response(org.apache.dubbo.remoting.exchange.Response) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) ByteArrayInputStream(java.io.ByteArrayInputStream) TMessage(org.apache.thrift.protocol.TMessage) AppResponse(org.apache.dubbo.rpc.AppResponse) Test(org.junit.jupiter.api.Test)

Example 32 with Channel

use of org.apache.dubbo.remoting.Channel in project dubbo by alibaba.

the class ThriftCodecTest method testEncodeExceptionResponse.

@Test
public void testEncodeExceptionResponse() throws Exception {
    int port = NetUtils.getAvailablePort();
    URL url = URL.valueOf(ThriftProtocol.NAME + "://127.0.0.1:" + port + "/" + Demo.Iface.class.getName());
    Channel channel = new MockedChannel(url);
    Request request = createRequest();
    AppResponse appResponse = new AppResponse();
    String exceptionMessage = "failed";
    appResponse.setException(new RuntimeException(exceptionMessage));
    Response response = new Response();
    response.setResult(appResponse);
    response.setId(request.getId());
    ChannelBuffer bos = ChannelBuffers.dynamicBuffer(1024);
    ThriftCodec.RequestData rd = ThriftCodec.RequestData.create(ThriftCodec.getSeqId(), Demo.Iface.class.getName(), "echoString");
    ThriftCodec.CACHED_REQUEST.put(request.getId(), rd);
    codec.encode(channel, bos, response);
    byte[] buf = new byte[bos.writerIndex() - 4];
    System.arraycopy(bos.array(), 4, buf, 0, bos.writerIndex() - 4);
    ByteArrayInputStream bis = new ByteArrayInputStream(buf);
    if (bis.markSupported()) {
        bis.mark(0);
    }
    TIOStreamTransport transport = new TIOStreamTransport(bis);
    TBinaryProtocol protocol = new TBinaryProtocol(transport);
    Assertions.assertEquals(ThriftCodec.MAGIC, protocol.readI16());
    Assertions.assertEquals(protocol.readI32() + 4, bos.writerIndex());
    int headerLength = protocol.readI16();
    Assertions.assertEquals(ThriftCodec.VERSION, protocol.readByte());
    Assertions.assertEquals(Demo.Iface.class.getName(), protocol.readString());
    Assertions.assertEquals(request.getId(), protocol.readI64());
    if (bis.markSupported()) {
        bis.reset();
        bis.skip(headerLength);
    }
    TMessage message = protocol.readMessageBegin();
    Assertions.assertEquals("echoString", message.name);
    Assertions.assertEquals(TMessageType.EXCEPTION, message.type);
    Assertions.assertEquals(ThriftCodec.getSeqId(), message.seqid);
    TApplicationException exception = TApplicationException.readFrom(protocol);
    protocol.readMessageEnd();
    Assertions.assertEquals(exceptionMessage, exception.getMessage());
}
Also used : Demo(org.apache.dubbo.rpc.gen.thrift.Demo) Channel(org.apache.dubbo.remoting.Channel) Request(org.apache.dubbo.remoting.exchange.Request) TIOStreamTransport(org.apache.thrift.transport.TIOStreamTransport) URL(org.apache.dubbo.common.URL) ChannelBuffer(org.apache.dubbo.remoting.buffer.ChannelBuffer) TApplicationException(org.apache.thrift.TApplicationException) AppResponse(org.apache.dubbo.rpc.AppResponse) Response(org.apache.dubbo.remoting.exchange.Response) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) ByteArrayInputStream(java.io.ByteArrayInputStream) TMessage(org.apache.thrift.protocol.TMessage) AppResponse(org.apache.dubbo.rpc.AppResponse) Test(org.junit.jupiter.api.Test)

Example 33 with Channel

use of org.apache.dubbo.remoting.Channel in project dubbo by alibaba.

the class TraceFilter method addTracer.

public static void addTracer(Class<?> type, String method, Channel channel, int max) {
    channel.setAttribute(TRACE_MAX, max);
    channel.setAttribute(TRACE_COUNT, new AtomicInteger());
    String key = method != null && method.length() > 0 ? type.getName() + "." + method : type.getName();
    Set<Channel> channels = TRACERS.computeIfAbsent(key, k -> new ConcurrentHashSet<>());
    channels.add(channel);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Channel(org.apache.dubbo.remoting.Channel)

Example 34 with Channel

use of org.apache.dubbo.remoting.Channel in project dubbo by alibaba.

the class TraceFilterTest method testInvoke.

@Test
public void testInvoke() throws Exception {
    String method = "sayHello";
    Class<?> type = DemoService.class;
    String key = type.getName() + "." + method;
    // add tracer
    TraceFilter.addTracer(type, method, mockChannel, 2);
    Invoker<DemoService> mockInvoker = mock(Invoker.class);
    Invocation mockInvocation = mock(Invocation.class);
    Result mockResult = mock(Result.class);
    TraceFilter filter = new TraceFilter();
    given(mockInvoker.getInterface()).willReturn(DemoService.class);
    given(mockInvocation.getMethodName()).willReturn(method);
    given(mockInvocation.getArguments()).willReturn(new Object[0]);
    given(mockInvoker.invoke(mockInvocation)).willReturn(mockResult);
    given(mockResult.getValue()).willReturn("result");
    // test invoke
    filter.invoke(mockInvoker, mockInvocation);
    String message = listToString(mockChannel.getReceivedObjects());
    String expectMessage = "org.apache.dubbo.rpc.protocol.dubbo.support.DemoService.sayHello([]) -> \"result\"";
    System.out.println("actual message: " + message);
    Assertions.assertTrue(message.contains(expectMessage));
    Assertions.assertTrue(message.contains("elapsed:"));
    AtomicInteger traceCount = (AtomicInteger) mockChannel.getAttribute(TRACE_COUNT);
    Assertions.assertEquals(1, traceCount.get());
    // test remove channel when count >= max - 1
    filter.invoke(mockInvoker, mockInvocation);
    Field tracers = TraceFilter.class.getDeclaredField(TRACERS_FIELD_NAME);
    tracers.setAccessible(true);
    ConcurrentHashMap<String, Set<Channel>> o = (ConcurrentHashMap<String, Set<Channel>>) tracers.get(new ConcurrentHashMap<String, Set<Channel>>());
    Assertions.assertTrue(o.containsKey(key));
    Set<Channel> channels = o.get(key);
    Assertions.assertNotNull(channels);
    Assertions.assertFalse(channels.contains(mockChannel));
}
Also used : Set(java.util.Set) Invocation(org.apache.dubbo.rpc.Invocation) Channel(org.apache.dubbo.remoting.Channel) DemoService(org.apache.dubbo.rpc.protocol.dubbo.support.DemoService) Result(org.apache.dubbo.rpc.Result) Field(java.lang.reflect.Field) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.jupiter.api.Test)

Example 35 with Channel

use of org.apache.dubbo.remoting.Channel in project dubbo by alibaba.

the class HelpTelnetHandlerTest method test.

@Test
public void test() {
    Channel channel = Mockito.mock(Channel.class);
    Mockito.when(channel.getUrl()).thenReturn(URL.valueOf("dubbo://127.0.0.1:12345"));
    HelpTelnetHandler helpTelnetHandler = new HelpTelnetHandler();
    // default output
    String prompt = "Please input \"help [command]\" show detail.\r\n";
    Assertions.assertTrue(helpTelnetHandler.telnet(channel, "").contains(prompt));
    // "help" command output
    String demoOutput = "Command:\r\n" + "    help [command]\r\n" + "Summary:\r\n" + "    Show help.\r\n" + "Detail:\r\n" + "    Show help.";
    Assertions.assertEquals(helpTelnetHandler.telnet(channel, "help"), demoOutput);
}
Also used : HelpTelnetHandler(org.apache.dubbo.remoting.telnet.support.command.HelpTelnetHandler) Channel(org.apache.dubbo.remoting.Channel) Test(org.junit.jupiter.api.Test)

Aggregations

Channel (org.apache.dubbo.remoting.Channel)53 Test (org.junit.jupiter.api.Test)32 Request (org.apache.dubbo.remoting.exchange.Request)18 ChannelBuffer (org.apache.dubbo.remoting.buffer.ChannelBuffer)16 Response (org.apache.dubbo.remoting.exchange.Response)12 ExchangeChannel (org.apache.dubbo.remoting.exchange.ExchangeChannel)9 URL (org.apache.dubbo.common.URL)8 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)7 HeaderExchangeHandler (org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler)6 RemotingException (org.apache.dubbo.remoting.RemotingException)5 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 ExchangeHandler (org.apache.dubbo.remoting.exchange.ExchangeHandler)4 MockedChannel (org.apache.dubbo.remoting.handler.MockedChannel)4 AppResponse (org.apache.dubbo.rpc.AppResponse)4 Demo (org.apache.dubbo.rpc.gen.thrift.Demo)4 TBinaryProtocol (org.apache.thrift.protocol.TBinaryProtocol)4 TMessage (org.apache.thrift.protocol.TMessage)4 TIOStreamTransport (org.apache.thrift.transport.TIOStreamTransport)4 IOException (java.io.IOException)3