Skip to content

Commit cad8b8d

Browse files
committed
Repeat access to reveal more leak scenarios
1 parent b6a0583 commit cad8b8d

File tree

2 files changed

+58
-14
lines changed

2 files changed

+58
-14
lines changed

tests/org.eclipse.rcptt.core.tests/src/org/eclipse/rcptt/core/tests/NoErrorsInLog.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
*******************************************************************************/
1111
package org.eclipse.rcptt.core.tests;
1212

13+
import java.util.Arrays;
14+
1315
import org.eclipse.core.runtime.CoreException;
1416
import org.eclipse.core.runtime.ILog;
1517
import org.eclipse.core.runtime.ILogListener;
@@ -37,6 +39,13 @@ public NoErrorsInLog(Class<?> clazzToWatchLogFor) {
3739
this.log = Platform.getLog(FrameworkUtil.getBundle(clazzToWatchLogFor));
3840
this.statusLog = new MultiStatus("org.eclipse.rcptt.tesla.swt.test", 0, "Errors in the error log", null);
3941
}
42+
43+
public void assertNoErrors() {
44+
synchronized (statusLog) {
45+
checkErrors();
46+
}
47+
}
48+
4049
@Override
4150
protected void before() throws Throwable {
4251
super.before();
@@ -48,11 +57,19 @@ protected void after() {
4857
System.gc(); // Detect leaks with org.eclipse.rcptt.core.persistence.LeakDetector
4958
log.removeLogListener(listener);
5059
synchronized (statusLog) {
51-
if (statusLog.matches(IStatus.ERROR)) {
52-
throw new AssertionError(new CoreException(statusLog));
53-
}
60+
checkErrors();
5461
}
5562
super.after();
5663
}
5764

65+
private void checkErrors() throws AssertionError {
66+
if (statusLog.matches(IStatus.ERROR)) {
67+
Arrays.stream(statusLog.getChildren()).filter(s -> s.matches(IStatus.ERROR)).map(s -> s.getException())
68+
.filter(RuntimeException.class::isInstance).map(RuntimeException.class::cast).forEach(e -> {
69+
throw e;
70+
});
71+
throw new AssertionError(new CoreException(statusLog));
72+
}
73+
}
74+
5875
}

tests/org.eclipse.rcptt.core.tests/src/org/eclipse/rcptt/internal/core/model/Q7NamedElementTest.java

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,16 @@
1010
*******************************************************************************/
1111
package org.eclipse.rcptt.internal.core.model;
1212

13+
import static org.junit.Assert.assertTrue;
14+
1315
import java.io.IOException;
1416
import java.io.InputStream;
17+
import java.util.function.Consumer;
1518

1619
import org.eclipse.core.resources.IFile;
1720
import org.eclipse.core.resources.IProject;
1821
import org.eclipse.core.resources.IProjectDescription;
22+
import org.eclipse.core.resources.IResource;
1923
import org.eclipse.core.resources.IWorkspace;
2024
import org.eclipse.core.resources.ResourcesPlugin;
2125
import org.eclipse.core.runtime.CoreException;
@@ -54,17 +58,40 @@ public void before() throws CoreException {
5458
}
5559

5660
@Test
57-
public void noResourceleaks() throws CoreException, IOException {
58-
try (InputStream is = getClass().getResourceAsStream("testcase.test")) {
59-
TESTCASE_FILE.create(is, IFile.REPLACE|IFile.FORCE, null);
60-
}
61-
IFile previousFile = TESTCASE_FILE;
62-
for (int i = 0; i < 10000; i++) {
63-
IFile currentFile = PROJECT.getFile("t"+i+".test");
64-
previousFile.move(currentFile.getFullPath(), true, false, null);
65-
previousFile = currentFile;
66-
ITestCase testcase = (ITestCase) RcpttCore.create(currentFile);
67-
Assert.assertEquals("_-dqP0BOHEeOQfY3L4mNcSA", testcase.getID());
61+
public void noResourceleaksID() {
62+
noResourceleaks(testcase -> Assert.assertEquals("_-dqP0BOHEeOQfY3L4mNcSA", testcase.getID()));
63+
}
64+
65+
@Test
66+
public void noResourceleaksExists() {
67+
noResourceleaks(testcase -> assertTrue(testcase.exists()));
68+
}
69+
70+
71+
private interface ThrowingConsumer<T> {
72+
void accept(T data) throws Exception;
73+
}
74+
75+
public void noResourceleaks(ThrowingConsumer<ITestCase> action) {
76+
try {
77+
try (InputStream is = getClass().getResourceAsStream("testcase.test")) {
78+
TESTCASE_FILE.create(is, IFile.REPLACE|IFile.FORCE, null);
79+
}
80+
IFile previousFile = TESTCASE_FILE;
81+
for (int i = 0; i < 1000; i++) {
82+
IFile currentFile = PROJECT.getFile("t"+i+".test");
83+
previousFile.move(currentFile.getFullPath(), true, false, null);
84+
previousFile = currentFile;
85+
ITestCase testcase = (ITestCase) RcpttCore.create(currentFile);
86+
action.accept(testcase);
87+
if ( i % 2 == 0) {
88+
action.accept(testcase); // sometimes single access does not cause leak
89+
}
90+
System.gc(); // Detect leaks with org.eclipse.rcptt.core.persistence.LeakDetector
91+
NO_ERRORS.assertNoErrors();
92+
}
93+
} catch (Exception e) {
94+
throw new AssertionError(e);
6895
}
6996
}
7097

0 commit comments

Comments
 (0)