Handle ApiVersion in CachingOperationInvoker
Prior to this commit, ApiVersion was treated as a mandatory parameter in CachingOperationInvokerAdvisor and thus prevented the CachingOperationInvoker to kick in. By skipping ApiVersion in the same way we're skipping SecurityContext we can avoid this. In order to not return the same cached response, this commit also changes the cache handling in CachingOperationInvoker to account for different ApiVersions being passed. See gh-18961
This commit is contained in:
committed by
Stephane Nicoll
parent
81c0d6a5f7
commit
0bdcd2ee67
@@ -27,6 +27,7 @@ import org.mockito.MockitoAnnotations;
|
||||
import org.springframework.boot.actuate.endpoint.EndpointId;
|
||||
import org.springframework.boot.actuate.endpoint.OperationType;
|
||||
import org.springframework.boot.actuate.endpoint.SecurityContext;
|
||||
import org.springframework.boot.actuate.endpoint.http.ApiVersion;
|
||||
import org.springframework.boot.actuate.endpoint.invoke.OperationInvoker;
|
||||
import org.springframework.boot.actuate.endpoint.invoke.OperationParameters;
|
||||
import org.springframework.boot.actuate.endpoint.invoke.reflect.OperationMethod;
|
||||
@@ -117,6 +118,13 @@ class CachingOperationInvokerAdvisorTests {
|
||||
assertAdviseIsApplied(parameters);
|
||||
}
|
||||
|
||||
@Test
|
||||
void applyWithApiVersionShouldAddAdvise() {
|
||||
OperationParameters parameters = getParameters("getWithApiVersion", ApiVersion.class, String.class);
|
||||
given(this.timeToLive.apply(any())).willReturn(100L);
|
||||
assertAdviseIsApplied(parameters);
|
||||
}
|
||||
|
||||
private void assertAdviseIsApplied(OperationParameters parameters) {
|
||||
OperationInvoker advised = this.advisor.apply(EndpointId.of("foo"), OperationType.READ, parameters,
|
||||
this.invoker);
|
||||
@@ -152,6 +160,10 @@ class CachingOperationInvokerAdvisorTests {
|
||||
return "";
|
||||
}
|
||||
|
||||
String getWithApiVersion(ApiVersion apiVersion, @Nullable String bar) {
|
||||
return "";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ import reactor.core.publisher.Mono;
|
||||
|
||||
import org.springframework.boot.actuate.endpoint.InvocationContext;
|
||||
import org.springframework.boot.actuate.endpoint.SecurityContext;
|
||||
import org.springframework.boot.actuate.endpoint.http.ApiVersion;
|
||||
import org.springframework.boot.actuate.endpoint.invoke.MissingParametersException;
|
||||
import org.springframework.boot.actuate.endpoint.invoke.OperationInvoker;
|
||||
|
||||
@@ -152,6 +153,26 @@ class CachingOperationInvokerTests {
|
||||
verify(target, times(2)).invoke(context);
|
||||
}
|
||||
|
||||
@Test
|
||||
void targetInvokedWithDifferentApiVersion() {
|
||||
OperationInvoker target = mock(OperationInvoker.class);
|
||||
Object expectedV2 = new Object();
|
||||
Object expectedV3 = new Object();
|
||||
InvocationContext contextV2 = new InvocationContext(ApiVersion.V2, mock(SecurityContext.class),
|
||||
Collections.emptyMap());
|
||||
InvocationContext contextV3 = new InvocationContext(ApiVersion.V3, mock(SecurityContext.class),
|
||||
Collections.emptyMap());
|
||||
given(target.invoke(contextV2)).willReturn(expectedV2);
|
||||
given(target.invoke(contextV3)).willReturn(expectedV3);
|
||||
CachingOperationInvoker invoker = new CachingOperationInvoker(target, 500L);
|
||||
Object response = invoker.invoke(contextV2);
|
||||
assertThat(response).isSameAs(expectedV2);
|
||||
verify(target, times(1)).invoke(contextV2);
|
||||
Object cachedResponse = invoker.invoke(contextV3);
|
||||
assertThat(cachedResponse).isNotSameAs(response);
|
||||
verify(target, times(1)).invoke(contextV3);
|
||||
}
|
||||
|
||||
private static class MonoOperationInvoker implements OperationInvoker {
|
||||
|
||||
static int invocations;
|
||||
|
||||
Reference in New Issue
Block a user