use of org.apache.dubbo.remoting.exchange.Request in project dubbo by alibaba.
the class DefaultFutureTest method testClose.
@Test
public void testClose() throws Exception {
Channel channel = new MockedChannel();
Request request = new Request(123);
ExecutorService executor = ExtensionLoader.getExtensionLoader(ExecutorRepository.class).getDefaultExtension().createExecutorIfAbsent(URL.valueOf("dubbo://127.0.0.1:23456"));
DefaultFuture.newFuture(channel, request, 1000, executor);
DefaultFuture.closeChannel(channel);
Assertions.assertFalse(executor.isTerminated());
}
use of org.apache.dubbo.remoting.exchange.Request in project dubbo by alibaba.
the class DefaultFutureTest method timeoutSend.
/**
* for example, it will print like this:
* before a future is create , time is : 2018-06-21 15:11:31
* after a future is timeout , time is : 2018-06-21 15:11:36
* <p>
* The exception info print like:
* Waiting server-side response timeout by scan timer.
* start time: 2018-06-21 15:12:38.337, end time: 2018-06-21 15:12:43.354...
*/
@Test
@Disabled
public void timeoutSend() 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 5 seconds.
Channel channel = new MockedChannel();
Request request = new Request(10);
DefaultFuture f = DefaultFuture.newFuture(channel, request, 5000, null);
// mark the future is sent
DefaultFuture.sent(channel, request);
while (!f.isDone()) {
// spin
Thread.sleep(100);
}
System.out.println("after a future is timeout , time is : " + LocalDateTime.now().format(formatter));
// get operate will throw a timeout exception, because the future is timeout.
try {
f.get();
} catch (Exception e) {
Assertions.assertTrue(e.getCause() instanceof TimeoutException, "catch exception is not timeout exception!");
System.out.println(e.getMessage());
}
}
use of org.apache.dubbo.remoting.exchange.Request in project dubbo by alibaba.
the class HeartBeatTaskTest method testHeartBeat.
@Test
public void testHeartBeat() throws Exception {
long now = System.currentTimeMillis();
url = url.addParameter(DUBBO_VERSION_KEY, "2.1.1");
channel.setAttribute(HeartbeatHandler.KEY_READ_TIMESTAMP, now);
channel.setAttribute(HeartbeatHandler.KEY_WRITE_TIMESTAMP, now);
heartbeatTimer.newTimeout(heartbeatTimerTask, 250, TimeUnit.MILLISECONDS);
Thread.sleep(2000L);
List<Object> objects = channel.getSentObjects();
Assertions.assertTrue(objects.size() > 0);
Object obj = objects.get(0);
Assertions.assertTrue(obj instanceof Request);
Request request = (Request) obj;
Assertions.assertTrue(request.isHeartbeat());
}
use of org.apache.dubbo.remoting.exchange.Request in project dubbo by alibaba.
the class ExchangeCodecTest method testMessageLengthGreaterThanMessageActualLength.
@Test
public void testMessageLengthGreaterThanMessageActualLength() throws Exception {
Channel channel = getCliendSideChannel(url);
Request request = new Request(1L);
request.setVersion(Version.getProtocolVersion());
Date date = new Date();
request.setData(date);
ChannelBuffer encodeBuffer = ChannelBuffers.dynamicBuffer(1024);
codec.encode(channel, encodeBuffer, request);
byte[] bytes = new byte[encodeBuffer.writerIndex()];
encodeBuffer.readBytes(bytes);
int len = Bytes.bytes2int(bytes, 12);
ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
out.write(bytes, 0, 12);
/*
* The fill length can not be less than 256, because by default, hessian reads 256 bytes from the stream each time.
* Refer Hessian2Input.readBuffer for more details
*/
int padding = 512;
out.write(Bytes.int2bytes(len + padding));
out.write(bytes, 16, bytes.length - 16);
for (int i = 0; i < padding; i++) {
out.write(1);
}
out.write(bytes);
/* request|1111...|request */
ChannelBuffer decodeBuffer = ChannelBuffers.wrappedBuffer(out.toByteArray());
Request decodedRequest = (Request) codec.decode(channel, decodeBuffer);
Assertions.assertEquals(date, decodedRequest.getData());
Assertions.assertEquals(bytes.length + padding, decodeBuffer.readerIndex());
decodedRequest = (Request) codec.decode(channel, decodeBuffer);
Assertions.assertEquals(date, decodedRequest.getData());
}
use of org.apache.dubbo.remoting.exchange.Request in project dubbo by alibaba.
the class ExchangeCodecTest method test_Decode_Return_Request_Heartbeat_Object.
@Test
public void test_Decode_Return_Request_Heartbeat_Object() throws IOException {
// |10011111|20-stats=ok|id=0|length=0
byte[] header = new byte[] { MAGIC_HIGH, MAGIC_LOW, (byte) 0xe2, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
byte[] request = getRequestBytes(null, header);
Request obj = (Request) decode(request);
Assertions.assertNull(obj.getData());
Assertions.assertTrue(obj.isTwoWay());
Assertions.assertTrue(obj.isHeartbeat());
Assertions.assertEquals(Version.getProtocolVersion(), obj.getVersion());
System.out.println(obj);
}
Aggregations