View Javadoc

1   package com.atlassian.vcache.internal.test;
2   
3   import com.atlassian.vcache.DirectExternalCache;
4   import com.atlassian.vcache.PutPolicy;
5   import com.atlassian.vcache.TransactionalExternalCache;
6   import org.junit.Test;
7   import org.slf4j.Logger;
8   import org.slf4j.LoggerFactory;
9   
10  import java.util.Optional;
11  
12  import static com.atlassian.vcache.VCacheUtils.fold;
13  import static org.hamcrest.CoreMatchers.equalTo;
14  import static org.hamcrest.Matchers.is;
15  import static org.junit.Assert.assertThat;
16  
17  /**
18   * Base test class for the {@link TransactionalExternalCache}s that rely on external services
19   * which could be unreliable. These tests ensure that the behaviour of the cache is reasonable
20   * when the external service is unavailable
21   */
22  public abstract class AbstractUnreliableTransactionalExternalCacheIT extends AbstractTransactionalExternalCacheIT {
23      private static final Logger log = LoggerFactory.getLogger(AbstractUnreliableTransactionalExternalCacheIT.class);
24  
25      // Points to a non-responsive memcached
26      protected abstract TransactionalExternalCache<String> offlineCache();
27      protected abstract DirectExternalCache<String> offlineDirectCache();
28  
29      protected abstract void offlineCacheTransactionSync();
30      protected abstract void offlineCacheTransactionDiscard();
31  
32      @Test
33      public void getWithSupplier() {
34          final String result = fold(cache().get("key", () -> "value"),
35                  t -> t,
36                  err -> "failed");
37  
38          assertThat(result, is(equalTo("value")));
39      }
40  
41      @Test
42      public void getWithSupplierOfflineClient() {
43          final String result = fold(offlineCache().get("key", () -> "value"),
44                  t -> t,
45                  err -> "failed");
46  
47          assertThat(result, is(equalTo("failed")));
48      }
49  
50      @Test
51      public void putThenSync() {
52          cache().put("key", "value", PutPolicy.PUT_ALWAYS);
53          cacheTransactionSync();
54  
55          final Optional<String> result = fold(cache().get("key"),
56                  t -> t,
57                  err -> Optional.of("failed"));
58  
59          assertThat(result, is(equalTo(Optional.of("value"))));
60      }
61  
62      @Test
63      public void putThenSyncOfflineClient() {
64          offlineCache().put("key", "value", PutPolicy.PUT_ALWAYS);
65          offlineCacheTransactionSync();
66  
67          final Optional<String> result = fold(offlineCache().get("key"),
68                  t -> t,
69                  err -> Optional.of("failed"));
70  
71          // Sync should fail but not throw an exception - subsequent remote get should fail
72          assertThat(result, is(equalTo(Optional.of("failed"))));
73      }
74  
75      @Test
76      public void removeThenSync() {
77          cache().remove("key");
78          cacheTransactionSync();
79  
80          final Optional<String> result = fold(cache().get("key"),
81                  t -> t,
82                  err -> Optional.of("failed"));
83  
84          assertThat(result, is(equalTo(Optional.empty())));
85      }
86  
87      @Test
88      public void removeThenSyncOfflineClient() {
89          offlineCache().remove("key");
90          offlineCacheTransactionSync();
91  
92          final Optional<String> result = fold(offlineCache().get("key"),
93                  t -> t,
94                  err -> Optional.of("failed"));
95  
96          assertThat(result, is(equalTo(Optional.of("failed"))));
97      }
98  }
99