Merge branch '2.2.x'

Closes gh-19314
pull/19316/head
Madhura Bhave 5 years ago
commit b42bc1b23d

@ -17,6 +17,7 @@
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.time.Duration;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
@ -49,6 +50,8 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
*/ */
class CachingOperationInvokerTests { class CachingOperationInvokerTests {
private static final long CACHE_TTL = Duration.ofHours(1).toMillis();
@Test @Test
void createInstanceWithTtlSetToZero() { void createInstanceWithTtlSetToZero() {
assertThatIllegalArgumentException() assertThatIllegalArgumentException()
@ -74,7 +77,7 @@ class CachingOperationInvokerTests {
MonoOperationInvoker.invocations = 0; MonoOperationInvoker.invocations = 0;
MonoOperationInvoker target = new MonoOperationInvoker(); MonoOperationInvoker target = new MonoOperationInvoker();
InvocationContext context = new InvocationContext(mock(SecurityContext.class), Collections.emptyMap()); InvocationContext context = new InvocationContext(mock(SecurityContext.class), Collections.emptyMap());
CachingOperationInvoker invoker = new CachingOperationInvoker(target, 500L); CachingOperationInvoker invoker = new CachingOperationInvoker(target, CACHE_TTL);
Object response = ((Mono<?>) invoker.invoke(context)).block(); Object response = ((Mono<?>) invoker.invoke(context)).block();
Object cachedResponse = ((Mono<?>) invoker.invoke(context)).block(); Object cachedResponse = ((Mono<?>) invoker.invoke(context)).block();
assertThat(MonoOperationInvoker.invocations).isEqualTo(1); assertThat(MonoOperationInvoker.invocations).isEqualTo(1);
@ -86,7 +89,7 @@ class CachingOperationInvokerTests {
FluxOperationInvoker.invocations = 0; FluxOperationInvoker.invocations = 0;
FluxOperationInvoker target = new FluxOperationInvoker(); FluxOperationInvoker target = new FluxOperationInvoker();
InvocationContext context = new InvocationContext(mock(SecurityContext.class), Collections.emptyMap()); InvocationContext context = new InvocationContext(mock(SecurityContext.class), Collections.emptyMap());
CachingOperationInvoker invoker = new CachingOperationInvoker(target, 500L); CachingOperationInvoker invoker = new CachingOperationInvoker(target, CACHE_TTL);
Object response = ((Flux<?>) invoker.invoke(context)).blockLast(); Object response = ((Flux<?>) invoker.invoke(context)).blockLast();
Object cachedResponse = ((Flux<?>) invoker.invoke(context)).blockLast(); Object cachedResponse = ((Flux<?>) invoker.invoke(context)).blockLast();
assertThat(FluxOperationInvoker.invocations).isEqualTo(1); assertThat(FluxOperationInvoker.invocations).isEqualTo(1);
@ -98,7 +101,7 @@ class CachingOperationInvokerTests {
Object expected = new Object(); Object expected = new Object();
InvocationContext context = new InvocationContext(mock(SecurityContext.class), parameters); InvocationContext context = new InvocationContext(mock(SecurityContext.class), parameters);
given(target.invoke(context)).willReturn(expected); given(target.invoke(context)).willReturn(expected);
CachingOperationInvoker invoker = new CachingOperationInvoker(target, 500L); CachingOperationInvoker invoker = new CachingOperationInvoker(target, CACHE_TTL);
Object response = invoker.invoke(context); Object response = invoker.invoke(context);
assertThat(response).isSameAs(expected); assertThat(response).isSameAs(expected);
verify(target, times(1)).invoke(context); verify(target, times(1)).invoke(context);
@ -115,7 +118,7 @@ class CachingOperationInvokerTests {
parameters.put("something", null); parameters.put("something", null);
InvocationContext context = new InvocationContext(mock(SecurityContext.class), parameters); InvocationContext context = new InvocationContext(mock(SecurityContext.class), parameters);
given(target.invoke(context)).willReturn(new Object()); given(target.invoke(context)).willReturn(new Object());
CachingOperationInvoker invoker = new CachingOperationInvoker(target, 500L); CachingOperationInvoker invoker = new CachingOperationInvoker(target, CACHE_TTL);
invoker.invoke(context); invoker.invoke(context);
invoker.invoke(context); invoker.invoke(context);
invoker.invoke(context); invoker.invoke(context);
@ -130,7 +133,7 @@ class CachingOperationInvokerTests {
given(securityContext.getPrincipal()).willReturn(mock(Principal.class)); given(securityContext.getPrincipal()).willReturn(mock(Principal.class));
InvocationContext context = new InvocationContext(securityContext, parameters); InvocationContext context = new InvocationContext(securityContext, parameters);
given(target.invoke(context)).willReturn(new Object()); given(target.invoke(context)).willReturn(new Object());
CachingOperationInvoker invoker = new CachingOperationInvoker(target, 500L); CachingOperationInvoker invoker = new CachingOperationInvoker(target, CACHE_TTL);
invoker.invoke(context); invoker.invoke(context);
invoker.invoke(context); invoker.invoke(context);
invoker.invoke(context); invoker.invoke(context);
@ -164,7 +167,7 @@ class CachingOperationInvokerTests {
Collections.emptyMap()); Collections.emptyMap());
given(target.invoke(contextV2)).willReturn(expectedV2); given(target.invoke(contextV2)).willReturn(expectedV2);
given(target.invoke(contextV3)).willReturn(expectedV3); given(target.invoke(contextV3)).willReturn(expectedV3);
CachingOperationInvoker invoker = new CachingOperationInvoker(target, 500L); CachingOperationInvoker invoker = new CachingOperationInvoker(target, CACHE_TTL);
Object response = invoker.invoke(contextV2); Object response = invoker.invoke(contextV2);
assertThat(response).isSameAs(expectedV2); assertThat(response).isSameAs(expectedV2);
verify(target, times(1)).invoke(contextV2); verify(target, times(1)).invoke(contextV2);

Loading…
Cancel
Save