View Javadoc

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