Skip to content

Commit 3ada9a0

Browse files
committed
Polishing in tests of ThreadLocalAccessor implementations
See gh-32296
1 parent 5cb4985 commit 3ada9a0

File tree

2 files changed

+72
-75
lines changed

2 files changed

+72
-75
lines changed

spring-context/src/test/java/org/springframework/context/i18n/LocaleContextThreadLocalAccessorTests.java

Lines changed: 37 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -17,72 +17,71 @@
1717
package org.springframework.context.i18n;
1818

1919
import java.util.Locale;
20-
import java.util.concurrent.CountDownLatch;
21-
import java.util.concurrent.TimeUnit;
2220
import java.util.concurrent.atomic.AtomicReference;
2321
import java.util.stream.Stream;
2422

2523
import io.micrometer.context.ContextRegistry;
2624
import io.micrometer.context.ContextSnapshot;
2725
import io.micrometer.context.ContextSnapshotFactory;
28-
import org.junit.jupiter.api.AfterEach;
2926
import org.junit.jupiter.params.ParameterizedTest;
3027
import org.junit.jupiter.params.provider.Arguments;
3128
import org.junit.jupiter.params.provider.MethodSource;
3229

33-
import org.springframework.lang.Nullable;
34-
3530
import static org.assertj.core.api.Assertions.assertThat;
3631

3732
/**
3833
* Tests for {@link LocaleContextThreadLocalAccessor}.
3934
*
4035
* @author Tadaya Tsuyukubo
36+
* @author Rossen Stoyanchev
4137
*/
4238
class LocaleContextThreadLocalAccessorTests {
4339

44-
private final ContextRegistry registry = new ContextRegistry()
45-
.registerThreadLocalAccessor(new LocaleContextThreadLocalAccessor());
40+
private final ContextRegistry registry =
41+
new ContextRegistry().registerThreadLocalAccessor(new LocaleContextThreadLocalAccessor());
42+
4643

47-
@AfterEach
48-
void cleanUp() {
49-
LocaleContextHolder.resetLocaleContext();
44+
private static Stream<Arguments> propagation() {
45+
LocaleContext previousContext = new SimpleLocaleContext(Locale.ENGLISH);
46+
LocaleContext currentContext = new SimpleLocaleContext(Locale.ENGLISH);
47+
return Stream.of(Arguments.of(null, currentContext), Arguments.of(previousContext, currentContext));
5048
}
5149

5250
@ParameterizedTest
5351
@MethodSource
54-
@SuppressWarnings("try")
55-
void propagation(@Nullable LocaleContext previous, LocaleContext current) throws Exception {
56-
LocaleContextHolder.setLocaleContext(current);
57-
ContextSnapshot snapshot = ContextSnapshotFactory.builder()
58-
.contextRegistry(this.registry)
59-
.clearMissing(true)
60-
.build()
61-
.captureAll();
62-
63-
AtomicReference<LocaleContext> previousHolder = new AtomicReference<>();
64-
AtomicReference<LocaleContext> currentHolder = new AtomicReference<>();
65-
CountDownLatch latch = new CountDownLatch(1);
66-
new Thread(() -> {
67-
LocaleContextHolder.setLocaleContext(previous);
52+
@SuppressWarnings({ "try", "unused" })
53+
void propagation(LocaleContext previousContext, LocaleContext currentContext) throws Exception {
54+
55+
ContextSnapshot snapshot = createContextSnapshotFor(currentContext);
56+
57+
AtomicReference<LocaleContext> contextInScope = new AtomicReference<>();
58+
AtomicReference<LocaleContext> contextAfterScope = new AtomicReference<>();
59+
60+
Thread thread = new Thread(() -> {
61+
LocaleContextHolder.setLocaleContext(previousContext);
6862
try (ContextSnapshot.Scope scope = snapshot.setThreadLocals()) {
69-
currentHolder.set(LocaleContextHolder.getLocaleContext());
63+
contextInScope.set(LocaleContextHolder.getLocaleContext());
7064
}
71-
previousHolder.set(LocaleContextHolder.getLocaleContext());
72-
latch.countDown();
73-
}).start();
65+
contextAfterScope.set(LocaleContextHolder.getLocaleContext());
66+
});
67+
68+
thread.start();
69+
thread.join(1000);
7470

75-
latch.await(1, TimeUnit.SECONDS);
76-
assertThat(previousHolder).hasValueSatisfying(value -> assertThat(value).isSameAs(previous));
77-
assertThat(currentHolder).hasValueSatisfying(value -> assertThat(value).isSameAs(current));
71+
assertThat(contextAfterScope).hasValueSatisfying(value -> assertThat(value).isSameAs(previousContext));
72+
assertThat(contextInScope).hasValueSatisfying(value -> assertThat(value).isSameAs(currentContext));
7873
}
7974

80-
private static Stream<Arguments> propagation() {
81-
LocaleContext previous = new SimpleLocaleContext(Locale.ENGLISH);
82-
LocaleContext current = new SimpleLocaleContext(Locale.ENGLISH);
83-
return Stream.of(
84-
Arguments.of(null, current),
85-
Arguments.of(previous, current)
86-
);
75+
private ContextSnapshot createContextSnapshotFor(LocaleContext context) {
76+
LocaleContextHolder.setLocaleContext(context);
77+
try {
78+
return ContextSnapshotFactory.builder()
79+
.contextRegistry(this.registry).clearMissing(true).build()
80+
.captureAll();
81+
}
82+
finally {
83+
LocaleContextHolder.resetLocaleContext();
84+
}
8785
}
86+
8887
}

spring-web/src/test/java/org/springframework/web/context/request/RequestAttributesThreadLocalAccessorTests.java

Lines changed: 35 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,75 +16,73 @@
1616

1717
package org.springframework.web.context.request;
1818

19-
import java.util.concurrent.CountDownLatch;
20-
import java.util.concurrent.TimeUnit;
2119
import java.util.concurrent.atomic.AtomicReference;
2220
import java.util.stream.Stream;
2321

2422
import io.micrometer.context.ContextRegistry;
2523
import io.micrometer.context.ContextSnapshot;
2624
import io.micrometer.context.ContextSnapshot.Scope;
2725
import io.micrometer.context.ContextSnapshotFactory;
28-
import org.junit.jupiter.api.AfterEach;
2926
import org.junit.jupiter.params.ParameterizedTest;
3027
import org.junit.jupiter.params.provider.Arguments;
3128
import org.junit.jupiter.params.provider.MethodSource;
3229

33-
import org.springframework.lang.Nullable;
34-
3530
import static org.assertj.core.api.Assertions.assertThat;
3631
import static org.mockito.Mockito.mock;
3732

3833
/**
3934
* Tests for {@link RequestAttributesThreadLocalAccessor}.
4035
*
4136
* @author Tadaya Tsuyukubo
37+
* @author Rossen Stoyanchev
4238
*/
4339
class RequestAttributesThreadLocalAccessorTests {
4440

45-
private final ContextRegistry registry = new ContextRegistry()
46-
.registerThreadLocalAccessor(new RequestAttributesThreadLocalAccessor());
41+
private final ContextRegistry registry =
42+
new ContextRegistry().registerThreadLocalAccessor(new RequestAttributesThreadLocalAccessor());
43+
4744

48-
@AfterEach
49-
void cleanUp() {
50-
RequestContextHolder.resetRequestAttributes();
45+
private static Stream<Arguments> propagation() {
46+
RequestAttributes previous = mock(RequestAttributes.class);
47+
RequestAttributes current = mock(RequestAttributes.class);
48+
return Stream.of(Arguments.of(null, current), Arguments.of(previous, current));
5149
}
5250

5351
@ParameterizedTest
5452
@MethodSource
5553
@SuppressWarnings({ "try", "unused" })
56-
void propagation(@Nullable RequestAttributes previous, RequestAttributes current) throws Exception {
57-
RequestContextHolder.setRequestAttributes(current);
58-
ContextSnapshot snapshot = ContextSnapshotFactory.builder()
59-
.contextRegistry(this.registry)
60-
.clearMissing(true)
61-
.build()
62-
.captureAll();
63-
64-
AtomicReference<RequestAttributes> previousHolder = new AtomicReference<>();
65-
AtomicReference<RequestAttributes> currentHolder = new AtomicReference<>();
66-
CountDownLatch latch = new CountDownLatch(1);
67-
new Thread(() -> {
68-
RequestContextHolder.setRequestAttributes(previous);
54+
void propagation(RequestAttributes previousRequest, RequestAttributes currentRequest) throws Exception {
55+
56+
ContextSnapshot snapshot = getSnapshotFor(currentRequest);
57+
58+
AtomicReference<RequestAttributes> requestInScope = new AtomicReference<>();
59+
AtomicReference<RequestAttributes> requestAfterScope = new AtomicReference<>();
60+
61+
Thread thread = new Thread(() -> {
62+
RequestContextHolder.setRequestAttributes(previousRequest);
6963
try (Scope scope = snapshot.setThreadLocals()) {
70-
currentHolder.set(RequestContextHolder.getRequestAttributes());
64+
requestInScope.set(RequestContextHolder.getRequestAttributes());
7165
}
72-
previousHolder.set(RequestContextHolder.getRequestAttributes());
73-
latch.countDown();
74-
}).start();
66+
requestAfterScope.set(RequestContextHolder.getRequestAttributes());
67+
});
68+
69+
thread.start();
70+
thread.join(1000);
7571

76-
latch.await(1, TimeUnit.SECONDS);
77-
assertThat(previousHolder).hasValueSatisfying(value -> assertThat(value).isSameAs(previous));
78-
assertThat(currentHolder).hasValueSatisfying(value -> assertThat(value).isSameAs(current));
72+
assertThat(requestInScope).hasValueSatisfying(value -> assertThat(value).isSameAs(currentRequest));
73+
assertThat(requestAfterScope).hasValueSatisfying(value -> assertThat(value).isSameAs(previousRequest));
7974
}
8075

81-
private static Stream<Arguments> propagation() {
82-
RequestAttributes previous = mock(RequestAttributes.class);
83-
RequestAttributes current = mock(RequestAttributes.class);
84-
return Stream.of(
85-
Arguments.of(null, current),
86-
Arguments.of(previous, current)
87-
);
76+
private ContextSnapshot getSnapshotFor(RequestAttributes request) {
77+
RequestContextHolder.setRequestAttributes(request);
78+
try {
79+
return ContextSnapshotFactory.builder()
80+
.contextRegistry(this.registry).clearMissing(true).build()
81+
.captureAll();
82+
}
83+
finally {
84+
RequestContextHolder.resetRequestAttributes();
85+
}
8886
}
8987

9088
}

0 commit comments

Comments
 (0)