|
27 | 27 |
|
28 | 28 | import java.time.LocalDateTime; |
29 | 29 | import java.time.format.DateTimeFormatter; |
| 30 | +import java.util.concurrent.ArrayBlockingQueue; |
30 | 31 | import java.util.concurrent.ExecutorService; |
| 32 | +import java.util.concurrent.ThreadPoolExecutor; |
| 33 | +import java.util.concurrent.TimeUnit; |
31 | 34 | import java.util.concurrent.atomic.AtomicInteger; |
32 | 35 |
|
33 | 36 | import org.junit.jupiter.api.Assertions; |
@@ -224,6 +227,43 @@ void testClose1() { |
224 | 227 | Assertions.assertFalse(executor.isTerminated()); |
225 | 228 | } |
226 | 229 |
|
| 230 | + @Test |
| 231 | + void testTimeoutWithRejectedExecution() throws Exception { |
| 232 | + // Create a ThreadPoolExecutor with a queue capacity of 1 |
| 233 | + ThreadPoolExecutor customExecutor = new ThreadPoolExecutor( |
| 234 | + 1, // corePoolSize |
| 235 | + 1, // maxPoolSize |
| 236 | + 60L, |
| 237 | + TimeUnit.SECONDS, |
| 238 | + new ArrayBlockingQueue<>(1), // queue capacity is 1 |
| 239 | + new ThreadPoolExecutor.AbortPolicy() // default rejection policy: throws exception |
| 240 | + ); |
| 241 | + // Submit two tasks to occupy the thread and the queue |
| 242 | + customExecutor.submit(() -> { |
| 243 | + try { |
| 244 | + Thread.sleep(500); // occupy the thread for a while |
| 245 | + } catch (InterruptedException ignored) { |
| 246 | + } |
| 247 | + }); |
| 248 | + customExecutor.submit(() -> { |
| 249 | + try { |
| 250 | + Thread.sleep(500); // occupy the queue |
| 251 | + } catch (InterruptedException ignored) { |
| 252 | + } |
| 253 | + }); |
| 254 | + // Create a Dubbo Mock Channel and a request |
| 255 | + Channel channel = new MockedChannel(); |
| 256 | + Request request = new Request(999); |
| 257 | + // Use Dubbo's newFuture and pass in the custom thread pool |
| 258 | + DefaultFuture future = DefaultFuture.newFuture(channel, request, 100, customExecutor); |
| 259 | + // Mark the request as sent |
| 260 | + DefaultFuture.sent(channel, request); |
| 261 | + // Wait for the timeout task to trigger |
| 262 | + Thread.sleep(300); |
| 263 | + Assertions.assertNull(DefaultFuture.getFuture(999), "Future should be removed from FUTURES after timeout"); |
| 264 | + customExecutor.shutdown(); |
| 265 | + } |
| 266 | + |
227 | 267 | @Test |
228 | 268 | void testClose2() { |
229 | 269 | Channel channel = new MockedChannel(); |
|
0 commit comments