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 .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
public class CachingOperationInvokerTests {
44
50
@@ -62,6 +68,30 @@ public void cacheInTtlWithNullParameters() {
62
68
assertCacheIsUsed (parameters );
63
69
}
64
70
71
+ @ Test
72
+ public 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
+ public 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 ();
@@ -119,4 +149,32 @@ public void targetInvokedWhenCacheExpires() throws InterruptedException {
119
149
verify (target , times (2 )).invoke (context );
120
150
}
121
151
152
+ private static class MonoOperationInvoker implements OperationInvoker {
153
+
154
+ static int invocations ;
155
+
156
+ @ Override
157
+ public Object invoke (InvocationContext context ) throws MissingParametersException {
158
+ return Mono .fromCallable (() -> {
159
+ invocations ++;
160
+ return Mono .just ("test" );
161
+ });
162
+ }
163
+
164
+ }
165
+
166
+ private static class FluxOperationInvoker implements OperationInvoker {
167
+
168
+ static int invocations ;
169
+
170
+ @ Override
171
+ public Object invoke (InvocationContext context ) throws MissingParametersException {
172
+ return Flux .fromIterable (() -> {
173
+ invocations ++;
174
+ return Arrays .asList ("spring" , "boot" ).iterator ();
175
+ });
176
+ }
177
+
178
+ }
179
+
122
180
}
0 commit comments