|
31 | 31 | import java.nio.channels.spi.SelectorProvider; |
32 | 32 | import java.util.Queue; |
33 | 33 | import java.util.concurrent.ConcurrentLinkedQueue; |
| 34 | +import java.util.concurrent.TimeUnit; |
34 | 35 | import java.util.concurrent.atomic.AtomicBoolean; |
35 | 36 |
|
| 37 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
36 | 38 | import static org.junit.jupiter.api.Assertions.assertTrue; |
37 | 39 |
|
38 | 40 | public class NioIoHandlerTest { |
@@ -133,4 +135,86 @@ protected void handle(ServerSocketChannel channel, SelectionKey key) { |
133 | 135 | handler.prepareToDestroy(); |
134 | 136 | handler.destroy(); |
135 | 137 | } |
| 138 | + |
| 139 | + @Test |
| 140 | + void testMillisBeforeDeadlineRoundsUp() { |
| 141 | + long now = 1_000_000_000L; |
| 142 | + // The timeout is only 0 if the deadline is within 5 microseconds, or already reached. |
| 143 | + assertEquals(0, NioIoHandler.millisBeforeDeadline(now - 1, now)); |
| 144 | + assertEquals(0, NioIoHandler.millisBeforeDeadline(now, now)); |
| 145 | + assertEquals(0, NioIoHandler.millisBeforeDeadline(now + 1, now)); |
| 146 | + assertEquals(0, NioIoHandler.millisBeforeDeadline(now + 4_999, now)); |
| 147 | + // Everything from 5 microseconds up to 1 ms is rounded up to 1 ms, also less than 0.5 ms. |
| 148 | + // After that the timeout grows by 1 ms every ms, 5 microseconds early. |
| 149 | + assertEquals(1, NioIoHandler.millisBeforeDeadline(now + 5_000, now)); |
| 150 | + assertEquals(1, NioIoHandler.millisBeforeDeadline(now + 100_000, now)); |
| 151 | + assertEquals(1, NioIoHandler.millisBeforeDeadline(now + 499_999, now)); |
| 152 | + assertEquals(1, NioIoHandler.millisBeforeDeadline(now + 500_000, now)); |
| 153 | + assertEquals(1, NioIoHandler.millisBeforeDeadline(now + 999_999, now)); |
| 154 | + assertEquals(1, NioIoHandler.millisBeforeDeadline(now + 1_000_000, now)); |
| 155 | + assertEquals(1, NioIoHandler.millisBeforeDeadline(now + 1_004_999, now)); |
| 156 | + assertEquals(2, NioIoHandler.millisBeforeDeadline(now + 1_005_000, now)); |
| 157 | + assertEquals(2, NioIoHandler.millisBeforeDeadline(now + 2_004_999, now)); |
| 158 | + assertEquals(3, NioIoHandler.millisBeforeDeadline(now + 2_005_000, now)); |
| 159 | + assertEquals(1_000, NioIoHandler.millisBeforeDeadline(now + TimeUnit.SECONDS.toNanos(1), now)); |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + void testMillisBeforeDeadlineDoesNotOverflow() { |
| 164 | + long maxMillis = Long.MAX_VALUE / 1_000_000L; |
| 165 | + // Far deadlines are capped, they must never look like a deadline that is already reached. |
| 166 | + assertEquals(maxMillis, NioIoHandler.millisBeforeDeadline(Long.MAX_VALUE - 1, 0)); |
| 167 | + assertEquals(maxMillis, NioIoHandler.millisBeforeDeadline(Long.MAX_VALUE - 995_000L, 0)); |
| 168 | + assertEquals(maxMillis, NioIoHandler.millisBeforeDeadline(Long.MAX_VALUE - 995_001L, 0)); |
| 169 | + assertEquals(maxMillis, NioIoHandler.millisBeforeDeadline(Long.MAX_VALUE - 1_000_000L, 0)); |
| 170 | + assertTrue(NioIoHandler.millisBeforeDeadline(Long.MAX_VALUE - 10_000_000L, 0) > 0); |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + void testNoBusySpinIfDeadlineIsLessThanHalfAMillisecondAway() throws Exception { |
| 175 | + final long[] deadline = new long[1]; |
| 176 | + final ThreadAwareExecutor executor = new ThreadAwareExecutor() { |
| 177 | + @Override |
| 178 | + public boolean isExecutorThread(Thread thread) { |
| 179 | + return true; |
| 180 | + } |
| 181 | + |
| 182 | + @Override |
| 183 | + public void execute(Runnable command) { |
| 184 | + throw new UnsupportedOperationException(); |
| 185 | + } |
| 186 | + }; |
| 187 | + IoHandlerContext context = new IoHandlerContext() { |
| 188 | + @Override |
| 189 | + public boolean canBlock() { |
| 190 | + return true; |
| 191 | + } |
| 192 | + |
| 193 | + @Override |
| 194 | + public long delayNanos(long currentTimeNanos) { |
| 195 | + return Math.max(0, deadline[0] - currentTimeNanos); |
| 196 | + } |
| 197 | + |
| 198 | + @Override |
| 199 | + public long deadlineNanos() { |
| 200 | + return deadline[0]; |
| 201 | + } |
| 202 | + }; |
| 203 | + IoHandler handler = NioIoHandler.newFactory().newHandler(executor); |
| 204 | + try { |
| 205 | + deadline[0] = System.nanoTime() + TimeUnit.MICROSECONDS.toNanos(300); |
| 206 | + // An event loop calls run(...) again as long as the scheduled task is not due. If run(...) returns |
| 207 | + // without sleeping, as it did when the timeout was rounded to the nearest millisecond, this loops |
| 208 | + // thousands of times until the deadline is reached. |
| 209 | + int iterations = 0; |
| 210 | + while (System.nanoTime() < deadline[0]) { |
| 211 | + handler.run(context); |
| 212 | + iterations++; |
| 213 | + } |
| 214 | + assertTrue(iterations <= 3, "run(...) returned " + iterations + " times before the deadline"); |
| 215 | + } finally { |
| 216 | + handler.prepareToDestroy(); |
| 217 | + handler.destroy(); |
| 218 | + } |
| 219 | + } |
136 | 220 | } |