Polishing in tests of ThreadLocalAccessor implementations
See gh-32296
This commit is contained in:
parent
5cb4985234
commit
3ada9a0c79
|
@ -17,72 +17,71 @@
|
||||||
package org.springframework.context.i18n;
|
package org.springframework.context.i18n;
|
||||||
|
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.concurrent.CountDownLatch;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
import io.micrometer.context.ContextRegistry;
|
import io.micrometer.context.ContextRegistry;
|
||||||
import io.micrometer.context.ContextSnapshot;
|
import io.micrometer.context.ContextSnapshot;
|
||||||
import io.micrometer.context.ContextSnapshotFactory;
|
import io.micrometer.context.ContextSnapshotFactory;
|
||||||
import org.junit.jupiter.api.AfterEach;
|
|
||||||
import org.junit.jupiter.params.ParameterizedTest;
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
import org.junit.jupiter.params.provider.Arguments;
|
import org.junit.jupiter.params.provider.Arguments;
|
||||||
import org.junit.jupiter.params.provider.MethodSource;
|
import org.junit.jupiter.params.provider.MethodSource;
|
||||||
|
|
||||||
import org.springframework.lang.Nullable;
|
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for {@link LocaleContextThreadLocalAccessor}.
|
* Tests for {@link LocaleContextThreadLocalAccessor}.
|
||||||
*
|
*
|
||||||
* @author Tadaya Tsuyukubo
|
* @author Tadaya Tsuyukubo
|
||||||
|
* @author Rossen Stoyanchev
|
||||||
*/
|
*/
|
||||||
class LocaleContextThreadLocalAccessorTests {
|
class LocaleContextThreadLocalAccessorTests {
|
||||||
|
|
||||||
private final ContextRegistry registry = new ContextRegistry()
|
private final ContextRegistry registry =
|
||||||
.registerThreadLocalAccessor(new LocaleContextThreadLocalAccessor());
|
new ContextRegistry().registerThreadLocalAccessor(new LocaleContextThreadLocalAccessor());
|
||||||
|
|
||||||
@AfterEach
|
|
||||||
void cleanUp() {
|
private static Stream<Arguments> propagation() {
|
||||||
LocaleContextHolder.resetLocaleContext();
|
LocaleContext previousContext = new SimpleLocaleContext(Locale.ENGLISH);
|
||||||
|
LocaleContext currentContext = new SimpleLocaleContext(Locale.ENGLISH);
|
||||||
|
return Stream.of(Arguments.of(null, currentContext), Arguments.of(previousContext, currentContext));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ParameterizedTest
|
@ParameterizedTest
|
||||||
@MethodSource
|
@MethodSource
|
||||||
@SuppressWarnings("try")
|
@SuppressWarnings({ "try", "unused" })
|
||||||
void propagation(@Nullable LocaleContext previous, LocaleContext current) throws Exception {
|
void propagation(LocaleContext previousContext, LocaleContext currentContext) throws Exception {
|
||||||
LocaleContextHolder.setLocaleContext(current);
|
|
||||||
ContextSnapshot snapshot = ContextSnapshotFactory.builder()
|
|
||||||
.contextRegistry(this.registry)
|
|
||||||
.clearMissing(true)
|
|
||||||
.build()
|
|
||||||
.captureAll();
|
|
||||||
|
|
||||||
AtomicReference<LocaleContext> previousHolder = new AtomicReference<>();
|
ContextSnapshot snapshot = createContextSnapshotFor(currentContext);
|
||||||
AtomicReference<LocaleContext> currentHolder = new AtomicReference<>();
|
|
||||||
CountDownLatch latch = new CountDownLatch(1);
|
AtomicReference<LocaleContext> contextInScope = new AtomicReference<>();
|
||||||
new Thread(() -> {
|
AtomicReference<LocaleContext> contextAfterScope = new AtomicReference<>();
|
||||||
LocaleContextHolder.setLocaleContext(previous);
|
|
||||||
|
Thread thread = new Thread(() -> {
|
||||||
|
LocaleContextHolder.setLocaleContext(previousContext);
|
||||||
try (ContextSnapshot.Scope scope = snapshot.setThreadLocals()) {
|
try (ContextSnapshot.Scope scope = snapshot.setThreadLocals()) {
|
||||||
currentHolder.set(LocaleContextHolder.getLocaleContext());
|
contextInScope.set(LocaleContextHolder.getLocaleContext());
|
||||||
}
|
}
|
||||||
previousHolder.set(LocaleContextHolder.getLocaleContext());
|
contextAfterScope.set(LocaleContextHolder.getLocaleContext());
|
||||||
latch.countDown();
|
});
|
||||||
}).start();
|
|
||||||
|
|
||||||
latch.await(1, TimeUnit.SECONDS);
|
thread.start();
|
||||||
assertThat(previousHolder).hasValueSatisfying(value -> assertThat(value).isSameAs(previous));
|
thread.join(1000);
|
||||||
assertThat(currentHolder).hasValueSatisfying(value -> assertThat(value).isSameAs(current));
|
|
||||||
|
assertThat(contextAfterScope).hasValueSatisfying(value -> assertThat(value).isSameAs(previousContext));
|
||||||
|
assertThat(contextInScope).hasValueSatisfying(value -> assertThat(value).isSameAs(currentContext));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Stream<Arguments> propagation() {
|
private ContextSnapshot createContextSnapshotFor(LocaleContext context) {
|
||||||
LocaleContext previous = new SimpleLocaleContext(Locale.ENGLISH);
|
LocaleContextHolder.setLocaleContext(context);
|
||||||
LocaleContext current = new SimpleLocaleContext(Locale.ENGLISH);
|
try {
|
||||||
return Stream.of(
|
return ContextSnapshotFactory.builder()
|
||||||
Arguments.of(null, current),
|
.contextRegistry(this.registry).clearMissing(true).build()
|
||||||
Arguments.of(previous, current)
|
.captureAll();
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
finally {
|
||||||
|
LocaleContextHolder.resetLocaleContext();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,8 +16,6 @@
|
||||||
|
|
||||||
package org.springframework.web.context.request;
|
package org.springframework.web.context.request;
|
||||||
|
|
||||||
import java.util.concurrent.CountDownLatch;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
import java.util.concurrent.atomic.AtomicReference;
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
|
@ -25,13 +23,10 @@ import io.micrometer.context.ContextRegistry;
|
||||||
import io.micrometer.context.ContextSnapshot;
|
import io.micrometer.context.ContextSnapshot;
|
||||||
import io.micrometer.context.ContextSnapshot.Scope;
|
import io.micrometer.context.ContextSnapshot.Scope;
|
||||||
import io.micrometer.context.ContextSnapshotFactory;
|
import io.micrometer.context.ContextSnapshotFactory;
|
||||||
import org.junit.jupiter.api.AfterEach;
|
|
||||||
import org.junit.jupiter.params.ParameterizedTest;
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
import org.junit.jupiter.params.provider.Arguments;
|
import org.junit.jupiter.params.provider.Arguments;
|
||||||
import org.junit.jupiter.params.provider.MethodSource;
|
import org.junit.jupiter.params.provider.MethodSource;
|
||||||
|
|
||||||
import org.springframework.lang.Nullable;
|
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.mockito.Mockito.mock;
|
import static org.mockito.Mockito.mock;
|
||||||
|
|
||||||
|
@ -39,52 +34,55 @@ import static org.mockito.Mockito.mock;
|
||||||
* Tests for {@link RequestAttributesThreadLocalAccessor}.
|
* Tests for {@link RequestAttributesThreadLocalAccessor}.
|
||||||
*
|
*
|
||||||
* @author Tadaya Tsuyukubo
|
* @author Tadaya Tsuyukubo
|
||||||
|
* @author Rossen Stoyanchev
|
||||||
*/
|
*/
|
||||||
class RequestAttributesThreadLocalAccessorTests {
|
class RequestAttributesThreadLocalAccessorTests {
|
||||||
|
|
||||||
private final ContextRegistry registry = new ContextRegistry()
|
private final ContextRegistry registry =
|
||||||
.registerThreadLocalAccessor(new RequestAttributesThreadLocalAccessor());
|
new ContextRegistry().registerThreadLocalAccessor(new RequestAttributesThreadLocalAccessor());
|
||||||
|
|
||||||
@AfterEach
|
|
||||||
void cleanUp() {
|
private static Stream<Arguments> propagation() {
|
||||||
RequestContextHolder.resetRequestAttributes();
|
RequestAttributes previous = mock(RequestAttributes.class);
|
||||||
|
RequestAttributes current = mock(RequestAttributes.class);
|
||||||
|
return Stream.of(Arguments.of(null, current), Arguments.of(previous, current));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ParameterizedTest
|
@ParameterizedTest
|
||||||
@MethodSource
|
@MethodSource
|
||||||
@SuppressWarnings({ "try", "unused" })
|
@SuppressWarnings({ "try", "unused" })
|
||||||
void propagation(@Nullable RequestAttributes previous, RequestAttributes current) throws Exception {
|
void propagation(RequestAttributes previousRequest, RequestAttributes currentRequest) throws Exception {
|
||||||
RequestContextHolder.setRequestAttributes(current);
|
|
||||||
ContextSnapshot snapshot = ContextSnapshotFactory.builder()
|
|
||||||
.contextRegistry(this.registry)
|
|
||||||
.clearMissing(true)
|
|
||||||
.build()
|
|
||||||
.captureAll();
|
|
||||||
|
|
||||||
AtomicReference<RequestAttributes> previousHolder = new AtomicReference<>();
|
ContextSnapshot snapshot = getSnapshotFor(currentRequest);
|
||||||
AtomicReference<RequestAttributes> currentHolder = new AtomicReference<>();
|
|
||||||
CountDownLatch latch = new CountDownLatch(1);
|
AtomicReference<RequestAttributes> requestInScope = new AtomicReference<>();
|
||||||
new Thread(() -> {
|
AtomicReference<RequestAttributes> requestAfterScope = new AtomicReference<>();
|
||||||
RequestContextHolder.setRequestAttributes(previous);
|
|
||||||
|
Thread thread = new Thread(() -> {
|
||||||
|
RequestContextHolder.setRequestAttributes(previousRequest);
|
||||||
try (Scope scope = snapshot.setThreadLocals()) {
|
try (Scope scope = snapshot.setThreadLocals()) {
|
||||||
currentHolder.set(RequestContextHolder.getRequestAttributes());
|
requestInScope.set(RequestContextHolder.getRequestAttributes());
|
||||||
}
|
}
|
||||||
previousHolder.set(RequestContextHolder.getRequestAttributes());
|
requestAfterScope.set(RequestContextHolder.getRequestAttributes());
|
||||||
latch.countDown();
|
});
|
||||||
}).start();
|
|
||||||
|
|
||||||
latch.await(1, TimeUnit.SECONDS);
|
thread.start();
|
||||||
assertThat(previousHolder).hasValueSatisfying(value -> assertThat(value).isSameAs(previous));
|
thread.join(1000);
|
||||||
assertThat(currentHolder).hasValueSatisfying(value -> assertThat(value).isSameAs(current));
|
|
||||||
|
assertThat(requestInScope).hasValueSatisfying(value -> assertThat(value).isSameAs(currentRequest));
|
||||||
|
assertThat(requestAfterScope).hasValueSatisfying(value -> assertThat(value).isSameAs(previousRequest));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Stream<Arguments> propagation() {
|
private ContextSnapshot getSnapshotFor(RequestAttributes request) {
|
||||||
RequestAttributes previous = mock(RequestAttributes.class);
|
RequestContextHolder.setRequestAttributes(request);
|
||||||
RequestAttributes current = mock(RequestAttributes.class);
|
try {
|
||||||
return Stream.of(
|
return ContextSnapshotFactory.builder()
|
||||||
Arguments.of(null, current),
|
.contextRegistry(this.registry).clearMissing(true).build()
|
||||||
Arguments.of(previous, current)
|
.captureAll();
|
||||||
);
|
}
|
||||||
|
finally {
|
||||||
|
RequestContextHolder.resetRequestAttributes();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue