Skip to content

Commit 3a940a2

Browse files
committed
Added more logging and fixed waitFor
Signed-off-by: David Matějček <[email protected]>
1 parent 2bcac27 commit 3a940a2

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

appserver/tests/embedded/runnable/src/test/java/org/glassfish/tests/embedded/runnable/MonitoringTest.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
import java.lang.System.Logger;
2323
import java.nio.file.Files;
2424
import java.nio.file.Path;
25+
import java.time.Duration;
2526
import java.util.List;
2627
import java.util.Properties;
2728
import java.util.Set;
2829
import java.util.concurrent.TimeUnit;
2930

30-
import javax.management.JMException;
3131
import javax.management.MBeanServerConnection;
3232
import javax.management.ObjectName;
3333
import javax.management.remote.JMXConnector;
@@ -65,10 +65,11 @@ public class MonitoringTest {
6565
@AfterEach
6666
void deleteFiles() throws Exception {
6767
if (jmxConnector != null) {
68+
LOG.log(INFO, () -> "Closing JMX Connector: " + jmxConnector);
6869
try {
6970
jmxConnector.close();
7071
} catch (Exception e) {
71-
LOG.log(ERROR, e);
72+
LOG.log(ERROR, "JMX Connector close failed!", e);
7273
}
7374
}
7475
if (gfEmbeddedProcess != null && gfEmbeddedProcess.isAlive()) {
@@ -87,6 +88,7 @@ void testJmxMonitoringWithFlashlightAgent() throws Exception {
8788
assertTrue(gfEmbeddedProcess.isAlive(), "gf is alive");
8889

8990
jmxConnector = getJMXConnector();
91+
LOG.log(INFO, () -> "Connecting with JMX Connector: " + jmxConnector);
9092
assertNotNull(jmxConnector, "JMX Connector");
9193
jmxConnector.connect();
9294
MBeanServerConnection connection = jmxConnector.getMBeanServerConnection();
@@ -141,7 +143,7 @@ private String getJmxUrl() throws Exception {
141143
* Find GlassFish process by looking for our jar
142144
*/
143145
private VirtualMachineDescriptor findVirtualMachineDescriptor() throws Exception {
144-
return waitFor("Virtual machine descriptor", () -> {
146+
return waitFor(Duration.ofSeconds(10L), "Virtual machine descriptor", () -> {
145147
for (VirtualMachineDescriptor vmd : VirtualMachine.list()) {
146148
if (vmd.displayName().contains("glassfish-embedded")) {
147149
LOG.log(INFO, () -> "Detected virtual machine descriptor: " + vmd);
@@ -153,13 +155,22 @@ private VirtualMachineDescriptor findVirtualMachineDescriptor() throws Exception
153155
}
154156

155157
private void bootAmx(MBeanServerConnection connection) throws Exception {
158+
LOG.log(INFO, "Booting AMX!");
156159
ObjectName bootAMXObjectName = new ObjectName("amx-support:type=boot-amx");
157-
waitFor("bootAMX", () -> connection.isRegistered(bootAMXObjectName) ? true : null);
158-
assertNotNull(connection.invoke(bootAMXObjectName, "bootAMX", null, null), "bootAMX operation");
160+
waitFor(Duration.ofSeconds(10L), "bootAMX", () -> {
161+
final boolean registered = connection.isRegistered(bootAMXObjectName);
162+
LOG.log(INFO, () -> "bootAMX registered: " + registered);
163+
return registered ? true : null;
164+
});
165+
LOG.log(INFO, "Invoking bootAMX...");
166+
Object response = connection.invoke(bootAMXObjectName, "bootAMX", null, null);
167+
LOG.log(INFO, () -> "AMX booted: " + response);
168+
assertNotNull(response, "bootAMX operation");
159169
}
160170

161-
private void verifyMonitoringMBeans(final MBeanServerConnection connection) throws InterruptedException, IOException, JMException {
162-
Set<ObjectName> requestBeans = waitFor("equest-mon bean", () -> {
171+
private void verifyMonitoringMBeans(final MBeanServerConnection connection) throws Exception {
172+
LOG.log(INFO, () -> "Retrieving monitor data using connection: " + connection);
173+
Set<ObjectName> requestBeans = waitFor(Duration.ofSeconds(10L), "equest-mon bean", () -> {
163174
Set<ObjectName> result = connection.queryNames(new ObjectName("amx:type=request-mon,*"), null);
164175
return result.isEmpty() ? null : result;
165176
});

appserver/tests/embedded/runnable/src/test/java/org/glassfish/tests/embedded/runnable/TestUtils.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@
1515
*/
1616
package org.glassfish.tests.embedded.runnable;
1717

18+
import java.time.Duration;
19+
import java.time.Instant;
20+
1821
import org.junit.jupiter.api.function.ThrowingSupplier;
1922

2023
/**
2124
* @author Ondro Mihalyi
2225
*/
2326
public final class TestUtils {
2427

25-
private static final int WAIT_SECONDS = 30;
26-
2728
private TestUtils() {
2829
}
2930

@@ -33,28 +34,32 @@ private TestUtils() {
3334
* Ignores null and all exceptions except {@link InterruptedException}.
3435
*
3536
* @param <T> Expected return type
36-
* @param what description of the expected result.
37-
* @param supplier
37+
* @param maxTime maximal duration of waiting for the non-null result.
38+
* @param actionDescription description of the expected result.
39+
* @param action
3840
* @return result.
3941
* @throws InterruptedException
4042
*/
41-
public static <T> T waitFor(String what, ThrowingSupplier<T> supplier) throws InterruptedException {
43+
public static <T> T waitFor(Duration maxTime, String actionDescription, ThrowingSupplier<T> action) throws InterruptedException {
4244
Throwable lastThrowable = null;
43-
for (int i = 0; i < WAIT_SECONDS * 10; i++) {
45+
final Instant deadline = Instant.now().plus(maxTime);
46+
while (true) {
4447
try {
45-
T result = supplier.get();
48+
T result = action.get();
4649
if (result != null) {
4750
return result;
4851
}
52+
if (deadline.isBefore(Instant.now())) {
53+
throw new RuntimeException(actionDescription + " not received within timeout", lastThrowable);
54+
}
55+
Thread.sleep(100);
4956
} catch (InterruptedException e) {
5057
throw e;
5158
} catch (UnsupportedOperationException unrecoverableError) {
5259
throw unrecoverableError;
5360
} catch (Throwable ignore) {
5461
lastThrowable = ignore;
5562
}
56-
Thread.sleep(100);
5763
}
58-
throw new RuntimeException(what + " not received within timeout", lastThrowable);
5964
}
6065
}

0 commit comments

Comments
 (0)