17
17
package org .springframework .boot .actuate .endpoint .invoker .cache ;
18
18
19
19
import java .security .Principal ;
20
+ import java .time .Duration ;
20
21
import java .util .Arrays ;
21
22
import java .util .Collections ;
22
23
import java .util .HashMap ;
49
50
*/
50
51
class CachingOperationInvokerTests {
51
52
53
+ private static final long CACHE_TTL = Duration .ofHours (1 ).toMillis ();
54
+
52
55
@ Test
53
56
void createInstanceWithTtlSetToZero () {
54
57
assertThatIllegalArgumentException ()
@@ -74,7 +77,7 @@ void cacheInTtlWithMonoResponse() {
74
77
MonoOperationInvoker .invocations = 0 ;
75
78
MonoOperationInvoker target = new MonoOperationInvoker ();
76
79
InvocationContext context = new InvocationContext (mock (SecurityContext .class ), Collections .emptyMap ());
77
- CachingOperationInvoker invoker = new CachingOperationInvoker (target , 500L );
80
+ CachingOperationInvoker invoker = new CachingOperationInvoker (target , CACHE_TTL );
78
81
Object response = ((Mono <?>) invoker .invoke (context )).block ();
79
82
Object cachedResponse = ((Mono <?>) invoker .invoke (context )).block ();
80
83
assertThat (MonoOperationInvoker .invocations ).isEqualTo (1 );
@@ -86,7 +89,7 @@ void cacheInTtlWithFluxResponse() {
86
89
FluxOperationInvoker .invocations = 0 ;
87
90
FluxOperationInvoker target = new FluxOperationInvoker ();
88
91
InvocationContext context = new InvocationContext (mock (SecurityContext .class ), Collections .emptyMap ());
89
- CachingOperationInvoker invoker = new CachingOperationInvoker (target , 500L );
92
+ CachingOperationInvoker invoker = new CachingOperationInvoker (target , CACHE_TTL );
90
93
Object response = ((Flux <?>) invoker .invoke (context )).blockLast ();
91
94
Object cachedResponse = ((Flux <?>) invoker .invoke (context )).blockLast ();
92
95
assertThat (FluxOperationInvoker .invocations ).isEqualTo (1 );
@@ -98,7 +101,7 @@ private void assertCacheIsUsed(Map<String, Object> parameters) {
98
101
Object expected = new Object ();
99
102
InvocationContext context = new InvocationContext (mock (SecurityContext .class ), parameters );
100
103
given (target .invoke (context )).willReturn (expected );
101
- CachingOperationInvoker invoker = new CachingOperationInvoker (target , 500L );
104
+ CachingOperationInvoker invoker = new CachingOperationInvoker (target , CACHE_TTL );
102
105
Object response = invoker .invoke (context );
103
106
assertThat (response ).isSameAs (expected );
104
107
verify (target , times (1 )).invoke (context );
@@ -115,7 +118,7 @@ void targetAlwaysInvokedWithParameters() {
115
118
parameters .put ("something" , null );
116
119
InvocationContext context = new InvocationContext (mock (SecurityContext .class ), parameters );
117
120
given (target .invoke (context )).willReturn (new Object ());
118
- CachingOperationInvoker invoker = new CachingOperationInvoker (target , 500L );
121
+ CachingOperationInvoker invoker = new CachingOperationInvoker (target , CACHE_TTL );
119
122
invoker .invoke (context );
120
123
invoker .invoke (context );
121
124
invoker .invoke (context );
@@ -130,7 +133,7 @@ void targetAlwaysInvokedWithPrincipal() {
130
133
given (securityContext .getPrincipal ()).willReturn (mock (Principal .class ));
131
134
InvocationContext context = new InvocationContext (securityContext , parameters );
132
135
given (target .invoke (context )).willReturn (new Object ());
133
- CachingOperationInvoker invoker = new CachingOperationInvoker (target , 500L );
136
+ CachingOperationInvoker invoker = new CachingOperationInvoker (target , CACHE_TTL );
134
137
invoker .invoke (context );
135
138
invoker .invoke (context );
136
139
invoker .invoke (context );
@@ -164,7 +167,7 @@ void targetInvokedWithDifferentApiVersion() {
164
167
Collections .emptyMap ());
165
168
given (target .invoke (contextV2 )).willReturn (expectedV2 );
166
169
given (target .invoke (contextV3 )).willReturn (expectedV3 );
167
- CachingOperationInvoker invoker = new CachingOperationInvoker (target , 500L );
170
+ CachingOperationInvoker invoker = new CachingOperationInvoker (target , CACHE_TTL );
168
171
Object response = invoker .invoke (contextV2 );
169
172
assertThat (response ).isSameAs (expectedV2 );
170
173
verify (target , times (1 )).invoke (contextV2 );
0 commit comments