From 23193f47a156f8b490bd9a0d125e72b2b93c59a0 Mon Sep 17 00:00:00 2001 From: Martin Oberhuber < martin.oberhuber@windriver.com> Date: Fri, 15 Feb 2008 15:47:33 +0000 Subject: [PATCH] [219086] flush event queue to shield tests from each other --- .../eclipse/rse/tests/core/HostMoveTest.java | 21 ++++--- .../rse/tests/core/RSECoreTestCase.java | 61 ++++++++++++++++++- .../tests/internal/RSEConnectionManager.java | 8 +++ 3 files changed, 79 insertions(+), 11 deletions(-) diff --git a/rse/tests/org.eclipse.rse.tests/src/org/eclipse/rse/tests/core/HostMoveTest.java b/rse/tests/org.eclipse.rse.tests/src/org/eclipse/rse/tests/core/HostMoveTest.java index 8200297051f..88970fec23a 100644 --- a/rse/tests/org.eclipse.rse.tests/src/org/eclipse/rse/tests/core/HostMoveTest.java +++ b/rse/tests/org.eclipse.rse.tests/src/org/eclipse/rse/tests/core/HostMoveTest.java @@ -50,30 +50,35 @@ public class HostMoveTest extends RSEBaseConnectionTestCase { super.tearDown(); } - public void testMoveOneUp() { + public void testMoveOneUp() throws Exception { //-test-author-:DavidDykstal checkPrecondition(); IHost host = hostArray[NUMBER_OF_HOSTS - 1]; IHost[] hosts = new IHost[] {host}; registry.moveHosts("TestProfile", hosts, -1); assertEquals(NUMBER_OF_HOSTS - 2, registry.getHostPosition(host)); + flushEventQueue(); + assertEquals(NUMBER_OF_HOSTS - 2, registry.getHostPosition(host)); registry.moveHosts("TestProfile", hosts, 1); assertEquals(NUMBER_OF_HOSTS - 1, registry.getHostPosition(host)); } - public void testMoveManyUp() { + public void testMoveManyUp() throws Exception { //-test-author-:DavidDykstal checkPrecondition(); IHost[] hosts = new IHost[] {hostArray[NUMBER_OF_HOSTS - 1], hostArray[NUMBER_OF_HOSTS - 2]}; registry.moveHosts("TestProfile", hosts, -2); assertEquals(NUMBER_OF_HOSTS - 3, registry.getHostPosition(hostArray[NUMBER_OF_HOSTS - 1])); assertEquals(NUMBER_OF_HOSTS - 4, registry.getHostPosition(hostArray[NUMBER_OF_HOSTS - 2])); + flushEventQueue(); + assertEquals(NUMBER_OF_HOSTS - 3, registry.getHostPosition(hostArray[NUMBER_OF_HOSTS - 1])); + assertEquals(NUMBER_OF_HOSTS - 4, registry.getHostPosition(hostArray[NUMBER_OF_HOSTS - 2])); registry.moveHosts("TestProfile", hosts, 2); assertEquals(NUMBER_OF_HOSTS - 1, registry.getHostPosition(hostArray[NUMBER_OF_HOSTS - 1])); assertEquals(NUMBER_OF_HOSTS - 2, registry.getHostPosition(hostArray[NUMBER_OF_HOSTS - 2])); } - public void testMoveFirstUp() { + public void testMoveFirstUp() throws Exception { //-test-author-:DavidDykstal checkPrecondition(); IHost host = hostArray[0]; @@ -83,7 +88,7 @@ public class HostMoveTest extends RSEBaseConnectionTestCase { assertEquals(0, registry.getHostPosition(host)); } - public void testMoveOneDown() { + public void testMoveOneDown() throws Exception { //-test-author-:DavidDykstal checkPrecondition(); IHost host = hostArray[1]; // second in the list @@ -95,7 +100,7 @@ public class HostMoveTest extends RSEBaseConnectionTestCase { assertEquals(1, registry.getHostPosition(host)); } - public void testMoveManyDown() { + public void testMoveManyDown() throws Exception { //-test-author-:DavidDykstal checkPrecondition(); IHost[] hosts = new IHost[] {hostArray[0], hostArray[2], hostArray[4]}; @@ -112,7 +117,7 @@ public class HostMoveTest extends RSEBaseConnectionTestCase { assertEquals(4, registry.getHostPosition(hostArray[4])); } - public void testMoveLastDown() { + public void testMoveLastDown() throws Exception { //-test-author-:DavidDykstal checkPrecondition(); IHost host = hostArray[NUMBER_OF_HOSTS - 1]; @@ -122,7 +127,7 @@ public class HostMoveTest extends RSEBaseConnectionTestCase { assertEquals(NUMBER_OF_HOSTS - 1, registry.getHostPosition(host)); } - public void testNoHost() { + public void testNoHost() throws Exception { //-test-author-:DavidDykstal checkPrecondition(); IHost[] hosts = new IHost[] {}; @@ -132,7 +137,7 @@ public class HostMoveTest extends RSEBaseConnectionTestCase { /** * Create the test hosts. */ - private void createHosts() { + private void createHosts() throws Exception { hostArray = new IHost[NUMBER_OF_HOSTS]; diff --git a/rse/tests/org.eclipse.rse.tests/src/org/eclipse/rse/tests/core/RSECoreTestCase.java b/rse/tests/org.eclipse.rse.tests/src/org/eclipse/rse/tests/core/RSECoreTestCase.java index 3497056c206..8fc6b49841a 100644 --- a/rse/tests/org.eclipse.rse.tests/src/org/eclipse/rse/tests/core/RSECoreTestCase.java +++ b/rse/tests/org.eclipse.rse.tests/src/org/eclipse/rse/tests/core/RSECoreTestCase.java @@ -1,5 +1,5 @@ /******************************************************************************** - * Copyright (c) 2006, 2007 Wind River Systems, Inc. and others. + * Copyright (c) 2006, 2008 Wind River Systems, Inc. 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 @@ -9,6 +9,7 @@ * Uwe Stieber (Wind River) - initial API and implementation. * Martin Oberhuber (Wind River) - fix build against 3.2.1, fix javadoc errors * Martin Oberhuber (Wind River) - [168870] refactor org.eclipse.rse.core package of the UI plugin + * Martin Oberhuber (Wind River) - [219086] flush event queue to shield tests from each other ********************************************************************************/ package org.eclipse.rse.tests.core; @@ -48,6 +49,7 @@ import org.eclipse.rse.persistence.IRSEPersistenceManager; import org.eclipse.rse.tests.RSETestsPlugin; import org.eclipse.rse.tests.core.RSEWaitAndDispatchUtil.IInterruptCondition; import org.eclipse.rse.ui.SystemBasePlugin; +import org.eclipse.swt.widgets.Display; import org.eclipse.ui.IViewPart; import org.eclipse.ui.IViewReference; import org.eclipse.ui.IWorkbench; @@ -59,7 +61,7 @@ import org.eclipse.ui.WorkbenchException; import org.osgi.framework.Bundle; /** - * Core RSE test case infra structure implementation. + * Core RSE test case infrastructure implementation. */ public class RSECoreTestCase extends TestCase { // Test properties storage. @@ -351,7 +353,7 @@ public class RSECoreTestCase extends TestCase { /** * Wait until the SystemProfileManager has finished loading all "autoload" profiles, * and the RSEUIPlugin InitRSEJob has finished filling it with the default connections. - * @throws InterruptedException + * @throws InterruptedException when initialization is interrupted e.g. by shutting down Eclipse. */ protected void waitForRSEWorkspaceInit() throws InterruptedException { //RSEUIPlugin is loaded automatically because RSETestsPlugins extends SystemBasePlugin, @@ -385,12 +387,65 @@ public class RSECoreTestCase extends TestCase { waitForRSEWorkspaceInit(); switchMaximizeSystemsView(); } + + /** + * Flush the event queue in order to ensure that no left-over events influence later test cases. + *

+ * Unhandled exceptions in the event loop event are caught as follows: + * In case multiple events from the event loop throw exceptions these are printed + * to stdout. The first exception found in the event loop is thrown to the caller. + * + * @throws Exception in case an unhandled event loop exception was found. + */ + protected void flushEventQueue() throws Exception { + Display display = Display.getCurrent(); + if (display!=null) { + //on the dispatch thread already + Exception eventLoopException = null; + while(!display.isDisposed()) { + //loop until event queue is flushed + try { + if (!display.readAndDispatch()) { + break; + } + } catch(Exception e) { + if (eventLoopException==null) { + eventLoopException = e; + } else { + System.out.println("Multiple unhandled event loop exceptions:"); + e.printStackTrace(); + } + } + } + if (eventLoopException!=null) { + throw eventLoopException; + } + } else { + //calling from background thread + final Exception[] ex = new Exception[1]; + display = Display.getDefault(); + display.syncExec(new Runnable() { + public void run() { + try { + flushEventQueue(); + } catch(Exception e) { + ex[0] = e; + } + } + }); + if (ex[0]!=null) throw ex[0]; + } + } /* (non-Javadoc) * @see junit.framework.TestCase#tearDown() */ protected void tearDown() throws Exception { restoreMaximizeSystemsView(); + //if running on main thread: wait until all asynchronous events are fired, + //in order to ensure that individual test cases do not influence each other + //See also https://bugs.eclipse.org/bugs/show_bug.cgi?id=219086 + flushEventQueue(); super.tearDown(); } diff --git a/rse/tests/org.eclipse.rse.tests/src/org/eclipse/rse/tests/internal/RSEConnectionManager.java b/rse/tests/org.eclipse.rse.tests/src/org/eclipse/rse/tests/internal/RSEConnectionManager.java index 2685e2b5d5b..dae487148cf 100644 --- a/rse/tests/org.eclipse.rse.tests/src/org/eclipse/rse/tests/internal/RSEConnectionManager.java +++ b/rse/tests/org.eclipse.rse.tests/src/org/eclipse/rse/tests/internal/RSEConnectionManager.java @@ -14,6 +14,7 @@ * Martin Oberhuber (Wind River) - [177523] Unify singleton getter methods * Martin Oberhuber (Wind River) - [186773] split ISystemRegistryUI from ISystemRegistry * David Dykstal (IBM) - [217556] remove service subsystem types + * Martin Oberhuber (Wind River) - [219086] flush event queue to shield tests from each other *******************************************************************************/ package org.eclipse.rse.tests.internal; @@ -49,6 +50,7 @@ import org.eclipse.rse.tests.RSETestsPlugin; import org.eclipse.rse.tests.core.connection.IRSEConnectionManager; import org.eclipse.rse.tests.core.connection.IRSEConnectionProperties; import org.eclipse.rse.tests.testsubsystem.interfaces.ITestSubSystem; +import org.eclipse.swt.widgets.Display; import org.osgi.framework.Bundle; /** @@ -244,6 +246,12 @@ public class RSEConnectionManager implements IRSEConnectionManager { } } Assert.assertNotNull("FAILED(findOrCreateConnection): Failed to find and/or create connection IHost object!", connection); //$NON-NLS-1$ + final Display display = Display.getCurrent(); + if (display!=null) { + while(!display.isDisposed() && display.readAndDispatch()) { + //running on main thread: wait until all async events are fired + } + } return connection; }