From 9dfb6c5375a0fe128f1fb402be5095a0b270c367 Mon Sep 17 00:00:00 2001 From: digitalbuddha Date: Tue, 31 Jan 2017 08:03:16 -0500 Subject: [PATCH] clear needs to call noop disk and inflight --- .../store/base/impl/RealInternalStore.java | 11 +++ .../external/store/ClearStoreMemoryTest.java | 73 +++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 store/src/test/java/com/nytimes/android/external/store/ClearStoreMemoryTest.java diff --git a/store/src/main/java/com/nytimes/android/external/store/base/impl/RealInternalStore.java b/store/src/main/java/com/nytimes/android/external/store/base/impl/RealInternalStore.java index 29ef242..5c35af1 100644 --- a/store/src/main/java/com/nytimes/android/external/store/base/impl/RealInternalStore.java +++ b/store/src/main/java/com/nytimes/android/external/store/base/impl/RealInternalStore.java @@ -6,6 +6,7 @@ import com.nytimes.android.external.cache.CacheBuilder; import com.nytimes.android.external.store.base.Fetcher; import com.nytimes.android.external.store.base.InternalStore; import com.nytimes.android.external.store.base.Persister; +import com.nytimes.android.external.store.util.NoopPersister; import com.nytimes.android.external.store.util.OnErrorResumeWithEmpty; import java.util.concurrent.Callable; @@ -265,6 +266,14 @@ final class RealInternalStore implements InternalStore { @Override public void clearMemory() { memCache.invalidateAll(); + inFlightRequests.invalidateAll(); + clearDiskIfNoOp(); + } + + private void clearDiskIfNoOp() { + if (persister() instanceof NoopPersister) { + persister = new NoopPersister<>(); + } } /** @@ -275,6 +284,8 @@ final class RealInternalStore implements InternalStore { @Override public void clearMemory(@Nonnull final BarCode barCode) { memCache.invalidate(barCode); + inFlightRequests.invalidate(barCode); + clearDiskIfNoOp(); } /** diff --git a/store/src/test/java/com/nytimes/android/external/store/ClearStoreMemoryTest.java b/store/src/test/java/com/nytimes/android/external/store/ClearStoreMemoryTest.java new file mode 100644 index 0000000..80f2194 --- /dev/null +++ b/store/src/test/java/com/nytimes/android/external/store/ClearStoreMemoryTest.java @@ -0,0 +1,73 @@ +package com.nytimes.android.external.store; + +import com.nytimes.android.external.store.base.Fetcher; +import com.nytimes.android.external.store.base.Store; +import com.nytimes.android.external.store.base.impl.BarCode; +import com.nytimes.android.external.store.base.impl.StoreBuilder; + +import org.junit.Before; +import org.junit.Test; + +import java.util.concurrent.Callable; + +import javax.annotation.Nonnull; + +import rx.Observable; + +import static org.assertj.core.api.Assertions.assertThat; + +public class ClearStoreMemoryTest { + + private int networkCalls = 0; + private Store store; + + @Before + public void setUp() { + networkCalls = 0; + store = StoreBuilder.builder() + .fetcher(new Fetcher() { + @Nonnull + @Override + public Observable fetch(BarCode barCode) { + return Observable.fromCallable(new Callable() { + @Override + public Integer call() throws Exception { + return networkCalls++; + } + }); + } + }) + .open(); + } + + @Test + public void testClearSingleBarCode() { + // one request should produce one call + BarCode barcode = new BarCode("type", "key"); + store.get(barcode).test().awaitTerminalEvent(); + assertThat(networkCalls).isEqualTo(1); + + // after clearing the memory another call should be made + store.clearMemory(barcode); + store.get(barcode).test().awaitTerminalEvent(); + assertThat(networkCalls).isEqualTo(2); + } + + @Test + public void testClearAllBarCodes() { + BarCode b1 = new BarCode("type1", "key1"); + BarCode b2 = new BarCode("type2", "key2"); + + // each request should produce one call + store.get(b1).test().awaitTerminalEvent(); + store.get(b2).test().awaitTerminalEvent(); + assertThat(networkCalls).isEqualTo(2); + + store.clearMemory(); + + // after everything is cleared each request should produce another 2 calls + store.get(b1).test().awaitTerminalEvent(); + store.get(b2).test().awaitTerminalEvent(); + assertThat(networkCalls).isEqualTo(4); + } +}