Skip to content

Commit 5d1cbe5

Browse files
SyntevoAlexiloveeclipse
authored andcommitted
Bug 576655 - Use ResourceTracker in JUnit tests
The goal of this change is to automatically detect leaks in SWT internal code such as leak in CTabFolder.setFont() that was fixed in 569752. Such errors happen rarely, but on the other hand, detecting them is cheap, so it makes sense to detect. I decided to enable tracking in 'AllNonBrowserTests' because it's often chosen over 'AllTests'. Also fixes some existing leaks in JUnit tests. Change-Id: Ida9c2a0b31465546adedbe6bd1850d1d9ac618e7 Signed-off-by: Alexandr Miloslavskiy <[email protected]> Reviewed-on: https://git.eclipse.org/r/c/platform/eclipse.platform.swt/+/186543 Tested-by: Platform Bot <[email protected]> Reviewed-by: Andrey Loskutov <[email protected]>
1 parent de93ab0 commit 5d1cbe5

File tree

3 files changed

+59
-1
lines changed

3 files changed

+59
-1
lines changed

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/AllNonBrowserTests.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,16 @@
1414
*******************************************************************************/
1515
package org.eclipse.swt.tests.junit;
1616

17+
import org.eclipse.swt.graphics.Resource;
18+
import org.junit.AfterClass;
19+
import org.junit.BeforeClass;
1720
import org.junit.runner.JUnitCore;
1821
import org.junit.runner.RunWith;
1922
import org.junit.runners.Suite;
2023

24+
import java.util.ArrayList;
25+
import java.util.List;
26+
2127
/**
2228
* Suite for running most SWT test cases (all except for browser tests).
2329
*/
@@ -41,6 +47,50 @@
4147
Test_org_eclipse_swt_accessibility_AccessibleEvent.class,
4248
Test_org_eclipse_swt_accessibility_AccessibleTextEvent.class })
4349
public class AllNonBrowserTests {
50+
private static List<Error> leakedResources;
51+
52+
@BeforeClass
53+
public static void beforeClass() {
54+
// Set up ResourceTracked to detect any leaks
55+
leakedResources = new ArrayList<> ();
56+
Resource.setNonDisposeHandler (error -> {
57+
// Can't 'Assert.fail()' here, because Handler is called on
58+
// some other thread and Exception will simply be ignored.
59+
// Workaround: collect errors and report later.
60+
leakedResources.add (error);
61+
});
62+
}
63+
64+
/*
65+
* It would be easier to understand if errors here were reported
66+
* through a test and not through @AfterClass, but this is a
67+
* suite class and not a test class, so it can't have tests.
68+
*/
69+
@AfterClass
70+
public static void afterClass() {
71+
// Run GC in order do detect any outstanding leaks
72+
System.gc ();
73+
// And wait a bit to let Resource.ResourceTracker, that is
74+
// queued to another thread, do its job. It is unfortunate that
75+
// there is no simple better way to wait except waiting some
76+
// fixed timeout.
77+
try {
78+
Thread.sleep (100);
79+
} catch (InterruptedException e) {
80+
// Just ignore
81+
}
82+
83+
for (Error leak : leakedResources) {
84+
// For some reason, printing to System.err in JUnit test has no effect
85+
leak.printStackTrace (System.out);
86+
}
87+
88+
if (0 != leakedResources.size ()) {
89+
// In order to help tools to filter fail reason out of entire log, throw the first leak.
90+
String hint = leakedResources.size () + " leaks found, the first is:";
91+
throw new AssertionError(hint, leakedResources.get (0));
92+
}
93+
}
4494

4595
public static void main(String[] args) {
4696
JUnitCore.main(AllNonBrowserTests.class.getName());

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_custom_StyledText_multiCaretsSelections.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,18 @@ protected void failed(Throwable e, org.junit.runner.Description description) {
5050

5151
Shell shell;
5252
StyledText text;
53+
GC gc;
5354

5455
@Before
5556
public void setUp() {
5657
shell = new Shell();
5758
text = new StyledText(shell, SWT.NULL);
59+
gc = new GC(text);
5860
}
5961

6062
@After
6163
public void tearDown() {
64+
gc.dispose();
6265
shell.dispose();
6366
}
6467
@Test
@@ -184,7 +187,7 @@ private Event paintEvent() {
184187
paintEvent.height = text.getSize().y;
185188
paintEvent.widget = text;
186189
paintEvent.type = SWT.Paint;
187-
paintEvent.gc = new GC(text);
190+
paintEvent.gc = gc;
188191
return paintEvent;
189192
}
190193

tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_graphics_Image.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,11 @@ public void test_bug566545_efficientGrayscaleImage() {
11161116
ImageTestUtil.assertImagesEqual(imageDataIndexed, imageDataDirect);
11171117
ImageTestUtil.assertImagesEqual(imageIndexed.getImageData(), imageDirect.getImageData());
11181118
ImageTestUtil.assertImagesEqual(outImageIndexed.getImageData(), outImageDirect.getImageData());
1119+
1120+
imageIndexed.dispose();
1121+
imageDirect.dispose();
1122+
outImageIndexed.dispose();
1123+
outImageDirect.dispose();
11191124
}
11201125

11211126
}

0 commit comments

Comments
 (0)