|
24 | 24 |
|
25 | 25 | import java.util.ArrayList; |
26 | 26 | import java.util.Collection; |
| 27 | +import java.util.Collections; |
| 28 | +import java.util.Iterator; |
27 | 29 | import java.util.List; |
28 | 30 | import java.util.concurrent.CountDownLatch; |
| 31 | +import java.util.concurrent.RejectedExecutionException; |
29 | 32 | import java.util.concurrent.TimeUnit; |
30 | 33 | import java.util.concurrent.atomic.AtomicInteger; |
31 | 34 | import java.util.concurrent.atomic.AtomicReference; |
32 | 35 |
|
33 | 36 | import static org.junit.jupiter.api.Assertions.assertFalse; |
| 37 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
34 | 38 | import static org.junit.jupiter.api.Assertions.assertThrows; |
35 | 39 | import static org.junit.jupiter.api.Assertions.assertTrue; |
36 | 40 |
|
@@ -129,6 +133,153 @@ public void run() { |
129 | 133 | } |
130 | 134 | } |
131 | 135 |
|
| 136 | + @Test |
| 137 | + public void testInEventLoopAfterReschedulingFailure() throws Exception { |
| 138 | + final UnorderedThreadPoolEventExecutor underlying = new UnorderedThreadPoolEventExecutor(1); |
| 139 | + final AtomicInteger executeCount = new AtomicInteger(); |
| 140 | + |
| 141 | + final EventExecutorGroup wrapper = new AbstractEventExecutorGroup() { |
| 142 | + @Override |
| 143 | + public void shutdown() { |
| 144 | + shutdownGracefully(); |
| 145 | + } |
| 146 | + |
| 147 | + private final EventExecutor executor = new AbstractEventExecutor(this) { |
| 148 | + @Override |
| 149 | + public boolean inEventLoop(Thread thread) { |
| 150 | + return underlying.inEventLoop(thread); |
| 151 | + } |
| 152 | + |
| 153 | + @Override |
| 154 | + public void shutdown() { |
| 155 | + shutdownGracefully(); |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + public void execute(Runnable command) { |
| 160 | + // Reject the 2nd execute() call (the reschedule attempt) |
| 161 | + // 1st call: initial task submission |
| 162 | + // 2nd call: reschedule after maxTaskExecutePerRun |
| 163 | + if (executeCount.incrementAndGet() == 2) { |
| 164 | + throw new RejectedExecutionException("Simulated queue full"); |
| 165 | + } |
| 166 | + underlying.execute(command); |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public boolean isShuttingDown() { |
| 171 | + return underlying.isShuttingDown(); |
| 172 | + } |
| 173 | + |
| 174 | + @Override |
| 175 | + public Future<?> shutdownGracefully(long quietPeriod, long timeout, TimeUnit unit) { |
| 176 | + return underlying.shutdownGracefully(quietPeriod, timeout, unit); |
| 177 | + } |
| 178 | + |
| 179 | + @Override |
| 180 | + public Future<?> terminationFuture() { |
| 181 | + return underlying.terminationFuture(); |
| 182 | + } |
| 183 | + |
| 184 | + @Override |
| 185 | + public boolean isShutdown() { |
| 186 | + return underlying.isShutdown(); |
| 187 | + } |
| 188 | + |
| 189 | + @Override |
| 190 | + public boolean isTerminated() { |
| 191 | + return underlying.isTerminated(); |
| 192 | + } |
| 193 | + |
| 194 | + @Override |
| 195 | + public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException { |
| 196 | + return underlying.awaitTermination(timeout, unit); |
| 197 | + } |
| 198 | + }; |
| 199 | + |
| 200 | + @Override |
| 201 | + public EventExecutor next() { |
| 202 | + return executor; |
| 203 | + } |
| 204 | + |
| 205 | + @Override |
| 206 | + public Iterator<EventExecutor> iterator() { |
| 207 | + return Collections.singletonList(executor).iterator(); |
| 208 | + } |
| 209 | + |
| 210 | + @Override |
| 211 | + public boolean isShuttingDown() { |
| 212 | + return underlying.isShuttingDown(); |
| 213 | + } |
| 214 | + |
| 215 | + @Override |
| 216 | + public Future<?> shutdownGracefully(long quietPeriod, long timeout, TimeUnit unit) { |
| 217 | + return underlying.shutdownGracefully(quietPeriod, timeout, unit); |
| 218 | + } |
| 219 | + |
| 220 | + @Override |
| 221 | + public Future<?> terminationFuture() { |
| 222 | + return underlying.terminationFuture(); |
| 223 | + } |
| 224 | + |
| 225 | + @Override |
| 226 | + public boolean isShutdown() { |
| 227 | + return underlying.isShutdown(); |
| 228 | + } |
| 229 | + |
| 230 | + @Override |
| 231 | + public boolean isTerminated() { |
| 232 | + return underlying.isTerminated(); |
| 233 | + } |
| 234 | + |
| 235 | + @Override |
| 236 | + public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException { |
| 237 | + return underlying.awaitTermination(timeout, unit); |
| 238 | + } |
| 239 | + }; |
| 240 | + |
| 241 | + // Use maxTaskExecutePerRun=1 so reschedule happens after first task |
| 242 | + NonStickyEventExecutorGroup nonStickyGroup = new NonStickyEventExecutorGroup(wrapper, 1); |
| 243 | + |
| 244 | + try { |
| 245 | + final EventExecutor executor = nonStickyGroup.next(); |
| 246 | + |
| 247 | + final CountDownLatch latch = new CountDownLatch(1); |
| 248 | + final AtomicReference<Boolean> inEventLoopResult = new AtomicReference<Boolean>(); |
| 249 | + |
| 250 | + // Submit 2 tasks: |
| 251 | + // Task 1: completes, triggers reschedule which will be rejected |
| 252 | + // Task 2: verifies inEventLoop() still works after failed reschedule |
| 253 | + executor.execute(new Runnable() { |
| 254 | + @Override |
| 255 | + public void run() { |
| 256 | + // First task - will trigger reschedule attempt that fails |
| 257 | + } |
| 258 | + }); |
| 259 | + |
| 260 | + executor.execute(new Runnable() { |
| 261 | + @Override |
| 262 | + public void run() { |
| 263 | + // This runs AFTER the failed rescheduling |
| 264 | + // WITHOUT line 262 fix: executingThread is null, inEventLoop() returns false |
| 265 | + // WITH line 262 fix: executingThread restored, inEventLoop() returns true |
| 266 | + inEventLoopResult.set(executor.inEventLoop()); |
| 267 | + latch.countDown(); |
| 268 | + } |
| 269 | + }); |
| 270 | + |
| 271 | + assertTrue(latch.await(5, TimeUnit.SECONDS), "Tasks should complete"); |
| 272 | + Boolean result = inEventLoopResult.get(); |
| 273 | + assertNotNull(result, "inEventLoop() should have been called"); |
| 274 | + assertTrue(result, |
| 275 | + "inEventLoop() should return true even after failed reschedule attempt. " + |
| 276 | + "This indicates executingThread was properly restored in the exception handler."); |
| 277 | + } finally { |
| 278 | + nonStickyGroup.shutdownGracefully(); |
| 279 | + underlying.shutdownGracefully(); |
| 280 | + } |
| 281 | + } |
| 282 | + |
132 | 283 | private static void execute(EventExecutorGroup group, CountDownLatch startLatch) throws Throwable { |
133 | 284 | final EventExecutor executor = group.next(); |
134 | 285 | assertTrue(executor instanceof OrderedEventExecutor); |
|