|
| 1 | +/* |
| 2 | + * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | + |
| 24 | +import java.util.concurrent.CompletableFuture; |
| 25 | +import java.util.concurrent.CountDownLatch; |
| 26 | +import java.util.concurrent.ThreadLocalRandom; |
| 27 | +import java.util.concurrent.atomic.AtomicReference; |
| 28 | + |
| 29 | +/* |
| 30 | + * @test |
| 31 | + * @bug 8254350 |
| 32 | + * @run main SwallowedInterruptedException |
| 33 | + * @key randomness |
| 34 | + */ |
| 35 | + |
| 36 | +public class SwallowedInterruptedException { |
| 37 | + static final int ITERATIONS = 100; |
| 38 | + |
| 39 | + public static void main(String[] args) throws Throwable { |
| 40 | + for (int i = 1; i <= ITERATIONS; i++) { |
| 41 | + System.out.format("Iteration %d%n", i); |
| 42 | + |
| 43 | + CompletableFuture<Void> future = new CompletableFuture<>(); |
| 44 | + CountDownLatch running = new CountDownLatch(1); |
| 45 | + AtomicReference<String> failed = new AtomicReference<>(); |
| 46 | + |
| 47 | + Thread thread = new Thread(() -> { |
| 48 | + // signal main thread that child is running |
| 49 | + running.countDown(); |
| 50 | + |
| 51 | + // invoke Future.get, it complete with the interrupt status set or |
| 52 | + // else throw InterruptedException with the interrupt status not set. |
| 53 | + try { |
| 54 | + future.get(); |
| 55 | + |
| 56 | + // interrupt status should be set |
| 57 | + if (!Thread.currentThread().isInterrupted()) { |
| 58 | + failed.set("Future.get completed with interrupt status not set"); |
| 59 | + } |
| 60 | + } catch (InterruptedException ex) { |
| 61 | + // interrupt status should be cleared |
| 62 | + if (Thread.currentThread().isInterrupted()) { |
| 63 | + failed.set("InterruptedException with interrupt status set"); |
| 64 | + } |
| 65 | + } catch (Throwable ex) { |
| 66 | + failed.set("Unexpected exception " + ex); |
| 67 | + } |
| 68 | + }); |
| 69 | + thread.setDaemon(true); |
| 70 | + thread.start(); |
| 71 | + |
| 72 | + // wait for thread to run |
| 73 | + running.await(); |
| 74 | + |
| 75 | + // interrupt thread and set result after an optional (random) delay |
| 76 | + thread.interrupt(); |
| 77 | + long sleepMillis = ThreadLocalRandom.current().nextLong(10); |
| 78 | + if (sleepMillis > 0) |
| 79 | + Thread.sleep(sleepMillis); |
| 80 | + future.complete(null); |
| 81 | + |
| 82 | + // wait for thread to terminate and check for failure |
| 83 | + thread.join(); |
| 84 | + String failedReason = failed.get(); |
| 85 | + if (failedReason != null) { |
| 86 | + throw new RuntimeException("Test failed: " + failedReason); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments