|
|
@ -17,14 +17,18 @@
|
|
|
|
package org.springframework.boot.actuate.endpoint.invoker.cache;
|
|
|
|
package org.springframework.boot.actuate.endpoint.invoker.cache;
|
|
|
|
|
|
|
|
|
|
|
|
import java.security.Principal;
|
|
|
|
import java.security.Principal;
|
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
|
|
|
|
import reactor.core.publisher.Flux;
|
|
|
|
|
|
|
|
import reactor.core.publisher.Mono;
|
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.boot.actuate.endpoint.InvocationContext;
|
|
|
|
import org.springframework.boot.actuate.endpoint.InvocationContext;
|
|
|
|
import org.springframework.boot.actuate.endpoint.SecurityContext;
|
|
|
|
import org.springframework.boot.actuate.endpoint.SecurityContext;
|
|
|
|
|
|
|
|
import org.springframework.boot.actuate.endpoint.invoke.MissingParametersException;
|
|
|
|
import org.springframework.boot.actuate.endpoint.invoke.OperationInvoker;
|
|
|
|
import org.springframework.boot.actuate.endpoint.invoke.OperationInvoker;
|
|
|
|
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
|
@ -39,6 +43,8 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
|
|
|
|
* Tests for {@link CachingOperationInvoker}.
|
|
|
|
* Tests for {@link CachingOperationInvoker}.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @author Stephane Nicoll
|
|
|
|
* @author Stephane Nicoll
|
|
|
|
|
|
|
|
* @author Christoph Dreis
|
|
|
|
|
|
|
|
* @author Phillip Webb
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
class CachingOperationInvokerTests {
|
|
|
|
class CachingOperationInvokerTests {
|
|
|
|
|
|
|
|
|
|
|
@ -62,6 +68,30 @@ class CachingOperationInvokerTests {
|
|
|
|
assertCacheIsUsed(parameters);
|
|
|
|
assertCacheIsUsed(parameters);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
|
|
void cacheInTtlWithMonoResponse() {
|
|
|
|
|
|
|
|
MonoOperationInvoker.invocations = 0;
|
|
|
|
|
|
|
|
MonoOperationInvoker target = new MonoOperationInvoker();
|
|
|
|
|
|
|
|
InvocationContext context = new InvocationContext(mock(SecurityContext.class), Collections.emptyMap());
|
|
|
|
|
|
|
|
CachingOperationInvoker invoker = new CachingOperationInvoker(target, 500L);
|
|
|
|
|
|
|
|
Object response = ((Mono<?>) invoker.invoke(context)).block();
|
|
|
|
|
|
|
|
Object cachedResponse = ((Mono<?>) invoker.invoke(context)).block();
|
|
|
|
|
|
|
|
assertThat(MonoOperationInvoker.invocations).isEqualTo(1);
|
|
|
|
|
|
|
|
assertThat(response).isSameAs(cachedResponse);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test
|
|
|
|
|
|
|
|
void cacheInTtlWithFluxResponse() {
|
|
|
|
|
|
|
|
FluxOperationInvoker.invocations = 0;
|
|
|
|
|
|
|
|
FluxOperationInvoker target = new FluxOperationInvoker();
|
|
|
|
|
|
|
|
InvocationContext context = new InvocationContext(mock(SecurityContext.class), Collections.emptyMap());
|
|
|
|
|
|
|
|
CachingOperationInvoker invoker = new CachingOperationInvoker(target, 500L);
|
|
|
|
|
|
|
|
Object response = ((Flux<?>) invoker.invoke(context)).blockLast();
|
|
|
|
|
|
|
|
Object cachedResponse = ((Flux<?>) invoker.invoke(context)).blockLast();
|
|
|
|
|
|
|
|
assertThat(FluxOperationInvoker.invocations).isEqualTo(1);
|
|
|
|
|
|
|
|
assertThat(response).isSameAs(cachedResponse);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void assertCacheIsUsed(Map<String, Object> parameters) {
|
|
|
|
private void assertCacheIsUsed(Map<String, Object> parameters) {
|
|
|
|
OperationInvoker target = mock(OperationInvoker.class);
|
|
|
|
OperationInvoker target = mock(OperationInvoker.class);
|
|
|
|
Object expected = new Object();
|
|
|
|
Object expected = new Object();
|
|
|
@ -122,4 +152,32 @@ class CachingOperationInvokerTests {
|
|
|
|
verify(target, times(2)).invoke(context);
|
|
|
|
verify(target, times(2)).invoke(context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static class MonoOperationInvoker implements OperationInvoker {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int invocations;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public Object invoke(InvocationContext context) throws MissingParametersException {
|
|
|
|
|
|
|
|
return Mono.fromCallable(() -> {
|
|
|
|
|
|
|
|
invocations++;
|
|
|
|
|
|
|
|
return Mono.just("test");
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static class FluxOperationInvoker implements OperationInvoker {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static int invocations;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public Object invoke(InvocationContext context) throws MissingParametersException {
|
|
|
|
|
|
|
|
return Flux.fromIterable(() -> {
|
|
|
|
|
|
|
|
invocations++;
|
|
|
|
|
|
|
|
return Arrays.asList("spring", "boot").iterator();
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|