|
12 | 12 | import java.util.function.Function; |
13 | 13 | import java.util.function.Supplier; |
14 | 14 |
|
| 15 | +import javax.annotation.Nonnull; |
| 16 | +import javax.annotation.Nullable; |
| 17 | + |
15 | 18 | import org.junit.jupiter.api.Test; |
16 | 19 |
|
17 | 20 | import com.sap.cloud.sdk.cloudplatform.thread.ThreadContext; |
18 | 21 | import com.sap.cloud.sdk.cloudplatform.thread.ThreadContextAccessor; |
19 | 22 | import com.sap.cloud.sdk.cloudplatform.thread.ThreadContextExecutors; |
20 | 23 |
|
21 | | -public class ResilienceDecorationStrategyTest extends TestDecorationStrategy |
22 | | -{ |
23 | | - |
24 | | - @Test |
25 | | - void testNamedThreadQueueCallable() |
26 | | - throws ExecutionException, |
27 | | - InterruptedException |
28 | | - { |
29 | | - final CompletableFuture<String> threadNameQueueCallable = |
30 | | - queueCallable(this::getThreadName, ResilienceConfiguration.empty("identifier"), null); |
31 | | - final String threadName = threadNameQueueCallable.get(); |
32 | | - assertThat(threadName).matches("cloudsdk-executor-\\d+"); |
33 | | - } |
34 | | - |
35 | | - @Test |
36 | | - void testNamedThreadQueueSupplier() |
37 | | - throws ExecutionException, |
38 | | - InterruptedException |
39 | | - { |
40 | | - final CompletableFuture<String> threadNameQueueSupplier = |
41 | | - queueSupplier(this::getThreadName, ResilienceConfiguration.empty("identifier"), null); |
42 | | - final String threadName = threadNameQueueSupplier.get(); |
43 | | - assertThat(threadName).matches("cloudsdk-executor-\\d+"); |
44 | | - } |
45 | | - |
46 | | - @Test |
47 | | - void testThreadNameFromExecutor() |
48 | | - throws ExecutionException, |
49 | | - InterruptedException |
50 | | - { |
51 | | - final Supplier<String> nameSupplier = () -> Thread.currentThread().getName(); |
52 | | - final CompletableFuture<String> nameFuture = |
53 | | - CompletableFuture.supplyAsync(nameSupplier, ThreadContextExecutors.getExecutor()); |
54 | | - assertThat(nameFuture.get()).matches("cloudsdk-executor-\\d+"); |
55 | | - } |
56 | | - |
57 | | - @Test |
58 | | - void testExecuteCallableWithFallback() |
59 | | - { |
60 | | - final String positiveTest = |
61 | | - new TestDecorationStrategy() |
62 | | - .executeCallable(() -> "Expected result", null, ( exception ) -> "Fallback result"); |
63 | | - |
64 | | - assertThat(positiveTest).isEqualTo("Expected result"); |
| 24 | +import io.vavr.control.Try; |
65 | 25 |
|
66 | | - final Callable<String> failingCallable = () -> { |
67 | | - throw new RuntimeException("Simulated failure"); |
68 | | - }; |
69 | | - |
70 | | - final String positiveFallbackTest = |
71 | | - new TestDecorationStrategy().executeCallable(failingCallable, null, ( exception ) -> "Fallback result"); |
72 | | - |
73 | | - assertThat(positiveFallbackTest).isEqualTo("Fallback result"); |
74 | | - |
75 | | - final Function<Throwable, String> negativeTest = ( exception ) -> { |
76 | | - throw new RuntimeException("Bad example", exception); |
77 | | - }; |
78 | | - |
79 | | - assertThatThrownBy(() -> new TestDecorationStrategy().executeCallable(failingCallable, null, negativeTest)) |
80 | | - .isInstanceOf(ResilienceRuntimeException.class); |
81 | | - } |
82 | | - |
83 | | - @Test |
84 | | - void testThreadContextPropagated() |
85 | | - { |
86 | | - ThreadContextExecutors.execute(() -> { |
87 | | - final ThreadContext parentThreadContext = ThreadContextAccessor.getCurrentContext(); |
88 | | - |
89 | | - try { |
90 | | - assertThat(new TestDecorationStrategy().queueCallable(() -> { |
91 | | - final ThreadContext childContext = ThreadContextAccessor.getCurrentContext(); |
92 | | - assertThat(childContext).isEqualTo(parentThreadContext); |
93 | | - assertThat(childContext).isNotSameAs(parentThreadContext); |
94 | | - return null; |
95 | | - }, null, ( exception ) -> "Fallback result").get()).isNotEqualTo("Fallback result"); |
96 | | - } |
97 | | - catch( final InterruptedException | ExecutionException e ) { |
98 | | - throw new RuntimeException(e); |
99 | | - } |
100 | | - }); |
101 | | - |
102 | | - } |
103 | | - |
104 | | - @Test |
105 | | - void testThreadContextPropagatedWithTimeLimiter() |
| 26 | +class ResilienceDecorationStrategyTest |
| 27 | +{ |
| 28 | + public static class TestDecorationStrategy implements ResilienceDecorationStrategy |
106 | 29 | { |
107 | | - ThreadContextExecutors.execute(() -> { |
108 | | - final ThreadContext parentThreadContext = ThreadContextAccessor.getCurrentContext(); |
109 | | - |
110 | | - try { |
111 | | - assertThat(new TestDecorationStrategy().queueCallable(() -> { |
112 | | - final ThreadContext childContext = ThreadContextAccessor.getCurrentContext(); |
113 | | - assertThat(childContext).isEqualTo(parentThreadContext); |
114 | | - assertThat(childContext).isNotSameAs(parentThreadContext); |
115 | | - return null; |
116 | | - }, |
117 | | - ResilienceConfiguration |
118 | | - .of("identifier") |
119 | | - .timeLimiterConfiguration(ResilienceConfiguration.TimeLimiterConfiguration.of()), |
120 | | - ( exception ) -> "Fallback result").get()).isNotEqualTo("Fallback result"); |
| 30 | + @Nonnull |
| 31 | + @Override |
| 32 | + public <T> Supplier<T> decorateSupplier( |
| 33 | + @Nonnull final Supplier<T> supplier, |
| 34 | + @Nonnull final ResilienceConfiguration configuration, |
| 35 | + @Nullable final Function<? super Throwable, T> fallbackFunction ) |
| 36 | + { |
| 37 | + if( fallbackFunction != null ) { |
| 38 | + return () -> { |
| 39 | + final Try<T> callWithFallback = Try.ofSupplier(supplier).recover(fallbackFunction); |
| 40 | + return callWithFallback.get(); |
| 41 | + }; |
121 | 42 | } |
122 | | - catch( final InterruptedException | ExecutionException e ) { |
123 | | - throw new RuntimeException(e); |
| 43 | + return supplier; |
| 44 | + } |
| 45 | + |
| 46 | + @Nonnull |
| 47 | + @Override |
| 48 | + public <T> Callable<T> decorateCallable( |
| 49 | + @Nonnull final Callable<T> callable, |
| 50 | + @Nonnull final ResilienceConfiguration configuration, |
| 51 | + @Nullable final Function<? super Throwable, T> fallbackFunction ) |
| 52 | + { |
| 53 | + if( fallbackFunction != null ) { |
| 54 | + return () -> { |
| 55 | + final Try<T> callWithFallback = Try.ofCallable(callable).recover(fallbackFunction); |
| 56 | + return callWithFallback.get(); |
| 57 | + }; |
124 | 58 | } |
125 | | - }); |
| 59 | + return callable; |
| 60 | + } |
| 61 | + |
| 62 | + @Nonnull |
| 63 | + public String getThreadName() |
| 64 | + { |
| 65 | + return Thread.currentThread().getName(); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + void testNamedThreadQueueCallable() |
| 70 | + throws ExecutionException, |
| 71 | + InterruptedException |
| 72 | + { |
| 73 | + final CompletableFuture<String> threadNameQueueCallable = |
| 74 | + queueCallable(this::getThreadName, ResilienceConfiguration.empty("identifier"), null); |
| 75 | + final String threadName = threadNameQueueCallable.get(); |
| 76 | + assertThat(threadName).matches("cloudsdk-executor-\\d+"); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void testNamedThreadQueueSupplier() |
| 81 | + throws ExecutionException, |
| 82 | + InterruptedException |
| 83 | + { |
| 84 | + final CompletableFuture<String> threadNameQueueSupplier = |
| 85 | + queueSupplier(this::getThreadName, ResilienceConfiguration.empty("identifier"), null); |
| 86 | + final String threadName = threadNameQueueSupplier.get(); |
| 87 | + assertThat(threadName).matches("cloudsdk-executor-\\d+"); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + void testThreadNameFromExecutor() |
| 92 | + throws ExecutionException, |
| 93 | + InterruptedException |
| 94 | + { |
| 95 | + final Supplier<String> nameSupplier = () -> Thread.currentThread().getName(); |
| 96 | + final CompletableFuture<String> nameFuture = |
| 97 | + CompletableFuture.supplyAsync(nameSupplier, ThreadContextExecutors.getExecutor()); |
| 98 | + assertThat(nameFuture.get()).matches("cloudsdk-executor-\\d+"); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + void testExecuteCallableWithFallback() |
| 103 | + { |
| 104 | + final String positiveTest = |
| 105 | + new TestDecorationStrategy() |
| 106 | + .executeCallable(() -> "Expected result", null, ( exception ) -> "Fallback result"); |
| 107 | + |
| 108 | + assertThat(positiveTest).isEqualTo("Expected result"); |
| 109 | + |
| 110 | + final Callable<String> failingCallable = () -> { |
| 111 | + throw new RuntimeException("Simulated failure"); |
| 112 | + }; |
| 113 | + |
| 114 | + final String positiveFallbackTest = |
| 115 | + new TestDecorationStrategy().executeCallable(failingCallable, null, ( exception ) -> "Fallback result"); |
| 116 | + |
| 117 | + assertThat(positiveFallbackTest).isEqualTo("Fallback result"); |
| 118 | + |
| 119 | + final Function<Throwable, String> negativeTest = ( exception ) -> { |
| 120 | + throw new RuntimeException("Bad example", exception); |
| 121 | + }; |
| 122 | + |
| 123 | + assertThatThrownBy(() -> new TestDecorationStrategy().executeCallable(failingCallable, null, negativeTest)) |
| 124 | + .isInstanceOf(ResilienceRuntimeException.class); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + void testThreadContextPropagated() |
| 129 | + { |
| 130 | + ThreadContextExecutors.execute(() -> { |
| 131 | + final ThreadContext parentThreadContext = ThreadContextAccessor.getCurrentContext(); |
| 132 | + |
| 133 | + try { |
| 134 | + assertThat(new TestDecorationStrategy().queueCallable(() -> { |
| 135 | + final ThreadContext childContext = ThreadContextAccessor.getCurrentContext(); |
| 136 | + assertThat(childContext).isEqualTo(parentThreadContext); |
| 137 | + assertThat(childContext).isNotSameAs(parentThreadContext); |
| 138 | + return null; |
| 139 | + }, null, ( exception ) -> "Fallback result").get()).isNotEqualTo("Fallback result"); |
| 140 | + } |
| 141 | + catch( final InterruptedException | ExecutionException e ) { |
| 142 | + throw new RuntimeException(e); |
| 143 | + } |
| 144 | + }); |
| 145 | + |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + void testThreadContextPropagatedWithTimeLimiter() |
| 150 | + { |
| 151 | + ThreadContextExecutors.execute(() -> { |
| 152 | + final ThreadContext parentThreadContext = ThreadContextAccessor.getCurrentContext(); |
| 153 | + |
| 154 | + try { |
| 155 | + assertThat(new TestDecorationStrategy().queueCallable(() -> { |
| 156 | + final ThreadContext childContext = ThreadContextAccessor.getCurrentContext(); |
| 157 | + assertThat(childContext).isEqualTo(parentThreadContext); |
| 158 | + assertThat(childContext).isNotSameAs(parentThreadContext); |
| 159 | + return null; |
| 160 | + }, |
| 161 | + ResilienceConfiguration |
| 162 | + .of("identifier") |
| 163 | + .timeLimiterConfiguration(ResilienceConfiguration.TimeLimiterConfiguration.of()), |
| 164 | + ( exception ) -> "Fallback result").get()).isNotEqualTo("Fallback result"); |
| 165 | + } |
| 166 | + catch( final InterruptedException | ExecutionException e ) { |
| 167 | + throw new RuntimeException(e); |
| 168 | + } |
| 169 | + }); |
| 170 | + } |
126 | 171 | } |
127 | | - |
128 | 172 | } |
0 commit comments