Search in sources :

Example 6 with Mock

use of io.rx_cache2.internal.Mock in project RxCache by VictorAlbertos.

the class ProcessorProvidersTest method getSubscriberCompleted.

private TestObserver getSubscriberCompleted(boolean hasCache, final boolean evictCache, boolean detailResponse, Loader loader, boolean useExpiredDataIfLoaderNotAvailable) {
    Observable observable;
    switch(loader) {
        case VALID:
            observable = Observable.just(new Mock("message"));
            break;
        case NULL:
            observable = Observable.error(new RuntimeException("No data"));
            break;
        default:
            observable = Observable.create(new ObservableOnSubscribe<Object>() {

                @Override
                public void subscribe(ObservableEmitter<Object> e) throws Exception {
                    throw new RuntimeException("error");
                }
            });
            break;
    }
    ConfigProvider configProvider = new ConfigProvider("mockKey", null, null, detailResponse, true, false, "", "", observable, new EvictProvider(evictCache));
    if (hasCache)
        twoLayersCacheMock.save("mockKey", "", "", new Mock("message"), configProvider.getLifeTimeMillis(), configProvider.isExpirable(), configProvider.isEncrypted());
    processorProvidersUT = new ProcessorProvidersBehaviour(twoLayersCacheMock, useExpiredDataIfLoaderNotAvailable, evictExpiredRecordsPersistence, getDeepCopy, doMigrations);
    TestObserver observerMock = processorProvidersUT.getData(configProvider).test();
    observerMock.awaitTerminalEvent();
    return observerMock;
}
Also used : ObservableOnSubscribe(io.reactivex.ObservableOnSubscribe) ConfigProvider(io.rx_cache2.ConfigProvider) EvictProvider(io.rx_cache2.EvictProvider) Observable(io.reactivex.Observable) ObservableEmitter(io.reactivex.ObservableEmitter) TestObserver(io.reactivex.observers.TestObserver)

Example 7 with Mock

use of io.rx_cache2.internal.Mock in project RxCache by VictorAlbertos.

the class EvictExpiredRecordsPersistenceTest method Evict_Just_Expired_Records.

@Test
public void Evict_Just_Expired_Records() {
    int recordsCount = 100;
    for (int i = 0; i < recordsCount / 2; i++) {
        twoLayersCache.save(i + "_expired", "", "", new Mock(i + "_expired"), ONE_SECOND_LIFE, true, false);
        twoLayersCache.save(i + "_live", "", "", new Mock(i + "_live"), THIRTY_SECOND_LIFE, true, false);
    }
    waitTime(MORE_THAN_ONE_SECOND_LIFE);
    assertThat(disk.allKeys().size(), is(recordsCount));
    TestObserver<Integer> testObserver = evictExpiredRecordsPersistenceUT.startEvictingExpiredRecords().test();
    testObserver.awaitTerminalEvent();
    testObserver.assertNoErrors();
    List<String> allKeys = disk.allKeys();
    assertThat(allKeys.size(), is(recordsCount / 2));
    for (String key : allKeys) {
        key = key.substring(0, key.indexOf("$"));
        Record<Mock> record = twoLayersCache.retrieve(key, "", "", false, THIRTY_SECOND_LIFE, false);
        assert (record.getData().getMessage().contains("live"));
        assert (!record.getData().getMessage().contains("expired"));
    }
}
Also used : Mock(io.rx_cache2.internal.Mock) BaseTest(io.rx_cache2.internal.common.BaseTest) Test(org.junit.Test)

Example 8 with Mock

use of io.rx_cache2.internal.Mock in project RxCache by VictorAlbertos.

the class TwoLayersCacheTest method When_Expired_Date_And_Not_Use_ExpiredDataIfLoaderNotAvailable_Then_Get_Null.

@Test
public void When_Expired_Date_And_Not_Use_ExpiredDataIfLoaderNotAvailable_Then_Get_Null() {
    twoLayersCacheUT = new io.rx_cache2.internal.cache.TwoLayersCache(evictRecord(memory), retrieveRecord(memory), saveRecord(memory));
    twoLayersCacheUT.save(PROVIDER_KEY, "1", "", new Mock(MOCK_VALUE), DUMMY_LIFE_TIME, true, false);
    waitTime(MORE_THAN_ONE_SECOND_LIFE);
    Record<Mock> record = twoLayersCacheUT.retrieve(PROVIDER_KEY, "1", "", false, ONE_SECOND_LIFE, false);
    assertThat(record, is(nullValue()));
}
Also used : Mock(io.rx_cache2.internal.Mock) BaseTest(io.rx_cache2.internal.common.BaseTest) Test(org.junit.Test)

Example 9 with Mock

use of io.rx_cache2.internal.Mock in project RxCache by VictorAlbertos.

the class TwoLayersCacheTest method When_Save_And_Dynamic_Key_Record_Has_Expired_Only_Get_Null_For_Dynamic_Key.

@Test
public void When_Save_And_Dynamic_Key_Record_Has_Expired_Only_Get_Null_For_Dynamic_Key() {
    twoLayersCacheUT = new io.rx_cache2.internal.cache.TwoLayersCache(evictRecord(memory), retrieveRecord(memory), saveRecord(memory));
    twoLayersCacheUT.save(PROVIDER_KEY, "1", "", new Mock(MOCK_VALUE), ONE_SECOND_LIFE, true, false);
    twoLayersCacheUT.save(PROVIDER_KEY, "2", "", new Mock(MOCK_VALUE), ONE_SECOND_LIFE, true, false);
    waitTime(MORE_THAN_ONE_SECOND_LIFE);
    Record<Mock> record = twoLayersCacheUT.retrieve(PROVIDER_KEY, "1", "", false, ONE_SECOND_LIFE, false);
    assertThat(record, is(nullValue()));
    record = twoLayersCacheUT.retrieve(PROVIDER_KEY, "1", "", false, THREE_SECOND_LIFE, false);
    assertThat(record, is(nullValue()));
    record = twoLayersCacheUT.retrieve(PROVIDER_KEY, "2", "", false, THREE_SECOND_LIFE, false);
    assertNotNull(record);
}
Also used : Mock(io.rx_cache2.internal.Mock) BaseTest(io.rx_cache2.internal.common.BaseTest) Test(org.junit.Test)

Example 10 with Mock

use of io.rx_cache2.internal.Mock in project RxCache by VictorAlbertos.

the class TwoLayersCacheTest method When_Save_And_Dynamic_Key_Group_Record_Has_Expired_Only_Get_Null_For_Dynamic_Key.

@Test
public void When_Save_And_Dynamic_Key_Group_Record_Has_Expired_Only_Get_Null_For_Dynamic_Key() {
    twoLayersCacheUT = new io.rx_cache2.internal.cache.TwoLayersCache(evictRecord(memory), retrieveRecord(memory), saveRecord(memory));
    twoLayersCacheUT.save(PROVIDER_KEY, "1", "1", new Mock(MOCK_VALUE), ONE_SECOND_LIFE, true, false);
    twoLayersCacheUT.save(PROVIDER_KEY, "1", "2", new Mock(MOCK_VALUE), DUMMY_LIFE_TIME, true, false);
    twoLayersCacheUT.save(PROVIDER_KEY, "2", "1", new Mock(MOCK_VALUE), DUMMY_LIFE_TIME, true, false);
    twoLayersCacheUT.save(PROVIDER_KEY, "2", "2", new Mock(MOCK_VALUE), DUMMY_LIFE_TIME, true, false);
    waitTime(MORE_THAN_ONE_SECOND_LIFE);
    Record<Mock> record = twoLayersCacheUT.retrieve(PROVIDER_KEY, "1", "1", false, ONE_SECOND_LIFE, false);
    assertThat(record, is(nullValue()));
    record = twoLayersCacheUT.retrieve(PROVIDER_KEY, "1", "1", false, THREE_SECOND_LIFE, false);
    assertThat(record, is(nullValue()));
    record = twoLayersCacheUT.retrieve(PROVIDER_KEY, "1", "2", false, THREE_SECOND_LIFE, false);
    assertNotNull(record);
    record = twoLayersCacheUT.retrieve(PROVIDER_KEY, "2", "1", false, THREE_SECOND_LIFE, false);
    assertNotNull(record);
    record = twoLayersCacheUT.retrieve(PROVIDER_KEY, "2", "2", false, THREE_SECOND_LIFE, false);
    assertNotNull(record);
}
Also used : Mock(io.rx_cache2.internal.Mock) BaseTest(io.rx_cache2.internal.common.BaseTest) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)22 BaseTest (io.rx_cache2.internal.common.BaseTest)21 Mock (io.rx_cache2.internal.Mock)20 TestObserver (io.reactivex.observers.TestObserver)5 Reply (io.rx_cache2.Reply)3 ArrayList (java.util.ArrayList)3 EvictProvider (io.rx_cache2.EvictProvider)2 Observable (io.reactivex.Observable)1 ObservableEmitter (io.reactivex.ObservableEmitter)1 ObservableOnSubscribe (io.reactivex.ObservableOnSubscribe)1 ConfigProvider (io.rx_cache2.ConfigProvider)1 Record (io.rx_cache2.internal.Record)1 Type (java.lang.reflect.Type)1 List (java.util.List)1 DataPoint (org.junit.experimental.theories.DataPoint)1