Skip to content

Commit 936e720

Browse files
Fix proxy attribute name mismatch in RetryOperationsInterceptor
* Add tests for `RetryOperationsInterceptor` proxy attribute cleanup * Add author to `RetryOperationsInterceptorTests` * Remove unused import fail * Fix `RetryOperationsInterceptorTests` extract `MethodInvocation` and simplify mocking Signed-off-by: Kim Jun Hyeong <ggprgrkjh@naver.com>
1 parent 544b24e commit 936e720

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

src/main/java/org/springframework/retry/interceptor/RetryOperationsInterceptor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public Object doWithRetry(RetryContext context) throws Exception {
137137
finally {
138138
RetryContext context = RetrySynchronizationManager.getContext();
139139
if (context != null) {
140-
context.removeAttribute("__proxy__");
140+
context.removeAttribute("___proxy___");
141141
}
142142
}
143143
}

src/test/java/org/springframework/retry/interceptor/RetryOperationsInterceptorTests.java

+48-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006-2023 the original author or authors.
2+
* Copyright 2006-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,9 +36,11 @@
3636
import org.springframework.retry.RetryCallback;
3737
import org.springframework.retry.RetryContext;
3838
import org.springframework.retry.RetryListener;
39+
import org.springframework.retry.context.RetryContextSupport;
3940
import org.springframework.retry.listener.MethodInvocationRetryListenerSupport;
4041
import org.springframework.retry.policy.NeverRetryPolicy;
4142
import org.springframework.retry.policy.SimpleRetryPolicy;
43+
import org.springframework.retry.support.RetrySynchronizationManager;
4244
import org.springframework.retry.support.RetryTemplate;
4345
import org.springframework.transaction.support.TransactionSynchronization;
4446
import org.springframework.transaction.support.TransactionSynchronizationManager;
@@ -47,6 +49,8 @@
4749
import static org.assertj.core.api.Assertions.assertThat;
4850
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
4951
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
52+
import static org.mockito.Mockito.mock;
53+
import static org.mockito.Mockito.when;
5054

5155
/**
5256
* @author Dave Syer
@@ -55,6 +59,7 @@
5559
* @author Stéphane Nicoll
5660
* @author Henning Pöttker
5761
* @author Artem Bilan
62+
* @author Kim Jun Hyeong
5863
*/
5964
public class RetryOperationsInterceptorTests {
6065

@@ -257,6 +262,48 @@ public Object proceed() {
257262
})).withMessageContaining("MethodInvocation");
258263
}
259264

265+
@Test
266+
public void testProxyAttributeCleanupWithCorrectKey() {
267+
RetryContext directContext = new RetryContextSupport(null);
268+
Object mockProxy = new Object();
269+
RetrySynchronizationManager.register(directContext);
270+
directContext.setAttribute("___proxy___", mockProxy);
271+
RetryContext retrievedContext = RetrySynchronizationManager.getContext();
272+
retrievedContext.removeAttribute("___proxy___");
273+
assertThat(RetrySynchronizationManager.getContext().getAttribute("___proxy___")).isNull();
274+
RetrySynchronizationManager.clear();
275+
}
276+
277+
@Test
278+
public void testProxyAttributeRemainWithWrongKey() {
279+
RetryContext directContext = new RetryContextSupport(null);
280+
Object mockProxy = new Object();
281+
RetrySynchronizationManager.register(directContext);
282+
directContext.setAttribute("___proxy___", mockProxy);
283+
RetryContext retrievedContext = RetrySynchronizationManager.getContext();
284+
retrievedContext.removeAttribute("__proxy__");
285+
Object remainingProxy = RetrySynchronizationManager.getContext().getAttribute("___proxy___");
286+
assertThat(remainingProxy).isNotNull();
287+
assertThat(remainingProxy).isSameAs(mockProxy);
288+
RetrySynchronizationManager.clear();
289+
}
290+
291+
@Test
292+
public void testProxyAttributeCleanupEvenWhenIllegalStateExceptionThrown() throws NoSuchMethodException {
293+
RetryContext context = new RetryContextSupport(null);
294+
Object mockProxy = new Object();
295+
RetrySynchronizationManager.register(context);
296+
context.setAttribute("___proxy___", mockProxy);
297+
assertThat(context.getAttribute("___proxy___")).isNotNull();
298+
MethodInvocation mockInvocation = mock(MethodInvocation.class);
299+
Method testMethod = this.getClass().getMethod("testProxyAttributeCleanupEvenWhenIllegalStateExceptionThrown");
300+
when(mockInvocation.getMethod()).thenReturn(testMethod);
301+
assertThatIllegalStateException().isThrownBy(() -> this.interceptor.invoke(mockInvocation))
302+
.withMessageContaining("MethodInvocation");
303+
assertThat(context.getAttribute("___proxy___")).isNull();
304+
RetrySynchronizationManager.clear();
305+
}
306+
260307
public static interface Service {
261308

262309
void service() throws Exception;

0 commit comments

Comments
 (0)