Skip to content

Commit bbebe70

Browse files
EcljpseB0Tjukzi
authored andcommitted
[tests] close some leaked Shells #2615
#2615
1 parent 51df26c commit bbebe70

File tree

10 files changed

+162
-38
lines changed

10 files changed

+162
-38
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2000, 2023 IBM Corporation and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* IBM Corporation - initial API and implementation
13+
*******************************************************************************/
14+
package org.eclipse.ui.tests;
15+
16+
import java.util.ArrayList;
17+
import java.util.List;
18+
import java.util.Set;
19+
20+
import org.eclipse.swt.widgets.Shell;
21+
import org.eclipse.ui.IWorkbench;
22+
import org.eclipse.ui.PlatformUI;
23+
import org.junit.Assert;
24+
import org.junit.rules.TestWatcher;
25+
import org.junit.runner.Description;
26+
27+
public class SwtLeakTestWatcher extends TestWatcher {
28+
29+
IWorkbench workbench;
30+
Set<Shell> preExistingShells;
31+
32+
@Override
33+
protected void starting(Description description) {
34+
workbench = PlatformUI.getWorkbench();
35+
preExistingShells = Set.of(workbench.getDisplay().getShells());
36+
super.starting(description);
37+
}
38+
39+
@Override
40+
protected void finished(Description description) {
41+
// Check for shell leak.
42+
List<String> leakedModalShellTitles = new ArrayList<>();
43+
Shell[] shells = workbench.getDisplay().getShells();
44+
for (Shell shell : shells) {
45+
if (!shell.isDisposed() && !preExistingShells.contains(shell)) {
46+
leakedModalShellTitles.add(shell.getText());
47+
// closing shell may introduce "not disposed" errors in next tests :
48+
// shell.close();
49+
}
50+
}
51+
if (!leakedModalShellTitles.isEmpty()) {
52+
Assert.fail(description.getClassName() + "." + description.getDisplayName()
53+
+ " Test leaked modal shell(s): [" + String.join(", ", leakedModalShellTitles) + "]");
54+
}
55+
super.finished(description);
56+
}
57+
58+
}

tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/concurrency/NestedSyncExecDeadlockTest.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,22 @@
2929
import org.eclipse.swt.widgets.Display;
3030
import org.eclipse.swt.widgets.Shell;
3131
import org.eclipse.ui.actions.WorkspaceModifyOperation;
32+
import org.eclipse.ui.tests.SwtLeakTestWatcher;
3233
import org.junit.After;
3334
import org.junit.Before;
35+
import org.junit.Rule;
3436
import org.junit.Test;
37+
import org.junit.rules.TestWatcher;
3538

3639
/**
3740
* This is a regression test for a case where a recursive attempt to syncExec
3841
* from within code that owns a lock would cause deadlock. See bug 76378 for details.
3942
*/
4043
public class NestedSyncExecDeadlockTest {
4144

45+
@Rule
46+
public TestWatcher swtLeakTestWatcher = new SwtLeakTestWatcher();
47+
4248
private static class ResourceListener implements IResourceChangeListener {
4349
@Override
4450
public void resourceChanged(IResourceChangeEvent event) {
@@ -53,7 +59,8 @@ public void resourceChanged(IResourceChangeEvent event) {
5359
private final IWorkspace workspace = ResourcesPlugin.getWorkspace();
5460

5561
public void doTest(final long timeToSleep) throws Exception {
56-
ProgressMonitorDialog dialog = new ProgressMonitorDialog(new Shell());
62+
Shell shell = new Shell();
63+
ProgressMonitorDialog dialog = new ProgressMonitorDialog(shell);
5764
dialog.run(true, false, new WorkspaceModifyOperation() {
5865
@Override
5966
public void execute(final IProgressMonitor pm) {
@@ -77,6 +84,7 @@ public void execute(final IProgressMonitor pm) {
7784
});
7885
}
7986
});
87+
shell.close();
8088
}
8189

8290
@Before

tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/concurrency/TestBug108162.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
2727
import org.eclipse.swt.widgets.Shell;
2828
import org.eclipse.ui.actions.WorkspaceModifyOperation;
29+
import org.eclipse.ui.tests.SwtLeakTestWatcher;
30+
import org.junit.Rule;
2931
import org.junit.Test;
32+
import org.junit.rules.TestWatcher;
3033

3134
/**
3235
* Tests the following sequence of events:
@@ -38,6 +41,10 @@
3841
* This test asserts that the exception is thrown and that deadlock does not occur.
3942
*/
4043
public class TestBug108162 {
44+
45+
@Rule
46+
public TestWatcher swtLeakTestWatcher = new SwtLeakTestWatcher();
47+
4148
static class LockAcquiringOperation extends WorkspaceModifyOperation {
4249
@Override
4350
public void execute(final IProgressMonitor pm) {
@@ -52,8 +59,9 @@ public void execute(final IProgressMonitor pm) {
5259
*/
5360
@Test
5461
public void testBug() throws CoreException {
62+
Shell shell = new Shell();
5563
workspace.run((IWorkspaceRunnable) monitor -> {
56-
ProgressMonitorDialog dialog = new ProgressMonitorDialog(new Shell());
64+
ProgressMonitorDialog dialog = new ProgressMonitorDialog(shell);
5765
try {
5866
dialog.run(true, false, new LockAcquiringOperation());
5967
// should not succeed
@@ -63,5 +71,6 @@ public void testBug() throws CoreException {
6371
// lock.
6472
}
6573
}, workspace.getRoot(), IResource.NONE, null);
74+
shell.close();
6675
}
6776
}

tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/concurrency/TestBug269121.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@
2929
import org.eclipse.swt.widgets.Shell;
3030
import org.eclipse.ui.actions.WorkspaceModifyOperation;
3131
import org.eclipse.ui.progress.UIJob;
32+
import org.eclipse.ui.tests.SwtLeakTestWatcher;
33+
import org.junit.Rule;
3234
import org.junit.Test;
35+
import org.junit.rules.TestWatcher;
3336

3437
import junit.framework.AssertionFailedError;
3538

@@ -56,6 +59,9 @@
5659
*/
5760
public class TestBug269121 {
5861

62+
@Rule
63+
public TestWatcher swtLeakTestWatcher = new SwtLeakTestWatcher();
64+
5965
@Test
6066
public void testBug() throws InterruptedException,
6167
InvocationTargetException {
@@ -73,8 +79,9 @@ protected void execute(IProgressMonitor monitor) {
7379
status.set(0, TestBarrier2.STATUS_DONE);
7480
}
7581
};
82+
Shell shell = new Shell();
7683
final ProgressMonitorDialog dialog = new ProgressMonitorDialog(
77-
new Shell());
84+
shell);
7885
Job statusJob = new Job("Checking for deadlock") {
7986
@Override
8087
protected IStatus run(IProgressMonitor monitor) {
@@ -104,5 +111,6 @@ protected IStatus run(IProgressMonitor monitor) {
104111
}
105112
job.join();
106113
assertTrue("Timeout occurred - possible Deadlock. See logging!", statusJob.getResult().isOK());
114+
shell.close();
107115
}
108116
}

tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/concurrency/TestBug98621.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@
2828
import org.eclipse.swt.widgets.Display;
2929
import org.eclipse.swt.widgets.Shell;
3030
import org.eclipse.ui.actions.WorkspaceModifyOperation;
31+
import org.eclipse.ui.tests.SwtLeakTestWatcher;
32+
import org.junit.Rule;
3133
import org.junit.Test;
34+
import org.junit.rules.TestWatcher;
3235

3336
/**
3437
* Tests the following sequence of events:
@@ -47,6 +50,10 @@
4750
* @since 3.2
4851
*/
4952
public class TestBug98621 {
53+
54+
@Rule
55+
public TestWatcher swtLeakTestWatcher = new SwtLeakTestWatcher();
56+
5057
class TransferTestOperation extends WorkspaceModifyOperation {
5158
@Override
5259
public void execute(final IProgressMonitor pm) {
@@ -80,8 +87,9 @@ public void threadChange(Thread thread) {
8087
*/
8188
@Test
8289
public void testBug() throws CoreException {
90+
Shell shell = new Shell();
8391
workspace.run((IWorkspaceRunnable) monitor -> {
84-
ProgressMonitorDialog dialog = new ProgressMonitorDialog(new Shell());
92+
ProgressMonitorDialog dialog = new ProgressMonitorDialog(shell);
8593
try {
8694
dialog.run(true, false, new TransferTestOperation());
8795
} catch (InvocationTargetException e1) {
@@ -91,5 +99,6 @@ public void testBug() throws CoreException {
9199
// ignore
92100
}
93101
}, workspace.getRoot(), IResource.NONE, null);
102+
shell.close();
94103
}
95104
}

tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/dialogs/DeprecatedUIPreferencesAuto.java

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
*******************************************************************************/
1414
package org.eclipse.ui.tests.dialogs;
1515

16+
import static org.junit.Assume.assumeNotNull;
17+
1618
import org.eclipse.jface.dialogs.Dialog;
1719
import org.eclipse.jface.preference.IPreferenceNode;
1820
import org.eclipse.jface.preference.PreferenceDialog;
@@ -21,11 +23,17 @@
2123
import org.eclipse.ui.PlatformUI;
2224
import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
2325
import org.eclipse.ui.internal.WorkbenchPlugin;
26+
import org.eclipse.ui.tests.SwtLeakTestWatcher;
2427
import org.eclipse.ui.tests.harness.util.DialogCheck;
28+
import org.junit.Rule;
2529
import org.junit.Test;
30+
import org.junit.rules.TestWatcher;
2631

2732
public class DeprecatedUIPreferencesAuto {
2833

34+
@Rule
35+
public TestWatcher swtLeakTestWatcher = new SwtLeakTestWatcher();
36+
2937
protected Shell getShell() {
3038
return DialogCheck.getShell();
3139
}
@@ -122,23 +130,22 @@ public void testProjectReferencesProp() {
122130
@Test
123131
public void testFieldEditorEnablePref() {
124132

125-
PreferenceDialogWrapper dialog = null;
126133
PreferenceManager manager = WorkbenchPlugin.getDefault().getPreferenceManager();
127-
if (manager != null) {
128-
dialog = new PreferenceDialogWrapper(getShell(), manager);
129-
dialog.create();
130-
131-
for (IPreferenceNode node : manager.getElements(PreferenceManager.PRE_ORDER)) {
132-
if (node.getId().equals("org.eclipse.ui.tests.dialogs.EnableTestPreferencePage")) {
133-
dialog.showPage(node);
134-
EnableTestPreferencePage page = (EnableTestPreferencePage) dialog.getPage(node);
135-
page.flipState();
136-
page.flipState();
137-
break;
138-
}
134+
assumeNotNull(manager);
135+
PreferenceDialogWrapper dialog = new PreferenceDialogWrapper(
136+
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), manager);
137+
dialog.create();
138+
139+
for (IPreferenceNode node : manager.getElements(PreferenceManager.PRE_ORDER)) {
140+
if (node.getId().equals("org.eclipse.ui.tests.dialogs.EnableTestPreferencePage")) {
141+
dialog.showPage(node);
142+
EnableTestPreferencePage page = (EnableTestPreferencePage) dialog.getPage(node);
143+
page.flipState();
144+
page.flipState();
145+
break;
139146
}
140147
}
141-
148+
dialog.close();
142149
}
143150

144151
}

tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/dialogs/UIPreferencesAuto.java

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
*******************************************************************************/
1414
package org.eclipse.ui.tests.dialogs;
1515

16+
import static org.junit.Assume.assumeNotNull;
17+
1618
import org.eclipse.jface.dialogs.Dialog;
1719
import org.eclipse.jface.preference.IPreferenceNode;
1820
import org.eclipse.jface.preference.PreferenceDialog;
@@ -21,11 +23,16 @@
2123
import org.eclipse.ui.PlatformUI;
2224
import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
2325
import org.eclipse.ui.internal.WorkbenchPlugin;
26+
import org.eclipse.ui.tests.SwtLeakTestWatcher;
2427
import org.eclipse.ui.tests.harness.util.DialogCheck;
28+
import org.junit.Rule;
2529
import org.junit.Test;
30+
import org.junit.rules.TestWatcher;
2631

2732
public class UIPreferencesAuto {
2833

34+
@Rule
35+
public TestWatcher swtLeakTestWatcher = new SwtLeakTestWatcher();
2936
protected Shell getShell() {
3037
return DialogCheck.getShell();
3138
}
@@ -124,25 +131,23 @@ public void testProjectReferencesProp() {
124131
*/
125132
@Test
126133
public void testFieldEditorEnablePref() {
127-
128-
PreferenceDialogWrapper dialog = null;
129134
PreferenceManager manager = WorkbenchPlugin.getDefault()
130135
.getPreferenceManager();
131-
if (manager != null) {
132-
dialog = new PreferenceDialogWrapper(getShell(), manager);
133-
dialog.create();
134-
135-
for (IPreferenceNode node : manager.getElements(PreferenceManager.PRE_ORDER)) {
136-
if (node.getId().equals("org.eclipse.ui.tests.dialogs.EnableTestPreferencePage")) {
137-
dialog.showPage(node);
138-
EnableTestPreferencePage page = (EnableTestPreferencePage) dialog.getPage(node);
139-
page.flipState();
140-
page.flipState();
141-
break;
142-
}
136+
assumeNotNull(manager);
137+
PreferenceDialogWrapper dialog = new PreferenceDialogWrapper(
138+
PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), manager);
139+
dialog.create();
140+
141+
for (IPreferenceNode node : manager.getElements(PreferenceManager.PRE_ORDER)) {
142+
if (node.getId().equals("org.eclipse.ui.tests.dialogs.EnableTestPreferencePage")) {
143+
dialog.showPage(node);
144+
EnableTestPreferencePage page = (EnableTestPreferencePage) dialog.getPage(node);
145+
page.flipState();
146+
page.flipState();
147+
break;
143148
}
144149
}
145-
150+
dialog.close();
146151
}
147152

148153
}

tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/dialogs/UIWizardsAuto.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,22 @@
3939
import org.eclipse.ui.internal.dialogs.NewWizard;
4040
import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
4141
import org.eclipse.ui.internal.wizards.newresource.ResourceMessages;
42+
import org.eclipse.ui.tests.SwtLeakTestWatcher;
4243
import org.eclipse.ui.tests.harness.util.DialogCheck;
4344
import org.eclipse.ui.tests.harness.util.FileUtil;
4445
import org.eclipse.ui.wizards.newresource.BasicNewFileResourceWizard;
4546
import org.eclipse.ui.wizards.newresource.BasicNewFolderResourceWizard;
4647
import org.eclipse.ui.wizards.newresource.BasicNewProjectResourceWizard;
4748
import org.junit.After;
4849
import org.junit.Ignore;
50+
import org.junit.Rule;
4951
import org.junit.Test;
52+
import org.junit.rules.TestWatcher;
5053

5154
public class UIWizardsAuto {
55+
@Rule
56+
public TestWatcher swtLeakTestWatcher = new SwtLeakTestWatcher();
57+
5258
private static final int SIZING_WIZARD_WIDTH = 470;
5359

5460
private static final int SIZING_WIZARD_HEIGHT = 550;
@@ -332,7 +338,8 @@ private void checkWizardWindowTitle(String windowTitle) {
332338

333339
initNewWizard(newWizard);
334340

335-
WizardDialog dialog = new WizardDialog(getShell(), newWizard);
341+
WizardDialog dialog = new WizardDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
342+
newWizard);
336343
dialog.create();
337344

338345
if(windowTitle == null) {

0 commit comments

Comments
 (0)