testNioHandlerCanBeDrivenByMainThread() — netty Function Reference
Architecture documentation for the testNioHandlerCanBeDrivenByMainThread() function in NioIoHandlerTest.java from the netty codebase.
Entity Profile
Dependency Diagram
graph TD 995658cf_0c94_91f6_3e39_2f3df800cae4["testNioHandlerCanBeDrivenByMainThread()"] 77992452_9860_5bb3_1005_1a8be4c6ad5f["NioIoHandlerTest"] 995658cf_0c94_91f6_3e39_2f3df800cae4 -->|defined in| 77992452_9860_5bb3_1005_1a8be4c6ad5f style 995658cf_0c94_91f6_3e39_2f3df800cae4 fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
transport/src/test/java/io/netty/channel/nio/NioIoHandlerTest.java lines 39–134
@Test
void testNioHandlerCanBeDrivenByMainThread() throws Exception {
IoHandlerFactory factory = NioIoHandler.newFactory();
final Thread current = Thread.currentThread();
final Queue<Runnable> tasks = new ConcurrentLinkedQueue<>();
// Create our own IoExecutor that is driven by the main thread
final ThreadAwareExecutor executor = new ThreadAwareExecutor() {
@Override
public boolean isExecutorThread(Thread thread) {
return current == thread;
}
@Override
public void execute(Runnable command) {
tasks.add(command);
}
};
IoHandlerContext context = new IoHandlerContext() {
@Override
public boolean canBlock() {
// Just busy spin.
return false;
}
@Override
public long delayNanos(long currentTimeNanos) {
return 0;
}
@Override
public long deadlineNanos() {
return -1;
}
};
IoHandler handler = factory.newHandler(executor);
// Open a ServerSocketChannel that we can connect to.
ServerSocketChannel channel = SelectorProvider.provider().openServerSocketChannel();
channel.configureBlocking(false);
channel.bind(new InetSocketAddress(0));
// Do the connect in another thread so we don't block our io execution loop.
Thread t = new Thread(new Runnable() {
@Override
public void run() {
Socket socket = new Socket();
try {
socket.connect(channel.getLocalAddress());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
AtomicBoolean acceptedConnection = new AtomicBoolean();
IoRegistration registration = handler.register(new NioSelectableChannelIoHandle<ServerSocketChannel>(channel) {
@Override
protected void handle(ServerSocketChannel channel, SelectionKey key) {
if (key.isAcceptable()) {
try {
// Just accept the connection and close it.
java.nio.channels.SocketChannel accepted = channel.accept();
accepted.close();
} catch (Exception e) {
// ignore
} finally {
acceptedConnection.set(true);
}
}
}
});
registration.submit(NioIoOps.ACCEPT);
t.start();
// Let's loop until our registration was cancelled.
while (registration.isValid()) {
handler.run(context);
for (;;) {
// Execute all tasks that were dispatched.
Runnable r = tasks.poll();
if (r == null) {
break;
}
Domain
Subdomains
Source
Frequently Asked Questions
What does testNioHandlerCanBeDrivenByMainThread() do?
testNioHandlerCanBeDrivenByMainThread() is a function in the netty codebase, defined in transport/src/test/java/io/netty/channel/nio/NioIoHandlerTest.java.
Where is testNioHandlerCanBeDrivenByMainThread() defined?
testNioHandlerCanBeDrivenByMainThread() is defined in transport/src/test/java/io/netty/channel/nio/NioIoHandlerTest.java at line 39.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free