use of org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler in project dubbo by alibaba.
the class HeaderExchangeHandlerTest method test_received_request_twoway_error_reqeustBroken.
@Test
public void test_received_request_twoway_error_reqeustBroken() throws RemotingException {
final Request request = new Request();
request.setTwoWay(true);
request.setData(new BizException());
request.setBroken(true);
final AtomicInteger count = new AtomicInteger(0);
final Channel mchannel = new MockedChannel() {
@Override
public void send(Object message) throws RemotingException {
Response res = (Response) message;
Assertions.assertEquals(request.getId(), res.getId());
Assertions.assertEquals(request.getVersion(), res.getVersion());
Assertions.assertEquals(Response.BAD_REQUEST, res.getStatus());
Assertions.assertNull(res.getResult());
Assertions.assertTrue(res.getErrorMessage().contains(BizException.class.getName()));
count.incrementAndGet();
}
};
HeaderExchangeHandler hexhandler = new HeaderExchangeHandler(new MockedExchangeHandler());
hexhandler.received(mchannel, request);
Assertions.assertEquals(1, count.get());
}
use of org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler in project dubbo by alibaba.
the class HeaderExchangeHandlerTest method test_received_request_twoway_error_reply.
@Test
public void test_received_request_twoway_error_reply() throws RemotingException {
final Person requestdata = new Person("charles");
final Request request = new Request();
request.setTwoWay(true);
request.setData(requestdata);
final AtomicInteger count = new AtomicInteger(0);
final Channel mchannel = new MockedChannel() {
@Override
public void send(Object message) throws RemotingException {
Response res = (Response) message;
Assertions.assertEquals(request.getId(), res.getId());
Assertions.assertEquals(request.getVersion(), res.getVersion());
Assertions.assertEquals(Response.SERVICE_ERROR, res.getStatus());
Assertions.assertNull(res.getResult());
Assertions.assertTrue(res.getErrorMessage().contains(BizException.class.getName()));
count.incrementAndGet();
}
};
ExchangeHandler exhandler = new MockedExchangeHandler() {
@Override
public CompletableFuture<Object> reply(ExchangeChannel channel, Object request) throws RemotingException {
throw new BizException();
}
};
HeaderExchangeHandler hexhandler = new HeaderExchangeHandler(exhandler);
hexhandler.received(mchannel, request);
Assertions.assertEquals(1, count.get());
}
use of org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler in project dubbo by alibaba.
the class HeaderExchangeHandlerTest method test_received_request_event_other_discard.
@Test
public void test_received_request_event_other_discard() throws RemotingException {
final Request request = new Request();
request.setTwoWay(true);
request.setEvent("my event");
final Channel mchannel = new MockedChannel() {
@Override
public void send(Object message) throws RemotingException {
Assertions.fail();
}
};
HeaderExchangeHandler hexhandler = new HeaderExchangeHandler(new MockedExchangeHandler() {
@Override
public CompletableFuture<Object> reply(ExchangeChannel channel, Object request) throws RemotingException {
Assertions.fail();
throw new RemotingException(channel, "");
}
@Override
public void received(Channel channel, Object message) throws RemotingException {
Assertions.fail();
throw new RemotingException(channel, "");
}
});
hexhandler.received(mchannel, request);
}
use of org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler in project dubbo by alibaba.
the class DubboTelnetDecodeTest method testTelnetTelnetDecoded.
/**
* NOTE: This test case actually will fail, but the probability of this case is very small,
* and users should use telnet in new QOS port(default port is 22222) since dubbo 2.5.8,
* so we could ignore this problem.
*
* <p>
* telnet and telnet request
*
* <p>
* First ByteBuf (firstByteBuf):
* +--------------------------------------------------+
* | telnet(incomplete) |
* +--------------------------------------------------+
* <p>
*
* Second ByteBuf (secondByteBuf):
* +--------------------------------------------------+
* | telnet(the remaining) | telnet(complete) |
* +--------------------------------------------------+
*
* @throws InterruptedException
*/
@Disabled
@Test
public void testTelnetTelnetDecoded() throws InterruptedException {
ByteBuf firstByteBuf = Unpooled.wrappedBuffer("ls\r".getBytes());
ByteBuf secondByteBuf = Unpooled.wrappedBuffer("\nls\r\n".getBytes());
EmbeddedChannel ch = null;
try {
Codec2 codec = ExtensionLoader.getExtensionLoader(Codec2.class).getExtension("dubbo");
URL url = new URL("dubbo", "localhost", 22226);
NettyCodecAdapter adapter = new NettyCodecAdapter(codec, url, new MockChannelHandler());
MockHandler mockHandler = new MockHandler((msg) -> {
if (checkTelnetDecoded(msg)) {
telnetTelnet.incrementAndGet();
}
}, new MultiMessageHandler(new DecodeHandler(new HeaderExchangeHandler(new ExchangeHandlerAdapter() {
@Override
public CompletableFuture<Object> reply(ExchangeChannel channel, Object msg) {
return getDefaultFuture();
}
}))));
ch = new LocalEmbeddedChannel();
ch.pipeline().addLast("decoder", adapter.getDecoder()).addLast("handler", mockHandler);
ch.writeInbound(firstByteBuf);
ch.writeInbound(secondByteBuf);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ch != null) {
ch.close().await(200, TimeUnit.MILLISECONDS);
}
}
TimeUnit.MILLISECONDS.sleep(100);
Assertions.assertEquals(2, telnetTelnet.get());
}
use of org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler in project dubbo by alibaba.
the class DubboTelnetDecodeTest method testTelnetDubboDecoded.
/**
* telnet and dubbo request
*
* <p>
* First ByteBuf:
* +--------------------------------------------------+
* | telnet(incomplete) |
* +--------------------------------------------------+
* <p>
*
* Second ByteBuf:
* +--------------------------++----------------------+
* | telnet(the remaining) || dubbo(complete) |
* +--------------------------++----------------------+
* ||
* Magic Code
*
* @throws InterruptedException
*/
@Test
public void testTelnetDubboDecoded() throws InterruptedException, IOException {
ByteBuf dubboByteBuf = createDubboByteBuf();
ByteBuf telnetByteBuf = Unpooled.wrappedBuffer("test\r".getBytes());
EmbeddedChannel ch = null;
try {
Codec2 codec = ExtensionLoader.getExtensionLoader(Codec2.class).getExtension("dubbo");
URL url = new URL("dubbo", "localhost", 22226);
NettyCodecAdapter adapter = new NettyCodecAdapter(codec, url, new MockChannelHandler());
MockHandler mockHandler = new MockHandler((msg) -> {
if (checkTelnetDecoded(msg)) {
telnetDubbo.incrementAndGet();
}
}, new MultiMessageHandler(new DecodeHandler(new HeaderExchangeHandler(new ExchangeHandlerAdapter() {
@Override
public CompletableFuture<Object> reply(ExchangeChannel channel, Object msg) {
if (checkDubboDecoded(msg)) {
telnetDubbo.incrementAndGet();
}
return getDefaultFuture();
}
}))));
ch = new LocalEmbeddedChannel();
ch.pipeline().addLast("decoder", adapter.getDecoder()).addLast("handler", mockHandler);
ch.writeInbound(telnetByteBuf);
ch.writeInbound(Unpooled.wrappedBuffer(Unpooled.wrappedBuffer("\n".getBytes()), dubboByteBuf));
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ch != null) {
ch.close().await(200, TimeUnit.MILLISECONDS);
}
}
TimeUnit.MILLISECONDS.sleep(100);
Assertions.assertEquals(2, telnetDubbo.get());
}
Aggregations