|
| 1 | +/* |
| 2 | + * Copyright 2026 The Netty Project |
| 3 | + * |
| 4 | + * The Netty Project licenses this file to you under the Apache License, |
| 5 | + * version 2.0 (the "License"); you may not use this file except in compliance |
| 6 | + * with the License. You may obtain a copy of the License at: |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations |
| 14 | + * under the License. |
| 15 | + */ |
| 16 | +package io.netty.channel.uring; |
| 17 | + |
| 18 | +import io.netty.channel.IoEvent; |
| 19 | +import io.netty.channel.IoHandler; |
| 20 | +import io.netty.channel.IoHandlerContext; |
| 21 | +import io.netty.channel.IoHandlerFactory; |
| 22 | +import io.netty.channel.IoRegistration; |
| 23 | +import io.netty.util.concurrent.ThreadAwareExecutor; |
| 24 | +import org.junit.jupiter.api.BeforeAll; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import org.junit.jupiter.api.Timeout; |
| 27 | + |
| 28 | +import java.util.concurrent.CountDownLatch; |
| 29 | +import java.util.concurrent.atomic.AtomicInteger; |
| 30 | +import java.util.concurrent.atomic.AtomicReference; |
| 31 | + |
| 32 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 33 | +import static org.junit.jupiter.api.Assumptions.assumeTrue; |
| 34 | + |
| 35 | +public class IoUringIoHandlerWakeupEventCountTest { |
| 36 | + |
| 37 | + @BeforeAll |
| 38 | + public static void loadJNI() { |
| 39 | + assumeTrue(IoUring.isAvailable()); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Verify that waking up the handler via wakeup() (which writes to the internal eventfd) |
| 44 | + * does NOT count as a real I/O event in the return value of run(). |
| 45 | + */ |
| 46 | + @Test |
| 47 | + @Timeout(5) |
| 48 | + public void testWakeupDoesNotCountAsIoEvent() throws Exception { |
| 49 | + final IoHandlerFactory factory = IoUringIoHandler.newFactory(); |
| 50 | + final AtomicReference<Thread> handlerThreadRef = new AtomicReference<>(); |
| 51 | + final IoHandler handler = factory.newHandler(new ThreadAwareExecutor() { |
| 52 | + @Override |
| 53 | + public boolean isExecutorThread(Thread thread) { |
| 54 | + return thread == handlerThreadRef.get(); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void execute(Runnable command) { |
| 59 | + command.run(); |
| 60 | + } |
| 61 | + }); |
| 62 | + handler.initialize(); |
| 63 | + |
| 64 | + final AtomicInteger runResult = new AtomicInteger(-1); |
| 65 | + final CountDownLatch handlerRunning = new CountDownLatch(1); |
| 66 | + final AtomicReference<Throwable> failure = new AtomicReference<>(); |
| 67 | + |
| 68 | + Thread handlerThread = new Thread(() -> { |
| 69 | + handlerThreadRef.set(Thread.currentThread()); |
| 70 | + handlerRunning.countDown(); |
| 71 | + try { |
| 72 | + int result = handler.run(new IoHandlerContext() { |
| 73 | + @Override |
| 74 | + public boolean canBlock() { |
| 75 | + return true; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public long delayNanos(long currentTimeNanos) { |
| 80 | + return Long.MAX_VALUE; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public long deadlineNanos() { |
| 85 | + return -1L; |
| 86 | + } |
| 87 | + }); |
| 88 | + runResult.set(result); |
| 89 | + } catch (Throwable t) { |
| 90 | + failure.set(t); |
| 91 | + } |
| 92 | + }); |
| 93 | + handlerThread.start(); |
| 94 | + |
| 95 | + // Wait for the handler thread to be running and blocked in io_uring_enter. |
| 96 | + handlerRunning.await(); |
| 97 | + // Small delay to ensure io_uring_enter is entered. |
| 98 | + Thread.sleep(100); |
| 99 | + |
| 100 | + // Wakeup from the test thread (not the executor thread) -- writes to eventfd. |
| 101 | + handler.wakeup(); |
| 102 | + |
| 103 | + handlerThread.join(); |
| 104 | + |
| 105 | + Throwable t = failure.get(); |
| 106 | + if (t != null) { |
| 107 | + throw new AssertionError("Handler thread failed", t); |
| 108 | + } |
| 109 | + |
| 110 | + // The fix: eventfd wakeup should NOT be counted as a real I/O event. |
| 111 | + assertEquals(0, runResult.get(), "wakeup via eventfd should not be counted as an I/O event"); |
| 112 | + |
| 113 | + handler.prepareToDestroy(); |
| 114 | + handler.destroy(); |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * Verify that actual channel I/O (a NOP submitted to a registered handle) |
| 119 | + * IS counted in the return value of run(). |
| 120 | + */ |
| 121 | + @Test |
| 122 | + @Timeout(5) |
| 123 | + public void testRealIoEventIsCounted() throws Exception { |
| 124 | + final IoHandlerFactory factory = IoUringIoHandler.newFactory(); |
| 125 | + final IoHandler handler = factory.newHandler(new ThreadAwareExecutor() { |
| 126 | + @Override |
| 127 | + public boolean isExecutorThread(Thread thread) { |
| 128 | + return true; |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public void execute(Runnable command) { |
| 133 | + command.run(); |
| 134 | + } |
| 135 | + }); |
| 136 | + handler.initialize(); |
| 137 | + |
| 138 | + final AtomicReference<IoEvent> eventRef = new AtomicReference<>(); |
| 139 | + IoRegistration registration = handler.register(new IoUringIoHandle() { |
| 140 | + @Override |
| 141 | + public void handle(IoRegistration registration, IoEvent ioEvent) { |
| 142 | + eventRef.set(ioEvent); |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public void close() { |
| 147 | + // Noop |
| 148 | + } |
| 149 | + }); |
| 150 | + |
| 151 | + // Submit a NOP -- this will produce a completion that counts as real I/O. |
| 152 | + registration.submit(new IoUringIoOps(Native.IORING_OP_NOP, (byte) 0, |
| 153 | + (short) 0, -1, 0, 0, 0, 0, (short) 0, (short) 0, (short) 0, 0, 0)); |
| 154 | + |
| 155 | + // Non-blocking run: io_uring processes the NOP completion. |
| 156 | + int handled = handler.run(new IoHandlerContext() { |
| 157 | + @Override |
| 158 | + public boolean canBlock() { |
| 159 | + return false; |
| 160 | + } |
| 161 | + |
| 162 | + @Override |
| 163 | + public long delayNanos(long currentTimeNanos) { |
| 164 | + return 0; |
| 165 | + } |
| 166 | + |
| 167 | + @Override |
| 168 | + public long deadlineNanos() { |
| 169 | + return 0; |
| 170 | + } |
| 171 | + }); |
| 172 | + |
| 173 | + // Real I/O on a registered handle must be counted. |
| 174 | + assertEquals(1, handled, "real I/O event on a registered handle should be counted"); |
| 175 | + |
| 176 | + registration.cancel(); |
| 177 | + handler.prepareToDestroy(); |
| 178 | + handler.destroy(); |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Verify that when both a wakeup and real I/O occur simultaneously, |
| 183 | + * only the real I/O event is counted. |
| 184 | + */ |
| 185 | + @Test |
| 186 | + @Timeout(5) |
| 187 | + public void testWakeupPlusRealIoCountsOnlyRealIo() throws Exception { |
| 188 | + final IoHandlerFactory factory = IoUringIoHandler.newFactory(); |
| 189 | + final AtomicReference<Thread> handlerThreadRef = new AtomicReference<>(); |
| 190 | + final IoHandler handler = factory.newHandler(new ThreadAwareExecutor() { |
| 191 | + @Override |
| 192 | + public boolean isExecutorThread(Thread thread) { |
| 193 | + return thread == handlerThreadRef.get(); |
| 194 | + } |
| 195 | + |
| 196 | + @Override |
| 197 | + public void execute(Runnable command) { |
| 198 | + command.run(); |
| 199 | + } |
| 200 | + }); |
| 201 | + handler.initialize(); |
| 202 | + |
| 203 | + final AtomicReference<IoEvent> eventRef = new AtomicReference<>(); |
| 204 | + IoRegistration registration = handler.register(new IoUringIoHandle() { |
| 205 | + @Override |
| 206 | + public void handle(IoRegistration registration, IoEvent ioEvent) { |
| 207 | + eventRef.set(ioEvent); |
| 208 | + } |
| 209 | + |
| 210 | + @Override |
| 211 | + public void close() { |
| 212 | + // Noop |
| 213 | + } |
| 214 | + }); |
| 215 | + |
| 216 | + final AtomicInteger runResult = new AtomicInteger(-1); |
| 217 | + final CountDownLatch handlerRunning = new CountDownLatch(1); |
| 218 | + final AtomicReference<Throwable> failure = new AtomicReference<>(); |
| 219 | + |
| 220 | + Thread handlerThread = new Thread(() -> { |
| 221 | + handlerThreadRef.set(Thread.currentThread()); |
| 222 | + // Submit a NOP on the handler thread (executor thread). |
| 223 | + registration.submit(new IoUringIoOps(Native.IORING_OP_NOP, (byte) 0, |
| 224 | + (short) 0, -1, 0, 0, 0, 0, (short) 0, (short) 0, (short) 0, 0, 0)); |
| 225 | + handlerRunning.countDown(); |
| 226 | + try { |
| 227 | + int result = handler.run(new IoHandlerContext() { |
| 228 | + @Override |
| 229 | + public boolean canBlock() { |
| 230 | + return true; |
| 231 | + } |
| 232 | + |
| 233 | + @Override |
| 234 | + public long delayNanos(long currentTimeNanos) { |
| 235 | + return Long.MAX_VALUE; |
| 236 | + } |
| 237 | + |
| 238 | + @Override |
| 239 | + public long deadlineNanos() { |
| 240 | + return -1L; |
| 241 | + } |
| 242 | + }); |
| 243 | + runResult.set(result); |
| 244 | + } catch (Throwable t) { |
| 245 | + failure.set(t); |
| 246 | + } |
| 247 | + }); |
| 248 | + handlerThread.start(); |
| 249 | + |
| 250 | + handlerRunning.await(); |
| 251 | + Thread.sleep(100); |
| 252 | + |
| 253 | + // Trigger wakeup from the test thread. |
| 254 | + handler.wakeup(); |
| 255 | + |
| 256 | + handlerThread.join(); |
| 257 | + |
| 258 | + Throwable t = failure.get(); |
| 259 | + if (t != null) { |
| 260 | + throw new AssertionError("Handler thread failed", t); |
| 261 | + } |
| 262 | + |
| 263 | + // Only the real I/O event (NOP) should be counted, not the eventfd wakeup. |
| 264 | + assertEquals(1, runResult.get(), |
| 265 | + "only real I/O events should be counted, not the internal eventfd wakeup"); |
| 266 | + |
| 267 | + registration.cancel(); |
| 268 | + handler.prepareToDestroy(); |
| 269 | + handler.destroy(); |
| 270 | + } |
| 271 | +} |