Skip to content

Commit

Permalink
Implement getAttributes() method of MBean, and update Number-based MB…
Browse files Browse the repository at this point in the history
…ean attributes to return Number objects instead of Strings (#14153)
  • Loading branch information
cgossett committed Aug 14, 2024
1 parent e4d3db3 commit b3db1d6
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 27 deletions.
14 changes: 13 additions & 1 deletion java/src/org/openqa/selenium/grid/jmx/MBean.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,8 @@ public Object getAttribute(String attribute) {
return ((Map<?, ?>) res)
.entrySet().stream()
.collect(Collectors.toMap(Map.Entry::getKey, e -> e.getValue().toString()));
} else if (res instanceof Number) {
return res;
} else {
return res.toString();
}
Expand All @@ -241,7 +243,17 @@ public void setAttribute(Attribute attribute) {

@Override
public AttributeList getAttributes(String[] attributes) {
return null;
AttributeList resultList = new AttributeList();

// if attributeNames is empty, return an empty result list
if (attributes == null || attributes.length == 0) return resultList;

for (int i = 0; i < attributes.length; i++) {
Object value = getAttribute(attributes[i]);
resultList.add(new Attribute(attributes[i], value));
}

return resultList;
}

@Override
Expand Down
99 changes: 73 additions & 26 deletions java/test/org/openqa/selenium/grid/router/JmxTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.time.Duration;
import java.time.Instant;
import java.util.logging.Logger;
import javax.management.AttributeList;
import javax.management.AttributeNotFoundException;
import javax.management.InstanceNotFoundException;
import javax.management.IntrospectionException;
Expand Down Expand Up @@ -85,6 +86,9 @@ void shouldBeAbleToRegisterBaseServerConfig() {
MBeanAttributeInfo[] attributeInfoArray = info.getAttributes();
assertThat(attributeInfoArray).hasSize(3);

AttributeList attributeList = getAttributeList(name, attributeInfoArray);
assertThat(attributeList).isNotNull().hasSize(3);

String uriValue = (String) beanServer.getAttribute(name, "Uri");
assertThat(uriValue).isEqualTo(baseServerOptions.getExternalUri().toString());

Expand Down Expand Up @@ -130,23 +134,26 @@ id, nodeUri, new ImmutableCapabilities(), caps, Instant.now())))
MBeanAttributeInfo[] attributeInfo = info.getAttributes();
assertThat(attributeInfo).hasSize(9);

String currentSessions = (String) beanServer.getAttribute(name, "CurrentSessions");
assertThat(Integer.parseInt(currentSessions)).isZero();
AttributeList attributeList = getAttributeList(name, attributeInfo);
assertThat(attributeList).isNotNull().hasSize(9);

Object currentSessions = beanServer.getAttribute(name, "CurrentSessions");
assertNumberAttribute(currentSessions, 0);

String maxSessions = (String) beanServer.getAttribute(name, "MaxSessions");
assertThat(Integer.parseInt(maxSessions)).isEqualTo(1);
Object maxSessions = beanServer.getAttribute(name, "MaxSessions");
assertNumberAttribute(maxSessions, 1);

String status = (String) beanServer.getAttribute(name, "Status");
assertThat(status).isEqualTo("UP");

String totalSlots = (String) beanServer.getAttribute(name, "TotalSlots");
assertThat(Integer.parseInt(totalSlots)).isEqualTo(1);
Object totalSlots = beanServer.getAttribute(name, "TotalSlots");
assertNumberAttribute(totalSlots, 1);

String usedSlots = (String) beanServer.getAttribute(name, "UsedSlots");
assertThat(Integer.parseInt(usedSlots)).isZero();
Object usedSlots = beanServer.getAttribute(name, "UsedSlots");
assertNumberAttribute(usedSlots, 0);

String load = (String) beanServer.getAttribute(name, "Load");
assertThat(Float.parseFloat(load)).isEqualTo(0.0f);
Object load = beanServer.getAttribute(name, "Load");
assertNumberAttribute(load, 0.0f);

String remoteNodeUri = (String) beanServer.getAttribute(name, "RemoteNodeUri");
assertThat(remoteNodeUri).isEqualTo(nodeUri.toString());
Expand Down Expand Up @@ -182,13 +189,14 @@ void shouldBeAbleToRegisterSessionQueueServerConfig() {
MBeanAttributeInfo[] attributeInfoArray = info.getAttributes();
assertThat(attributeInfoArray).hasSize(2);

String requestTimeout = (String) beanServer.getAttribute(name, "RequestTimeoutSeconds");
assertThat(Long.parseLong(requestTimeout))
.isEqualTo(newSessionQueueOptions.getRequestTimeoutSeconds());
AttributeList attributeList = getAttributeList(name, attributeInfoArray);
assertThat(attributeList).isNotNull().hasSize(2);

Object requestTimeout = beanServer.getAttribute(name, "RequestTimeoutSeconds");
assertNumberAttribute(requestTimeout, newSessionQueueOptions.getRequestTimeoutSeconds());

String retryInterval = (String) beanServer.getAttribute(name, "RetryIntervalMilliseconds");
assertThat(Long.parseLong(retryInterval))
.isEqualTo(newSessionQueueOptions.getRetryIntervalMilliseconds());
Object retryInterval = beanServer.getAttribute(name, "RetryIntervalMilliseconds");
assertNumberAttribute(retryInterval, newSessionQueueOptions.getRetryIntervalMilliseconds());
} catch (InstanceNotFoundException
| IntrospectionException
| ReflectionException
Expand Down Expand Up @@ -227,8 +235,11 @@ void shouldBeAbleToRegisterSessionQueue() {
MBeanAttributeInfo[] attributeInfoArray = info.getAttributes();
assertThat(attributeInfoArray).hasSize(1);

String size = (String) beanServer.getAttribute(name, "NewSessionQueueSize");
assertThat(Integer.parseInt(size)).isZero();
AttributeList attributeList = getAttributeList(name, attributeInfoArray);
assertThat(attributeList).isNotNull().hasSize(1);

Object size = beanServer.getAttribute(name, "NewSessionQueueSize");
assertNumberAttribute(size, 0);
} catch (InstanceNotFoundException
| IntrospectionException
| ReflectionException
Expand Down Expand Up @@ -290,21 +301,57 @@ void shouldBeAbleToMonitorHub() throws Exception {
MBeanInfo info = beanServer.getMBeanInfo(name);
assertThat(info).isNotNull();

String nodeUpCount = (String) beanServer.getAttribute(name, "NodeUpCount");
MBeanAttributeInfo[] attributeInfoArray = info.getAttributes();
assertThat(attributeInfoArray).hasSize(4);

AttributeList attributeList = getAttributeList(name, attributeInfoArray);
assertThat(attributeList).isNotNull().hasSize(4);

Object nodeUpCount = beanServer.getAttribute(name, "NodeUpCount");
LOG.info("Node up count=" + nodeUpCount);
assertThat(Integer.parseInt(nodeUpCount)).isEqualTo(1);
assertNumberAttribute(nodeUpCount, 1);

String nodeDownCount = (String) beanServer.getAttribute(name, "NodeDownCount");
Object nodeDownCount = beanServer.getAttribute(name, "NodeDownCount");
LOG.info("Node down count=" + nodeDownCount);
assertThat(Integer.parseInt(nodeDownCount)).isZero();
assertNumberAttribute(nodeDownCount, 0);

String activeSlots = (String) beanServer.getAttribute(name, "ActiveSlots");
Object activeSlots = beanServer.getAttribute(name, "ActiveSlots");
LOG.info("Active slots count=" + activeSlots);
assertThat(Integer.parseInt(activeSlots)).isZero();
assertNumberAttribute(activeSlots, 0);

String idleSlots = (String) beanServer.getAttribute(name, "IdleSlots");
Object idleSlots = beanServer.getAttribute(name, "IdleSlots");
LOG.info("Idle slots count=" + idleSlots);
assertThat(Integer.parseInt(idleSlots)).isEqualTo(1);
assertNumberAttribute(idleSlots, 1);
}
}

private AttributeList getAttributeList(ObjectName name, MBeanAttributeInfo[] attributeInfoArray)
throws InstanceNotFoundException, ReflectionException {
String[] attributeNames = new String[attributeInfoArray.length];
for (int i = 0; i < attributeInfoArray.length; i++) {
attributeNames[i] = attributeInfoArray[i].getName();
}

return beanServer.getAttributes(name, attributeNames);
}

private void assertCommonNumberAttributes(Object attribute) {
assertThat(attribute).isNotNull();
assertThat(attribute).isInstanceOf(Number.class);
}

private void assertNumberAttribute(Object attribute, int expectedValue) {
assertCommonNumberAttributes(attribute);
assertThat(Integer.parseInt(attribute.toString())).isEqualTo(expectedValue);
}

private void assertNumberAttribute(Object attribute, long expectedValue) {
assertCommonNumberAttributes(attribute);
assertThat(Long.parseLong(attribute.toString())).isEqualTo(expectedValue);
}

private void assertNumberAttribute(Object attribute, float expectedValue) {
assertCommonNumberAttributes(attribute);
assertThat(Float.parseFloat(attribute.toString())).isEqualTo(expectedValue);
}
}

0 comments on commit b3db1d6

Please sign in to comment.