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();
}
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();
}
Aggregations