Skip to content

Commit e12c419

Browse files
committed
CRIU ensures the test thread prints pass/fail messages
Signed-off-by: Jason Feng <[email protected]>
1 parent 2fec841 commit e12c419

File tree

1 file changed

+72
-75
lines changed

1 file changed

+72
-75
lines changed

test/functional/cmdLineTests/criu/src/org/openj9/criu/JDK11UpTimeoutAdjustmentTest.java

Lines changed: 72 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,15 @@ public class JDK11UpTimeoutAdjustmentTest {
3030
private static final long NANOS_PER_MILLI = 1000_000L;
3131
private static final long NANOS_PER_SECOND = 1000_000_000L;
3232

33-
private static Unsafe unsafe = Unsafe.getUnsafe();
33+
private static final int nsTime500kns = 500000;
34+
private static final long msTime2s = 2 * MILLIS_PER_SECOND;
35+
private static final long nsTime2s = 2 * NANOS_PER_SECOND;
36+
private static final long msTime5s = 5 * MILLIS_PER_SECOND;
37+
private static final long nsTime5s = 5 * NANOS_PER_SECOND;
38+
39+
private static final Object objWait = new Object();
40+
private static final TestResult testResult = new TestResult(true, 0);
41+
private static final Unsafe unsafe = Unsafe.getUnsafe();
3442

3543
public static void main(String[] args) throws InterruptedException {
3644
if (args.length == 0) {
@@ -41,41 +49,37 @@ public static void main(String[] args) throws InterruptedException {
4149
}
4250
}
4351

44-
private static Object objWait = new Object();
45-
// 5s time in ms
46-
private static final long msWaitNotify5s = 5 * MILLIS_PER_SECOND;
47-
final TestResult testResult = new TestResult(true, 0);
48-
4952
private void test(String testName) throws InterruptedException {
5053
CRIUSupport criu = CRIUTestUtils.prepareCheckPointJVM(CRIUTestUtils.imagePath);
5154
System.out.println("Start test name: " + testName);
5255
CRIUTestUtils.showThreadCurrentTime("Before starting " + testName);
56+
Thread testThread;
5357
switch (testName) {
5458
case "testThreadPark":
5559
testThreadParkHelper("testThreadPark NO C/R");
56-
testThreadPark();
60+
testThread = testThreadPark();
5761
break;
5862
case "testThreadSleep":
59-
testThreadParkHelper("testThreadSleep NO C/R");
60-
testThreadSleep();
63+
testThreadSleepHelper("testThreadSleep NO C/R");
64+
testThread = testThreadSleep();
6165
break;
6266
case "testObjectWaitNotify":
63-
testObjectWaitNotify();
67+
testThread = testObjectWaitNotify();
6468
break;
6569
case "testObjectWaitTimedNoNanoSecond":
66-
testObjectWaitTimedHelper("testObjectWaitTimedNoNanoSecond NO C/R", msSleepTime10s, 0);
67-
testObjectWaitTimedNoNanoSecond();
70+
testObjectWaitTimedHelper("testObjectWaitTimedNoNanoSecond NO C/R", msTime2s, 0);
71+
testThread = testObjectWaitTimedNoNanoSecond();
6872
break;
6973
case "testObjectWaitTimedWithNanoSecond":
70-
testObjectWaitTimedHelper("testObjectWaitTimedWithNanoSecond NO C/R", msSleepTime10s, 500000);
71-
testObjectWaitTimedWithNanoSecond();
74+
testObjectWaitTimedHelper("testObjectWaitTimedWithNanoSecond NO C/R", msTime2s, nsTime500kns);
75+
testThread = testObjectWaitTimedWithNanoSecond();
7276
break;
7377
default:
7478
throw new RuntimeException("Unrecognized test name: " + testName);
7579
}
7680

7781
while (testResult.lockStatus == 0) {
78-
Thread.yield();
82+
Thread.currentThread().yield();
7983
}
8084
CRIUTestUtils.checkPointJVMNoSetup(criu, CRIUTestUtils.imagePath, false);
8185

@@ -87,24 +91,28 @@ private void test(String testName) throws InterruptedException {
8791
testThreadSleepHelper("testThreadSleep NO C/R");
8892
break;
8993
case "testObjectWaitNotify":
90-
Thread.sleep(msWaitNotify5s);
91-
CRIUTestUtils.showThreadCurrentTime("Before objWait.notify()");
94+
Thread.sleep(msTime5s);
9295
synchronized (objWait) {
9396
objWait.notify();
9497
}
95-
Thread.sleep(5 * MILLIS_PER_SECOND);
9698
break;
9799
case "testObjectWaitTimedNoNanoSecond":
98-
testObjectWaitTimedHelper("testObjectWaitTimedNoNanoSecond NO C/R", msSleepTime10s, 0);
100+
testObjectWaitTimedHelper("testObjectWaitTimedNoNanoSecond NO C/R", msTime2s, 0);
99101
break;
100102
case "testObjectWaitTimedWithNanoSecond":
101-
testObjectWaitTimedHelper("testObjectWaitTimedWithNanoSecond NO C/R", msSleepTime10s, 500000);
103+
testObjectWaitTimedHelper("testObjectWaitTimedWithNanoSecond NO C/R", msTime2s, nsTime500kns);
102104
break;
103105
default:
104106
}
107+
CRIUTestUtils.showThreadCurrentTime("After run test : " + testName);
105108

106-
// maximum test running time is 12s, sleep another 2s
107-
Thread.sleep(2 * MILLIS_PER_SECOND);
109+
if (testThread != null) {
110+
try {
111+
testThread.join();
112+
} catch (InterruptedException e) {
113+
e.printStackTrace();
114+
}
115+
}
108116
CRIUTestUtils.showThreadCurrentTime("End " + testName);
109117
}
110118

@@ -127,130 +135,119 @@ public static void showMessages(String logStr, long expectedTime, boolean isMill
127135
+ "ns, CheckpointRestoreNanoTimeDelta: " + crDeltaNs + "ns (~" + (crDeltaNs / NANOS_PER_MILLI) + "ms)");
128136
}
129137

130-
// 10s parkt time in ns
131-
private static final long nsParkTime10s = 10 * NANOS_PER_SECOND;
132-
133138
private void testThreadParkHelper(String testName) {
134139
CRIUTestUtils.showThreadCurrentTime(testName + " before park()");
135140
final long startNanoTime = System.nanoTime();
136141
testResult.lockStatus = 1;
137-
unsafe.park(false, nsParkTime10s);
142+
unsafe.park(false, nsTime5s);
138143
final long endNanoTime = System.nanoTime();
139-
final long elapsedTime = endNanoTime - startNanoTime;
140-
boolean pass = false;
141-
if (elapsedTime >= nsParkTime10s) {
142-
pass = true;
143-
}
144144
CRIUTestUtils.showThreadCurrentTime(testName + " after park()");
145-
if (pass) {
146-
showMessages("PASSED: expected park time ", nsParkTime10s, false, elapsedTime, startNanoTime, endNanoTime);
145+
final long nsElapsedTime = endNanoTime - startNanoTime;
146+
if (nsElapsedTime < nsTime5s) {
147+
showMessages("FAILED: expected park time ", nsTime5s, false, nsElapsedTime, startNanoTime, endNanoTime);
147148
} else {
148-
showMessages("FAILED: expected park time ", nsParkTime10s, false, elapsedTime, startNanoTime, endNanoTime);
149+
showMessages("PASSED: expected park time ", nsTime5s, false, nsElapsedTime, startNanoTime, endNanoTime);
149150
}
150151
}
151152

152-
private void testThreadPark() {
153-
new Thread(() -> testThreadParkHelper("testThreadPark")).start();
153+
private Thread testThreadPark() {
154+
Thread parkThread = new Thread(() -> testThreadParkHelper("testThreadPark"));
155+
parkThread.start();
156+
return parkThread;
154157
}
155158

156-
// 10s sleep time in ms
157-
private static final long msSleepTime10s = 10 * MILLIS_PER_SECOND;
158-
159159
private void testThreadSleepHelper(String testName) {
160160
CRIUTestUtils.showThreadCurrentTime(testName + " before sleep()");
161161
final long startNanoTime = System.nanoTime();
162162
try {
163163
testResult.lockStatus = 1;
164-
Thread.sleep(msSleepTime10s);
165-
boolean pass = false;
164+
Thread.sleep(msTime5s);
166165
final long endNanoTime = System.nanoTime();
167-
final long elapsedTime = endNanoTime - startNanoTime;
168-
if (elapsedTime >= msSleepTime10s) {
169-
pass = true;
170-
}
171166
CRIUTestUtils.showThreadCurrentTime(testName + " after sleep()");
172-
if (pass) {
173-
showMessages("PASSED: expected sleep time ", msSleepTime10s, true, elapsedTime, startNanoTime,
167+
final long nsElapsedTime = endNanoTime - startNanoTime;
168+
if (nsElapsedTime < nsTime5s) {
169+
showMessages("FAILED: expected sleep time ", msTime5s, true, nsElapsedTime, startNanoTime,
174170
endNanoTime);
175171
} else {
176-
showMessages("FAILED: expected sleep time ", msSleepTime10s, true, elapsedTime, startNanoTime,
172+
showMessages("PASSED: expected sleep time ", msTime5s, true, nsElapsedTime, startNanoTime,
177173
endNanoTime);
178174
}
179175
} catch (InterruptedException ie) {
180176
ie.printStackTrace();
181177
}
182178
}
183179

184-
private void testThreadSleep() {
185-
new Thread(() -> testThreadParkHelper("testThreadSleep")).start();
180+
private Thread testThreadSleep() {
181+
Thread sleepThread = new Thread(() -> testThreadSleepHelper("testThreadSleep"));
182+
sleepThread.start();
183+
return sleepThread;
186184
}
187185

188-
private void testObjectWaitNotify() {
186+
private Thread testObjectWaitNotify() {
189187
Thread threadWait = new Thread(new Runnable() {
190188
public void run() {
191189
CRIUTestUtils.showThreadCurrentTime("testObjectWaitNotify() before wait()");
192190
try {
193-
final long startNanoTime = System.nanoTime();
191+
final long startNanoTime;
194192
synchronized (objWait) {
193+
startNanoTime = System.nanoTime();
195194
testResult.lockStatus = 1;
196195
objWait.wait();
197196
}
198-
boolean pass = false;
199197
final long endNanoTime = System.nanoTime();
200-
final long elapsedTime = endNanoTime - startNanoTime;
201-
if (elapsedTime >= msWaitNotify5s) {
202-
pass = true;
203-
}
204198
CRIUTestUtils.showThreadCurrentTime("testObjectWaitNotify() after wait()");
205-
if (pass) {
206-
showMessages("PASSED: expected wait time ", msWaitNotify5s, true, elapsedTime, startNanoTime,
199+
final long msElapsedTime = (endNanoTime - startNanoTime) / NANOS_PER_MILLI;
200+
if (msElapsedTime < msTime5s) {
201+
showMessages("FAILED: expected wait time ", msTime5s, true, msElapsedTime, startNanoTime,
207202
endNanoTime);
208203
} else {
209-
showMessages("FAILED: expected wait time ", msWaitNotify5s, true, elapsedTime, startNanoTime,
204+
showMessages("PASSED: expected wait time ", msTime5s, true, msElapsedTime, startNanoTime,
210205
endNanoTime);
211206
}
212207
} catch (InterruptedException ie) {
213208
ie.printStackTrace();
214209
}
215210
}
216211
});
217-
threadWait.setDaemon(true);
218212
threadWait.start();
213+
214+
return threadWait;
219215
}
220216

221217
private void testObjectWaitTimedHelper(String testName, long ms, int ns) {
222218
CRIUTestUtils.showThreadCurrentTime(testName + " before wait(" + ms + ", " + ns + ")");
223219
try {
224-
final long startNanoTime = System.nanoTime();
220+
final long startNanoTime;
225221
synchronized (objWait) {
222+
startNanoTime = System.nanoTime();
226223
testResult.lockStatus = 1;
227224
objWait.wait(ms, ns);
228225
}
229-
boolean pass = false;
230226
final long endNanoTime = System.nanoTime();
231-
final long elapsedTime = endNanoTime - startNanoTime;
232-
final long nsSleepTime = ms * NANOS_PER_MILLI + ns;
233-
if (elapsedTime >= nsSleepTime) {
234-
pass = true;
235-
}
236227
CRIUTestUtils.showThreadCurrentTime(testName + " after wait(" + ms + ", " + ns + ")");
237-
if (pass) {
238-
showMessages("PASSED: expected wait time ", nsSleepTime, false, elapsedTime, startNanoTime,
228+
final long nsElapsedTime = endNanoTime - startNanoTime;
229+
final long nsSleepTime = ms * NANOS_PER_MILLI + ns;
230+
if (nsElapsedTime < nsSleepTime) {
231+
showMessages("FAILED: expected wait time ", nsSleepTime, false, nsElapsedTime, startNanoTime,
239232
endNanoTime);
240233
} else {
241-
showMessages("FAILED: expected wait time ", nsSleepTime, false, elapsedTime, startNanoTime,
234+
showMessages("PASSED: expected wait time ", nsSleepTime, false, nsElapsedTime, startNanoTime,
242235
endNanoTime);
243236
}
244237
} catch (InterruptedException ie) {
245238
ie.printStackTrace();
246239
}
247240
}
248241

249-
private void testObjectWaitTimedNoNanoSecond() {
250-
new Thread(() -> testObjectWaitTimedHelper("testObjectWaitTimedNoNanoSecond", msSleepTime10s, 0)).start();
242+
private Thread testObjectWaitTimedNoNanoSecond() {
243+
Thread waitThread = new Thread(() -> testObjectWaitTimedHelper("testObjectWaitTimedNoNanoSecond", msTime5s, 0));
244+
waitThread.start();
245+
return waitThread;
251246
}
252247

253-
private void testObjectWaitTimedWithNanoSecond() {
254-
new Thread(() -> testObjectWaitTimedHelper("testObjectWaitTimedWithNanoSecond", msSleepTime10s, 500000)).start();
248+
private Thread testObjectWaitTimedWithNanoSecond() {
249+
Thread waitThread = new Thread(() -> testObjectWaitTimedHelper("testObjectWaitTimedWithNanoSecond", msTime5s, nsTime500kns));
250+
waitThread.start();
251+
return waitThread;
255252
}
256253
}

0 commit comments

Comments
 (0)