Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
serkan-ozal committed Aug 13, 2024
1 parent ee09b87 commit 8b93008
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 0 deletions.
13 changes: 13 additions & 0 deletions javaagent-tooling/testing-java18/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
plugins {
id("otel.javaagent-instrumentation")
}

dependencies {
// To be able to have it as part of agent,
// this dependency needs to be added as "testInstrumentation", not as "testImplementation"
testInstrumentation(project(":instrumentation:resources:library"))
}

otelJava {
minJavaVersionSupported.set(JavaVersion.VERSION_18)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.tooling.inetaddress;

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

import java.net.InetAddress;
import org.junit.jupiter.api.Test;

public class InetAddressResolverTest {

@Test
void agentStartShouldNotTriggerLoadingCustomInetAddressResolvers() throws Exception {
// Agent start should not trigger loading (and instantiating) custom InetAddress resolvers
assertThat(TestAddressResolver.isInstantiated()).isFalse();

// Trigger loading (and instantiating) custom InetAddress resolvers manually
InetAddress.getAllByName("test");

// Verify that custom InetAddress resolver loaded and instantiated
assertThat(TestAddressResolver.isInstantiated()).isTrue();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.tooling.inetaddress;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.net.spi.InetAddressResolver;
import java.util.stream.Stream;

public class TestAddressResolver implements InetAddressResolver {

private static volatile boolean instantiated = false;

@SuppressWarnings("StaticAssignmentInConstructor")
public TestAddressResolver() {
TestAddressResolver.instantiated = true;
}

public static boolean isInstantiated() {
return instantiated;
}

@Override
public Stream<InetAddress> lookupByName(String host, LookupPolicy lookupPolicy)
throws UnknownHostException {
if (host.equals("test")) {
return Stream.of(InetAddress.getByAddress(new byte[] {127, 0, 0, 1}));
}
throw new UnknownHostException();
}

@Override
public String lookupByAddress(byte[] addr) {
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.tooling.inetaddress;

import java.net.spi.InetAddressResolver;
import java.net.spi.InetAddressResolverProvider;

public class TestAddressResolverProvider extends InetAddressResolverProvider {

@Override
public InetAddressResolver get(Configuration configuration) {
return new TestAddressResolver();
}

@Override
public String name() {
return "Test Internet Address Resolver Provider";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.opentelemetry.javaagent.tooling.inetaddress.TestAddressResolverProvider
1 change: 1 addition & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ include(":javaagent-bootstrap")
include(":javaagent-extension-api")
include(":javaagent-tooling")
include(":javaagent-tooling:javaagent-tooling-java9")
include(":javaagent-tooling:testing-java18")
include(":javaagent-internal-logging-application")
include(":javaagent-internal-logging-simple")
include(":javaagent")
Expand Down

0 comments on commit 8b93008

Please sign in to comment.