|
17 | 17 | package org.springframework.cache.interceptor;
|
18 | 18 |
|
19 | 19 | import java.util.Collections;
|
| 20 | +import java.util.concurrent.Callable; |
| 21 | +import java.util.concurrent.CompletableFuture; |
20 | 22 | import java.util.concurrent.atomic.AtomicLong;
|
21 | 23 |
|
22 | 24 | import org.junit.jupiter.api.AfterEach;
|
23 | 25 | import org.junit.jupiter.api.BeforeEach;
|
24 | 26 | import org.junit.jupiter.api.Test;
|
| 27 | +import reactor.core.publisher.Flux; |
| 28 | +import reactor.core.publisher.Mono; |
25 | 29 |
|
26 | 30 | import org.springframework.cache.Cache;
|
27 | 31 | import org.springframework.cache.CacheManager;
|
|
39 | 43 |
|
40 | 44 | import static org.assertj.core.api.Assertions.assertThat;
|
41 | 45 | import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
| 46 | +import static org.mockito.ArgumentMatchers.any; |
| 47 | +import static org.mockito.ArgumentMatchers.eq; |
42 | 48 | import static org.mockito.BDDMockito.given;
|
43 | 49 | import static org.mockito.BDDMockito.willReturn;
|
44 | 50 | import static org.mockito.BDDMockito.willThrow;
|
@@ -83,11 +89,56 @@ void getFail() {
|
83 | 89 | willThrow(exception).given(this.cache).get(0L);
|
84 | 90 |
|
85 | 91 | Object result = this.simpleService.get(0L);
|
86 |
| - verify(this.errorHandler).handleCacheGetError(exception, cache, 0L); |
| 92 | + verify(this.errorHandler).handleCacheGetError(exception, this.cache, 0L); |
87 | 93 | verify(this.cache).get(0L);
|
88 | 94 | verify(this.cache).put(0L, result); // result of the invocation
|
89 | 95 | }
|
90 | 96 |
|
| 97 | + @Test |
| 98 | + public void getSyncFail() { |
| 99 | + UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on get"); |
| 100 | + willThrow(exception).given(this.cache).get(eq(0L), any(Callable.class)); |
| 101 | + |
| 102 | + Object result = this.simpleService.getSync(0L); |
| 103 | + assertThat(result).isEqualTo(0L); |
| 104 | + verify(this.errorHandler).handleCacheGetError(exception, this.cache, 0L); |
| 105 | + verify(this.cache).get(eq(0L), any(Callable.class)); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void getCompletableFutureFail() { |
| 110 | + UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on get"); |
| 111 | + willThrow(exception).given(this.cache).retrieve(eq(0L)); |
| 112 | + |
| 113 | + Object result = this.simpleService.getFuture(0L).join(); |
| 114 | + assertThat(result).isEqualTo(0L); |
| 115 | + verify(this.errorHandler).handleCacheGetError(exception, this.cache, 0L); |
| 116 | + verify(this.cache).retrieve(eq(0L)); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void getMonoFail() { |
| 121 | + UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on get"); |
| 122 | + willThrow(exception).given(this.cache).retrieve(eq(0L)); |
| 123 | + |
| 124 | + Object result = this.simpleService.getMono(0L).block(); |
| 125 | + assertThat(result).isEqualTo(0L); |
| 126 | + verify(this.errorHandler).handleCacheGetError(exception, this.cache, 0L); |
| 127 | + verify(this.cache).retrieve(eq(0L)); |
| 128 | + } |
| 129 | + |
| 130 | + |
| 131 | + @Test |
| 132 | + public void getFluxFail() { |
| 133 | + UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on get"); |
| 134 | + willThrow(exception).given(this.cache).retrieve(eq(0L)); |
| 135 | + |
| 136 | + Object result = this.simpleService.getFlux(0L).blockLast(); |
| 137 | + assertThat(result).isEqualTo(0L); |
| 138 | + verify(this.errorHandler).handleCacheGetError(exception, this.cache, 0L); |
| 139 | + verify(this.cache).retrieve(eq(0L)); |
| 140 | + } |
| 141 | + |
91 | 142 | @Test
|
92 | 143 | void getAndPutFail() {
|
93 | 144 | UnsupportedOperationException exception = new UnsupportedOperationException("Test exception on get");
|
@@ -220,6 +271,26 @@ public Object get(long id) {
|
220 | 271 | return this.counter.getAndIncrement();
|
221 | 272 | }
|
222 | 273 |
|
| 274 | + @Cacheable(sync = true) |
| 275 | + public Object getSync(long id) { |
| 276 | + return this.counter.getAndIncrement(); |
| 277 | + } |
| 278 | + |
| 279 | + @Cacheable |
| 280 | + public CompletableFuture<Long> getFuture(long id) { |
| 281 | + return CompletableFuture.completedFuture(this.counter.getAndIncrement()); |
| 282 | + } |
| 283 | + |
| 284 | + @Cacheable |
| 285 | + public Mono<Long> getMono(long id) { |
| 286 | + return Mono.just(this.counter.getAndIncrement()); |
| 287 | + } |
| 288 | + |
| 289 | + @Cacheable |
| 290 | + public Flux<Long> getFlux(long id) { |
| 291 | + return Flux.just(this.counter.getAndIncrement(), 0L); |
| 292 | + } |
| 293 | + |
223 | 294 | @CachePut
|
224 | 295 | public Object put(long id) {
|
225 | 296 | return this.counter.getAndIncrement();
|
|
0 commit comments