|
| 1 | +/* |
| 2 | + * Copyright 2012-2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.autoconfigure.reactor; |
| 18 | + |
| 19 | +import java.util.concurrent.atomic.AtomicReference; |
| 20 | + |
| 21 | +import io.micrometer.context.ContextRegistry; |
| 22 | +import org.junit.jupiter.api.BeforeAll; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | +import reactor.core.publisher.Mono; |
| 25 | +import reactor.util.context.Context; |
| 26 | + |
| 27 | +import org.springframework.boot.autoconfigure.AutoConfigurations; |
| 28 | +import org.springframework.boot.test.context.runner.ApplicationContextRunner; |
| 29 | + |
| 30 | +import static org.assertj.core.api.Assertions.assertThat; |
| 31 | + |
| 32 | +/** |
| 33 | + * Tests for {@link ReactorAutoConfiguration}. |
| 34 | + * |
| 35 | + * @author Brian Clozel |
| 36 | + */ |
| 37 | +class ReactorAutoConfigurationTests { |
| 38 | + |
| 39 | + private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() |
| 40 | + .withConfiguration(AutoConfigurations.of(ReactorAutoConfiguration.class)); |
| 41 | + |
| 42 | + private static final String THREADLOCAL_KEY = "ReactorAutoConfigurationTests"; |
| 43 | + |
| 44 | + private static final ThreadLocal<String> THREADLOCAL_VALUE = ThreadLocal.withInitial(() -> "failure"); |
| 45 | + |
| 46 | + @BeforeAll |
| 47 | + static void initializeThreadLocalAccessors() { |
| 48 | + ContextRegistry globalRegistry = ContextRegistry.getInstance(); |
| 49 | + globalRegistry.registerThreadLocalAccessor(THREADLOCAL_KEY, THREADLOCAL_VALUE); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + void shouldConfigureAutomaticContextPropagation() { |
| 54 | + AtomicReference<String> threadLocalValue = new AtomicReference<>(); |
| 55 | + this.contextRunner.run((applicationContext) -> { |
| 56 | + Mono.just("test") |
| 57 | + .doOnNext((element) -> threadLocalValue.set(THREADLOCAL_VALUE.get())) |
| 58 | + .contextWrite(Context.of(THREADLOCAL_KEY, "success")) |
| 59 | + .block(); |
| 60 | + assertThat(threadLocalValue.get()).isEqualTo("success"); |
| 61 | + }); |
| 62 | + } |
| 63 | + |
| 64 | +} |
0 commit comments