|
25 | 25 | * @test |
26 | 26 | * @bug 8303742 |
27 | 27 | * @summary CompletableFuture.orTimeout can leak memory if completed exceptionally |
| 28 | + * @modules java.base/java.util.concurrent:open |
28 | 29 | * @run junit/othervm -Xmx128m CompletableFutureOrTimeoutExceptionallyTest |
29 | 30 | */ |
30 | 31 |
|
31 | 32 | import java.time.Duration; |
| 33 | +import java.util.concurrent.BlockingQueue; |
32 | 34 | import java.util.concurrent.CompletableFuture; |
| 35 | +import java.util.concurrent.ScheduledThreadPoolExecutor; |
33 | 36 | import java.util.concurrent.TimeUnit; |
34 | 37 |
|
35 | 38 | import org.junit.jupiter.api.Test; |
| 39 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
36 | 40 |
|
37 | 41 | class CompletableFutureOrTimeoutExceptionallyTest { |
| 42 | + static final BlockingQueue<Runnable> delayerQueue; |
| 43 | + static { |
| 44 | + try { |
| 45 | + var delayerClass = Class.forName("java.util.concurrent.CompletableFuture$Delayer", |
| 46 | + true, |
| 47 | + CompletableFuture.class.getClassLoader()); |
| 48 | + var delayerField = delayerClass.getDeclaredField("delayer"); |
| 49 | + delayerField.setAccessible(true); |
| 50 | + delayerQueue = ((ScheduledThreadPoolExecutor)delayerField.get(null)).getQueue(); |
| 51 | + } catch (Throwable t) { |
| 52 | + throw new ExceptionInInitializerError(t); |
| 53 | + } |
| 54 | + } |
| 55 | + |
38 | 56 | /** |
39 | 57 | * Test that orTimeout task is cancelled if the CompletableFuture is completed Exceptionally |
40 | 58 | */ |
41 | 59 | @Test |
42 | | - void testOrTimeoutWithCompleteExceptionallyDoesNotLeak() { |
43 | | - var count = 0L; |
44 | | - while (count++ < 2_000_000) { |
45 | | - new CompletableFuture<>() |
46 | | - .orTimeout(12, TimeUnit.HOURS) |
47 | | - .completeExceptionally(new RuntimeException("This is fine")); |
48 | | - } |
| 60 | + void testOrTimeoutWithCompleteExceptionallyDoesNotLeak() throws InterruptedException { |
| 61 | + assertTrue(delayerQueue.peek() == null); |
| 62 | + var future = new CompletableFuture<>().orTimeout(12, TimeUnit.HOURS); |
| 63 | + assertTrue(delayerQueue.peek() != null); |
| 64 | + future.completeExceptionally(new RuntimeException("This is fine")); |
| 65 | + while (delayerQueue.peek() != null) { |
| 66 | + Thread.sleep(100); |
| 67 | + }; |
49 | 68 | } |
50 | 69 |
|
51 | 70 | /** |
52 | 71 | * Test that the completeOnTimeout task is cancelled if the CompletableFuture is completed Exceptionally |
53 | 72 | */ |
54 | 73 | @Test |
55 | | - void testCompleteOnTimeoutWithCompleteExceptionallyDoesNotLeak() { |
56 | | - var count = 0L; |
57 | | - while (count++ < 2_000_000) { |
58 | | - new CompletableFuture<>() |
59 | | - .completeOnTimeout(null, 12, TimeUnit.HOURS) |
60 | | - .completeExceptionally(new RuntimeException("This is fine")); |
61 | | - } |
| 74 | + void testCompleteOnTimeoutWithCompleteExceptionallyDoesNotLeak() throws InterruptedException { |
| 75 | + assertTrue(delayerQueue.peek() == null); |
| 76 | + var future = new CompletableFuture<>().completeOnTimeout(null, 12, TimeUnit.HOURS); |
| 77 | + assertTrue(delayerQueue.peek() != null); |
| 78 | + future.completeExceptionally(new RuntimeException("This is fine")); |
| 79 | + while (delayerQueue.peek() != null) { |
| 80 | + Thread.sleep(100); |
| 81 | + }; |
62 | 82 | } |
63 | 83 | } |
0 commit comments