use of org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler in project dubbo by alibaba.
the class HeaderExchangeHandlerTest method test_received_request_oneway.
@Test
public void test_received_request_oneway() throws RemotingException {
final Channel mchannel = new MockedChannel();
final Person requestdata = new Person("charles");
Request request = new Request();
request.setTwoWay(false);
request.setData(requestdata);
ExchangeHandler exhandler = new MockedExchangeHandler() {
@Override
public void received(Channel channel, Object message) throws RemotingException {
Assertions.assertEquals(requestdata, message);
}
};
HeaderExchangeHandler hexhandler = new HeaderExchangeHandler(exhandler);
hexhandler.received(mchannel, request);
}
use of org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler in project dubbo by alibaba.
the class HeaderExchangeHandlerTest method test_received_request_event_readonly.
@Test
public void test_received_request_event_readonly() throws RemotingException {
final Request request = new Request();
request.setTwoWay(true);
request.setEvent(READONLY_EVENT);
final Channel mchannel = new MockedChannel();
HeaderExchangeHandler hexhandler = new HeaderExchangeHandler(new MockedExchangeHandler());
hexhandler.received(mchannel, request);
Assertions.assertTrue(mchannel.hasAttribute(Constants.CHANNEL_ATTRIBUTE_READONLY_KEY));
}
use of org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler in project dubbo by alibaba.
the class HeaderExchangeHandlerTest method test_received_request_twoway.
@Test
public void test_received_request_twoway() 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.OK, res.getStatus());
Assertions.assertEquals(requestdata, res.getResult());
Assertions.assertNull(res.getErrorMessage());
count.incrementAndGet();
}
};
ExchangeHandler exhandler = new MockedExchangeHandler() {
@Override
public CompletableFuture<Object> reply(ExchangeChannel channel, Object request) throws RemotingException {
return CompletableFuture.completedFuture(request);
}
@Override
public void received(Channel channel, Object message) throws RemotingException {
Assertions.fail();
}
};
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 DubboTelnetDecodeTest method testDubboDubboDecoded.
/**
* dubbo and dubbo request
*
* <p>
* First ByteBuf (firstDubboByteBuf):
* ++-------------------------------------------------+
* || dubbo(incomplete) |
* ++-------------------------------------------------+
* ||
* Magic Code
* <p>
*
* <p>
* Second ByteBuf (secondDubboByteBuf):
* +-------------------------++-----------------------+
* | dubbo(the remaining) || dubbo(complete) |
* +-------------------------++-----------------------+
* ||
* Magic Code
*
* @throws InterruptedException
*/
@Test
public void testDubboDubboDecoded() throws InterruptedException, IOException {
ByteBuf dubboByteBuf = createDubboByteBuf();
ByteBuf firstDubboByteBuf = dubboByteBuf.copy(0, 50);
ByteBuf secondLeftDubboByteBuf = dubboByteBuf.copy(50, dubboByteBuf.readableBytes() - 50);
ByteBuf secondDubboByteBuf = Unpooled.wrappedBuffer(secondLeftDubboByteBuf, dubboByteBuf);
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(null, new MultiMessageHandler(new DecodeHandler(new HeaderExchangeHandler(new ExchangeHandlerAdapter() {
@Override
public CompletableFuture<Object> reply(ExchangeChannel channel, Object msg) {
if (checkDubboDecoded(msg)) {
dubboDubbo.incrementAndGet();
}
return getDefaultFuture();
}
}))));
ch = new LocalEmbeddedChannel();
ch.pipeline().addLast("decoder", adapter.getDecoder()).addLast("handler", mockHandler);
ch.writeInbound(firstDubboByteBuf);
ch.writeInbound(secondDubboByteBuf);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ch != null) {
ch.close().await(200, TimeUnit.MILLISECONDS);
}
}
TimeUnit.MILLISECONDS.sleep(100);
Assertions.assertEquals(2, dubboDubbo.get());
}
use of org.apache.dubbo.remoting.exchange.support.header.HeaderExchangeHandler in project dubbo by alibaba.
the class DubboTelnetDecodeTest method testTelnetDecode.
/**
* just telnet request
*
* @throws InterruptedException
*/
@Test
public void testTelnetDecode() throws InterruptedException {
ByteBuf telnetByteBuf = Unpooled.wrappedBuffer("test\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)) {
telnet.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(telnetByteBuf);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (ch != null) {
ch.close().await(200, TimeUnit.MILLISECONDS);
}
}
TimeUnit.MILLISECONDS.sleep(100);
Assertions.assertEquals(1, telnet.get());
}
Aggregations