diff options
5 files changed, 203 insertions, 2 deletions
diff --git a/bundles/org.eclipse.equinox.compendium.tests/META-INF/MANIFEST.MF b/bundles/org.eclipse.equinox.compendium.tests/META-INF/MANIFEST.MF index f0557bf73..0c50b1c15 100644 --- a/bundles/org.eclipse.equinox.compendium.tests/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.equinox.compendium.tests/META-INF/MANIFEST.MF @@ -14,7 +14,8 @@ Import-Package: junit.framework;version="3.8.2", org.osgi.service.event;version="1.1.0", org.osgi.service.metatype;version="1.2", org.osgi.service.packageadmin;version="1.2.0", - org.osgi.service.useradmin;version="1.0" + org.osgi.service.useradmin;version="1.0", + org.osgi.service.coordinator;version="1.0" Bundle-RequiredExecutionEnvironment: J2SE-1.4 Bundle-Localization: plugin Bundle-Vendor: %bundleVendor diff --git a/bundles/org.eclipse.equinox.compendium.tests/src/org/eclipse/equinox/compendium/tests/Activator.java b/bundles/org.eclipse.equinox.compendium.tests/src/org/eclipse/equinox/compendium/tests/Activator.java index fb83665fd..a3447abfc 100644 --- a/bundles/org.eclipse.equinox.compendium.tests/src/org/eclipse/equinox/compendium/tests/Activator.java +++ b/bundles/org.eclipse.equinox.compendium.tests/src/org/eclipse/equinox/compendium/tests/Activator.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008, 2010 IBM Corporation and others + * Copyright (c) 2008, 2011 IBM Corporation 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 @@ -18,6 +18,7 @@ import org.osgi.util.tracker.ServiceTracker; * The activator class controls the plug-in life cycle */ public class Activator implements BundleActivator { + public static String BUNDLE_COORDINATOR = "org.eclipse.equinox.coordinator"; //$NON-NLS-1$ public static String BUNDLE_EVENT = "org.eclipse.equinox.event"; //$NON-NLS-1$ public static String BUNDLE_METATYPE = "org.eclipse.equinox.metatype"; //$NON-NLS-1$ public static String BUNDLE_USERADMIN = "org.eclipse.equinox.useradmin"; //$NON-NLS-1$ diff --git a/bundles/org.eclipse.equinox.compendium.tests/src/org/eclipse/equinox/compendium/tests/AllTests.java b/bundles/org.eclipse.equinox.compendium.tests/src/org/eclipse/equinox/compendium/tests/AllTests.java index 3e612162f..c5e518962 100644 --- a/bundles/org.eclipse.equinox.compendium.tests/src/org/eclipse/equinox/compendium/tests/AllTests.java +++ b/bundles/org.eclipse.equinox.compendium.tests/src/org/eclipse/equinox/compendium/tests/AllTests.java @@ -22,6 +22,7 @@ public class AllTests { suite.addTest(org.eclipse.equinox.metatype.tests.AllTests.suite()); suite.addTest(org.eclipse.equinox.useradmin.tests.AllTests.suite()); suite.addTest(org.eclipse.equinox.event.tests.AllTests.suite()); + suite.addTest(org.eclipse.equinox.coordinator.tests.AllTests.suite()); return suite; } diff --git a/bundles/org.eclipse.equinox.compendium.tests/src/org/eclipse/equinox/coordinator/tests/AllTests.java b/bundles/org.eclipse.equinox.compendium.tests/src/org/eclipse/equinox/coordinator/tests/AllTests.java new file mode 100644 index 000000000..b7d988e77 --- /dev/null +++ b/bundles/org.eclipse.equinox.compendium.tests/src/org/eclipse/equinox/coordinator/tests/AllTests.java @@ -0,0 +1,22 @@ +/******************************************************************************* + * Copyright (c) 2011 IBM Corporation 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: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.equinox.coordinator.tests; + +import junit.framework.Test; +import junit.framework.TestSuite; + +public class AllTests { + public static Test suite() { + TestSuite suite = new TestSuite("Tests for Equinox Coordinator"); //$NON-NLS-1$ + suite.addTestSuite(CoordinationMaxTimeoutTest.class); + return suite; + } +} diff --git a/bundles/org.eclipse.equinox.compendium.tests/src/org/eclipse/equinox/coordinator/tests/CoordinationMaxTimeoutTest.java b/bundles/org.eclipse.equinox.compendium.tests/src/org/eclipse/equinox/coordinator/tests/CoordinationMaxTimeoutTest.java new file mode 100644 index 000000000..7741d7aa3 --- /dev/null +++ b/bundles/org.eclipse.equinox.compendium.tests/src/org/eclipse/equinox/coordinator/tests/CoordinationMaxTimeoutTest.java @@ -0,0 +1,176 @@ +/******************************************************************************* + * Copyright (c) 2011 IBM Corporation 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: + * IBM Corporation - initial API and implementation + *******************************************************************************/ +package org.eclipse.equinox.coordinator.tests; + +import junit.framework.TestCase; +import org.eclipse.equinox.compendium.tests.Activator; +import org.osgi.framework.ServiceReference; +import org.osgi.service.coordinator.*; + +/* + * Ensures the Coordinator implementation honors a specified maximum timeout + * for coordinations. + * + * https://bugs.eclipse.org/bugs/show_bug.cgi?id=362137 + */ +public class CoordinationMaxTimeoutTest extends TestCase { + private static final long DEVIATION = 500; + private static final String PROPERTY_NAME = "org.eclipse.equinox.coordinator.timeout"; //$NON-NLS-1$ + private static final long TIMEOUT = 5000; + + private Coordinator coordinator; + private ServiceReference coordinatorRef; + + public void testMaxTimeoutWithNoTimeout() throws Exception { + long start = System.currentTimeMillis(); + Coordination c = coordinator.create("c", 0); //$NON-NLS-1$ + try { + assertTimeout(start, TIMEOUT, c); + c.join(10000); + assertTerminated(c); + assertTimeoutDuration(start, TIMEOUT); + } finally { + try { + c.end(); + } catch (CoordinationException e) { + // noop + } + } + } + + public void testMaxTimeoutWithGreaterTimeout() throws Exception { + long start = System.currentTimeMillis(); + Coordination c = coordinator.create("c", 10000); //$NON-NLS-1$ + try { + assertTimeout(start, TIMEOUT, c); + c.join(10000); + assertTerminated(c); + assertTimeoutDuration(start, TIMEOUT); + } finally { + try { + c.end(); + } catch (CoordinationException e) { + // noop + } + } + } + + public void testMaxTimeoutWithLesserTimeout() throws Exception { + long start = System.currentTimeMillis(); + long timeout = 2000; + Coordination c = coordinator.create("c", timeout); //$NON-NLS-1$ + try { + assertTimeout(start, timeout, c); + c.join(5000); + assertTerminated(c); + assertTimeoutDuration(start, timeout); + } finally { + try { + c.end(); + } catch (CoordinationException e) { + // noop + } + } + } + + public void testExtendTimeoutWhenMaxTimeoutAlreadyReached() throws Exception { + long start = System.currentTimeMillis(); + Coordination c = coordinator.create("c", 0); //$NON-NLS-1$ + try { + assertTimeout(start, TIMEOUT, c); + assertEquals("No change in deadline should have occurred", 0, c.extendTimeout(TIMEOUT)); //$NON-NLS-1$ + assertTimeout(start, TIMEOUT, c); + } finally { + try { + c.end(); + } catch (CoordinationException e) { + // noop + } + } + } + + public void testExtendTimeoutWhenMaxTimeoutNotAlreadyReached() throws Exception { + long start = System.currentTimeMillis(); + long timeout = 1500; + Coordination c = coordinator.create("c", timeout); //$NON-NLS-1$ + try { + assertTimeout(start, timeout, c); + c.extendTimeout(timeout); + assertTimeout(start, timeout * 2, c); + c.extendTimeout(TIMEOUT); + assertTimeout(start, TIMEOUT, c); + assertEquals("No change in deadline should have occurred", 0, c.extendTimeout(TIMEOUT)); //$NON-NLS-1$ + assertTimeout(start, TIMEOUT, c); + } finally { + try { + c.end(); + } catch (CoordinationException e) { + // noop + } + } + } + + public void testNoMaxTimeoutWithTimeout() throws Exception { + Activator.getBundleContext().ungetService(coordinatorRef); + System.setProperty(PROPERTY_NAME, String.valueOf(0)); + coordinatorRef = Activator.getBundleContext().getServiceReference(Coordinator.class.getName()); + coordinator = (Coordinator) Activator.getBundleContext().getService(coordinatorRef); + long start = System.currentTimeMillis(); + long timeout = 2000; + Coordination c = coordinator.create("c", timeout); //$NON-NLS-1$ + try { + assertTimeout(start, timeout, c); + c.join(5000); + assertTerminated(c); + assertTimeoutDuration(start, timeout); + } finally { + try { + c.end(); + } catch (CoordinationException e) { + // noop + } + } + } + + protected void setUp() throws Exception { + System.setProperty(PROPERTY_NAME, String.valueOf(TIMEOUT)); + assertSystemProperty(PROPERTY_NAME, String.valueOf(TIMEOUT)); + assertFrameworkProperty(PROPERTY_NAME, String.valueOf(TIMEOUT)); + Activator.getBundle(Activator.BUNDLE_COORDINATOR).start(); + coordinatorRef = Activator.getBundleContext().getServiceReference(Coordinator.class.getName()); + coordinator = (Coordinator) Activator.getBundleContext().getService(coordinatorRef); + } + + protected void tearDown() throws Exception { + Activator.getBundleContext().ungetService(coordinatorRef); + Activator.getBundle(Activator.BUNDLE_COORDINATOR).stop(); + } + + private void assertFrameworkProperty(String name, String value) { + assertEquals("Wrong value for framework property " + name, value, Activator.getBundleContext().getProperty(name)); //$NON-NLS-1$ + } + + private void assertSystemProperty(String name, String value) { + assertEquals("Wrong value for system property " + name, value, System.getProperty(name)); //$NON-NLS-1$ + } + + private void assertTerminated(Coordination c) { + assertTrue("Not terminated", c.isTerminated()); //$NON-NLS-1$ + } + + private void assertTimeout(long start, long timeout, Coordination c) { + assertTrue("Wrong timeout", c.extendTimeout(0) >= start + timeout); //$NON-NLS-1$ + } + + private void assertTimeoutDuration(long start, long timeout) { + assertTrue("Timeout too long", System.currentTimeMillis() - start <= timeout + DEVIATION); //$NON-NLS-1$ + } +} |