2 * Copyright (C) 2014, 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.vm.threads;
20 import gov.nasa.jpf.util.test.TestJPF;
22 import java.util.concurrent.locks.LockSupport;
24 import org.junit.Test;
27 * raw test for Thread.interrupt conformance
29 public class InterruptTest extends TestJPF {
31 @Test public void testInterruptStatus () {
32 if (verifyNoPropertyViolation()) {
33 Thread t = Thread.currentThread();
35 assert !t.isInterrupted() : "initial interrupt status is set";
37 System.out.println("setting interrupt status");
40 assert t.isInterrupted() : "interrupt status not set";
42 System.out.println("query and clear interrupt status");
43 assert Thread.interrupted() : "interrupt status prematurely reset";
45 assert !Thread.interrupted() : "interrupt status wasn't reset";
49 @Test public void testWaitSyncInterrupt() {
50 if (verifyNoPropertyViolation()) {
51 Runnable r = new Runnable() {
56 // doesn't matter from where we interrupt (thread can interrupt itself)
57 Thread t = Thread.currentThread();
61 // if we don't enter here, this should be reported as a deadlock
63 System.out.println("T waiting");
65 wait(); // this should immediately throw, i.e. return
66 assert false : "wait() did not throw InterruptedException";
67 } catch (InterruptedException ix) {
68 ix.printStackTrace(); // should include Object.wait()
69 System.out.println("T interrupted, terminating");
70 assert !t.isInterrupted() : "throw didn't reset interrupt status";
72 } catch (Throwable x) {
73 assert false : "wait did throw wrong exception: " + x;
77 assert false : "should never get here";
81 Thread t1 = new Thread(r);
83 t1.interrupt(); // should have no effect - it's not runnable yet
84 assert !t1.isInterrupted() : "non-started thread has interrupt status set";
87 System.out.println("main terminated");
91 @Test public void testWaitAsyncInterrupt() {
92 if (verifyNoPropertyViolation()) {
93 Runnable r = new Runnable() {
98 // if we don't enter here, this should be reported as a deadlock
100 System.out.println("T waiting");
102 assert false : "wait() did not throw InterruptedException";
103 } catch (InterruptedException ix) {
104 ix.printStackTrace(); // should include Object.wait()
105 System.out.println("T interrupted, terminating");
106 assert !Thread.currentThread().isInterrupted() : "throw didn't reset interrupt status";
108 } catch (Throwable x) {
109 assert false : "wait did throw wrong exception: " + x;
113 assert false : "should never get here";
117 Thread t1 = new Thread(r);
120 // no matter if this is executed before or after t1 enters Object.wait()
121 // it should terminate it by throwing. In fact, JPF should explore both
123 System.out.println("main interrupting t1");
126 System.out.println("main terminated");
133 @Test public void testBlockedWaitAsyncInterrupt() {
134 if (verifyNoPropertyViolation()) {
139 Runnable r = new Runnable() {
143 synchronized (this) {
144 // if we don't enter here, this should be reported as a deadlock
146 System.out.println("T waiting");
149 assert false : "wait() did not throw InterruptedException";
150 } catch (InterruptedException ix) {
151 ix.printStackTrace(); // should include Object.wait()
152 assert !Thread.currentThread().isInterrupted() : "throw didn't reset interrupt status";
153 System.out.println("T interrupted, terminating");
156 } catch (Throwable x) {
157 assert false : "wait did throw wrong exception: " + x;
161 assert false : "should never get here";
165 Thread t1 = new Thread(r);
173 System.out.println("main interrupting t1");
176 // t1 can't run before we release the lock
177 Thread.yield(); // this shouldn't reschedule, t1 is blocked
178 assert !interrupted : "t1 prematurely scheduled w/o acquiring the lock";
179 System.out.println("main terminated, t1 runnable again");
184 @Test public void testPark() {
185 if (verifyNoPropertyViolation()) {
189 Thread t1 = new Thread(new Runnable() {
193 System.out.println("T parking..");
196 System.out.println("T terminated");
202 System.out.println("main interrupting");
206 System.out.println("main joining t1..");
208 System.out.println("main joined t1");
209 } catch (InterruptedException e) {
210 assert false : "t1.join() interrupted in main";
213 assert interrupted : "LockSupport.park() didn't get interrupted";
214 System.out.println("main terminated");