jdk/test/java/lang/annotation/AnnotationType/AnnotationTypeDeadlockTest.java
author shurailine
Tue, 27 Oct 2015 20:06:02 -0700
changeset 33402 1156d495a525
parent 19579 ed6afb248908
permissions -rw-r--r--
8140336: Add @modules for exported dependencies to jdk_core tests Reviewed-by: alanb, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18827
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
     1
/*
33402
1156d495a525 8140336: Add @modules for exported dependencies to jdk_core tests
shurailine
parents: 19579
diff changeset
     2
 * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
18827
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
     4
 *
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
     8
 *
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    13
 * accompanied this code).
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    14
 *
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    18
 *
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    21
 * questions.
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    22
 */
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    23
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    24
/*
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    25
 * @test
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    26
 * @bug 7122142
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    27
 * @summary Test deadlock situation when recursive annotations are parsed
33402
1156d495a525 8140336: Add @modules for exported dependencies to jdk_core tests
shurailine
parents: 19579
diff changeset
    28
 * @modules java.management
18827
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    29
 */
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    30
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    31
import java.lang.annotation.Retention;
19579
ed6afb248908 8022721: AnnotationTypeDeadlockTest.java throws java.lang.IllegalStateException: unexpected condition
plevart
parents: 18827
diff changeset
    32
import java.lang.management.ManagementFactory;
ed6afb248908 8022721: AnnotationTypeDeadlockTest.java throws java.lang.IllegalStateException: unexpected condition
plevart
parents: 18827
diff changeset
    33
import java.lang.management.ThreadInfo;
ed6afb248908 8022721: AnnotationTypeDeadlockTest.java throws java.lang.IllegalStateException: unexpected condition
plevart
parents: 18827
diff changeset
    34
import java.lang.management.ThreadMXBean;
18827
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    35
import java.util.concurrent.CountDownLatch;
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    36
import java.util.concurrent.atomic.AtomicInteger;
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    37
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    38
import static java.lang.annotation.RetentionPolicy.RUNTIME;
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    39
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    40
public class AnnotationTypeDeadlockTest {
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    41
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    42
    @Retention(RUNTIME)
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    43
    @AnnB
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    44
    public @interface AnnA {
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    45
    }
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    46
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    47
    @Retention(RUNTIME)
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    48
    @AnnA
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    49
    public @interface AnnB {
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    50
    }
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    51
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    52
    static class Task extends Thread {
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    53
        final CountDownLatch prepareLatch;
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    54
        final AtomicInteger goLatch;
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    55
        final Class<?> clazz;
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    56
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    57
        Task(CountDownLatch prepareLatch, AtomicInteger goLatch, Class<?> clazz) {
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    58
            super(clazz.getSimpleName());
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    59
            setDaemon(true); // in case it deadlocks
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    60
            this.prepareLatch = prepareLatch;
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    61
            this.goLatch = goLatch;
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    62
            this.clazz = clazz;
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    63
        }
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    64
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    65
        @Override
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    66
        public void run() {
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    67
            prepareLatch.countDown();  // notify we are prepared
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    68
            while (goLatch.get() > 0); // spin-wait before go
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    69
            clazz.getDeclaredAnnotations();
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    70
        }
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    71
    }
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    72
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    73
    public static void main(String[] args) throws Exception {
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    74
        CountDownLatch prepareLatch = new CountDownLatch(2);
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    75
        AtomicInteger goLatch = new AtomicInteger(1);
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    76
        Task taskA = new Task(prepareLatch, goLatch, AnnA.class);
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    77
        Task taskB = new Task(prepareLatch, goLatch, AnnB.class);
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    78
        taskA.start();
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    79
        taskB.start();
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    80
        // wait until both threads start-up
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    81
        prepareLatch.await();
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    82
        // let them go
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
    83
        goLatch.set(0);
19579
ed6afb248908 8022721: AnnotationTypeDeadlockTest.java throws java.lang.IllegalStateException: unexpected condition
plevart
parents: 18827
diff changeset
    84
        // obtain ThreadMXBean
ed6afb248908 8022721: AnnotationTypeDeadlockTest.java throws java.lang.IllegalStateException: unexpected condition
plevart
parents: 18827
diff changeset
    85
        ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
ed6afb248908 8022721: AnnotationTypeDeadlockTest.java throws java.lang.IllegalStateException: unexpected condition
plevart
parents: 18827
diff changeset
    86
        // wait for threads to finish or dead-lock
ed6afb248908 8022721: AnnotationTypeDeadlockTest.java throws java.lang.IllegalStateException: unexpected condition
plevart
parents: 18827
diff changeset
    87
        while (taskA.isAlive() || taskB.isAlive()) {
ed6afb248908 8022721: AnnotationTypeDeadlockTest.java throws java.lang.IllegalStateException: unexpected condition
plevart
parents: 18827
diff changeset
    88
            // attempt to join threads
ed6afb248908 8022721: AnnotationTypeDeadlockTest.java throws java.lang.IllegalStateException: unexpected condition
plevart
parents: 18827
diff changeset
    89
            taskA.join(500L);
ed6afb248908 8022721: AnnotationTypeDeadlockTest.java throws java.lang.IllegalStateException: unexpected condition
plevart
parents: 18827
diff changeset
    90
            taskB.join(500L);
ed6afb248908 8022721: AnnotationTypeDeadlockTest.java throws java.lang.IllegalStateException: unexpected condition
plevart
parents: 18827
diff changeset
    91
            // detect dead-lock
ed6afb248908 8022721: AnnotationTypeDeadlockTest.java throws java.lang.IllegalStateException: unexpected condition
plevart
parents: 18827
diff changeset
    92
            long[] deadlockedIds = threadBean.findMonitorDeadlockedThreads();
ed6afb248908 8022721: AnnotationTypeDeadlockTest.java throws java.lang.IllegalStateException: unexpected condition
plevart
parents: 18827
diff changeset
    93
            if (deadlockedIds != null && deadlockedIds.length > 0) {
ed6afb248908 8022721: AnnotationTypeDeadlockTest.java throws java.lang.IllegalStateException: unexpected condition
plevart
parents: 18827
diff changeset
    94
                StringBuilder sb = new StringBuilder("deadlock detected:\n\n");
ed6afb248908 8022721: AnnotationTypeDeadlockTest.java throws java.lang.IllegalStateException: unexpected condition
plevart
parents: 18827
diff changeset
    95
                for (ThreadInfo ti : threadBean.getThreadInfo(deadlockedIds, Integer.MAX_VALUE)) {
ed6afb248908 8022721: AnnotationTypeDeadlockTest.java throws java.lang.IllegalStateException: unexpected condition
plevart
parents: 18827
diff changeset
    96
                    sb.append(ti);
ed6afb248908 8022721: AnnotationTypeDeadlockTest.java throws java.lang.IllegalStateException: unexpected condition
plevart
parents: 18827
diff changeset
    97
                }
ed6afb248908 8022721: AnnotationTypeDeadlockTest.java throws java.lang.IllegalStateException: unexpected condition
plevart
parents: 18827
diff changeset
    98
                throw new IllegalStateException(sb.toString());
ed6afb248908 8022721: AnnotationTypeDeadlockTest.java throws java.lang.IllegalStateException: unexpected condition
plevart
parents: 18827
diff changeset
    99
            }
18827
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
   100
        }
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
   101
    }
ecbd9c8bef12 7122142: (ann) Race condition between isAnnotationPresent and getAnnotations
plevart
parents:
diff changeset
   102
}