Search in sources :

Example 41 with Record

use of com.apollographql.apollo.cache.normalized.Record in project apollo-android by apollographql.

the class LruNormalizedCacheTest method testEviction_recordChange.

@Test
public void testEviction_recordChange() {
    LruNormalizedCache lruCache = new LruNormalizedCacheFactory(EvictionPolicy.builder().maxSizeBytes(2000).build()).create(basicFieldAdapter);
    Record.Builder testRecord1Builder = Record.builder("key1");
    testRecord1Builder.addField("a", new String(new byte[10], Charset.defaultCharset()));
    Record testRecord1 = testRecord1Builder.build();
    Record.Builder testRecord2Builder = Record.builder("key2");
    testRecord2Builder.addField("a", new String(new byte[10], Charset.defaultCharset()));
    Record testRecord2 = testRecord2Builder.build();
    Record.Builder testRecord3Builder = Record.builder("key3");
    testRecord3Builder.addField("a", new String(new byte[10], Charset.defaultCharset()));
    Record testRecord3 = testRecord3Builder.build();
    List<Record> records = Arrays.asList(testRecord1, testRecord2, testRecord3);
    lruCache.merge(records, CacheHeaders.NONE);
    // All records should present
    assertThat(lruCache.loadRecord("key1", CacheHeaders.NONE)).isNotNull();
    assertThat(lruCache.loadRecord("key2", CacheHeaders.NONE)).isNotNull();
    assertThat(lruCache.loadRecord("key3", CacheHeaders.NONE)).isNotNull();
    Record.Builder largeTestRecordBuilder = Record.builder("key1");
    largeTestRecordBuilder.addField("a", new String(new byte[2000], Charset.defaultCharset()));
    Record largeTestRecord = largeTestRecordBuilder.build();
    lruCache.merge(largeTestRecord, CacheHeaders.NONE);
    // The large record (Record 1) should be evicted. the other small records should remain.
    assertThat(lruCache.loadRecord("key1", CacheHeaders.NONE)).isNull();
    assertThat(lruCache.loadRecord("key2", CacheHeaders.NONE)).isNotNull();
    assertThat(lruCache.loadRecord("key3", CacheHeaders.NONE)).isNotNull();
}
Also used : Record(com.apollographql.apollo.cache.normalized.Record) Test(org.junit.Test)

Example 42 with Record

use of com.apollographql.apollo.cache.normalized.Record in project apollo-android by apollographql.

the class LruNormalizedCacheTest method testEviction.

@Test
public void testEviction() {
    LruNormalizedCache lruCache = new LruNormalizedCacheFactory(EvictionPolicy.builder().maxSizeBytes(2000).build()).create(basicFieldAdapter);
    Record.Builder testRecord1Builder = Record.builder("key1");
    testRecord1Builder.addField("a", new String(new byte[1100], Charset.defaultCharset()));
    Record testRecord1 = testRecord1Builder.build();
    Record.Builder testRecord2Builder = Record.builder("key2");
    testRecord2Builder.addField("a", new String(new byte[1100], Charset.defaultCharset()));
    Record testRecord2 = testRecord2Builder.build();
    Record.Builder testRecord3Builder = Record.builder("key3");
    testRecord3Builder.addField("a", new String(new byte[10], Charset.defaultCharset()));
    Record testRecord3 = testRecord3Builder.build();
    List<Record> records = Arrays.asList(testRecord1, testRecord2, testRecord3);
    lruCache.merge(records, CacheHeaders.NONE);
    // Cache does not reveal exactly how it handles eviction, but appears
    // to evict more than is strictly necessary. Regardless, any sane eviction
    // strategy should leave the third record in this test case, and evict the first record.
    assertThat(lruCache.loadRecord("key1", CacheHeaders.NONE)).isNull();
    assertThat(lruCache.loadRecord("key3", CacheHeaders.NONE)).isNotNull();
}
Also used : Record(com.apollographql.apollo.cache.normalized.Record) Test(org.junit.Test)

Aggregations

Record (com.apollographql.apollo.cache.normalized.Record)42 Test (org.junit.Test)35 CacheReference (com.apollographql.apollo.cache.normalized.CacheReference)15 NormalizedCache (com.apollographql.apollo.cache.normalized.NormalizedCache)7 Nonnull (javax.annotation.Nonnull)5 CacheKey (com.apollographql.apollo.cache.normalized.CacheKey)4 CacheHeaders (com.apollographql.apollo.cache.CacheHeaders)3 EpisodeHeroNameQuery (com.apollographql.apollo.integration.normalizer.EpisodeHeroNameQuery)3 HeroAndFriendsNamesQuery (com.apollographql.apollo.integration.normalizer.HeroAndFriendsNamesQuery)3 HeroTypeDependentAliasedFieldQuery (com.apollographql.apollo.integration.normalizer.HeroTypeDependentAliasedFieldQuery)3 NonNull (android.support.annotation.NonNull)2 ApolloCacheHeaders (com.apollographql.apollo.cache.ApolloCacheHeaders)2 NormalizedCacheFactory (com.apollographql.apollo.cache.normalized.NormalizedCacheFactory)2 RecordFieldJsonAdapter (com.apollographql.apollo.cache.normalized.RecordFieldJsonAdapter)2 ApolloException (com.apollographql.apollo.exception.ApolloException)2 HeroParentTypeDependentFieldQuery (com.apollographql.apollo.integration.normalizer.HeroParentTypeDependentFieldQuery)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Set (java.util.Set)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2