use of org.apache.dubbo.remoting.handler.MockedChannel 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.handler.MockedChannel 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);
}
use of org.apache.dubbo.remoting.handler.MockedChannel 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.handler.MockedChannel 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());
}
}
Aggregations