Created
September 9, 2025 21:25
-
-
Save jonatan-ivanov/cfdb70441e5af76d164fa68777efc826 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.junit.jupiter.api.BeforeEach; | |
| import org.junit.jupiter.api.Test; | |
| import java.util.concurrent.ExecutorService; | |
| import java.util.concurrent.Executors; | |
| import static org.assertj.core.api.Assertions.assertThat; | |
| @SuppressWarnings("NullAway") | |
| class CurrentObservationTest { | |
| private final ObservationRegistry registry = ObservationRegistry.create(); | |
| private final ExecutorService executor = Executors.newSingleThreadExecutor(); | |
| @BeforeEach | |
| void setUp() { | |
| registry.observationConfig().observationHandler(new ObservationTextPublisher()); | |
| registry.observationConfig().observationPredicate((name, context) -> !name.equals("b")); | |
| } | |
| @Test | |
| void currentTest() throws Exception { | |
| assertThat(registry.getCurrentObservation()).isNull(); | |
| Observation.createNotStarted("a", registry).observeChecked(this::doA); | |
| assertThat(registry.getCurrentObservation()).isNull(); | |
| } | |
| private void doA() throws Exception { | |
| assertThat(registry.getCurrentObservation().getContextView().getName()).isEqualTo("a"); | |
| System.out.println("A..."); | |
| Observation b = Observation.createNotStarted("b", registry).start(); | |
| executor.submit(() -> { | |
| try (Observation.Scope ignored = b.openScope()) { | |
| assertThat(registry.getCurrentObservation()).isSameAs(b); | |
| doB(); | |
| assertThat(registry.getCurrentObservation()).isSameAs(b); | |
| } | |
| assertThat(registry.getCurrentObservation()).isNull(); | |
| }).get(); | |
| assertThat(registry.getCurrentObservation().getContextView().getName()).isEqualTo("a"); | |
| } | |
| private void doB() { | |
| System.out.println("B..."); | |
| Observation.createNotStarted("c", registry).observe(this::doC); | |
| } | |
| private void doC() { | |
| assertThat(registry.getCurrentObservation().getContextView().getName()).isEqualTo("c"); | |
| System.out.println("C! :)"); | |
| assertThat(registry.getCurrentObservation().getContextView().getName()).isEqualTo("c"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment