Skip to content

Commit

Permalink
Fix unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
steverao committed Jan 9, 2025
1 parent 065372d commit 1c9e8e6
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ dependencies {
testLibrary("org.apache.dubbo:dubbo-config-api:2.7.0")
}

val latestDepTest = findProperty("testLatestDeps") as Boolean

testing {
suites {
val testLatestDepDubbo by registering(JvmTestSuite::class) {
dependencies {
implementation("org.apache.dubbo:dubbo:+")
implementation(project(":instrumentation:apache-dubbo-2.7:library-autoconfigure"))
implementation("org.apache.dubbo:dubbo:+")
}
}
}
Expand All @@ -34,7 +32,7 @@ tasks.withType<Test>().configureEach {
jvmArgs("--add-opens=java.base/java.lang=ALL-UNNAMED")
}

if (!(findProperty("testLatestDeps") as Boolean)) {
if (findProperty("testLatestDeps") as Boolean) {
tasks.check {
dependsOn(testing.suites)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ enum DubboHeadersGetter implements TextMapGetter<DubboRequest> {
MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodHandle getObjectAttachments = null;
try {
Class<?> rpcInvocation = Class.forName("org.apache.dubbo.rpc.RpcInvocation");
getObjectAttachments =
lookup.findStatic(
RpcInvocation.class, "getObjectAttachments", MethodType.methodType(void.class));
lookup.findVirtual(
rpcInvocation, "getObjectAttachments", MethodType.methodType(Map.class));
} catch (Throwable t) {
// ignore
}
Expand All @@ -37,7 +38,9 @@ public Iterable<String> keys(DubboRequest request) {
try {
// In 2.7.6, 2.7.7, the StringToObjectMap implementation does not correctly retrieve the
// keySet. Therefore, it's advisable to always call getObjectAttachments when it is available.
return ((Map<String, Object>) GET_OBJECT_ATTACHMENTS.invoke(invocation)).keySet();
if (GET_OBJECT_ATTACHMENTS != null) {
return ((Map<String, Object>) GET_OBJECT_ATTACHMENTS.invoke(invocation)).keySet();
}
} catch (Throwable t) {
// ignore
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.instrumentation.apachedubbo.v2_7;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;

import java.net.InetSocketAddress;
import java.util.Iterator;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.RpcInvocation;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
class DubboHeadersGetterTest {

@Mock RpcContext context;

@Test
@SuppressWarnings("deprecation") // deprecation for RpcInvocation()
void testKeys() {
when(context.getUrl()).thenReturn(new URL("http", "localhost", 1));
when(context.getRemoteAddress()).thenReturn(new InetSocketAddress(1));
when(context.getLocalAddress()).thenReturn(new InetSocketAddress(1));

RpcInvocation invocation = new RpcInvocation();
invocation.setAttachment("key", "value");
DubboRequest request = DubboRequest.create(invocation, context);

Iterator<String> iterator = DubboHeadersGetter.INSTANCE.keys(request).iterator();
assertThat(iterator.hasNext()).isTrue();
assertThat(iterator.next()).isEqualTo("key");
assertThat(iterator.hasNext()).isFalse();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static org.mockito.Mockito.when;

import java.net.InetSocketAddress;
import java.util.Collections;
import java.util.Iterator;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.rpc.RpcContext;
Expand All @@ -23,16 +24,17 @@ class DubboHeadersGetterTest {

@Mock RpcContext context;

@Mock RpcInvocation rpcInvocation;

@Test
@SuppressWarnings("deprecation") // deprecation for RpcInvocation()
@SuppressWarnings({"deprecation", "unchecked"}) // deprecation for RpcInvocation()
void testKeys() {
when(context.getUrl()).thenReturn(new URL("http", "localhost", 1));
when(context.getRemoteAddress()).thenReturn(new InetSocketAddress(1));
when(context.getLocalAddress()).thenReturn(new InetSocketAddress(1));

RpcInvocation invocation = new RpcInvocation();
invocation.setAttachment("key", "value");
DubboRequest request = DubboRequest.create(invocation, context);
when(rpcInvocation.getObjectAttachments()).thenReturn(Collections.singletonMap("key", "value"));
DubboRequest request = DubboRequest.create(rpcInvocation, context);

Iterator<String> iterator = DubboHeadersGetter.INSTANCE.keys(request).iterator();
assertThat(iterator.hasNext()).isTrue();
Expand Down

0 comments on commit 1c9e8e6

Please sign in to comment.