|
| 1 | +package io.github.xanthic.cache.spring; |
| 2 | + |
| 3 | +import io.github.xanthic.cache.spring.config.CacheConfiguration; |
| 4 | +import org.junit.jupiter.api.Assertions; |
| 5 | +import org.junit.jupiter.api.DisplayName; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 8 | +import org.springframework.beans.factory.annotation.Autowired; |
| 9 | +import org.springframework.cache.Cache; |
| 10 | +import org.springframework.cache.CacheManager; |
| 11 | +import org.springframework.test.context.ContextConfiguration; |
| 12 | +import org.springframework.test.context.junit.jupiter.SpringExtension; |
| 13 | + |
| 14 | +import java.util.Objects; |
| 15 | +import java.util.concurrent.Callable; |
| 16 | +import java.util.concurrent.ExecutorService; |
| 17 | +import java.util.concurrent.Executors; |
| 18 | +import java.util.concurrent.TimeUnit; |
| 19 | +import java.util.concurrent.atomic.AtomicInteger; |
| 20 | + |
| 21 | +import static org.awaitility.Awaitility.await; |
| 22 | + |
| 23 | +@ExtendWith(SpringExtension.class) |
| 24 | +@ContextConfiguration(classes = { CacheConfiguration.class }) |
| 25 | +public class SpringCacheTest { |
| 26 | + |
| 27 | + @Autowired |
| 28 | + CacheManager cacheManager; |
| 29 | + |
| 30 | + @Test |
| 31 | + @DisplayName("Tests cache get, put, putIfAbsent, clear") |
| 32 | + public void putGetClearTest() { |
| 33 | + Cache cache = Objects.requireNonNull(cacheManager.getCache("dev")); |
| 34 | + |
| 35 | + // cache should be listed in cacheManager |
| 36 | + Assertions.assertTrue(cacheManager.getCacheNames().contains("dev")); |
| 37 | + |
| 38 | + // Test put/get |
| 39 | + cache.put("4/20", 420); |
| 40 | + Assertions.assertEquals(420, Objects.requireNonNull(cache.get("4/20")).get()); |
| 41 | + |
| 42 | + // Test putIfAbsent |
| 43 | + Assertions.assertEquals(420, Objects.requireNonNull(cache.putIfAbsent("4/20", 69)).get()); |
| 44 | + Assertions.assertEquals(420, Objects.requireNonNull(cache.get("4/20")).get()); |
| 45 | + |
| 46 | + // Test clear |
| 47 | + cache.clear(); |
| 48 | + Assertions.assertNull(cache.get("4/20")); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + @DisplayName("Tests the registration and usage of a custom cache") |
| 53 | + public void registerCustomCacheTest() { |
| 54 | + XanthicSpringCacheManager xanthicSpringCacheManager = (XanthicSpringCacheManager) cacheManager; |
| 55 | + xanthicSpringCacheManager.registerCache("my-custom-cache", spec -> { |
| 56 | + spec.maxSize(1L); |
| 57 | + }); |
| 58 | + |
| 59 | + // registration check |
| 60 | + Assertions.assertTrue(xanthicSpringCacheManager.getCustomCacheNames().contains("my-custom-cache"), "getCustomCacheNames should contain my-custom-cache"); |
| 61 | + |
| 62 | + // cache available |
| 63 | + Cache cache = cacheManager.getCache("my-custom-cache"); |
| 64 | + Assertions.assertNotNull(cache, "my-custom-cache should not be null"); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + @DisplayName("Tests the eviction of entries based on max size") |
| 69 | + public void evictionTest() { |
| 70 | + XanthicSpringCacheManager xanthicSpringCacheManager = (XanthicSpringCacheManager) cacheManager; |
| 71 | + xanthicSpringCacheManager.registerCache("small-cache", spec -> { |
| 72 | + spec.maxSize(2L); |
| 73 | + }); |
| 74 | + |
| 75 | + Cache cache = Objects.requireNonNull(cacheManager.getCache("small-cache")); |
| 76 | + cache.put("first", 1); |
| 77 | + cache.put("second", 2); |
| 78 | + cache.put("third", 3); |
| 79 | + |
| 80 | + Assertions.assertEquals(2, Objects.requireNonNull(cache.get("second")).get()); |
| 81 | + Assertions.assertEquals(3, Objects.requireNonNull(cache.get("third")).get()); |
| 82 | + await().atLeast(100, TimeUnit.MILLISECONDS) |
| 83 | + .atMost(5, TimeUnit.SECONDS) |
| 84 | + .until(() -> cache.get("first") == null); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + @DisplayName("Tests the eviction of entries based on max size") |
| 89 | + public void valueLoaderTest() { |
| 90 | + XanthicSpringCacheManager xanthicSpringCacheManager = (XanthicSpringCacheManager) cacheManager; |
| 91 | + xanthicSpringCacheManager.registerCache("value-cache", spec -> { |
| 92 | + spec.maxSize(100L); |
| 93 | + }); |
| 94 | + Cache cache = Objects.requireNonNull(cacheManager.getCache("value-cache")); |
| 95 | + |
| 96 | + AtomicInteger callCounter = new AtomicInteger(0); |
| 97 | + Callable<String> valueLoader = () -> { |
| 98 | + callCounter.incrementAndGet(); |
| 99 | + return "value-loaded"; |
| 100 | + }; |
| 101 | + |
| 102 | + String value = cache.get("key", valueLoader); |
| 103 | + Assertions.assertEquals("value-loaded", value); |
| 104 | + Assertions.assertEquals(1, callCounter.get(), "Value loader should only be called once"); |
| 105 | + |
| 106 | + // Check that the valueLoader is not called again |
| 107 | + value = cache.get("key", valueLoader); |
| 108 | + Assertions.assertEquals("value-loaded", value); |
| 109 | + Assertions.assertEquals(1, callCounter.get(), "Value loader should still be called only once"); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + @DisplayName("Tests the eviction of entries based on max size") |
| 114 | + public void valueLoaderConcurrentTest() throws InterruptedException { |
| 115 | + XanthicSpringCacheManager xanthicSpringCacheManager = (XanthicSpringCacheManager) cacheManager; |
| 116 | + xanthicSpringCacheManager.registerCache("value-cache", spec -> { |
| 117 | + spec.maxSize(100L); |
| 118 | + }); |
| 119 | + Cache cache = Objects.requireNonNull(cacheManager.getCache("value-cache")); |
| 120 | + |
| 121 | + AtomicInteger callCounter = new AtomicInteger(0); |
| 122 | + Callable<String> valueLoader = () -> { |
| 123 | + callCounter.incrementAndGet(); |
| 124 | + return "value-loaded"; |
| 125 | + }; |
| 126 | + |
| 127 | + int numThreads = 10; |
| 128 | + ExecutorService executorService = Executors.newFixedThreadPool(numThreads); |
| 129 | + for (int i = 0; i < numThreads; i++) { |
| 130 | + executorService.submit(() -> { |
| 131 | + String value = cache.get("key", valueLoader); |
| 132 | + Assertions.assertEquals("value-loaded", value); |
| 133 | + }); |
| 134 | + } |
| 135 | + executorService.awaitTermination(5, TimeUnit.SECONDS); |
| 136 | + |
| 137 | + String value = cache.get("key", valueLoader); |
| 138 | + Assertions.assertEquals("value-loaded", value); |
| 139 | + Assertions.assertEquals(1, callCounter.get(), "Value loader should only be called once"); |
| 140 | + } |
| 141 | + |
| 142 | +} |
0 commit comments