2 * Copyright (C) 2015, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0.
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package gov.nasa.jpf.test.mc.threads;
20 import gov.nasa.jpf.util.test.TestJPF;
21 import org.junit.Test;
24 * test deadlock detection during concurrent class init.
26 * This models hotspot behavior that can explode the state space
27 * and hence needs to be explicitly configured to perform
28 * nested locking during class init
30 public class NestedInitTest extends TestJPF {
32 //------------------------------------------------ check normal clinit execution
37 System.out.print( Thread.currentThread().getName());
38 System.out.println(" in Root.<clinit>()");
43 static class Base extends Root {
47 System.out.print( Thread.currentThread().getName());
48 System.out.println(" in Base.<clinit>()");
53 static class Derived extends Base {
57 System.out.print( Thread.currentThread().getName());
58 System.out.println(" in Derived.<clinit>()");
64 public void testNestedInitSingleOk() {
65 if (verifyNoPropertyViolation("+jvm.nested_init")){
66 new Derived(); // force clinit
67 System.out.print("Derived.data = ");
68 System.out.println(Derived.data);
69 assertTrue(Derived.data == 42);
74 public void testNestedInitConcurrentOk() {
75 if (verifyNoPropertyViolation("+jvm.nested_init")){
76 new Thread( new Runnable(){
79 System.out.print("t: Derived.data = ");
80 System.out.println(Derived.data);
81 assertTrue(Derived.data == 42);
85 new Derived(); // force clinit
86 System.out.print("main: Derived.data = ");
87 System.out.println(Derived.data);
88 assertTrue(Derived.data == 42);
92 //--- and now the nasty cases
94 //------------------------------------------ symmetric case
97 static final B b = new B();
101 static final A a = new A();
105 public void testSymmetricDeadlock() {
106 if (verifyDeadlock("+jvm.nested_init")) {
116 //------------------------------------------- hierarchical case
117 public static class CyclicBase {
118 static CyclicDerived sub = new CyclicDerived();
121 public static class CyclicDerived extends CyclicBase {
126 public void testCyclicHierarchyDeadlock (){
127 if (verifyDeadlock("+jvm.nested_init")) {
130 new CyclicDerived(); // causes class inits via CyclicDerived
134 Object o = CyclicBase.sub; // causes class inits via CyclicBase