use of org.apache.dubbo.remoting.exchange.Request 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.exchange.Request in project dubbo by alibaba.
the class DubboTelnetDecodeTest method createDubboByteBuf.
private ByteBuf createDubboByteBuf() throws IOException {
Request request = new Request();
RpcInvocation rpcInvocation = new RpcInvocation();
rpcInvocation.setMethodName("sayHello");
rpcInvocation.setParameterTypes(new Class[] { String.class });
rpcInvocation.setParameterTypesDesc(ReflectUtils.getDesc(new Class[] { String.class }));
rpcInvocation.setArguments(new String[] { "dubbo" });
rpcInvocation.setAttachment("path", DemoService.class.getName());
rpcInvocation.setAttachment("interface", DemoService.class.getName());
rpcInvocation.setAttachment("version", "0.0.0");
request.setData(rpcInvocation);
request.setVersion("2.0.2");
ByteBuf dubboByteBuf = Unpooled.buffer();
ChannelBuffer buffer = new NettyBackedChannelBuffer(dubboByteBuf);
DubboCodec dubboCodec = new DubboCodec();
dubboCodec.encode(new MockChannel(), buffer, request);
return dubboByteBuf;
}
use of org.apache.dubbo.remoting.exchange.Request in project dubbo by alibaba.
the class HeaderExchangeChannelTest method requestTest02.
@Test
public void requestTest02() throws RemotingException {
Channel channel = Mockito.mock(MockChannel.class);
header = new HeaderExchangeChannel(channel);
when(channel.getUrl()).thenReturn(url);
Object requestob = new Object();
header.request(requestob);
ArgumentCaptor<Request> argumentCaptor = ArgumentCaptor.forClass(Request.class);
verify(channel, times(1)).send(argumentCaptor.capture());
Assertions.assertEquals(argumentCaptor.getValue().getData(), requestob);
}
use of org.apache.dubbo.remoting.exchange.Request in project dubbo by alibaba.
the class DefaultFutureTest method interruptSend.
/**
* for example, it will print like this:
*before a future is create , time is : 2021-01-22 10:55:03
* null
* after a future is timeout , time is : 2021-01-22 10:55:05
*/
@Test
public void interruptSend() throws Exception {
final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
System.out.println("before a future is create , time is : " + LocalDateTime.now().format(formatter));
// timeout after 1 seconds.
Channel channel = new MockedChannel();
int channelId = 10;
Request request = new Request(channelId);
ExecutorService sharedExecutor = ExtensionLoader.getExtensionLoader(ExecutorRepository.class).getDefaultExtension().createExecutorIfAbsent(URL.valueOf("dubbo://127.0.0.1:23456"));
ThreadlessExecutor executor = new ThreadlessExecutor(sharedExecutor);
DefaultFuture f = DefaultFuture.newFuture(channel, request, 1000, executor);
// mark the future is sent
DefaultFuture.sent(channel, request);
// get operate will throw a interrupted exception, because the thread is interrupted.
try {
new InterruptThread(Thread.currentThread()).start();
executor.waitAndDrain();
f.get();
} catch (Exception e) {
Assertions.assertTrue(e instanceof InterruptedException, "catch exception is not interrupted exception!");
System.out.println(e.getMessage());
}
// waiting timeout check task finished
Thread.sleep(1500);
System.out.println("after a future is timeout , time is : " + LocalDateTime.now().format(formatter));
DefaultFuture future = DefaultFuture.getFuture(channelId);
// waiting future should be removed by time out check task
Assertions.assertNull(future);
}
use of org.apache.dubbo.remoting.exchange.Request in project dubbo by alibaba.
the class DefaultFutureTest method defaultFuture.
/**
* mock a default future
*/
private DefaultFuture defaultFuture(int timeout) {
Channel channel = new MockedChannel();
Request request = new Request(index.getAndIncrement());
return DefaultFuture.newFuture(channel, request, timeout, null);
}
Aggregations