View Javadoc

1   package com.atlassian.vcache.internal.test;
2   
3   import com.atlassian.vcache.ChangeRate;
4   import com.atlassian.vcache.ExternalCacheSettings;
5   import com.atlassian.vcache.ExternalCacheSettingsBuilder;
6   import com.atlassian.vcache.PutPolicy;
7   import com.atlassian.vcache.StableReadExternalCache;
8   import org.junit.Before;
9   import org.junit.Test;
10  import org.slf4j.Logger;
11  import org.slf4j.LoggerFactory;
12  
13  import java.time.Duration;
14  import java.util.Collections;
15  import java.util.Map;
16  import java.util.Optional;
17  
18  import static com.atlassian.vcache.VCacheUtils.fold;
19  import static org.hamcrest.CoreMatchers.equalTo;
20  import static org.hamcrest.Matchers.is;
21  import static org.junit.Assert.assertThat;
22  
23  /**
24   * Base test class for the {@link StableReadExternalCache}s that rely on external services
25   * which could be unreliable. These tests ensure that the behaviour of the cache is reasonable
26   * when the external service is unavailable
27   */
28  public abstract class AbstractUnreliableStableReadExternalCacheIT extends AbstractStableReadExternalCacheIT {
29      private static final Logger log = LoggerFactory.getLogger(AbstractUnreliableStableReadExternalCacheIT.class);
30      private static final String CACHE_NAME = "kids_hobbies.offline";
31  
32      private StableReadExternalCache<String> offlineCache;
33  
34      protected abstract StableReadExternalCache<String> createOfflineCache(String name, ExternalCacheSettings settings, Duration lockTimeout);
35  
36      protected StableReadExternalCache<String> createOfflineCache(String name, ExternalCacheSettings settings) {
37          return createOfflineCache(name, settings, Duration.ofSeconds(2));
38      }
39  
40      @Before
41      public void ensureOfflineCache() {
42          final ExternalCacheSettings settings = new ExternalCacheSettingsBuilder()
43                  .entryGrowthRateHint(ChangeRate.LOW_CHANGE)
44                  .entryCountHint(5)
45                  .defaultTtl(Duration.ofMinutes(5))
46                  .dataChangeRateHint(ChangeRate.HIGH_CHANGE)
47                  .build();
48          offlineCache = createOfflineCache(CACHE_NAME, settings);
49      }
50  
51      @Test
52      public void getOperation() {
53          final Optional<String> result = fold(offlineCache.get("key"),
54                  t -> t,
55                  err -> Optional.of("failed"));
56  
57          assertThat(result, is(equalTo(Optional.of("failed"))));
58      }
59  
60      @Test
61      public void getWithSupplierOperation() {
62          final String result = fold(offlineCache.get("key", () -> "value"),
63                  t -> t,
64                  err -> "failed");
65  
66          assertThat(result, is(equalTo("failed")));
67      }
68  
69      @Test
70      public void getBulkOperation() {
71          final Map<String, Optional<String>> result = fold(offlineCache.getBulk("key1", "key2"),
72                  t -> t,
73                  err -> Collections.emptyMap());
74  
75          assertThat(result, is(equalTo(Collections.emptyMap())));
76      }
77  
78      @Test
79      public void putOperation() {
80          final boolean result = fold(offlineCache.put("key", "value", PutPolicy.PUT_ALWAYS),
81                  t -> t,
82                  err -> false);
83  
84          assertThat(result, is(equalTo(false)));
85      }
86  
87      @Test
88      public void removeOperation() {
89          // Just make sure no exceptions are thrown
90          fold(offlineCache.remove("key"),
91                  t -> t,
92                  err -> null);
93      }
94  
95      @Test
96      public void removeAllOperation() {
97          // Just make sure no exceptions are thrown
98          fold(offlineCache.removeAll(),
99                  t -> t,
100                 err -> null);
101     }
102 }