Skip to content

Commit

Permalink
Invalidate cached connection if explicitly closed
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewazores committed Dec 18, 2020
1 parent d883e6a commit d1220c3
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@

import java.net.MalformedURLException;
import java.time.Duration;
import java.util.Collections;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -125,6 +126,7 @@ private JFRConnection connect(ConnectionDescriptor connectionDescriptor) throws
private JFRConnection attemptConnectAsJMXServiceURL(ConnectionDescriptor connectionDescriptor)
throws Exception {
return connect(
connectionDescriptor,
new JMXServiceURL(connectionDescriptor.getTargetId()),
connectionDescriptor.getCredentials());
}
Expand All @@ -142,14 +144,21 @@ private JFRConnection attemptConnectAsHostPortPair(ConnectionDescriptor connecti
port = "9091";
}
return connect(
connectionDescriptor,
jfrConnectionToolkit.get().createServiceURL(host, Integer.parseInt(port)),
connectionDescriptor.getCredentials());
}

private JFRConnection connect(JMXServiceURL url, Optional<Credentials> credentials)
private JFRConnection connect(
ConnectionDescriptor cacheKey, JMXServiceURL url, Optional<Credentials> credentials)
throws Exception {
logger.info(String.format("Creating connection for %s", url.toString()));
return jfrConnectionToolkit.get().connect(url, credentials.orElse(null));
return jfrConnectionToolkit
.get()
.connect(
url,
credentials.orElse(null),
Collections.singletonList(() -> this.connections.invalidate(cacheKey)));
}

public interface ConnectedTask<T> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
package com.redhat.rhjmc.containerjfr.net;

import java.time.Duration;
import java.util.List;

import javax.management.remote.JMXServiceURL;

Expand All @@ -50,6 +51,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
Expand Down Expand Up @@ -89,7 +91,7 @@ public JMXServiceURL answer(InvocationOnMock args) throws Throwable {
String.format("/jndi/rmi://%s:%d/jmxrmi", host, port));
}
});
Mockito.when(jfrConnectionToolkit.connect(Mockito.any(), Mockito.any()))
Mockito.when(jfrConnectionToolkit.connect(Mockito.any(), Mockito.any(), Mockito.any()))
.thenAnswer(
new Answer<JFRConnection>() {
@Override
Expand Down Expand Up @@ -128,7 +130,7 @@ public JMXServiceURL answer(InvocationOnMock args) throws Throwable {
String.format("/jndi/rmi://%s:%d/jmxrmi", host, port));
}
});
Mockito.when(jfrConnectionToolkit.connect(Mockito.any(), Mockito.any()))
Mockito.when(jfrConnectionToolkit.connect(Mockito.any(), Mockito.any(), Mockito.any()))
.thenAnswer(
new Answer<JFRConnection>() {
@Override
Expand All @@ -143,6 +145,41 @@ public JFRConnection answer(InvocationOnMock invocation)
MatcherAssert.assertThat(conn1, Matchers.sameInstance(conn2));
}

@Test
void shouldCreateNewConnectionIfPreviousExplicitlyClosed() throws Exception {
Mockito.when(jfrConnectionToolkit.createServiceURL(Mockito.anyString(), Mockito.anyInt()))
.thenAnswer(
new Answer<JMXServiceURL>() {
@Override
public JMXServiceURL answer(InvocationOnMock args) throws Throwable {
String host = args.getArgument(0);
int port = args.getArgument(1);
return new JMXServiceURL(
"rmi",
"",
0,
String.format("/jndi/rmi://%s:%d/jmxrmi", host, port));
}
});
ArgumentCaptor<List<Runnable>> closeListeners = ArgumentCaptor.forClass(List.class);
Mockito.when(
jfrConnectionToolkit.connect(
Mockito.any(), Mockito.any(), closeListeners.capture()))
.thenAnswer(
new Answer<JFRConnection>() {
@Override
public JFRConnection answer(InvocationOnMock invocation)
throws Throwable {
return Mockito.mock(JFRConnection.class);
}
});
ConnectionDescriptor desc = new ConnectionDescriptor("foo");
JFRConnection conn1 = mgr.executeConnectedTask(desc, a -> a);
closeListeners.getValue().forEach(Runnable::run);
JFRConnection conn2 = mgr.executeConnectedTask(desc, a -> a);
MatcherAssert.assertThat(conn1, Matchers.not(Matchers.sameInstance(conn2)));
}

@Test
void shouldCreateNewConnectionForAccessDelayedLongerThanTTL() throws Exception {
TargetConnectionManager mgr =
Expand All @@ -162,7 +199,7 @@ public JMXServiceURL answer(InvocationOnMock args) throws Throwable {
String.format("/jndi/rmi://%s:%d/jmxrmi", host, port));
}
});
Mockito.when(jfrConnectionToolkit.connect(Mockito.any(), Mockito.any()))
Mockito.when(jfrConnectionToolkit.connect(Mockito.any(), Mockito.any(), Mockito.any()))
.thenAnswer(
new Answer<JFRConnection>() {
@Override
Expand Down

0 comments on commit d1220c3

Please sign in to comment.