|
36 | 36 | import org.springframework.retry.RetryCallback;
|
37 | 37 | import org.springframework.retry.RetryContext;
|
38 | 38 | import org.springframework.retry.RetryListener;
|
| 39 | +import org.springframework.retry.context.RetryContextSupport; |
39 | 40 | import org.springframework.retry.listener.MethodInvocationRetryListenerSupport;
|
40 | 41 | import org.springframework.retry.policy.NeverRetryPolicy;
|
41 | 42 | import org.springframework.retry.policy.SimpleRetryPolicy;
|
| 43 | +import org.springframework.retry.support.RetrySynchronizationManager; |
42 | 44 | import org.springframework.retry.support.RetryTemplate;
|
43 | 45 | import org.springframework.transaction.support.TransactionSynchronization;
|
44 | 46 | import org.springframework.transaction.support.TransactionSynchronizationManager;
|
|
47 | 49 | import static org.assertj.core.api.Assertions.assertThat;
|
48 | 50 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
49 | 51 | import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
| 52 | +import static org.junit.jupiter.api.Assertions.fail; |
50 | 53 |
|
51 | 54 | /**
|
52 | 55 | * @author Dave Syer
|
@@ -257,6 +260,70 @@ public Object proceed() {
|
257 | 260 | })).withMessageContaining("MethodInvocation");
|
258 | 261 | }
|
259 | 262 |
|
| 263 | + @Test |
| 264 | + public void testProxyAttributeCleanupWithCorrectKey() { |
| 265 | + RetryContext directContext = new RetryContextSupport(null); |
| 266 | + Object mockProxy = new Object(); |
| 267 | + RetrySynchronizationManager.register(directContext); |
| 268 | + directContext.setAttribute("___proxy___", mockProxy); |
| 269 | + RetryContext retrievedContext = RetrySynchronizationManager.getContext(); |
| 270 | + retrievedContext.removeAttribute("___proxy___"); |
| 271 | + assertThat(RetrySynchronizationManager.getContext().getAttribute("___proxy___")).isNull(); |
| 272 | + RetrySynchronizationManager.clear(); |
| 273 | + } |
| 274 | + |
| 275 | + @Test |
| 276 | + public void testProxyAttributeRemainWithWrongKey() { |
| 277 | + RetryContext directContext = new RetryContextSupport(null); |
| 278 | + Object mockProxy = new Object(); |
| 279 | + RetrySynchronizationManager.register(directContext); |
| 280 | + directContext.setAttribute("___proxy___", mockProxy); |
| 281 | + RetryContext retrievedContext = RetrySynchronizationManager.getContext(); |
| 282 | + retrievedContext.removeAttribute("__proxy__"); |
| 283 | + Object remainingProxy = RetrySynchronizationManager.getContext().getAttribute("___proxy___"); |
| 284 | + assertThat(remainingProxy).isNotNull(); |
| 285 | + assertThat(remainingProxy).isSameAs(mockProxy); |
| 286 | + RetrySynchronizationManager.clear(); |
| 287 | + } |
| 288 | + |
| 289 | + @Test |
| 290 | + public void testProxyAttributeCleanupEvenWhenIllegalStateExceptionThrown() { |
| 291 | + RetryContext context = new RetryContextSupport(null); |
| 292 | + Object mockProxy = new Object(); |
| 293 | + RetrySynchronizationManager.register(context); |
| 294 | + context.setAttribute("___proxy___", mockProxy); |
| 295 | + assertThat(context.getAttribute("___proxy___")).isNotNull(); |
| 296 | + assertThatIllegalStateException().isThrownBy(() -> this.interceptor.invoke(new MethodInvocation() { |
| 297 | + @Override |
| 298 | + public Method getMethod() { |
| 299 | + return ClassUtils.getMethod(RetryOperationsInterceptorTests.class, |
| 300 | + "testProxyAttributeCleanupEvenWhenIllegalStateExceptionThrown"); |
| 301 | + } |
| 302 | + |
| 303 | + @Override |
| 304 | + public Object[] getArguments() { |
| 305 | + return new Object[0]; |
| 306 | + } |
| 307 | + |
| 308 | + @Override |
| 309 | + public Object proceed() { |
| 310 | + return null; |
| 311 | + } |
| 312 | + |
| 313 | + @Override |
| 314 | + public Object getThis() { |
| 315 | + return new Object(); |
| 316 | + } |
| 317 | + |
| 318 | + @Override |
| 319 | + public AccessibleObject getStaticPart() { |
| 320 | + return null; |
| 321 | + } |
| 322 | + })).withMessageContaining("MethodInvocation"); |
| 323 | + assertThat(context.getAttribute("___proxy___")).isNull(); |
| 324 | + RetrySynchronizationManager.clear(); |
| 325 | + } |
| 326 | + |
260 | 327 | public static interface Service {
|
261 | 328 |
|
262 | 329 | void service() throws Exception;
|
|
0 commit comments