Search in sources :

Example 6 with Record

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

the class DeleteRecordMatchingClassNameTest method When_Class_Matches_Delete_Record_2_List.

@Test
public void When_Class_Matches_Delete_Record_2_List() {
    disk.saveRecord(Mock1.KEY, new Record(Arrays.asList(new Mock1()), true, 0l), false, null);
    disk.saveRecord(Mock2.KEY, new Record(Arrays.asList(new Mock2()), true, 0l), false, null);
    assertThat(disk.allKeys().size(), is(2));
    deleteRecordMatchingClassNameUT.with(Arrays.<Class>asList(Mock1.class, Mock2.class)).react().test().awaitTerminalEvent();
    assertThat(disk.allKeys().size(), is(0));
}
Also used : Record(io.rx_cache2.internal.Record) BaseTest(io.rx_cache2.internal.common.BaseTest) Test(org.junit.Test)

Example 7 with Record

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

the class ProcessorProvidersBehaviour method getDataFromLoader.

private Observable<Reply> getDataFromLoader(final io.rx_cache2.ConfigProvider configProvider, final Record record) {
    return configProvider.getLoaderObservable().map(new Function<Object, Reply>() {

        @Override
        public Reply apply(Object data) throws Exception {
            boolean useExpiredData = configProvider.useExpiredDataIfNotLoaderAvailable() != null ? configProvider.useExpiredDataIfNotLoaderAvailable() : useExpiredDataIfLoaderNotAvailable;
            if (data == null && useExpiredData && record != null) {
                return new Reply(record.getData(), record.getSource(), configProvider.isEncrypted());
            }
            clearKeyIfNeeded(configProvider);
            if (data == null) {
                throw new io.rx_cache2.RxCacheException(io.rx_cache2.internal.Locale.NOT_DATA_RETURN_WHEN_CALLING_OBSERVABLE_LOADER + " " + configProvider.getProviderKey());
            }
            twoLayersCache.save(configProvider.getProviderKey(), configProvider.getDynamicKey(), configProvider.getDynamicKeyGroup(), data, configProvider.getLifeTimeMillis(), configProvider.isExpirable(), configProvider.isEncrypted());
            return new Reply(data, Source.CLOUD, configProvider.isEncrypted());
        }
    }).onErrorReturn(new Function<Object, Object>() {

        @Override
        public Object apply(Object o) throws Exception {
            clearKeyIfNeeded(configProvider);
            boolean useExpiredData = configProvider.useExpiredDataIfNotLoaderAvailable() != null ? configProvider.useExpiredDataIfNotLoaderAvailable() : useExpiredDataIfLoaderNotAvailable;
            if (useExpiredData && record != null) {
                return new Reply(record.getData(), record.getSource(), configProvider.isEncrypted());
            }
            throw new io.rx_cache2.RxCacheException(io.rx_cache2.internal.Locale.NOT_DATA_RETURN_WHEN_CALLING_OBSERVABLE_LOADER + " " + configProvider.getProviderKey(), (Throwable) o);
        }
    });
}
Also used : Function(io.reactivex.functions.Function) Reply(io.rx_cache2.Reply)

Example 8 with Record

use of io.rx_cache2.internal.Record 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 9 with Record

use of io.rx_cache2.internal.Record 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 10 with Record

use of io.rx_cache2.internal.Record 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)

Aggregations

BaseTest (io.rx_cache2.internal.common.BaseTest)20 Test (org.junit.Test)20 Mock (io.rx_cache2.internal.Mock)17 Record (io.rx_cache2.internal.Record)7 ObservableEmitter (io.reactivex.ObservableEmitter)1 ObservableOnSubscribe (io.reactivex.ObservableOnSubscribe)1 Function (io.reactivex.functions.Function)1 Reply (io.rx_cache2.Reply)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 DataPoint (org.junit.experimental.theories.DataPoint)1