17
17
package org .springframework .boot .actuate .endpoint .invoker .cache ;
18
18
19
19
import java .security .Principal ;
20
+ import java .util .Arrays ;
20
21
import java .util .Collections ;
21
22
import java .util .HashMap ;
22
23
import java .util .Map ;
23
24
24
25
import org .junit .jupiter .api .Test ;
26
+ import reactor .core .publisher .Flux ;
27
+ import reactor .core .publisher .Mono ;
25
28
26
29
import org .springframework .boot .actuate .endpoint .InvocationContext ;
27
30
import org .springframework .boot .actuate .endpoint .SecurityContext ;
31
+ import org .springframework .boot .actuate .endpoint .invoke .MissingParametersException ;
28
32
import org .springframework .boot .actuate .endpoint .invoke .OperationInvoker ;
29
33
30
34
import static org .assertj .core .api .Assertions .assertThat ;
39
43
* Tests for {@link CachingOperationInvoker}.
40
44
*
41
45
* @author Stephane Nicoll
46
+ * @author Christoph Dreis
47
+ * @author Phillip Webb
42
48
*/
43
49
class CachingOperationInvokerTests {
44
50
@@ -62,6 +68,30 @@ void cacheInTtlWithNullParameters() {
62
68
assertCacheIsUsed (parameters );
63
69
}
64
70
71
+ @ Test
72
+ void cacheInTtlWithMonoResponse () {
73
+ MonoOperationInvoker .invocations = 0 ;
74
+ MonoOperationInvoker target = new MonoOperationInvoker ();
75
+ InvocationContext context = new InvocationContext (mock (SecurityContext .class ), Collections .emptyMap ());
76
+ CachingOperationInvoker invoker = new CachingOperationInvoker (target , 500L );
77
+ Object response = ((Mono <?>) invoker .invoke (context )).block ();
78
+ Object cachedResponse = ((Mono <?>) invoker .invoke (context )).block ();
79
+ assertThat (MonoOperationInvoker .invocations ).isEqualTo (1 );
80
+ assertThat (response ).isSameAs (cachedResponse );
81
+ }
82
+
83
+ @ Test
84
+ void cacheInTtlWithFluxResponse () {
85
+ FluxOperationInvoker .invocations = 0 ;
86
+ FluxOperationInvoker target = new FluxOperationInvoker ();
87
+ InvocationContext context = new InvocationContext (mock (SecurityContext .class ), Collections .emptyMap ());
88
+ CachingOperationInvoker invoker = new CachingOperationInvoker (target , 500L );
89
+ Object response = ((Flux <?>) invoker .invoke (context )).blockLast ();
90
+ Object cachedResponse = ((Flux <?>) invoker .invoke (context )).blockLast ();
91
+ assertThat (FluxOperationInvoker .invocations ).isEqualTo (1 );
92
+ assertThat (response ).isSameAs (cachedResponse );
93
+ }
94
+
65
95
private void assertCacheIsUsed (Map <String , Object > parameters ) {
66
96
OperationInvoker target = mock (OperationInvoker .class );
67
97
Object expected = new Object ();
@@ -122,4 +152,32 @@ void targetInvokedWhenCacheExpires() throws InterruptedException {
122
152
verify (target , times (2 )).invoke (context );
123
153
}
124
154
155
+ private static class MonoOperationInvoker implements OperationInvoker {
156
+
157
+ static int invocations ;
158
+
159
+ @ Override
160
+ public Object invoke (InvocationContext context ) throws MissingParametersException {
161
+ return Mono .fromCallable (() -> {
162
+ invocations ++;
163
+ return Mono .just ("test" );
164
+ });
165
+ }
166
+
167
+ }
168
+
169
+ private static class FluxOperationInvoker implements OperationInvoker {
170
+
171
+ static int invocations ;
172
+
173
+ @ Override
174
+ public Object invoke (InvocationContext context ) throws MissingParametersException {
175
+ return Flux .fromIterable (() -> {
176
+ invocations ++;
177
+ return Arrays .asList ("spring" , "boot" ).iterator ();
178
+ });
179
+ }
180
+
181
+ }
182
+
125
183
}
0 commit comments