Skip to content

Commit 2900435

Browse files
8328629: JUnit test without a timeout value can hang indefinitely
Reviewed-by: angorya, arapte, mhanl
1 parent 21601f8 commit 2900435

File tree

29 files changed

+92
-37
lines changed

29 files changed

+92
-37
lines changed

build.gradle

+7
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,11 @@ ext.IS_SWT_TEST = Boolean.parseBoolean(SWT_TEST);
537537
defineProperty("UNSTABLE_TEST", "false")
538538
ext.IS_UNSTABLE_TEST = Boolean.parseBoolean(UNSTABLE_TEST);
539539

540+
// Default timeout values to use in JUnit for tests and lifecycle methods
541+
// that don't specify one
542+
defineProperty("JUNIT_TEST_TIMEOUT", "120s")
543+
defineProperty("JUNIT_LIFECYCLE_TIMEOUT", "20s")
544+
540545
// Toggle diagnostic output from the Gradle workaround and the Sandbox test apps.
541546
defineProperty("WORKER_DEBUG", "false")
542547
ext.IS_WORKER_DEBUG = Boolean.parseBoolean(WORKER_DEBUG);
@@ -2184,6 +2189,8 @@ allprojects {
21842189
}
21852190

21862191
systemProperty 'unstable.test', IS_UNSTABLE_TEST
2192+
systemProperty 'junit.jupiter.execution.timeout.test.method.default', JUNIT_TEST_TIMEOUT
2193+
systemProperty 'junit.jupiter.execution.timeout.lifecycle.method.default', JUNIT_LIFECYCLE_TIMEOUT
21872194
}
21882195

21892196
compileTestJava {

modules/javafx.controls/src/test/java/test/javafx/scene/control/AlertTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import static org.junit.jupiter.api.Assertions.assertEquals;
2828
import static org.junit.jupiter.api.Assertions.assertFalse;
2929
import static org.junit.jupiter.api.Assertions.assertTrue;
30+
import static org.junit.jupiter.api.Assertions.fail;
3031
import java.util.Locale;
3132
import javafx.application.Platform;
3233
import javafx.scene.control.Alert;
@@ -94,7 +95,7 @@ private void showAndHideDialog(Dialog<?> dialog, boolean normalClose) {
9495
Platform.runLater(() -> getStage(dialog).close());
9596
}
9697
} catch (InterruptedException e) {
97-
e.printStackTrace();
98+
fail(e);
9899
}
99100
}).start();
100101

modules/javafx.controls/src/test/java/test/javafx/scene/control/ListViewTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ public void cleanup() {
504504
try {
505505
Thread.sleep(100);
506506
} catch (InterruptedException ex) {
507-
ex.printStackTrace();
507+
fail(ex);
508508
}
509509

510510
// the list is totally changing (it is being cleared), so we should

modules/javafx.controls/src/test/java/test/javafx/scene/control/ProgressIndicatorTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import static org.junit.jupiter.api.Assertions.assertEquals;
2929
import static org.junit.jupiter.api.Assertions.assertTrue;
30+
import static org.junit.jupiter.api.Assertions.fail;
3031
import java.lang.ref.WeakReference;
3132
import java.util.ArrayList;
3233
import javafx.geometry.Pos;
@@ -211,7 +212,7 @@ private void attemptGC(int n) {
211212
try {
212213
Thread.sleep(500);
213214
} catch (InterruptedException e) {
214-
System.err.println("InterruptedException occurred during Thread.sleep()");
215+
fail(e);
215216
}
216217
}
217218
}

modules/javafx.controls/src/test/java/test/javafx/scene/control/SelectionFocusModelMemoryTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
package test.javafx.scene.control;
2727

2828
import static org.junit.jupiter.api.Assertions.assertNull;
29+
import static org.junit.jupiter.api.Assertions.fail;
2930
import java.lang.ref.WeakReference;
3031
import java.util.Collection;
3132
import java.util.List;
@@ -242,7 +243,7 @@ private void attemptGC(WeakReference<?> weakRef, int n) {
242243
try {
243244
Thread.sleep(500);
244245
} catch (InterruptedException e) {
245-
System.err.println("InterruptedException occurred during Thread.sleep()");
246+
fail(e);
246247
}
247248
}
248249
}

modules/javafx.controls/src/test/java/test/javafx/scene/control/ToggleButtonTest.java

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import static org.junit.jupiter.api.Assertions.assertNull;
3131
import static org.junit.jupiter.api.Assertions.assertSame;
3232
import static org.junit.jupiter.api.Assertions.assertTrue;
33+
import static org.junit.jupiter.api.Assertions.fail;
3334
import static test.com.sun.javafx.scene.control.infrastructure.ControlTestUtils.assertPseudoClassDoesNotExist;
3435
import static test.com.sun.javafx.scene.control.infrastructure.ControlTestUtils.assertPseudoClassExists;
3536
import static test.com.sun.javafx.scene.control.infrastructure.ControlTestUtils.assertStyleClassContains;
@@ -246,6 +247,7 @@ public void setup() {
246247
Thread.sleep(2000);
247248
} catch (InterruptedException ex) {
248249
PlatformLogger.getLogger(ToggleButtonTest.class.getName()).severe(null, ex);
250+
fail(ex);
249251
}
250252
assertTrue(flag[0], "fire() doesnt emit ActionEvent!");
251253
}

modules/javafx.controls/src/test/java/test/javafx/scene/control/skin/ProgressBarSkinTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import static org.junit.jupiter.api.Assertions.assertEquals;
2929
import static org.junit.jupiter.api.Assertions.assertNull;
3030
import static org.junit.jupiter.api.Assertions.assertTrue;
31+
import static org.junit.jupiter.api.Assertions.fail;
3132
import java.lang.ref.WeakReference;
3233
import javafx.beans.value.ObservableValue;
3334
import javafx.scene.Scene;
@@ -125,7 +126,7 @@ private void attemptGC(int n) {
125126
try {
126127
Thread.sleep(500);
127128
} catch (InterruptedException e) {
128-
System.err.println("InterruptedException occurred during Thread.sleep()");
129+
fail(e);
129130
}
130131
}
131132
}

modules/javafx.controls/src/test/java/test/javafx/scene/control/skin/ScrollPaneSkinTest.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import static org.junit.jupiter.api.Assertions.assertEquals;
2929
import static org.junit.jupiter.api.Assertions.assertTrue;
30+
import static org.junit.jupiter.api.Assertions.fail;
3031
import java.lang.ref.WeakReference;
3132
import java.util.ArrayList;
3233
import javafx.beans.value.ObservableValue;
@@ -192,8 +193,9 @@ public void growW() {
192193
while (continueTest == false && count < 10) {
193194
try {
194195
Thread.sleep(100);
196+
} catch (InterruptedException e) {
197+
fail(e);
195198
}
196-
catch (Exception e) {}
197199
count++;
198200
}
199201

@@ -241,8 +243,9 @@ public void growW() {
241243
while (continueTest == false && count < 10) {
242244
try {
243245
Thread.sleep(100);
246+
} catch (InterruptedException e) {
247+
fail(e);
244248
}
245-
catch (Exception e) {}
246249
count++;
247250
}
248251

modules/javafx.graphics/src/test/java/test/com/sun/javafx/util/WeakReferenceQueueTest.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import static org.junit.jupiter.api.Assertions.assertEquals;
3535
import static org.junit.jupiter.api.Assertions.assertFalse;
3636
import static org.junit.jupiter.api.Assertions.assertTrue;
37+
import static org.junit.jupiter.api.Assertions.fail;
3738

3839
public class WeakReferenceQueueTest {
3940
@Test
@@ -203,7 +204,11 @@ private void tryGCReallyHard() {
203204
}
204205

205206
// finally, give the VM some idle time to perform gc
206-
try { Thread.sleep(100); } catch (InterruptedException e) {}
207+
try {
208+
Thread.sleep(100);
209+
} catch (InterruptedException e) {
210+
fail(e);
211+
}
207212

208213
// hope that worked!
209214
}

modules/javafx.web/src/test/java/test/javafx/scene/web/HistoryTest.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,11 @@ public void changed(ObservableValue<? extends String> observable, String oldValu
115115
//
116116
history.getEntries().get(history.getCurrentIndex() - 1).lastVisitedDateProperty().addListener(newDateListener());
117117

118-
try { Thread.sleep(150); } catch (Exception e) {} // ensure the next date doesn't fit into the same millisecond
118+
try {
119+
Thread.sleep(150); // ensure the next date doesn't fit into the same millisecond
120+
} catch (InterruptedException e) {
121+
fail(e);
122+
}
119123

120124
history.currentIndexProperty().addListener(new ChangeListener<Number>() {
121125
@Override
@@ -237,7 +241,11 @@ public void changed(ObservableValue<? extends java.lang.Number> observable, Numb
237241

238242
history.getEntries().get(history.getCurrentIndex()).lastVisitedDateProperty().addListener(newDateListener());
239243

240-
try { Thread.sleep(150); } catch (Exception e) {} // ensure the next date doesn't fit into the same millisecond
244+
try {
245+
Thread.sleep(150); // ensure the next date doesn't fit into the same millisecond
246+
} catch (InterruptedException e) {
247+
fail(e);
248+
}
241249

242250
reload();
243251

modules/javafx.web/src/test/java/test/javafx/scene/web/TestBase.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
package test.javafx.scene.web;
2727

28+
import static org.junit.jupiter.api.Assertions.fail;
29+
2830
import java.awt.Color;
2931
import java.io.File;
3032
import java.util.concurrent.Callable;
@@ -63,7 +65,9 @@ public static void setupOnce() {
6365

6466
try {
6567
startupLatch.await();
66-
} catch (InterruptedException ex) {}
68+
} catch (InterruptedException ex) {
69+
fail(ex);
70+
}
6771
}
6872

6973
public TestBase() {

tests/system/src/test/java/test/com/sun/glass/ui/monocle/LensUInput.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525

2626
package test.com.sun.glass.ui.monocle;
2727

28+
import static org.junit.jupiter.api.Assertions.fail;
29+
2830
import com.sun.glass.ui.monocle.LinuxInputShim;
2931
import com.sun.glass.ui.monocle.LinuxSystemShim;
3032
import javafx.animation.AnimationTimer;
@@ -232,7 +234,9 @@ private long openPipe(String path) throws IOException {
232234
if (LinuxSystemShim.errno() == LinuxSystemShim.ENXIO) { // no reader on pipe
233235
try {
234236
Thread.sleep(100l);
235-
} catch (InterruptedException e) { }
237+
} catch (InterruptedException e) {
238+
fail(e);
239+
}
236240
} else {
237241
break;
238242
}
@@ -266,7 +270,7 @@ public void write(ByteBuffer buffer) throws IOException {
266270
try {
267271
Thread.sleep(1);
268272
} catch (InterruptedException e) {
269-
e.printStackTrace();
273+
fail(e);
270274
}
271275
} else {
272276
throw new IOException(LinuxSystemShim.getErrorMessage());

tests/system/src/test/java/test/com/sun/javafx/application/StaticStartupTest.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ public void testStartupFromClinit() throws Exception {
4242
Thread thr = new Thread(() -> {
4343
try {
4444
Thread.sleep(20000);
45-
} catch (InterruptedException ex) {}
45+
} catch (InterruptedException ex) {
46+
// OK to not rethrow; the exit 1 signals an error
47+
}
4648
System.err.println("Test timeout exceeded -- calling System.exit");
4749
System.exit(1);
4850
});

tests/system/src/test/java/test/com/sun/javafx/image/impl/ImageRaceTest.java

+9-3
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@ public void run() {
6969
while (!ready) {
7070
try {
7171
sleep(1);
72-
} catch (InterruptedException ex) {}
72+
} catch (InterruptedException ex) {
73+
fail(ex);
74+
}
7375
}
7476
init.get();
7577
if (verbose) System.err.println(getName()+" done");
@@ -83,7 +85,9 @@ void forkAndJoinInitializers() {
8385
while (!i.isRunning() && System.currentTimeMillis() < limit) {
8486
try {
8587
Thread.sleep(1);
86-
} catch (InterruptedException ex) {}
88+
} catch (InterruptedException ex) {
89+
fail(ex);
90+
}
8791
}
8892
if (!i.isRunning()) {
8993
throw new RuntimeException("Initializer "+i+" never started");
@@ -93,7 +97,9 @@ void forkAndJoinInitializers() {
9397
if (verbose) System.err.println("\n[main] signal the threads to proceed\n");
9498
try {
9599
Thread.sleep(100);
96-
} catch (InterruptedException ex) {}
100+
} catch (InterruptedException ex) {
101+
fail(ex);
102+
}
97103
ready = true;
98104

99105
limit = System.currentTimeMillis() + TIMEOUT;

tests/system/src/test/java/test/com/sun/javafx/sg/prism/RT36296Test.java

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
package test.com.sun.javafx.sg.prism;
2626

2727
import static org.junit.jupiter.api.Assertions.assertEquals;
28+
import static org.junit.jupiter.api.Assertions.fail;
2829
import java.util.concurrent.CountDownLatch;
2930
import java.util.concurrent.TimeUnit;
3031
import java.util.logging.Level;
@@ -93,6 +94,7 @@ public void TestBug() {
9394
latch.await();
9495
} catch (InterruptedException ex) {
9596
Logger.getLogger(RT36296Test.class.getName()).log(Level.SEVERE, null, ex);
97+
fail(ex);
9698
}
9799
}
98100

tests/system/src/test/java/test/com/sun/javafx/tk/quantum/SystemMenuBarTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ private void createMenuBarWithItemsStage() {
159159
try {
160160
Thread.sleep(20);
161161
} catch (Exception e) {
162-
e.printStackTrace();
162+
fail(e);
163163
}
164164
Platform.runLater(() -> {
165165
menu.getItems().clear();

tests/system/src/test/java/test/com/sun/marlin/QPathTest.java

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import static org.junit.jupiter.api.Assertions.assertEquals;
2929
import static org.junit.jupiter.api.Assertions.assertFalse;
30+
import static org.junit.jupiter.api.Assertions.fail;
3031
import java.util.Locale;
3132
import java.util.concurrent.CountDownLatch;
3233
import java.util.concurrent.TimeUnit;
@@ -200,6 +201,7 @@ public void TestBug() {
200201
latch.await();
201202
} catch (InterruptedException ie) {
202203
Logger.getLogger(QPathTest.class.getName()).log(Level.SEVERE, "interrupted", ie);
204+
fail(ie);
203205
}
204206
assertFalse(doChecksFailed, "DoChecks detected a problem.");
205207
}

tests/system/src/test/java/test/com/sun/marlin/ScaleX0Test.java

+1
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ public void testMarlinAIOOBEwhenScaleXIs0() {
113113
Thread.sleep(500L);
114114
} catch (InterruptedException ie) {
115115
Logger.getLogger(ScaleX0Test.class.getName()).log(Level.SEVERE, "interrupted", ie);
116+
fail(ie);
116117
}
117118

118119
// Restore stderr:

tests/system/src/test/java/test/javafx/embed/swing/JFXPanelEmbeddedWindowTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*/
2525
package test.javafx.embed.swing;
2626

27+
import static org.junit.jupiter.api.Assertions.fail;
2728
import java.util.concurrent.CountDownLatch;
2829
import java.util.concurrent.TimeUnit;
2930
import javax.swing.JFrame;
@@ -80,7 +81,7 @@ public void testShowThenRemove() throws Throwable {
8081
try {
8182
innerLatch.await(5, TimeUnit.SECONDS);
8283
} catch (InterruptedException e) {
83-
e.printStackTrace();
84+
fail(e);
8485
}
8586
SwingUtilities.invokeLater(() -> outerLatch.countDown());
8687
});

tests/system/src/test/java/test/javafx/embed/swing/SwingNodeContentMemoryLeakTest.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
package test.javafx.embed.swing;
2727

2828
import static org.junit.jupiter.api.Assertions.assertFalse;
29+
import static org.junit.jupiter.api.Assertions.fail;
2930
import java.lang.ref.WeakReference;
3031
import java.lang.reflect.InvocationTargetException;
3132
import java.util.Collection;
@@ -85,7 +86,7 @@ public void testSwingNodeContentMemoryLeak() throws InterruptedException,
8586
try {
8687
Thread.sleep(100);
8788
} catch (InterruptedException e) {
88-
e.printStackTrace();
89+
fail(e);
8990
}
9091

9192
SwingUtilities.invokeLater(() -> {

tests/system/src/test/java/test/javafx/scene/text/ArabicWrappingTest.java

+2
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
package test.javafx.scene.text;
2727

2828
import static org.junit.jupiter.api.Assertions.assertTrue;
29+
import static org.junit.jupiter.api.Assertions.fail;
2930
import java.io.OutputStream;
3031
import java.io.PrintStream;
3132
import java.util.Timer;
@@ -108,6 +109,7 @@ public void testWrapping() {
108109
try {
109110
Thread.sleep(2000);
110111
} catch (Exception e) {
112+
fail(e);
111113
}
112114
}
113115

tests/system/src/test/java/test/robot/com/sun/glass/ui/monocle/MonocleUInput.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
package test.robot.com.sun.glass.ui.monocle;
2727

28+
import static org.junit.jupiter.api.Assertions.fail;
2829
import java.io.IOException;
2930
import java.nio.ByteBuffer;
3031
import java.nio.channels.Pipe;
@@ -97,7 +98,7 @@ protected void destroyDevice() {
9798
try {
9899
latch.await();
99100
} catch (InterruptedException e) {
100-
e.printStackTrace();
101+
fail(e);
101102
}
102103
}
103104
device = null;

0 commit comments

Comments
 (0)