|
17 | 17 | import static org.assertj.core.api.Assertions.assertThat;
|
18 | 18 | import static org.junit.Assert.assertEquals;
|
19 | 19 | import static org.junit.Assert.assertFalse;
|
| 20 | +import static org.junit.Assert.assertNotEquals; |
20 | 21 | import static org.junit.Assert.assertNotNull;
|
21 | 22 | import static org.junit.Assert.assertNotSame;
|
22 | 23 | import static org.junit.Assert.assertNull;
|
|
42 | 43 | import java.util.Map;
|
43 | 44 | import java.util.Set;
|
44 | 45 | import java.util.concurrent.atomic.AtomicBoolean;
|
| 46 | +import java.util.concurrent.atomic.AtomicInteger; |
45 | 47 |
|
46 | 48 | import org.eclipse.core.filesystem.EFS;
|
47 | 49 | import org.eclipse.core.filesystem.IFileSystem;
|
|
55 | 57 | import org.eclipse.core.resources.ResourcesPlugin;
|
56 | 58 | import org.eclipse.core.runtime.CoreException;
|
57 | 59 | import org.eclipse.core.runtime.IPath;
|
| 60 | +import org.eclipse.core.runtime.IProgressMonitor; |
| 61 | +import org.eclipse.core.runtime.NullProgressMonitor; |
58 | 62 | import org.eclipse.core.runtime.Platform;
|
| 63 | +import org.eclipse.core.runtime.Status; |
59 | 64 | import org.eclipse.core.runtime.preferences.IEclipsePreferences;
|
60 | 65 | import org.eclipse.core.runtime.preferences.InstanceScope;
|
61 | 66 | import org.eclipse.debug.core.DebugPlugin;
|
|
64 | 69 | import org.eclipse.debug.core.ILaunchConfigurationListener;
|
65 | 70 | import org.eclipse.debug.core.ILaunchConfigurationType;
|
66 | 71 | import org.eclipse.debug.core.ILaunchConfigurationWorkingCopy;
|
| 72 | +import org.eclipse.debug.core.ILaunchDelegate; |
67 | 73 | import org.eclipse.debug.core.ILaunchManager;
|
68 | 74 | import org.eclipse.debug.core.ILaunchesListener2;
|
69 | 75 | import org.eclipse.debug.core.Launch;
|
| 76 | +import org.eclipse.debug.core.model.ILaunchConfigurationDelegate2; |
70 | 77 | import org.eclipse.debug.core.model.IProcess;
|
71 | 78 | import org.eclipse.debug.internal.core.LaunchConfiguration;
|
72 | 79 | import org.eclipse.debug.internal.core.LaunchManager;
|
@@ -1331,6 +1338,79 @@ public void launchesTerminated(ILaunch[] launches) {
|
1331 | 1338 | }
|
1332 | 1339 | }
|
1333 | 1340 |
|
| 1341 | + /** |
| 1342 | + * Do not return null or throw on cancel |
| 1343 | + * |
| 1344 | + * @see https://github.com/eclipse-platform/eclipse.platform/issues/2009 |
| 1345 | + * @see org.eclipse.debug.core.ILaunchConfiguration#launch(String, |
| 1346 | + * IProgressMonitor) |
| 1347 | + */ |
| 1348 | + @Test |
| 1349 | + public void testCancel() throws CoreException { |
| 1350 | + final ILaunchDelegate launchDelegate = ((LaunchManager) DebugPlugin.getDefault().getLaunchManager()).getLaunchDelegate(LaunchConfigurationTests.ID_TEST_LAUNCH_TYPE); |
| 1351 | + final TestLaunchDelegate testLaunchDelegate = (TestLaunchDelegate) launchDelegate.getDelegate(); |
| 1352 | + ILaunchConfigurationDelegate2 customLaunchDelegate = new ILaunchConfigurationDelegate2() { |
| 1353 | + |
| 1354 | + void sleep() { |
| 1355 | + } |
| 1356 | + @Override |
| 1357 | + public void launch(ILaunchConfiguration configuration, String mode, ILaunch launch, IProgressMonitor monitor) throws CoreException { |
| 1358 | + sleep(); |
| 1359 | + if (monitor.isCanceled()) { |
| 1360 | + throw new CoreException(Status.CANCEL_STATUS); |
| 1361 | + } |
| 1362 | + } |
| 1363 | + |
| 1364 | + @Override |
| 1365 | + public boolean preLaunchCheck(ILaunchConfiguration configuration, String mode, IProgressMonitor monitor) throws CoreException { |
| 1366 | + sleep(); |
| 1367 | + return !monitor.isCanceled(); |
| 1368 | + } |
| 1369 | + |
| 1370 | + @Override |
| 1371 | + public ILaunch getLaunch(ILaunchConfiguration configuration, String mode) throws CoreException { |
| 1372 | + sleep(); |
| 1373 | + return null; |
| 1374 | + } |
| 1375 | + |
| 1376 | + @Override |
| 1377 | + public boolean finalLaunchCheck(ILaunchConfiguration configuration, String mode, IProgressMonitor monitor) throws CoreException { |
| 1378 | + sleep(); |
| 1379 | + return !monitor.isCanceled(); |
| 1380 | + } |
| 1381 | + |
| 1382 | + @Override |
| 1383 | + public boolean buildForLaunch(ILaunchConfiguration configuration, String mode, IProgressMonitor monitor) throws CoreException { |
| 1384 | + sleep(); |
| 1385 | + return !monitor.isCanceled(); |
| 1386 | + } |
| 1387 | + }; |
| 1388 | + testLaunchDelegate.setDelegate(customLaunchDelegate); |
| 1389 | + try { |
| 1390 | + ILaunchConfigurationWorkingCopy workingCopy = newConfiguration(null, "cancel me"); //$NON-NLS-1$ |
| 1391 | + int cancelCount = 0; |
| 1392 | + for (int i = 0; i < 10_000; i++) { |
| 1393 | + AtomicInteger checkCount = new AtomicInteger(i); |
| 1394 | + NullProgressMonitor monitor = new NullProgressMonitor() { |
| 1395 | + @Override |
| 1396 | + public boolean isCanceled() { |
| 1397 | + return super.isCanceled() || checkCount.getAndDecrement() <= 0; |
| 1398 | + } |
| 1399 | + }; |
| 1400 | + // Should not throw |
| 1401 | + ILaunch launch = workingCopy.launch(ILaunchManager.RUN_MODE, monitor); |
| 1402 | + assertNotNull(launch); |
| 1403 | + if (!monitor.isCanceled()) { |
| 1404 | + break; |
| 1405 | + } |
| 1406 | + cancelCount++; |
| 1407 | + } |
| 1408 | + assertNotEquals(0, cancelCount); |
| 1409 | + } finally { |
| 1410 | + testLaunchDelegate.setDelegate(null); |
| 1411 | + } |
| 1412 | + } |
| 1413 | + |
1334 | 1414 | /**
|
1335 | 1415 | * Tests that a launch is properly registered for notifications before a
|
1336 | 1416 | * process is spawned and may already propagate a termination event.
|
|
0 commit comments