Handle nulls in noop / multi (#2939)
* Handle nulls in noop / multi * Remove old file
This commit is contained in:
parent
bd6cfd85fb
commit
0b8131c79e
|
@ -45,6 +45,9 @@ final class MultiTextMapPropagator implements TextMapPropagator {
|
|||
|
||||
@Override
|
||||
public <C> void inject(Context context, @Nullable C carrier, TextMapSetter<C> setter) {
|
||||
if (context == null || setter == null) {
|
||||
return;
|
||||
}
|
||||
for (TextMapPropagator textPropagator : textPropagators) {
|
||||
textPropagator.inject(context, carrier, setter);
|
||||
}
|
||||
|
@ -52,6 +55,12 @@ final class MultiTextMapPropagator implements TextMapPropagator {
|
|||
|
||||
@Override
|
||||
public <C> Context extract(Context context, @Nullable C carrier, TextMapGetter<C> getter) {
|
||||
if (context == null) {
|
||||
return Context.root();
|
||||
}
|
||||
if (getter == null) {
|
||||
return context;
|
||||
}
|
||||
for (TextMapPropagator textPropagator : textPropagators) {
|
||||
context = textPropagator.extract(context, carrier, getter);
|
||||
}
|
||||
|
|
|
@ -27,6 +27,9 @@ final class NoopTextMapPropagator implements TextMapPropagator {
|
|||
|
||||
@Override
|
||||
public <C> Context extract(Context context, @Nullable C carrier, TextMapGetter<C> getter) {
|
||||
if (context == null) {
|
||||
return Context.root();
|
||||
}
|
||||
return context;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,9 +12,12 @@ import static org.mockito.Mockito.verify;
|
|||
import static org.mockito.Mockito.when;
|
||||
|
||||
import io.opentelemetry.context.Context;
|
||||
import io.opentelemetry.context.ContextKey;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import javax.annotation.Nullable;
|
||||
|
@ -26,6 +29,8 @@ import org.mockito.junit.jupiter.MockitoExtension;
|
|||
@ExtendWith(MockitoExtension.class)
|
||||
class MultiTextMapPropagatorTest {
|
||||
|
||||
private static final ContextKey<String> KEY = ContextKey.named("key");
|
||||
|
||||
@Mock private TextMapPropagator propagator1;
|
||||
@Mock private TextMapPropagator propagator2;
|
||||
@Mock private TextMapPropagator propagator3;
|
||||
|
@ -93,19 +98,6 @@ class MultiTextMapPropagatorTest {
|
|||
verify(propagator3).inject(context, carrier, setter);
|
||||
}
|
||||
|
||||
@Test
|
||||
void inject_nullContextAllDelegated() {
|
||||
Map<String, String> carrier = new HashMap<>();
|
||||
Context context = null;
|
||||
TextMapSetter<Map<String, String>> setter = Map::put;
|
||||
|
||||
TextMapPropagator prop = new MultiTextMapPropagator(propagator1, propagator2, propagator3);
|
||||
prop.inject(context, carrier, setter);
|
||||
verify(propagator1).inject(context, carrier, setter);
|
||||
verify(propagator2).inject(context, carrier, setter);
|
||||
verify(propagator3).inject(context, carrier, setter);
|
||||
}
|
||||
|
||||
@Test
|
||||
void extract_noPropagators() {
|
||||
Map<String, String> carrier = new HashMap<>();
|
||||
|
@ -147,14 +139,33 @@ class MultiTextMapPropagatorTest {
|
|||
|
||||
@Test
|
||||
void extract_nullContext() {
|
||||
Map<String, String> carrier = new HashMap<>();
|
||||
Context context = null;
|
||||
when(propagator1.extract(context, carrier, getter)).thenReturn(context);
|
||||
when(propagator2.extract(context, carrier, getter)).thenReturn(context);
|
||||
assertThat(
|
||||
new MultiTextMapPropagator(propagator1, propagator2)
|
||||
.extract(null, Collections.emptyMap(), getter))
|
||||
.isSameAs(Context.root());
|
||||
}
|
||||
|
||||
TextMapPropagator prop = new MultiTextMapPropagator(propagator1, propagator2);
|
||||
Context result = prop.extract(context, carrier, getter);
|
||||
@Test
|
||||
void extract_nullGetter() {
|
||||
Context context = Context.current().with(KEY, "treasure");
|
||||
assertThat(
|
||||
new MultiTextMapPropagator(propagator1, propagator2)
|
||||
.extract(context, Collections.emptyMap(), null))
|
||||
.isSameAs(context);
|
||||
}
|
||||
|
||||
assertThat(result).isSameAs(context);
|
||||
@Test
|
||||
void inject_nullContext() {
|
||||
Map<String, String> carrier = new LinkedHashMap<>();
|
||||
new MultiTextMapPropagator(propagator1, propagator2).inject(null, carrier, Map::put);
|
||||
assertThat(carrier).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void inject_nullSetter() {
|
||||
Map<String, String> carrier = new LinkedHashMap<>();
|
||||
Context context = Context.current().with(KEY, "treasure");
|
||||
new MultiTextMapPropagator(propagator1, propagator2).inject(context, carrier, null);
|
||||
assertThat(carrier).isEmpty();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,12 +8,18 @@ package io.opentelemetry.context.propagation;
|
|||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import io.opentelemetry.context.Context;
|
||||
import io.opentelemetry.context.ContextKey;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import javax.annotation.Nullable;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class NoopTextMapPropagatorTest {
|
||||
|
||||
private static final ContextKey<String> KEY = ContextKey.named("key");
|
||||
|
||||
@Test
|
||||
void noopFields() {
|
||||
assertThat(TextMapPropagator.noop().fields()).isEmpty();
|
||||
|
@ -23,28 +29,51 @@ class NoopTextMapPropagatorTest {
|
|||
void extract_contextUnchanged() {
|
||||
Context input = Context.current();
|
||||
Context result =
|
||||
TextMapPropagator.noop().extract(input, new HashMap<>(), new HashMapTextMapGetter());
|
||||
TextMapPropagator.noop().extract(input, new HashMap<>(), MapTextMapGetter.INSTANCE);
|
||||
assertThat(result).isSameAs(input);
|
||||
}
|
||||
|
||||
@Test
|
||||
void extract_nullContext() {
|
||||
Context input = null;
|
||||
Context result =
|
||||
TextMapPropagator.noop().extract(input, new HashMap<>(), new HashMapTextMapGetter());
|
||||
assertThat(result).isSameAs(input);
|
||||
assertThat(
|
||||
TextMapPropagator.noop()
|
||||
.extract(null, Collections.emptyMap(), MapTextMapGetter.INSTANCE))
|
||||
.isSameAs(Context.root());
|
||||
}
|
||||
|
||||
private static class HashMapTextMapGetter
|
||||
implements TextMapGetter<HashMap<? extends Object, ? extends Object>> {
|
||||
@Test
|
||||
void extract_nullGetter() {
|
||||
Context context = Context.current().with(KEY, "treasure");
|
||||
assertThat(TextMapPropagator.noop().extract(context, Collections.emptyMap(), null))
|
||||
.isSameAs(context);
|
||||
}
|
||||
|
||||
@Test
|
||||
void inject_nullContext() {
|
||||
Map<String, String> carrier = new LinkedHashMap<>();
|
||||
TextMapPropagator.noop().inject(null, carrier, Map::put);
|
||||
assertThat(carrier).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void inject_nullSetter() {
|
||||
Map<String, String> carrier = new LinkedHashMap<>();
|
||||
Context context = Context.current().with(KEY, "treasure");
|
||||
TextMapPropagator.noop().inject(context, carrier, null);
|
||||
assertThat(carrier).isEmpty();
|
||||
}
|
||||
|
||||
enum MapTextMapGetter implements TextMapGetter<Map<? extends Object, ? extends Object>> {
|
||||
INSTANCE;
|
||||
|
||||
@Override
|
||||
public Iterable<String> keys(HashMap<?, ?> carrier) {
|
||||
public Iterable<String> keys(Map<?, ?> carrier) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public String get(@Nullable HashMap<?, ?> carrier, String key) {
|
||||
public String get(@Nullable Map<?, ?> carrier, String key) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue