comparison src/tests/gov/nasa/jpf/test/vm/threads/SuspendResumeTest.java @ 0:61d41facf527

initial v8 import (history reset)
author Peter Mehlitz <Peter.C.Mehlitz@nasa.gov>
date Fri, 23 Jan 2015 10:14:01 -0800
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:61d41facf527
1 /*
2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
4 * All rights reserved.
5 *
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
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0.
11 *
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.
17 */
18 package gov.nasa.jpf.test.vm.threads;
19
20 import gov.nasa.jpf.util.test.TestJPF;
21
22 import org.junit.Test;
23
24 /**
25 * regression test for suspend/resume
26 */
27 @SuppressWarnings("deprecation")
28 public class SuspendResumeTest extends TestJPF {
29
30 static boolean isRunning;
31 static boolean pass = false;
32
33 static class T1 extends Thread {
34 @Override
35 public void run(){
36 System.out.println("t1 running");
37 isRunning = true;
38 while (!pass){
39 Thread.yield();
40 }
41 System.out.println("t1 terminating");
42 }
43 }
44
45 @Test
46 public void testBasicSuspendDeadlock(){
47 if (verifyDeadlock("+cg.threads.break_yield")) {
48 Thread t1 = new T1();
49 t1.start();
50
51 while (!isRunning) {
52 Thread.yield();
53 }
54
55 t1.suspend();
56 assertTrue(t1.getState() == Thread.State.RUNNABLE);
57
58 pass = true;
59
60 // without resuming, T1 should not be scheduled again, despite being in a RUNNABLE state
61 //t1.resume();
62 }
63 }
64
65 @Test
66 public void testBasicSuspendResume(){
67 if (verifyNoPropertyViolation("+cg.threads.break_yield")) {
68 Thread t1 = new T1();
69 t1.start();
70
71 while (!isRunning) {
72 Thread.yield();
73 }
74
75 System.out.println("main suspending t1");
76 t1.suspend();
77 assertTrue(t1.getState() == Thread.State.RUNNABLE);
78
79 pass = true;
80
81 System.out.println("main resuming t1");
82 t1.resume();
83 try {
84 System.out.println("main joining t1");
85 t1.join();
86 } catch (InterruptedException ix){
87 fail("t1.join got interrupted");
88 }
89
90 System.out.println("main terminating after t1.join");
91 }
92 }
93
94 //---------------
95 // this is the main reason to model suspend/resume, since suspension does
96 // *not* give up any held locks, and hence is very prone to creating deadlocks
97
98 static class T2 extends Thread {
99 @Override
100 public synchronized void run(){
101 System.out.println("t2 running with lock");
102 isRunning = true;
103 while (!pass){
104 Thread.yield();
105 }
106 System.out.println("t2 terminating");
107 }
108 }
109
110 @Test
111 public void testLockholderSuspendDeadlock(){
112
113 if (verifyDeadlock("+cg.threads.break_yield")) {
114 Thread t2 = new T2();
115 t2.start();
116
117 while (!isRunning) {
118 Thread.yield();
119 }
120
121 System.out.println("main suspending t2");
122 t2.suspend();
123 // now t2 should hold and never give up its lock
124
125 synchronized (t2){
126 fail("main should never get here");
127 }
128 }
129 }
130
131 //------------
132
133 static class T3 extends Thread {
134 @Override
135 public synchronized void run(){
136 System.out.println("t3 running");
137 isRunning = true;
138 try {
139 wait();
140 } catch (InterruptedException ix){
141 fail("t3 got interrupted");
142 }
143 System.out.println("t3 terminating");
144 }
145 }
146
147 @Test
148 public void testWaitingSuspendNotifyDeadlock(){
149 if (verifyDeadlock("+cg.threads.break_yield")) {
150 Thread t3 = new T3();
151 t3.start();
152
153 while (!isRunning) {
154 Thread.yield();
155 }
156
157 synchronized (t3){
158 assertTrue( t3.getState() == Thread.State.WAITING);
159
160 System.out.println("main suspending t3");
161 t3.suspend();
162
163 System.out.println("main notifying t3");
164 t3.notify();
165 // t3 should be still suspended, despite being notified
166 }
167 }
168 }
169
170 @Test
171 public void testWaitingSuspendNotifyResume(){
172 if (verifyNoPropertyViolation("+cg.threads.break_yield")) {
173 Thread t3 = new T3();
174 t3.start();
175
176 while (!isRunning) {
177 Thread.yield();
178 }
179
180 synchronized (t3){
181 assertTrue( t3.getState() == Thread.State.WAITING);
182
183 System.out.println("main suspending t3");
184 t3.suspend();
185
186 System.out.println("main notifying t3");
187 t3.notify();
188 // t3 should be still suspended, despite being notified
189
190 System.out.println("main resuming t3");
191 t3.resume();
192 try {
193 System.out.println("main joining t3");
194 t3.join();
195 } catch (InterruptedException ix) {
196 fail("t3.join got interrupted");
197 }
198
199 System.out.println("main terminating after t3.join");
200 }
201 }
202 }
203
204
205 //----------------
206
207 static class T4 extends Thread {
208 @Override
209 public void run(){
210 System.out.println("t4 running ");
211 isRunning = true;
212 while (!pass){
213 Thread.yield();
214 }
215
216 System.out.println("t4 trying to obtain lock");
217 synchronized (this){
218 System.out.println("t4 obtained lock");
219 }
220 System.out.println("t4 terminating");
221 }
222 }
223
224 @Test
225 public void testBlockSuspendUnblockDeadlock(){
226 if (verifyDeadlock("+cg.threads.break_yield")) {
227 Thread t4 = new T4();
228 t4.start();
229
230 while (!isRunning) {
231 Thread.yield();
232 }
233
234 synchronized (t4){
235 pass = true;
236
237 while (t4.getState() != Thread.State.BLOCKED){
238 Thread.yield();
239 }
240
241 System.out.println("main suspending t4");
242 t4.suspend();
243 }
244 System.out.println("main released t4 lock");
245 }
246 }
247
248
249 @Test
250 public void testBlockSuspendUnblockResume(){
251 if (verifyNoPropertyViolation("+cg.threads.break_yield")) {
252 Thread t4 = new T4();
253 t4.start();
254
255 while (!isRunning) {
256 Thread.yield();
257 }
258
259 synchronized (t4){
260 pass = true;
261
262 while (t4.getState() != Thread.State.BLOCKED){
263 Thread.yield();
264 }
265
266 System.out.println("main suspending t4");
267 t4.suspend();
268 }
269 System.out.println("main released t4 lock");
270
271 System.out.println("main resuming t4");
272 t4.resume();
273 try {
274 System.out.println("main joining t4");
275 t4.join();
276 } catch (InterruptedException ix) {
277 fail("t4.join got interrupted");
278 }
279
280 System.out.println("main terminating after t4.join");
281 }
282 }
283 }