1717package org .springframework .boot .actuate .endpoint .invoker .cache ;
1818
1919import java .security .Principal ;
20- import java .time . Duration ;
20+ import java .util . Arrays ;
2121import java .util .Collections ;
2222import java .util .HashMap ;
2323import java .util .Map ;
2424
25- import org .junit .Rule ;
2625import org .junit .Test ;
26+ import reactor .core .publisher .Flux ;
2727import reactor .core .publisher .Mono ;
2828
2929import org .springframework .boot .actuate .endpoint .InvocationContext ;
3030import org .springframework .boot .actuate .endpoint .SecurityContext ;
3131import org .springframework .boot .actuate .endpoint .invoke .MissingParametersException ;
3232import org .springframework .boot .actuate .endpoint .invoke .OperationInvoker ;
33- import org .springframework .boot .test .rule .OutputCapture ;
3433
3534import static org .assertj .core .api .Assertions .assertThat ;
3635import static org .assertj .core .api .Assertions .assertThatIllegalArgumentException ;
4443 * Tests for {@link CachingOperationInvoker}.
4544 *
4645 * @author Stephane Nicoll
46+ * @author Christoph Dreis
47+ * @author Phillip Webb
4748 */
4849public class CachingOperationInvokerTests {
4950
50- @ Rule
51- public OutputCapture outputCapture = new OutputCapture ();
52-
5351 @ Test
5452 public void createInstanceWithTtlSetToZero () {
5553 assertThatIllegalArgumentException ()
@@ -72,17 +70,26 @@ public void cacheInTtlWithNullParameters() {
7270
7371 @ Test
7472 public void cacheInTtlWithMonoResponse () {
73+ MonoOperationInvoker .invocations = 0 ;
7574 MonoOperationInvoker target = new MonoOperationInvoker ();
7675 InvocationContext context = new InvocationContext (mock (SecurityContext .class ), Collections .emptyMap ());
7776 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 );
8492 assertThat (response ).isSameAs (cachedResponse );
85- assertThat (this .outputCapture .toString ()).containsOnlyOnce ("invoked" );
8693 }
8794
8895 private void assertCacheIsUsed (Map <String , Object > parameters ) {
@@ -144,14 +151,28 @@ public void targetInvokedWhenCacheExpires() throws InterruptedException {
144151
145152 private static class MonoOperationInvoker implements OperationInvoker {
146153
154+ static int invocations ;
155+
147156 @ Override
148157 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+ });
150162 }
151163
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+ });
155176 }
156177
157178 }
0 commit comments