From ca72506148e7b9e0e722041f49c0d18682063ea9 Mon Sep 17 00:00:00 2001 From: Jonathan Knight Date: Wed, 28 Feb 2024 10:28:03 -0500 Subject: [PATCH] Bug 36333277 - Build: JmxMBeanTests.queryMBean failed (merge ce/main -> ce/23.09 107176) [git-p4: depot-paths = "//dev/coherence-ce/release/coherence-ce-v23.09/": change = 107178] --- .../src/main/java/extend/JmxMBeanTests.java | 330 ++++++++---------- 1 file changed, 152 insertions(+), 178 deletions(-) diff --git a/prj/test/functional/extend/src/main/java/extend/JmxMBeanTests.java b/prj/test/functional/extend/src/main/java/extend/JmxMBeanTests.java index 20020e5690fb6..f5db3c4a09f5a 100644 --- a/prj/test/functional/extend/src/main/java/extend/JmxMBeanTests.java +++ b/prj/test/functional/extend/src/main/java/extend/JmxMBeanTests.java @@ -1,15 +1,18 @@ /* - * Copyright (c) 2000, 2023, Oracle and/or its affiliates. + * Copyright (c) 2000, 2024, Oracle and/or its affiliates. * * Licensed under the Universal Permissive License v 1.0 as shown at * https://oss.oracle.com/licenses/upl. */ package extend; +import com.oracle.bedrock.options.Timeout; import com.oracle.bedrock.testsupport.deferred.Eventually; import com.oracle.bedrock.runtime.coherence.CoherenceClusterMember; +import com.oracle.coherence.common.base.Exceptions; +import com.oracle.coherence.common.base.Logger; import com.tangosol.io.pof.PofReader; import com.tangosol.io.pof.PofWriter; import com.tangosol.io.pof.PortableObject; @@ -32,10 +35,10 @@ import java.io.IOException; -import java.util.Iterator; import java.util.Map; import java.util.Properties; import java.util.Set; +import java.util.concurrent.TimeUnit; import javax.management.Attribute; import javax.management.MBeanServer; @@ -43,22 +46,26 @@ import javax.management.ObjectName; import static com.oracle.bedrock.deferred.DeferredHelper.invoking; +import static com.oracle.bedrock.testsupport.deferred.Eventually.assertThat; +import static org.hamcrest.CoreMatchers.instanceOf; import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.*; +import static org.hamcrest.CoreMatchers.not; +import static org.hamcrest.CoreMatchers.notNullValue; +import static org.junit.Assert.fail; /** -* A collection of JMX MBean functional tests for Coherence*Extend. -* -* @author lh 2011.01.20 -*/ + * A collection of JMX MBean functional tests for Coherence*Extend. + * + * @author lh 2011.01.20 + */ public class JmxMBeanTests extends AbstractFunctionalTest { // ----- constructors --------------------------------------------------- /** - * Default constructor. - */ + * Default constructor. + */ public JmxMBeanTests() { super(AbstractExtendTests.FILE_CLIENT_CFG_CACHE); @@ -68,8 +75,8 @@ public JmxMBeanTests() // ----- test lifecycle ------------------------------------------------- /** - * Initialize the test class. - */ + * Initialize the test class. + */ @BeforeClass public static void startup() { @@ -79,13 +86,13 @@ public static void startup() System.getProperties().putAll(propsMain); CoherenceClusterMember memberProxy = startCacheServer("JmxMBeanTests", "extend", - AbstractExtendTests.FILE_SERVER_CFG_CACHE); - Eventually.assertThat(invoking(memberProxy).isServiceRunning("ExtendTcpProxyService"), is(true)); + AbstractExtendTests.FILE_SERVER_CFG_CACHE); + assertThat(invoking(memberProxy).isServiceRunning("ExtendTcpProxyService"), is(true)); } /** - * Shutdown the test class. - */ + * Shutdown the test class. + */ @AfterClass public static void shutdown() { @@ -116,43 +123,43 @@ public void testConnectionMessagingDebug() /** * Run the {@link MessagingDebugInvocable} to set the attribute on the specified MBean. * - * @param sMBeanType the type of MBean to examine (e.g. ConnectionManager and Connection) + * @param sMBeanType the type of MBean to examine (e.g. ConnectionManager and Connection) */ public void testMessagingDebug(String sMBeanType) { InvocationService service = (InvocationService) - getFactory().ensureService(INVOCATION_SERVICE_NAME); - Eventually.assertThat(service.isRunning(), is(true)); + getFactory().ensureService(INVOCATION_SERVICE_NAME); + Eventually.assertDeferred(service::isRunning, is(true)); try { // initially should be false MessagingDebugInvocable task = new MessagingDebugInvocable(sMBeanType, false); - Map map = service.query(task, null); + Map map = service.query(task, null); - assertTrue(map != null); - assertTrue(map.size() == 1); + assertThat(map, is(notNullValue())); + assertThat(map.size(), is(1)); Object oMember = map.keySet().iterator().next(); - assertTrue(equals(oMember, service.getCluster().getLocalMember())); + assertThat(service.getCluster().getLocalMember(), is(oMember)); Object oResult = map.values().iterator().next(); - assertTrue(oResult instanceof Boolean); - assertTrue(oResult.equals(false)); + assertThat(oResult, is(instanceOf(Boolean.class))); + assertThat(oResult, is(Boolean.FALSE)); // make sure the attribute value can be changed task = new MessagingDebugInvocable(sMBeanType, true); map = service.query(task, null); - assertTrue(map != null); - assertTrue(map.size() == 1); + assertThat(map, is(notNullValue())); + assertThat(map.size(), is(1)); oMember = map.keySet().iterator().next(); - assertTrue(equals(oMember, service.getCluster().getLocalMember())); + assertThat(service.getCluster().getLocalMember(), is(oMember)); oResult = map.values().iterator().next(); - assertTrue(oResult instanceof Boolean); - assertTrue(oResult.equals(true)); + assertThat(oResult, is(instanceOf(Boolean.class))); + assertThat(oResult, is(Boolean.TRUE)); } finally { @@ -161,71 +168,77 @@ public void testMessagingDebug(String sMBeanType) } /** - * Query MBean attribute on the server using - * {@link InvocationService#query(Invocable, Set)}. - */ + * Query MBean attribute on the server using + * {@link InvocationService#query(Invocable, Set)}. + */ @Test public void queryMBean() { InvocationService service = (InvocationService) getFactory().ensureService(INVOCATION_SERVICE_NAME); + Eventually.assertDeferred(service::isRunning, is(true)); try { MBeanInvocable task = new MBeanInvocable(); - Map map = service.query(task, null); + Map map = service.query(task, null); - assertTrue(map != null); - assertTrue(map.size() == 1); + assertThat(map, is(notNullValue())); + assertThat(map.size(), is(1)); Object oMember = map.keySet().iterator().next(); Object oResult = map.values().iterator().next(); - assertTrue(equals(oMember, service.getCluster().getLocalMember())); + assertThat(service.getCluster().getLocalMember(), is(oMember)); - assertTrue("Expected String but got " + oResult, oResult instanceof String); - assertTrue("oResult != oMember, oResult=" + oResult, oResult.equals(oMember.toString())); + assertThat(oResult, is(instanceOf(String.class))); + assertThat(oResult, is(oMember.toString())); } finally { service.shutdown(); } } + /** - * Query MBean attribute on the server using - * {@link InvocationService#query(Invocable, Set)}. - */ + * Query MBean attribute on the server using + * {@link InvocationService#query(Invocable, Set)}. + */ @Test public void queryHostIPMBean() { InvocationService service = (InvocationService) getFactory().ensureService(INVOCATION_SERVICE_NAME); + Eventually.assertDeferred(service::isRunning, is(true)); try { MBeanHostIPInvocable task = new MBeanHostIPInvocable(); - Map map = service.query(task, null); + Map map = service.query(task, null); - assertTrue(map != null); - assertTrue(map.size() == 1); + assertThat(map, is(notNullValue())); + assertThat(map.size(), is(1)); Object oMember = map.keySet().iterator().next(); - assertTrue(equals(oMember, service.getCluster().getLocalMember())); + assertThat(service.getCluster().getLocalMember(), is(oMember)); Object oResult = map.values().iterator().next(); - assertTrue(oResult instanceof String); + assertThat(oResult, is(instanceOf(String.class))); + String sResult = (String) oResult; - int dotIdx = sResult.lastIndexOf('.'); - assertTrue(dotIdx != -1); + int dotIdx = sResult.lastIndexOf('.'); + assertThat(dotIdx, is(not(-1))); + String subPort = null; try { - subPort = sResult.substring(dotIdx+1); + subPort = sResult.substring(dotIdx + 1); } catch (IndexOutOfBoundsException e) { - fail("No subport when expected"); + fail("No sub-port when expected"); } - assertTrue(subPort.length() != 0); + assertThat(subPort, is(notNullValue())); + assertThat(subPort.length(), is(not(0))); } finally { @@ -240,13 +253,14 @@ public void queryHostIPMBean() * an MBean. */ public static class MessagingDebugInvocable - implements Invocable, PortableObject + implements Invocable, PortableObject { // ----- constructors --------------------------------------------- /** * Default constructor. */ + @SuppressWarnings("unused") public MessagingDebugInvocable() { } @@ -262,38 +276,32 @@ public MessagingDebugInvocable(String sMBeanType, boolean fEnableDebug) // ----- Invocable interface -------------------------------------- - /** - * {@inheritDoc} - */ + @Override public void init(InvocationService service) { - assertTrue(service.getInfo().getServiceType() - .equals(InvocationService.TYPE_REMOTE)); + assertThat(service.getInfo().getServiceType(), is(InvocationService.TYPE_REMOTE)); m_service = service; } - /** - * {@inheritDoc} - */ + @Override public void run() { if (m_service != null) { - Cluster cluster = CacheFactory.getCluster(); + Cluster cluster = CacheFactory.getCluster(); Registry registry = cluster.getManagement(); - assertTrue("JMX is disabled", registry != null); + assertThat("JMX is disabled", registry, is(notNullValue())); MBeanServer server = MBeanHelper.findMBeanServer(); try { - Set set = server.queryMBeans(new ObjectName("Coherence:*"), null); - for (Iterator iter = set.iterator(); iter.hasNext();) + Set set = server.queryMBeans(new ObjectName("Coherence:*"), null); + for (ObjectInstance instance : set) { - ObjectInstance instance = (ObjectInstance) iter.next(); - ObjectName objectName = instance.getObjectName(); + ObjectName objectName = instance.getObjectName(); if (objectName.toString().indexOf("type=" + m_sMBeanType + ',') > 0) { - server.setAttribute(objectName, new Attribute("MessagingDebug", Boolean.valueOf(m_fEnableDebug))); + server.setAttribute(objectName, new Attribute("MessagingDebug", m_fEnableDebug)); setValue((Boolean) server.getAttribute(objectName, "MessagingDebug")); break; @@ -308,31 +316,25 @@ public void run() } } - /** - * {@inheritDoc} - */ + @Override public Object getResult() { - return Boolean.valueOf(m_fValue); + return m_fValue; } // ----- PortableObject interface --------------------------------- - /** - * {@inheritDoc} - */ + @Override public void readExternal(PofReader in) - throws IOException + throws IOException { m_fEnableDebug = in.readBoolean(0); m_sMBeanType = in.readString(1); } - /** - * {@inheritDoc} - */ + @Override public void writeExternal(PofWriter out) - throws IOException + throws IOException { out.writeBoolean(0, m_fEnableDebug); out.writeString(1, m_sMBeanType); @@ -343,7 +345,7 @@ public void writeExternal(PofWriter out) /** * Set the boolean value. * - * @param fValue the value of the attribute + * @param fValue the value of the attribute */ public void setValue(boolean fValue) { @@ -376,85 +378,73 @@ public void setValue(boolean fValue) // ----- inner class: MBeanInvocable -------------------------------------- /** - * Invocable implementation that queries the Member attribute of the - * ConnectionMBean and returns the string value. - */ + * Invocable implementation that queries the Member attribute of the + * ConnectionMBean and returns the string value. + */ public static class MBeanInvocable implements Invocable, PortableObject { // ----- constructors --------------------------------------------- /** - * Default constructor. - */ + * Default constructor. + */ public MBeanInvocable() { } // ----- Invocable interface -------------------------------------- - /** - * {@inheritDoc} - */ + @Override public void init(InvocationService service) { - assertTrue(service.getInfo().getServiceType() - .equals(InvocationService.TYPE_REMOTE)); + assertThat(service.getInfo().getServiceType(), is(InvocationService.TYPE_REMOTE)); m_service = service; } - /** - * {@inheritDoc} - */ + @Override public void run() { if (m_service != null) { - Cluster cluster = CacheFactory.getCluster(); + Cluster cluster = CacheFactory.getCluster(); Registry registry = cluster.getManagement(); - assertTrue("JMX is disabled", registry != null); + assertThat("JMX is disabled", registry, is(notNullValue())); MBeanServer server = MBeanHelper.findMBeanServer(); - try + + Eventually.assertDeferred("Didn't find Connection MBean within timeout", + () -> findConnectionMBean(server), is(true), Timeout.after(1, TimeUnit.MINUTES)); + } + else + { + Logger.warn("MBeanInvocable.run(), m_service is not initialized."); + } + } + + protected boolean findConnectionMBean(MBeanServer server) + { + try + { + Set set = server.queryMBeans(new ObjectName("Coherence:*"), null); + for (ObjectInstance instance : set) { - int cRetry = 0; - do + ObjectName objectName = instance.getObjectName(); + if (objectName.toString().indexOf("type=Connection,") > 0) { - Set set = server.queryMBeans(new ObjectName("Coherence:*"), null); - for (Iterator iter = set.iterator(); iter.hasNext(); ) - { - ObjectInstance instance = (ObjectInstance) iter.next(); - ObjectName objectName = instance.getObjectName(); - if (objectName.toString().indexOf("type=Connection,") > 0) - { - setValue(server.getAttribute(objectName, "Member").toString()); - break; - } - } - cRetry++; - } - while (m_sValue == null && cRetry < 10); - - if (m_sValue == null) - { - // temp debug: if we get here then we didn't find the MBean after cRetry + 1 tries - setValue("Didn't find MBean in " + (cRetry + 1) + " goes"); + setValue(server.getAttribute(objectName, "Member").toString()); + return true; } } - catch (Exception e) - { - throw ensureRuntimeException(e); - } + return false; } - else + catch (Exception e) { - CacheFactory.log("MBeanInvocable.run(), m_service is not initialized.", Base.LOG_WARN); + throw Exceptions.ensureRuntimeException(e); } } - /** - * {@inheritDoc} - */ + @Override public Object getResult() { return m_sValue; @@ -462,18 +452,14 @@ public Object getResult() // ----- PortableObject interface --------------------------------- - /** - * {@inheritDoc} - */ + @Override public void readExternal(PofReader in) throws IOException { m_sValue = in.readString(0); } - /** - * {@inheritDoc} - */ + @Override public void writeExternal(PofWriter out) throws IOException { @@ -483,10 +469,10 @@ public void writeExternal(PofWriter out) // ----- accessors ------------------------------------------------ /** - * Set the string value. - * - * @param sValue the value of the attribute - */ + * Set the string value. + * + * @param sValue the value of the attribute + */ public void setValue(String sValue) { m_sValue = sValue; @@ -495,65 +481,59 @@ public void setValue(String sValue) // ----- data members --------------------------------------------- /** - * The string value of the attribute. - */ + * The string value of the attribute. + */ private String m_sValue; /** - * The InvocationService that is executing this Invocable. - */ + * The InvocationService that is executing this Invocable. + */ private transient InvocationService m_service; } // ----- inner class: MBeanHostIPInvocable -------------------------------------- /** - * Invocable implementation that queries the Member attribute of the - * ConnectionMBean and returns the string value. - */ + * Invocable implementation that queries the Member attribute of the + * ConnectionMBean and returns the string value. + */ public static class MBeanHostIPInvocable implements Invocable, PortableObject { // ----- constructors --------------------------------------------- /** - * Default constructor. - */ + * Default constructor. + */ public MBeanHostIPInvocable() { } // ----- Invocable interface -------------------------------------- - /** - * {@inheritDoc} - */ + @Override public void init(InvocationService service) { - assertTrue(service.getInfo().getServiceType() - .equals(InvocationService.TYPE_REMOTE)); + assertThat(service.getInfo().getServiceType(), is(InvocationService.TYPE_REMOTE)); m_service = service; } - /** - * {@inheritDoc} - */ + @Override public void run() { if (m_service != null) { - Cluster cluster = CacheFactory.getCluster(); + Cluster cluster = CacheFactory.getCluster(); Registry registry = cluster.getManagement(); - assertTrue("JMX is disabled", registry != null); + assertThat("JMX is disabled", registry, is(notNullValue())); MBeanServer server = MBeanHelper.findMBeanServer(); try { - Set set = server.queryMBeans(new ObjectName("Coherence:*"), null); - for (Iterator iter = set.iterator(); iter.hasNext();) + Set set = server.queryMBeans(new ObjectName("Coherence:*"), null); + for (ObjectInstance instance : set) { - ObjectInstance instance = (ObjectInstance) iter.next(); - ObjectName objectName = instance.getObjectName(); + ObjectName objectName = instance.getObjectName(); if (objectName.toString().indexOf("type=ConnectionManager,name=ExtendTcpProxyServiceJMX,") > 0) { setValue(server.getAttribute(objectName, "HostIP").toString()); @@ -568,9 +548,7 @@ public void run() } } - /** - * {@inheritDoc} - */ + @Override public Object getResult() { return m_sValue; @@ -578,18 +556,14 @@ public Object getResult() // ----- PortableObject interface --------------------------------- - /** - * {@inheritDoc} - */ + @Override public void readExternal(PofReader in) throws IOException { m_sValue = in.readString(0); } - /** - * {@inheritDoc} - */ + @Override public void writeExternal(PofWriter out) throws IOException { @@ -599,10 +573,10 @@ public void writeExternal(PofWriter out) // ----- accessors ------------------------------------------------ /** - * Set the string value. - * - * @param sValue the value of the attribute - */ + * Set the string value. + * + * @param sValue the value of the attribute + */ public void setValue(String sValue) { m_sValue = sValue; @@ -611,20 +585,20 @@ public void setValue(String sValue) // ----- data members --------------------------------------------- /** - * The string value of the attribute. - */ + * The string value of the attribute. + */ private String m_sValue; /** - * The InvocationService that is executing this Invocable. - */ + * The InvocationService that is executing this Invocable. + */ private transient InvocationService m_service; } // ----- constants ------------------------------------------------------ /** - * The name of the InvocationService used by all test methods. - */ + * The name of the InvocationService used by all test methods. + */ public static String INVOCATION_SERVICE_NAME = "ExtendTcpInvocationService"; } \ No newline at end of file