Add some more coverage of context code paths. (#2131)
This commit is contained in:
parent
abd5d642dd
commit
ebd35d0b14
|
|
@ -7,6 +7,11 @@ package io.opentelemetry.context;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.awaitility.Awaitility.await;
|
import static org.awaitility.Awaitility.await;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyLong;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
import com.google.common.util.concurrent.Futures;
|
import com.google.common.util.concurrent.Futures;
|
||||||
import com.google.common.util.concurrent.MoreExecutors;
|
import com.google.common.util.concurrent.MoreExecutors;
|
||||||
|
|
@ -33,8 +38,12 @@ import org.junit.jupiter.api.Nested;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.TestInstance;
|
import org.junit.jupiter.api.TestInstance;
|
||||||
import org.junit.jupiter.api.TestInstance.Lifecycle;
|
import org.junit.jupiter.api.TestInstance.Lifecycle;
|
||||||
|
import org.junit.jupiter.api.extension.ExtendWith;
|
||||||
|
import org.mockito.Mock;
|
||||||
|
import org.mockito.junit.jupiter.MockitoExtension;
|
||||||
|
|
||||||
@SuppressWarnings("ClassCanBeStatic")
|
@SuppressWarnings("ClassCanBeStatic")
|
||||||
|
@ExtendWith(MockitoExtension.class)
|
||||||
class ContextTest {
|
class ContextTest {
|
||||||
|
|
||||||
private static final ContextKey<String> ANIMAL = ContextKey.named("animal");
|
private static final ContextKey<String> ANIMAL = ContextKey.named("animal");
|
||||||
|
|
@ -356,6 +365,54 @@ class ContextTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void keyToString() {
|
||||||
|
assertThat(ANIMAL.toString()).isEqualTo("animal");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void attachSameContext() {
|
||||||
|
Context context = Context.current().with(ANIMAL, "cat");
|
||||||
|
try (Scope scope1 = context.makeCurrent()) {
|
||||||
|
assertThat(scope1).isNotSameAs(Scope.noop());
|
||||||
|
try (Scope scope2 = context.makeCurrent()) {
|
||||||
|
assertThat(scope2).isSameAs(Scope.noop());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// We test real context-related above but should test cleanup gets delegated, which is best with
|
||||||
|
// a mock.
|
||||||
|
@Nested
|
||||||
|
@TestInstance(Lifecycle.PER_CLASS)
|
||||||
|
class DelegatesToExecutorService {
|
||||||
|
|
||||||
|
@Mock private ExecutorService executor;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void delegatesCleanupMethods() throws Exception {
|
||||||
|
ExecutorService wrapped = CAT.wrap(executor);
|
||||||
|
wrapped.shutdown();
|
||||||
|
verify(executor).shutdown();
|
||||||
|
verifyNoMoreInteractions(executor);
|
||||||
|
wrapped.shutdownNow();
|
||||||
|
verify(executor).shutdownNow();
|
||||||
|
verifyNoMoreInteractions(executor);
|
||||||
|
when(executor.isShutdown()).thenReturn(true);
|
||||||
|
assertThat(wrapped.isShutdown()).isTrue();
|
||||||
|
verify(executor).isShutdown();
|
||||||
|
verifyNoMoreInteractions(executor);
|
||||||
|
when(wrapped.isTerminated()).thenReturn(true);
|
||||||
|
assertThat(wrapped.isTerminated()).isTrue();
|
||||||
|
verify(executor).isTerminated();
|
||||||
|
verifyNoMoreInteractions(executor);
|
||||||
|
when(executor.awaitTermination(anyLong(), any())).thenReturn(true);
|
||||||
|
assertThat(wrapped.awaitTermination(1, TimeUnit.SECONDS)).isTrue();
|
||||||
|
verify(executor).awaitTermination(1, TimeUnit.SECONDS);
|
||||||
|
verifyNoMoreInteractions(executor);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Nested
|
@Nested
|
||||||
@TestInstance(Lifecycle.PER_CLASS)
|
@TestInstance(Lifecycle.PER_CLASS)
|
||||||
class WrapScheduledExecutorService extends WrapExecutorService {
|
class WrapScheduledExecutorService extends WrapExecutorService {
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
/*
|
||||||
|
* Copyright The OpenTelemetry Authors
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
package io.opentelemetry.context.propagation;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class NoopTextMapPropagatorTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void noopFields() {
|
||||||
|
assertThat(TextMapPropagator.noop().fields()).isEmpty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,25 @@
|
||||||
|
/*
|
||||||
|
* Copyright The OpenTelemetry Authors
|
||||||
|
* SPDX-License-Identifier: Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
package io.opentelemetry.context.propagation;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
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 TextMapPropagatorTest {
|
||||||
|
|
||||||
|
@Mock private TextMapPropagator propagator;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void compositeNonMulti() {
|
||||||
|
assertThat(TextMapPropagator.composite()).isSameAs(TextMapPropagator.noop());
|
||||||
|
assertThat(TextMapPropagator.composite(propagator)).isSameAs(propagator);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue