<commit_before>package com.nytimes.android.external.store;

import com.nytimes.android.external.cache.Cache;
import com.nytimes.android.external.cache.CacheBuilder;
import com.nytimes.android.external.store.base.Fetcher;
import com.nytimes.android.external.store.base.Persister;
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.RealStore;
import com.nytimes.android.external.store.base.impl.StoreBuilder;
import com.nytimes.android.external.store.util.NoopPersister;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import rx.Emitter;
import rx.Observable;
import rx.functions.Action1;
import rx.functions.Func2;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class StoreTest {

    private static final String DISK = "disk";
    private static final String NETWORK = "fetch";
    private static final String MEMORY = "memory";

    @Mock
    Fetcher<String> fetcher;
    @Mock
    Persister<String> persister;
    private final BarCode barCode = new BarCode("key", "value");
    private final AtomicInteger counter = new AtomicInteger(0);

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void testSimple() {

        Store<String> simpleStore = new StoreBuilder<String>()
                .persister(persister)
                .fetcher(fetcher)
                .open();


        when(fetcher.fetch(barCode))
                .thenReturn(Observable.just(NETWORK));

        when(persister.read(barCode))
                .thenReturn(Observable.<String>empty())
                .thenReturn(Observable.just(DISK));

        when(persister.write(barCode, NETWORK))
                .thenReturn(Observable.just(true));

        String value = simpleStore.get(barCode).toBlocking().first();

        assertThat(value).isEqualTo(DISK);
        value = simpleStore.get(barCode).toBlocking().first();
        assertThat(value).isEqualTo(DISK);
        verify(fetcher, times(1)).fetch(barCode);
    }


    @Test
    public void testDoubleTap() {

        Store<String> simpleStore = new StoreBuilder<String>()
                .persister(persister)
                .fetcher(fetcher)
                .open();

        Observable<String> networkObservable =
                Observable.fromEmitter(new Action1<Emitter<String>>() {
                    @Override
                    public void call(Emitter<String> emitter) {
                        if (counter.incrementAndGet() == 1) {
                            emitter.onNext(NETWORK);

                        } else {
                            emitter.onError(new RuntimeException("Yo Dawg your inflight is broken"));
                        }
                    }
                }, Emitter.BackpressureMode.NONE);
        when(fetcher.fetch(barCode))
                .thenReturn(networkObservable);

        when(persister.read(barCode))
                .thenReturn(Observable.<String>empty())
                .thenReturn(Observable.just(DISK));

        when(persister.write(barCode, NETWORK))
                .thenReturn(Observable.just(true));


        String response = simpleStore.get(barCode).zipWith(simpleStore.get(barCode),
                new Func2<String, String, String>() {
                    @Override
                    public String call(String s, String s2) {
                        return "hello";
                    }
                })
                .toBlocking()
                .first();
        assertThat(response).isEqualTo("hello");
        verify(fetcher, times(1)).fetch(barCode);
    }


    @Test
    public void testSubclass() {

        Store<String> simpleStore = new SampleStore(fetcher, persister);
        simpleStore.clearMemory();

        when(fetcher.fetch(barCode))
                .thenReturn(Observable.just(NETWORK));

        when(persister.read(barCode))
                .thenReturn(Observable.<String>empty())
                .thenReturn(Observable.just(DISK));
        when(persister.write(barCode, NETWORK)).thenReturn(Observable.just(true));

        String value = simpleStore.get(barCode).toBlocking().first();
        assertThat(value).isEqualTo(DISK);
        value = simpleStore.get(barCode).toBlocking().first();
        assertThat(value).isEqualTo(DISK);
        verify(fetcher, times(1)).fetch(barCode);
    }


    @Test
    public void testNoopAndDefault() {

        NoopPersister<String> persister = spy(new NoopPersister<String>());
        Store<String> simpleStore = new RealStore<>(fetcher, persister);
        simpleStore.clearMemory();


        when(fetcher.fetch(barCode))
                .thenReturn(Observable.just(NETWORK));

        String value = simpleStore.get(barCode).toBlocking().first();
        verify(fetcher, times(1)).fetch(barCode);
        verify(persister, times(1)).write(barCode, NETWORK);
        verify(persister, times(2)).read(barCode);
        assertThat(value).isEqualTo(NETWORK);


        value = simpleStore.get(barCode).toBlocking().first();
        verify(persister, times(2)).read(barCode);
        verify(persister, times(1)).write(barCode, NETWORK);
        verify(fetcher, times(1)).fetch(barCode);

        assertThat(value).isEqualTo(NETWORK);
    }


    @Test
    public void testEquivalence() {
        Cache<BarCode, String> cache = CacheBuilder.newBuilder()
                .maximumSize(1)<commit_msg>fix flaky test<commit_after>package com.nytimes.android.external.store;

import com.nytimes.android.external.cache.Cache;
import com.nytimes.android.external.cache.CacheBuilder;
import com.nytimes.android.external.store.base.Fetcher;
import com.nytimes.android.external.store.base.Persister;
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.RealStore;
import com.nytimes.android.external.store.base.impl.StoreBuilder;
import com.nytimes.android.external.store.util.NoopPersister;

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

import rx.Emitter;
import rx.Observable;
import rx.functions.Action1;
import rx.functions.Func2;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class StoreTest {

    private static final String DISK = "disk";
    private static final String NETWORK = "fetch";
    private static final String MEMORY = "memory";

    @Mock
    Fetcher<String> fetcher;
    @Mock
    Persister<String> persister;
    private final BarCode barCode = new BarCode("key", "value");
    private final AtomicInteger counter = new AtomicInteger(0);

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void testSimple() {

        Store<String> simpleStore = new StoreBuilder<String>()
                .persister(persister)
                .fetcher(fetcher)
                .open();


        when(fetcher.fetch(barCode))
                .thenReturn(Observable.just(NETWORK));

        when(persister.read(barCode))
                .thenReturn(Observable.<String>empty())
                .thenReturn(Observable.just(DISK));

        when(persister.write(barCode, NETWORK))
                .thenReturn(Observable.just(true));

        String value = simpleStore.get(barCode).toBlocking().first();

        assertThat(value).isEqualTo(DISK);
        value = simpleStore.get(barCode).toBlocking().first();
        assertThat(value).isEqualTo(DISK);
        verify(fetcher, times(1)).fetch(barCode);
    }


    @Test
    public void testDoubleTap() {

        Store<String> simpleStore = new StoreBuilder<String>()
                .persister(persister)
                .fetcher(fetcher)
                .open();

        Observable<String> networkObservable =
                Observable.fromEmitter(new Action1<Emitter<String>>() {
                    @Override
                    public void call(Emitter<String> emitter) {
                        if (counter.incrementAndGet() == 1) {
                            emitter.onNext(NETWORK);

                        } else {
                            emitter.onError(new RuntimeException("Yo Dawg your inflight is broken"));
                        }
                    }
                }, Emitter.BackpressureMode.NONE);
        when(fetcher.fetch(barCode))
                .thenReturn(networkObservable);

        when(persister.read(barCode))
                .thenReturn(Observable.<String>empty())
                .thenReturn(Observable.just(DISK));

        when(persister.write(barCode, NETWORK))
                .thenReturn(Observable.just(true));


        String response = simpleStore.get(barCode).zipWith(simpleStore.get(barCode),
                new Func2<String, String, String>() {
                    @Override
                    public String call(String s, String s2) {
                        return "hello";
                    }
                })
                .toBlocking()
                .first();
        assertThat(response).isEqualTo("hello");
        verify(fetcher, times(1)).fetch(barCode);
    }


    @Test
    public void testSubclass() {

        Store<String> simpleStore = new SampleStore(fetcher, persister);
        simpleStore.clearMemory();

        when(fetcher.fetch(barCode))
                .thenReturn(Observable.just(NETWORK));

        when(persister.read(barCode))
                .thenReturn(Observable.<String>empty())
                .thenReturn(Observable.just(DISK));
        when(persister.write(barCode, NETWORK)).thenReturn(Observable.just(true));

        String value = simpleStore.get(barCode).toBlocking().first();
        assertThat(value).isEqualTo(DISK);
        value = simpleStore.get(barCode).toBlocking().first();
        assertThat(value).isEqualTo(DISK);
        verify(fetcher, times(1)).fetch(barCode);
    }


    @Test
    public void testNoopAndDefault() {

        NoopPersister<String> persister = spy(new NoopPersister<String>());
        Store<String> simpleStore = new RealStore<>(fetcher, persister);

        when(fetcher.fetch(barCode))
                .thenReturn(Observable.just(NETWORK));

        String value = simpleStore.get(barCode).toBlocking().first();
        verify(fetcher, times(1)).fetch(barCode);
        verify(persister, times(1)).write(barCode, NETWORK);
        verify(persister, times(2)).read(barCode);
        assertThat(value).isEqualTo(NETWORK);


        value = simpleStore.get(barCode).toBlocking().first();
        verify(persister, times(2)).read(barCode);
        verify(persister, times(1)).write(barCode, NETWORK);
        verify(fetcher, times(1)).fetch(barCode);

        assertThat(value).isEqualTo(NETWORK);
    }


    @Test
    public void testEquivalence() {
        Cache<BarCode, String> cache = CacheBuilder.newBuilder()
                .maximumSize(1)
{'fraction_non_alphanumeric': 0.11935120131841444, 'fraction_numerical': 0.002949084916298031, 'mean_word_length': 2.4897094430992737, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3957469', 'n_tokens_mistral': 3319, 'n_tokens_neox': 3214, 'n_words': 524}