diff options
author | Andrey Loskutov | 2017-05-25 12:09:51 +0000 |
---|---|---|
committer | Andrey Loskutov | 2017-07-03 14:22:15 +0000 |
commit | 19f71fda3568e05b39ec569758d21f02c1e6ab3c (patch) | |
tree | 7be18b8d3733358ea4bf7ef44901a9b808df5ca5 | |
parent | 1cd6adec4e47244e9d6766e30ba74e5a6e9248c3 (diff) | |
download | eclipse.platform.debug-19f71fda3568e05b39ec569758d21f02c1e6ab3c.tar.gz eclipse.platform.debug-19f71fda3568e05b39ec569758d21f02c1e6ab3c.tar.xz eclipse.platform.debug-19f71fda3568e05b39ec569758d21f02c1e6ab3c.zip |
Bug 439017 - Replace TestHelper with TestUtilI20170703-2000
Change-Id: I8de74690df87054e73e6ee8111a0f88bb6aea87f
Signed-off-by: Andrey Loskutov <loskutov@gmx.de>
5 files changed, 66 insertions, 98 deletions
diff --git a/org.eclipse.debug.tests/src/org/eclipse/debug/tests/TestUtil.java b/org.eclipse.debug.tests/src/org/eclipse/debug/tests/TestUtil.java index 1453b64dd..fba4831a7 100644 --- a/org.eclipse.debug.tests/src/org/eclipse/debug/tests/TestUtil.java +++ b/org.eclipse.debug.tests/src/org/eclipse/debug/tests/TestUtil.java @@ -73,16 +73,35 @@ public class TestUtil { } /** - * Utility for waiting until the execution of jobs of any family has finished or timeout is reached. If no jobs are running, the method waits - * given minimum wait time. While this method is waiting for jobs, UI events are processed. + * Process all queued UI events. If called from background thread, just + * waits + */ + public static void processUIEvents(final long millis) throws Exception { + long start = System.currentTimeMillis(); + while (System.currentTimeMillis() - start < millis) { + Display display = Display.getCurrent(); + if (display != null && !display.isDisposed()) { + while (display.readAndDispatch()) { + // loop until the queue is empty + } + } else { + Thread.sleep(10); + } + } + } + + /** + * Utility for waiting until the execution of jobs of any family has + * finished or timeout is reached. If no jobs are running, the method waits + * given minimum wait time. While this method is waiting for jobs, UI events + * are processed. * - * @param owner - * name of the caller which will be logged as prefix if the wait times out - * @param minTimeMs - * minimum wait time in milliseconds - * @param maxTimeMs - * maximum wait time in milliseconds - * @return true if the method timed out, false if all the jobs terminated before the timeout + * @param owner name of the caller which will be logged as prefix if the + * wait times out + * @param minTimeMs minimum wait time in milliseconds + * @param maxTimeMs maximum wait time in milliseconds + * @return true if the method timed out, false if all the jobs terminated + * before the timeout */ public static boolean waitForJobs(String owner, long minTimeMs, long maxTimeMs) { return waitForJobs(owner, minTimeMs, maxTimeMs, (Object[]) null); diff --git a/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/ConsoleManagerTests.java b/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/ConsoleManagerTests.java index 277ad9797..5ae877f15 100644 --- a/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/ConsoleManagerTests.java +++ b/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/ConsoleManagerTests.java @@ -17,6 +17,7 @@ import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import org.eclipse.debug.tests.AbstractDebugTest; +import org.eclipse.debug.tests.TestUtil; import org.eclipse.jface.resource.ImageDescriptor; import org.eclipse.jface.util.IPropertyChangeListener; import org.eclipse.swt.SWT; @@ -25,7 +26,6 @@ import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Listener; import org.eclipse.ui.IViewPart; -import org.eclipse.ui.IViewReference; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.console.ConsolePlugin; @@ -40,7 +40,6 @@ import org.eclipse.ui.part.MessagePage; */ public class ConsoleManagerTests extends AbstractDebugTest { - private static final String INTROVIEW_ID = "org.eclipse.ui.internal.introview"; //$NON-NLS-1$ private ExecutorService executorService; private IConsoleManager manager; private int count; @@ -58,8 +57,7 @@ public class ConsoleManagerTests extends AbstractDebugTest { executorService = Executors.newFixedThreadPool(count); manager = ConsolePlugin.getDefault().getConsoleManager(); IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); - hideWelcomePage(activePage); - TestHelper.processUIEvents(100); + TestUtil.processUIEvents(100); consoles = new ConsoleMock[count]; for (int i = 0; i < count; i++) { final ConsoleMock console = new ConsoleMock(i + 1); @@ -70,7 +68,7 @@ public class ConsoleManagerTests extends AbstractDebugTest { IViewPart consoleView = activePage.showView("org.eclipse.ui.console.ConsoleView"); //$NON-NLS-1$ activePage.activate(consoleView); - TestHelper.processUIEvents(100); + TestUtil.processUIEvents(100); // The test is unstable ("show" event on the the first console seem to // be not always sent), so make sure console view has shown at least @@ -78,8 +76,8 @@ public class ConsoleManagerTests extends AbstractDebugTest { firstConsole = new ConsoleMock(0); manager.addConsoles(new ConsoleMock[] { firstConsole }); manager.showConsoleView(firstConsole); - TestHelper.waitForJobs(); - TestHelper.processUIEvents(100); + TestUtil.waitForJobs(getName(), 200, 5000); + TestUtil.processUIEvents(100); ConsoleMock.allShownConsoles.set(0); } @@ -88,24 +86,10 @@ public class ConsoleManagerTests extends AbstractDebugTest { executorService.shutdownNow(); manager.removeConsoles(consoles); manager.removeConsoles(new ConsoleMock[] { firstConsole }); - TestHelper.processUIEvents(100); + TestUtil.processUIEvents(100); super.tearDown(); } - private void hideWelcomePage(IWorkbenchPage activePage) { - IViewReference[] refs = activePage.getViewReferences(); - IViewPart intro = null; - for (IViewReference ref : refs) { - if (INTROVIEW_ID.equals(ref.getId())) { - intro = ref.getView(false); - } - } - if (intro != null) { - activePage.hideView(intro); - TestHelper.processUIEvents(100); - } - } - /** * The test triggers {@link #count} simultaneous calls to the * {@link IConsoleManager#showConsoleView(IConsole)} and checks if all of @@ -122,15 +106,15 @@ public class ConsoleManagerTests extends AbstractDebugTest { showConsole(console); } System.out.println("All tasks scheduled, processing UI events now..."); //$NON-NLS-1$ - TestHelper.processUIEvents(1000); + TestUtil.processUIEvents(1000); // Console manager starts a job with delay, let wait for him a bit System.out.println("Waiting on jobs now..."); //$NON-NLS-1$ - TestHelper.waitForJobs(); + TestUtil.waitForJobs(getName(), 200, 5000); // Give UI a chance to proceed pending console manager jobs System.out.println("Done with jobs, processing UI events again..."); //$NON-NLS-1$ - TestHelper.processUIEvents(3000); + TestUtil.processUIEvents(3000); executorService.shutdown(); @@ -138,7 +122,7 @@ public class ConsoleManagerTests extends AbstractDebugTest { boolean OK = waitForExecutorService(); if (!OK) { System.out.println("Timed out..."); //$NON-NLS-1$ - TestHelper.processUIEvents(10000); + TestUtil.processUIEvents(10000); // timeout? assertTrue("Timeout occurred while waiting on console to be shown", //$NON-NLS-1$ @@ -150,12 +134,12 @@ public class ConsoleManagerTests extends AbstractDebugTest { assertEquals("Only " + shown + " consoles were shown from " + count, count, shown); //$NON-NLS-1$ //$NON-NLS-2$ } - private boolean waitForExecutorService() throws InterruptedException { + private boolean waitForExecutorService() throws Exception { for (int i = 0; i < 60; i++) { if (executorService.awaitTermination(1, TimeUnit.SECONDS)) { return true; } - TestHelper.processUIEvents(100); + TestUtil.processUIEvents(100); } return false; } @@ -171,7 +155,7 @@ public class ConsoleManagerTests extends AbstractDebugTest { latch.await(1, TimeUnit.MINUTES); System.out.println("Requesting to show: " + console); //$NON-NLS-1$ manager.showConsoleView(console); - TestHelper.waitForJobs(); + TestUtil.waitForJobs(getName(), 200, 5000); } catch (InterruptedException e) { e.printStackTrace(); Thread.interrupted(); diff --git a/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/ConsoleTests.java b/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/ConsoleTests.java index f59f0c75d..d0704a8d9 100644 --- a/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/ConsoleTests.java +++ b/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/ConsoleTests.java @@ -15,6 +15,7 @@ import java.io.IOException; import java.nio.charset.StandardCharsets; import org.eclipse.debug.tests.AbstractDebugTest; +import org.eclipse.debug.tests.TestUtil; import org.eclipse.jface.text.IDocument; import org.eclipse.ui.console.IConsoleConstants; import org.eclipse.ui.console.IOConsoleOutputStream; @@ -42,21 +43,21 @@ public class ConsoleTests extends AbstractDebugTest { MessageConsole console = new MessageConsole("Test Console", //$NON-NLS-1$ IConsoleConstants.MESSAGE_CONSOLE_TYPE, null, StandardCharsets.UTF_8.name(), true); IDocument document = console.getDocument(); - TestHelper.waitForJobs(); + TestUtil.waitForJobs(getName(), 200, 5000); TestCase.assertEquals("Document should be empty", "", document.get()); //$NON-NLS-1$ //$NON-NLS-2$ try (IOConsoleOutputStream outStream = console.newOutputStream()) { outStream.write(testStringBuffer, 0, 6); // half of ö (\u00f6) is written so we don't expect this char in // output but all previous chars can be decoded - TestHelper.waitForJobs(); + TestUtil.waitForJobs(getName(), 200, 5000); TestCase.assertEquals("First 4 chars should be written", testString.substring(0, 4), document.get()); //$NON-NLS-1$ outStream.write(testStringBuffer, 6, 6); // all remaining bytes are written so we expect the whole string // including the ö (\u00f6) which was at buffer boundary - TestHelper.waitForJobs(); + TestUtil.waitForJobs(getName(), 200, 5000); TestCase.assertEquals("whole test string should be written", testString, document.get()); //$NON-NLS-1$ } - TestHelper.waitForJobs(); + TestUtil.waitForJobs(getName(), 200, 5000); // after closing the stream, the document content should still be the // same TestCase.assertEquals("closing the stream should not alter the document", testString, document.get()); //$NON-NLS-1$ @@ -69,15 +70,15 @@ public class ConsoleTests extends AbstractDebugTest { MessageConsole console = new MessageConsole("Test Console 2", //$NON-NLS-1$ IConsoleConstants.MESSAGE_CONSOLE_TYPE, null, StandardCharsets.UTF_8.name(), true); IDocument document = console.getDocument(); - TestHelper.waitForJobs(); + TestUtil.waitForJobs(getName(), 200, 5000); TestCase.assertEquals("Document should be empty", "", document.get()); //$NON-NLS-1$ //$NON-NLS-2$ try (IOConsoleOutputStream outStream = console.newOutputStream()) { outStream.write(testStringBuffer); // everything but pending \r should be written - TestHelper.waitForJobs(); + TestUtil.waitForJobs(getName(), 200, 5000); TestCase.assertEquals("First char should be written", testString.substring(0, 1), document.get()); //$NON-NLS-1$ } - TestHelper.waitForJobs(); + TestUtil.waitForJobs(getName(), 200, 5000); // after closing the stream, the document content should still be the // same TestCase.assertEquals("closing the stream should write the pending \\r", testString, document.get()); //$NON-NLS-1$ diff --git a/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/TestHelper.java b/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/TestHelper.java deleted file mode 100644 index 981459a79..000000000 --- a/org.eclipse.debug.tests/src/org/eclipse/debug/tests/console/TestHelper.java +++ /dev/null @@ -1,49 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2017 Andreas Loth and others. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * Andreas Loth - initial API and implementation - *******************************************************************************/ - -package org.eclipse.debug.tests.console; - -import org.eclipse.core.runtime.jobs.Job; -import org.eclipse.swt.widgets.Display; -import org.eclipse.ui.PlatformUI; - - -public final class TestHelper { - - private TestHelper() { - throw new AssertionError("No instances of this utility class!"); //$NON-NLS-1$ - } - - public static void processUIEvents(final long millis) { - long start = System.currentTimeMillis(); - while (System.currentTimeMillis() - start < millis) { - while (PlatformUI.getWorkbench().getDisplay().readAndDispatch()) { - // loop untile the queue is empty - } - } - } - - public static void waitForJobs() throws InterruptedException { - if (Display.getCurrent() == null) { - Thread.sleep(200); - } else { - processUIEvents(200); - } - while (!Job.getJobManager().isIdle()) { - if (Display.getCurrent() == null) { - Thread.sleep(200); - } else { - processUIEvents(200); - } - } - } - -} diff --git a/org.eclipse.debug.tests/src/org/eclipse/debug/tests/launching/LaunchGroupTests.java b/org.eclipse.debug.tests/src/org/eclipse/debug/tests/launching/LaunchGroupTests.java index a15ddbdf5..c17d13078 100644 --- a/org.eclipse.debug.tests/src/org/eclipse/debug/tests/launching/LaunchGroupTests.java +++ b/org.eclipse.debug.tests/src/org/eclipse/debug/tests/launching/LaunchGroupTests.java @@ -20,6 +20,7 @@ import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.debug.core.ILaunch; import org.eclipse.debug.core.ILaunchConfiguration; @@ -35,6 +36,7 @@ import org.eclipse.debug.internal.core.groups.GroupLaunchConfigurationDelegate; import org.eclipse.debug.internal.core.groups.GroupLaunchElement; import org.eclipse.debug.internal.core.groups.GroupLaunchElement.GroupElementPostLaunchAction; import org.eclipse.debug.internal.ui.launchConfigurations.LaunchHistory; +import org.eclipse.debug.tests.TestUtil; import org.eclipse.debug.ui.IDebugUIConstants; public class LaunchGroupTests extends AbstractLaunchTest { @@ -77,7 +79,20 @@ public class LaunchGroupTests extends AbstractLaunchTest { protected void tearDown() throws Exception { // make sure listener is removed getLaunchManager().removeLaunchListener(lcListener); - + ILaunch[] launches = getLaunchManager().getLaunches(); + for (ILaunch launch : launches) { + try { + if (!launch.isTerminated()) { + IProcess[] processes = launch.getProcesses(); + for (IProcess process : processes) { + process.terminate(); + } + launch.terminate(); + } + } catch (Exception e) { + TestUtil.log(IStatus.ERROR, getName(), "Error terminating launch: " + launch, e); + } + } super.tearDown(); } @@ -170,8 +185,7 @@ public class LaunchGroupTests extends AbstractLaunchTest { } } } catch (Exception e) { - // uh oh - e.printStackTrace(); + TestUtil.log(IStatus.ERROR, getName(), e.getMessage(), e); } } @@ -193,7 +207,6 @@ public class LaunchGroupTests extends AbstractLaunchTest { assertTrue("history[2] should be Test1", history[2].contentsEqual(t1)); //$NON-NLS-1$ } - public void testAdopt() throws Exception { final ILaunchConfiguration t1 = getLaunchConfiguration("Test1"); //$NON-NLS-1$ final ILaunchConfiguration grp = createLaunchGroup(DEF_GRP_NAME, createLaunchGroupElement(t1, GroupElementPostLaunchAction.NONE, null, false), createLaunchGroupElement(t1, GroupElementPostLaunchAction.NONE, null, true)); |