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());
}
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());
}
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);
}
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));
}
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);
}
Aggregations