17
17
package org .springframework .boot .actuate .endpoint .invoker .cache ;
18
18
19
19
import java .security .Principal ;
20
- import java .time . Duration ;
20
+ import java .util . Arrays ;
21
21
import java .util .Collections ;
22
22
import java .util .HashMap ;
23
23
import java .util .Map ;
24
24
25
- import org .junit .Rule ;
26
25
import org .junit .Test ;
26
+ import reactor .core .publisher .Flux ;
27
27
import reactor .core .publisher .Mono ;
28
28
29
29
import org .springframework .boot .actuate .endpoint .InvocationContext ;
30
30
import org .springframework .boot .actuate .endpoint .SecurityContext ;
31
31
import org .springframework .boot .actuate .endpoint .invoke .MissingParametersException ;
32
32
import org .springframework .boot .actuate .endpoint .invoke .OperationInvoker ;
33
- import org .springframework .boot .test .rule .OutputCapture ;
34
33
35
34
import static org .assertj .core .api .Assertions .assertThat ;
36
35
import static org .assertj .core .api .Assertions .assertThatIllegalArgumentException ;
44
43
* Tests for {@link CachingOperationInvoker}.
45
44
*
46
45
* @author Stephane Nicoll
46
+ * @author Christoph Dreis
47
+ * @author Phillip Webb
47
48
*/
48
49
public class CachingOperationInvokerTests {
49
50
50
- @ Rule
51
- public OutputCapture outputCapture = new OutputCapture ();
52
-
53
51
@ Test
54
52
public void createInstanceWithTtlSetToZero () {
55
53
assertThatIllegalArgumentException ()
@@ -72,17 +70,26 @@ public void cacheInTtlWithNullParameters() {
72
70
73
71
@ Test
74
72
public void cacheInTtlWithMonoResponse () {
73
+ MonoOperationInvoker .invocations = 0 ;
75
74
MonoOperationInvoker target = new MonoOperationInvoker ();
76
75
InvocationContext context = new InvocationContext (mock (SecurityContext .class ), Collections .emptyMap ());
77
76
CachingOperationInvoker invoker = new CachingOperationInvoker (target , 500L );
78
- Object monoResponse = invoker .invoke (context );
79
- assertThat (monoResponse ).isInstanceOf (Mono .class );
80
- Object response = ((Mono ) monoResponse ).block (Duration .ofSeconds (30 ));
81
- Object cachedMonoResponse = invoker .invoke (context );
82
- assertThat (cachedMonoResponse ).isInstanceOf (Mono .class );
83
- Object cachedResponse = ((Mono ) cachedMonoResponse ).block (Duration .ofSeconds (30 ));
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 );
84
92
assertThat (response ).isSameAs (cachedResponse );
85
- assertThat (this .outputCapture .toString ()).containsOnlyOnce ("invoked" );
86
93
}
87
94
88
95
private void assertCacheIsUsed (Map <String , Object > parameters ) {
@@ -144,14 +151,28 @@ public void targetInvokedWhenCacheExpires() throws InterruptedException {
144
151
145
152
private static class MonoOperationInvoker implements OperationInvoker {
146
153
154
+ static int invocations ;
155
+
147
156
@ Override
148
157
public Object invoke (InvocationContext context ) throws MissingParametersException {
149
- return Mono .fromCallable (this ::printInvocation );
158
+ return Mono .fromCallable (() -> {
159
+ invocations ++;
160
+ return Mono .just ("test" );
161
+ });
150
162
}
151
163
152
- private Mono <String > printInvocation () {
153
- System .out .println ("MonoOperationInvoker invoked" );
154
- return Mono .just ("test" );
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
+ });
155
176
}
156
177
157
178
}
0 commit comments