@Test public void testShouldCallInit() throws Exception { final Cache mockCache = mock(Cache.class); final ConcurrentCache cache = new ConcurrentCache(mockCache); cache.init(); verify(mockCache, times(1)).init(); }
private boolean checkCache(@Nonnull Request request) { if (!mCache.hasCache()) { return false; } final String key = request.getCacheKey(); if (key == null) { return false; } final Cache.Entry entry = mCache.get(request.getType().getCacheKey(key)); if (entry == null) { return false; } request.onSuccess(entry.data); return true; }
@Override public void onPurchasesChanged() { mCache.removeAll(RequestType.GET_PURCHASES.getCacheKeyType()); } };
@Test public void testShouldPutOnlyIfNotExists() throws Exception { final ConcurrentCache cache = new ConcurrentCache(new MapCache()); final Cache.Entry entry = newEntry(); final Cache.Entry newEntry = newEntry(); final Cache.Key key = newKey(); cache.put(key, entry); cache.putIfNotExist(key, newEntry); assertSame(entry, cache.get(key)); }
@Test public void testShouldRemoveAllEntriesOfType() throws Exception { final ConcurrentCache cache = new ConcurrentCache(new MapCache()); final Cache.Key k1 = newKey(); cache.put(k1, newEntry()); final Cache.Key k11 = new Cache.Key(k1.type, k1.key + "test"); cache.put(k11, newEntry()); final Cache.Key k2 = new Cache.Key(k1.type + 1, "test"); cache.put(k2, newEntry()); final Cache.Key k3 = new Cache.Key(k1.type + 2, "test"); cache.put(k3, newEntry()); assertNotNull(cache.get(k1)); assertNotNull(cache.get(k11)); assertNotNull(cache.get(k2)); assertNotNull(cache.get(k3)); cache.removeAll(k1.type); assertNull(cache.get(k1)); assertNull(cache.get(k11)); assertNotNull(cache.get(k2)); assertNotNull(cache.get(k3)); }
@Test public void testShouldRemoveValue() throws Exception { final ConcurrentCache cache = new ConcurrentCache(new MapCache()); final Cache.Key key = newKey(); final Cache.Entry entry = newEntry(); cache.put(key, entry); assertSame(entry, cache.get(key)); cache.remove(key); assertNull(cache.get(key)); }
@Test public void testShouldRemoveAllEntries() throws Exception { final ConcurrentCache cache = new ConcurrentCache(new MapCache()); final Cache.Key k1 = newKey(); cache.put(k1, newEntry()); final Cache.Key k2 = newKey(); cache.put(k2, newEntry()); final Cache.Key k3 = newKey(); cache.put(k3, newEntry()); cache.clear(); assertNull(cache.get(k1)); assertNull(cache.get(k2)); assertNull(cache.get(k3)); }
@Test public void testShouldReturnNullIfValueExpired() throws Exception { final ConcurrentCache cache = new ConcurrentCache(new MapCache()); final Cache.Entry entry = newEntry(-DAY); final Cache.Key key = newKey(); cache.put(key, entry); final Cache.Entry actual = cache.get(key); assertNull(actual); }
@Override public void onSuccess(@Nonnull R result) { final String key = mRequest.getCacheKey(); final RequestType type = mRequest.getType(); if (key != null) { final long now = currentTimeMillis(); final Cache.Entry entry = new Cache.Entry(result, now + type.expiresIn); mCache.putIfNotExist(type.getCacheKey(key), entry); } switch (type) { case PURCHASE: case CHANGE_PURCHASE: case CONSUME_PURCHASE: // these requests might affect the state of purchases => we need to invalidate caches. // see Billing#onPurchaseFinished() also mCache.removeAll(RequestType.GET_PURCHASES.getCacheKeyType()); break; } super.onSuccess(result); }
@Test public void testShouldPutAllEntries() throws Exception { final Cache mockCache = mock(Cache.class); final ConcurrentCache cache = new ConcurrentCache(mockCache); final Cache.Entry entry = newEntry(); cache.put(newKey(), entry); cache.put(newKey(), entry); cache.put(newKey(), entry); verify(mockCache, times(3)).put(any(Cache.Key.class), eq(entry)); }
/** * @param context application or activity context. Needed to bind to the in-app billing * service. * @param configuration billing configuration */ public Billing(@Nonnull Context context, @Nonnull Handler handler, @Nonnull Configuration configuration) { if (context instanceof Application) { // mContext.getApplicationContext() might return null for applications as we allow create Billing before // Application#onCreate is called mContext = context; } else { mContext = context.getApplicationContext(); } mMainThread = new MainThread(handler); mConfiguration = new StaticConfiguration(configuration); Check.isNotEmpty(mConfiguration.getPublicKey()); final Cache cache = configuration.getCache(); mCache = new ConcurrentCache(cache == null ? null : new SafeCache(cache)); mPlayStoreBroadcastReceiver = new PlayStoreBroadcastReceiver(mContext, mLock); }
public CachingRequestListener(@Nonnull Request<R> request, @Nonnull RequestListener<R> listener) { super(listener); Check.isTrue(mCache.hasCache(), "Cache must exist"); mRequest = request; }
@Test public void testShouldReturnNotExpiredValue() throws Exception { final ConcurrentCache cache = new ConcurrentCache(new MapCache()); final Cache.Entry expected = newEntry(); final Cache.Key key = newKey(); cache.put(key, expected); final Cache.Entry actual = cache.get(key); assertSame(expected, actual); }
@Test public void testShouldBlockOperations() throws Exception { final OneThreadCache c = new OneThreadCache(); final ConcurrentCache cache = new ConcurrentCache(c); final Executor executor = Executors.newFixedThreadPool(20); final Random random = new Random(System.currentTimeMillis());
@Nonnull PurchaseFlow createPurchaseFlow(@Nonnull IntentStarter intentStarter, int requestCode, @Nonnull RequestListener<Purchase> listener) { if (mCache.hasCache()) { listener = new RequestListenerWrapper<Purchase>(listener) { @Override public void onSuccess(@Nonnull Purchase result) { mCache.removeAll(RequestType.GET_PURCHASES.getCacheKeyType()); super.onSuccess(result); } }; } return new PurchaseFlow(intentStarter, requestCode, listener, mConfiguration.getPurchaseVerifier()); }
@Override public void onSuccess(@Nonnull Purchase result) { mCache.removeAll(RequestType.GET_PURCHASES.getCacheKeyType()); super.onSuccess(result); } };
<R> int runWhenConnected(@Nonnull Request<R> request, @Nullable RequestListener<R> listener, @Nullable Object tag) { if (listener != null) { if (mCache.hasCache()) { listener = new CachingRequestListener<>(request, listener); } request.setListener(listener); } if (tag != null) { request.setTag(tag); } mPendingRequests.add(onConnectedService(request)); connect(); return request.getId(); }
@Override public void onError(int response, @Nonnull Exception e) { final RequestType type = mRequest.getType(); // sometimes it is possible that cached data is not synchronized with data on Google Play => we can // clear caches if such situation occurs switch (type) { case PURCHASE: case CHANGE_PURCHASE: if (response == ITEM_ALREADY_OWNED) { mCache.removeAll(RequestType.GET_PURCHASES.getCacheKeyType()); } break; case CONSUME_PURCHASE: if (response == ITEM_NOT_OWNED) { mCache.removeAll(RequestType.GET_PURCHASES.getCacheKeyType()); } break; } super.onError(response, e); } }