Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a class cast check to avoid ClassCastException #505

Merged
merged 1 commit into from
Oct 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ public class MemoryAllocationExports extends Collector {

public MemoryAllocationExports() {
AllocationCountingNotificationListener listener = new AllocationCountingNotificationListener(allocatedCounter);
for (GarbageCollectorMXBean garbageCollectorMXBean : ManagementFactory.getGarbageCollectorMXBeans()) {
((NotificationEmitter) garbageCollectorMXBean).addNotificationListener(listener, null, null);
for (GarbageCollectorMXBean garbageCollectorMXBean : getGarbageCollectorMXBeans()) {
if (garbageCollectorMXBean instanceof NotificationEmitter) {
((NotificationEmitter) garbageCollectorMXBean).addNotificationListener(listener, null, null);
}
}
}

Expand Down Expand Up @@ -97,4 +99,8 @@ private static long getAndSet(Map<String, Long> map, String key, long value) {
return last == null ? 0 : last;
}
}

protected List<GarbageCollectorMXBean> getGarbageCollectorMXBeans() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you add this function?

return ManagementFactory.getGarbageCollectorMXBeans();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

import io.prometheus.client.Counter;
import org.junit.Test;
import org.mockito.Mockito;

import javax.management.NotificationEmitter;
import javax.management.NotificationFilter;
import java.lang.management.GarbageCollectorMXBean;
import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.assertEquals;

Expand Down Expand Up @@ -42,4 +49,20 @@ public void testListenerLogic() {
listener.handleMemoryPool("TestPool", 17, 20);
assertEquals(153, child.get(), 0.001);
}

@Test
public void testConstructorShouldIgnoreNotNotificationEmitterClasses() {
final GarbageCollectorMXBean notificationEmitterMXBean = Mockito.mock(GarbageCollectorMXBean.class, Mockito.withSettings().extraInterfaces(NotificationEmitter.class));
final GarbageCollectorMXBean notNotificationEmitterMXBean = Mockito.mock(GarbageCollectorMXBean.class);

new MemoryAllocationExports() {
@Override
protected List<GarbageCollectorMXBean> getGarbageCollectorMXBeans() {
return Arrays.asList(notificationEmitterMXBean, notNotificationEmitterMXBean);
}
};

Mockito.verify((NotificationEmitter) notificationEmitterMXBean).addNotificationListener(Mockito.any(MemoryAllocationExports.AllocationCountingNotificationListener.class), Mockito.isNull(NotificationFilter.class), Mockito.isNull());
Mockito.verifyZeroInteractions(notNotificationEmitterMXBean);
}
}