clear needs to call noop disk and inflight

This commit is contained in:
digitalbuddha 2017-01-31 08:03:16 -05:00
parent 2668aec325
commit 9dfb6c5375
2 changed files with 84 additions and 0 deletions

View file

@ -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<Raw, Parsed> implements InternalStore<Parsed> {
@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<Raw, Parsed> implements InternalStore<Parsed> {
@Override
public void clearMemory(@Nonnull final BarCode barCode) {
memCache.invalidate(barCode);
inFlightRequests.invalidate(barCode);
clearDiskIfNoOp();
}
/**

View file

@ -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<Integer> store;
@Before
public void setUp() {
networkCalls = 0;
store = StoreBuilder.<Integer>builder()
.fetcher(new Fetcher<Integer>() {
@Nonnull
@Override
public Observable<Integer> fetch(BarCode barCode) {
return Observable.fromCallable(new Callable<Integer>() {
@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);
}
}