FELIX-4348 async locate service deadlock test
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1556078 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/scr/src/test/java/org/apache/felix/scr/integration/ComponentTestBase.java b/scr/src/test/java/org/apache/felix/scr/integration/ComponentTestBase.java
index 97efc21..130042c 100644
--- a/scr/src/test/java/org/apache/felix/scr/integration/ComponentTestBase.java
+++ b/scr/src/test/java/org/apache/felix/scr/integration/ComponentTestBase.java
@@ -140,6 +140,7 @@
"org.apache.felix.scr.integration.components.circular," +
"org.apache.felix.scr.integration.components.circularFactory," +
"org.apache.felix.scr.integration.components.concurrency," +
+ "org.apache.felix.scr.integration.components.deadlock," +
"org.apache.felix.scr.integration.components.felix3680," +
"org.apache.felix.scr.integration.components.felix3680_2");
builder.setHeader("Import-Package", "org.apache.felix.scr,org.apache.felix.scr.component;mandatory:=\"status\"; status=\"provisional\"");
diff --git a/scr/src/test/java/org/apache/felix/scr/integration/LocateTest.java b/scr/src/test/java/org/apache/felix/scr/integration/LocateTest.java
new file mode 100644
index 0000000..412afeb
--- /dev/null
+++ b/scr/src/test/java/org/apache/felix/scr/integration/LocateTest.java
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.scr.integration;
+
+import static org.junit.Assert.*;
+
+import java.util.Hashtable;
+
+import junit.framework.TestCase;
+
+import org.apache.felix.scr.Component;
+import org.apache.felix.scr.integration.components.SimpleServiceImpl;
+import org.apache.felix.scr.integration.components.deadlock.Consumer;
+import org.apache.felix.scr.integration.components.deadlock.TestComponent;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.junit.JUnit4TestRunner;
+import org.osgi.service.cm.Configuration;
+
+@RunWith(JUnit4TestRunner.class)
+public class LocateTest extends ComponentTestBase
+{
+
+ static
+ {
+ descriptorFile = "/integration_test_locate.xml";
+ // uncomment to enable debugging of this test class
+// paxRunnerVmOption = DEBUG_VM_OPTION;
+ COMPONENT_PACKAGE = COMPONENT_PACKAGE + ".deadlock";
+ }
+
+ @Test
+ public void testAsyncLocate() throws Exception
+ {
+ bundleContext.registerService( Object.class, new Object(), null );
+
+ final Component consumerComponent = findComponentByName( "AsyncLocate" );
+ TestCase.assertNotNull( consumerComponent );
+ TestCase.assertEquals( Component.STATE_ACTIVE, consumerComponent.getState() );
+
+ final String pid = "SimpleComponent";
+ Configuration config = getConfigurationAdmin().getConfiguration( pid, null );
+ final Hashtable props = new Hashtable();
+ props.put( "target", "bar" );
+ config.update(props);
+ delay();
+
+ final Component component = findComponentByName( pid );
+ TestCase.assertNotNull( component );
+ //when deadlock is present the state is actually unsatisfied.
+ TestCase.assertEquals( Component.STATE_ACTIVE, component.getState() );
+ delay();
+ props.put( "target", "foo" );
+ config.update(props);
+ delay();
+
+ TestComponent tc = (TestComponent) component.getComponentInstance().getInstance();
+ assertTrue(tc.isSuccess1());
+ assertTrue(tc.isSuccess2());
+ }
+
+}
diff --git a/scr/src/test/java/org/apache/felix/scr/integration/components/deadlock/Consumer.java b/scr/src/test/java/org/apache/felix/scr/integration/components/deadlock/Consumer.java
new file mode 100644
index 0000000..b99d824
--- /dev/null
+++ b/scr/src/test/java/org/apache/felix/scr/integration/components/deadlock/Consumer.java
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.scr.integration.components.deadlock;
+
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.component.ComponentContext;
+
+public class Consumer
+{
+
+ private ComponentContext cc;
+
+ protected void activate(ComponentContext cc)
+ {
+ this.cc = cc;
+ }
+
+ protected void setSimpleComponent(TestComponent sc)
+ {
+ sc.doIt( );
+ }
+
+ protected void unsetSimpleComponent(ServiceReference<TestComponent> sr)
+ {
+
+ }
+
+}
diff --git a/scr/src/test/java/org/apache/felix/scr/integration/components/deadlock/TestComponent.java b/scr/src/test/java/org/apache/felix/scr/integration/components/deadlock/TestComponent.java
new file mode 100644
index 0000000..e380900
--- /dev/null
+++ b/scr/src/test/java/org/apache/felix/scr/integration/components/deadlock/TestComponent.java
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.felix.scr.integration.components.deadlock;
+
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.component.ComponentContext;
+
+public class TestComponent
+{
+
+ private ComponentContext cc;
+
+ private ServiceReference sr;
+ private boolean success1;
+ private boolean success2;
+
+ protected void activate(ComponentContext cc)
+ {
+ this.cc =cc;
+ }
+
+ protected void setRef(ServiceReference sr)
+ {
+ this.sr = sr;
+ }
+
+ protected void unsetRef(ServiceReference sr)
+ {
+ if (sr == this.sr)
+ {
+ this.sr = null;
+ }
+ }
+
+ public void doIt()
+ {
+ Thread t = new Thread()
+ {
+
+ @Override
+ public void run()
+ {
+ Object sc = cc.locateService("Ref", sr);
+ if (sc != null)
+ {
+ success1 = true;
+ }
+ }
+
+ };
+ t.start();
+ try
+ {
+ t.join();
+ success2 = true;
+ }
+ catch ( InterruptedException e )
+ {
+ e.printStackTrace();
+ }
+
+ }
+
+ public boolean isSuccess1()
+ {
+ return success1;
+ }
+
+ public boolean isSuccess2()
+ {
+ return success2;
+ }
+
+
+}
diff --git a/scr/src/test/resources/integration_test_locate.xml b/scr/src/test/resources/integration_test_locate.xml
new file mode 100644
index 0000000..1ca586e
--- /dev/null
+++ b/scr/src/test/resources/integration_test_locate.xml
@@ -0,0 +1,45 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor
+ license agreements. See the NOTICE file distributed with this work for additional
+ information regarding copyright ownership. The ASF licenses this file to
+ you under the Apache License, Version 2.0 (the "License"); you may not use
+ this file except in compliance with the License. You may obtain a copy of
+ the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required
+ by applicable law or agreed to in writing, software distributed under the
+ License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
+ OF ANY KIND, either express or implied. See the License for the specific
+ language governing permissions and limitations under the License. -->
+<components xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0">
+
+ <scr:component name="SimpleComponent"
+ configuration-policy="require">
+ <implementation class="org.apache.felix.scr.integration.components.deadlock.TestComponent" />
+ <service>
+ <provide interface='org.apache.felix.scr.integration.components.deadlock.TestComponent' />
+ </service>
+ <reference
+ name="Ref"
+ interface="java.lang.Object"
+ cardinality="0..1"
+ policy="dynamic"
+ bind="setRef"
+ unbind="unsetRef"
+ />
+ </scr:component>
+
+
+ <scr:component name="AsyncLocate"
+ immediate="true">
+ <implementation class="org.apache.felix.scr.integration.components.deadlock.Consumer" />
+ <reference
+ name="SimpleComponent"
+ interface="org.apache.felix.scr.integration.components.deadlock.TestComponent"
+ cardinality="0..1"
+ policy="dynamic"
+ bind="setSimpleComponent"
+ unbind="unsetSimpleComponent"
+ target='target="foo"'
+ />
+ </scr:component>
+
+</components>