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

JDK 15+16 test fixes #2158

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnJre;
import org.junit.jupiter.api.condition.JRE;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -102,6 +104,7 @@ public void closeLogger() {
protected abstract LoggerFacade createLoggerFacade();

@Test
@DisabledOnJre(JRE.JAVA_15)
public void testSimpleLogShading() throws Exception {
setEcsReformattingConfig(LogEcsReformatting.SHADE);
initializeShadeDir("simple");
Expand Down Expand Up @@ -132,6 +135,7 @@ private void runSimpleScenario() throws Exception {
}

@Test
@DisabledOnJre(JRE.JAVA_15)
public void testMarkers() throws Exception {
if (markersSupported()) {
setEcsReformattingConfig(LogEcsReformatting.SHADE);
Expand Down Expand Up @@ -159,13 +163,15 @@ protected boolean markersSupported() {
}

@Test
@DisabledOnJre(JRE.JAVA_15)
public void testShadingIntoOriginalLogsDir() throws Exception {
setEcsReformattingConfig(LogEcsReformatting.SHADE);
initializeShadeDir("");
runSimpleScenario();
}

@Test
@DisabledOnJre(JRE.JAVA_15)
public void testLazyShadeFileCreation() throws Exception {
initializeShadeDir("delayed");
logger.trace(TRACE_MESSAGE);
Expand All @@ -186,6 +192,7 @@ public void testLazyShadeFileCreation() throws Exception {
}

@Test
@DisabledOnJre(JRE.JAVA_15)
public void testLogShadingReplaceOriginal() throws IOException {
initializeShadeDir("replace");
setEcsReformattingConfig(LogEcsReformatting.REPLACE);
Expand All @@ -203,6 +210,7 @@ public void testLogShadingReplaceOriginal() throws IOException {
}

@Test
@DisabledOnJre(JRE.JAVA_15)
public void testLogOverride() throws IOException {
setEcsReformattingConfig(LogEcsReformatting.OVERRIDE);
logger.trace(TRACE_MESSAGE);
Expand Down Expand Up @@ -231,6 +239,7 @@ public void testEmptyFormatterAllowList() throws Exception {
}

@Test
@DisabledOnJre(JRE.JAVA_15)
public void testDynamicConfiguration() throws Exception {
initializeShadeDir("dynamic");
for (int i = 0; i < 2; i++) {
Expand Down Expand Up @@ -298,7 +307,11 @@ private ArrayList<JsonNode> readShadeLogFile() throws IOException {
@Nonnull
private ArrayList<JsonNode> readEcsLogFile(String shadeLogFilePath) throws IOException {
ArrayList<JsonNode> ecsLogLines = new ArrayList<>();
try (Stream<String> stream = Files.lines(Paths.get(shadeLogFilePath))) {
Path path = Paths.get(shadeLogFilePath);
assertThat(path)
.describedAs("shaded log file should exist %s", path.toAbsolutePath())
.exists();
try (Stream<String> stream = Files.lines(path)) {
stream.forEach(line -> {
try {
ecsLogLines.add(objectMapper.readTree(line));
Expand Down Expand Up @@ -358,9 +371,11 @@ private void verifyEcsFormat(String[] splitRawLogLine, JsonNode ecsLogLineTree,
* is a notorious way to make tests flaky. If that proves to be the case, this test can be disabled, as its
* importance for regression testing is not crucial. It would be very useful if we decide to modify anything in
* our logging configuration, for example - change the rolling decision strategy.
*
* @throws IOException thrown if we fail to read the shade log file
*/
@Test
@DisabledOnJre(JRE.JAVA_15)
public void testShadeLogRolling() throws IOException {
setEcsReformattingConfig(LogEcsReformatting.SHADE);
initializeShadeDir("rolling");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.DisabledOnJre;
import org.junit.jupiter.api.condition.JRE;
import redis.clients.jedis.Jedis;

import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -48,6 +50,7 @@ void tearDownJedis() {
}

@Test
@DisabledOnJre(JRE.JAVA_15) // https://github.com/elastic/apm-agent-java/issues/1944
void testJedis() {
jedis.set("foo", "bar");
assertThat(jedis.get("foo".getBytes())).isEqualTo("bar".getBytes());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,52 +20,43 @@

import co.elastic.apm.agent.AbstractInstrumentationTest;
import co.elastic.apm.agent.util.ExecutorUtils;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import javax.net.SocketFactory;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.URL;
import java.net.URLConnection;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;

class SSLContextInstrumentationTest extends AbstractInstrumentationTest {

private static Field defaultSSLSocketFactoryField;
private static ThreadPoolExecutor elasticApmThreadPool = ExecutorUtils.createSingleThreadSchedulingDaemonPool("HttpsUrlConnection-Test");
private static final ThreadPoolExecutor elasticApmThreadPool = ExecutorUtils.createSingleThreadSchedulingDaemonPool("HttpsUrlConnection-Test");

@BeforeAll
static void setup() throws Exception {
defaultSSLSocketFactoryField = HttpsURLConnection.class.getDeclaredField("defaultSSLSocketFactory");
defaultSSLSocketFactoryField.setAccessible(true);
defaultSSLSocketFactoryField.set(null, null);
}

@BeforeEach
void resetState() throws Exception {
defaultSSLSocketFactoryField.set(null, null);
}
// note: we can't directly test that the default field is initialized or not by our agent
// thus we have to test the "implementation detail" that calling any of the getDefault() methods should return
// null when called from an agent thread to prevent eager initialization by the agent.w

@Test
void testNonSkipped() throws Exception {
createConnection();
Object defaultSslFactory = defaultSSLSocketFactoryField.get(null);
assertThat(defaultSslFactory).isNotNull();
assertThat(defaultSslFactory).isEqualTo(HttpsURLConnection.getDefaultSSLSocketFactory());
void testNonSkipped() {
checkDefaultFactory(false);
}

@Test
void testSkipped() throws Exception {
Future<?> connectionCreationFuture = elasticApmThreadPool.submit(this::createConnection);
Future<?> connectionCreationFuture = elasticApmThreadPool.submit(() -> {
checkDefaultFactory(true);
createConnection();
Comment on lines +54 to +55
Copy link
Contributor

Choose a reason for hiding this comment

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

No need - in the original test, we would test the field itself starts as null and that a creation of HTTPS connection initializes it.

checkDefaultFactory(true);
});
connectionCreationFuture.get();
assertThat(defaultSSLSocketFactoryField.get(null)).isNull();
createConnection();
assertThat(defaultSSLSocketFactoryField.get(null)).isNotNull();
checkDefaultFactory(false);
}

private void createConnection() {
Expand All @@ -76,4 +67,22 @@ private void createConnection() {
throw new RuntimeException(e);
}
}

private void checkDefaultFactory(boolean expectNull) {
try {
Stream.of(SSLContext.getDefault(),
SocketFactory.getDefault(),
SocketFactory.getDefault())
.forEach((d) -> {
if (expectNull) {
assertThat(d).isNull();
} else {
assertThat(d).isNotNull();
}
});

} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException(e);
}
}
}