### Question:
CachedFlamdexReader implements FlamdexReader, MetricCache { @Override public IntValueLookup getMetric(String metric) throws FlamdexOutOfMemoryException { return metricCache.getMetric(metric); } CachedFlamdexReader(final MemoryReservationContext memory,
                                  final FlamdexReader wrapped,
                                  final @Nullable Closeable readLockRef,
                                  final @Nullable String indexName,
                                  final @Nullable String shardName,
                                  final @Nullable ImhotepMemoryCache<MetricKey, IntValueLookup> freeCache); @Override Collection<String> getIntFields(); @Override Collection<String> getStringFields(); @Override int getNumDocs(); @Override String getDirectory(); @Override DocIdStream getDocIdStream(); @Override IntTermIterator getIntTermIterator(String field); @Override StringTermIterator getStringTermIterator(String field); @Override IntTermDocIterator getIntTermDocIterator(final String field); @Override StringTermDocIterator getStringTermDocIterator(final String field); @Override long getIntTotalDocFreq(String field); @Override long getStringTotalDocFreq(String field); @Override Collection<String> getAvailableMetrics(); @Override IntValueLookup getMetric(String metric); StringValueLookup getStringLookup(final String field); @Override List<ImhotepStatusDump.MetricDump> getMetricDump(); @Override Set<String> getLoadedMetrics(); @Override long memoryRequired(String metric); @Override void close();  }### Answer:
@Test public void testMemory3() throws FlamdexOutOfMemoryException { final ImhotepMemoryCache<MetricKey, IntValueLookup> cache = new ImhotepMemoryCache<MetricKey, IntValueLookup>(); FlamdexReader r = new SillyFlamdexReader(); MemoryReserver memory = new CachedMemoryReserver(new ImhotepMemoryPool(6L), cache); CachedFlamdexReader cfr = new CachedFlamdexReader(new MemoryReservationContext(memory), r, null, "test", "test", cache); cfr.getMetric("m1"); assertEquals(5L, memory.usedMemory()); cfr.getMetric("m1"); assertEquals(5L, memory.usedMemory()); try { cfr.getMetric("m2"); assertTrue(false); } catch (FlamdexOutOfMemoryException e) { assertEquals(0L, cache.memoryUsed()); assertEquals(5L, memory.usedMemory()); } }
@Test public void testLookupWrap() throws FlamdexOutOfMemoryException { final ImhotepMemoryCache<MetricKey, IntValueLookup> cache = new ImhotepMemoryCache<MetricKey, IntValueLookup>(); FlamdexReader r = new SillyFlamdexReader(); CachedFlamdexReader cfr = new CachedFlamdexReader(new MemoryReservationContext(new CachedMemoryReserver(new ImhotepMemoryPool(Long.MAX_VALUE), cache)), r, null, "test", "test", cache); IntValueLookup l = cfr.getMetric("m1"); int[] docIds = {0, 2, 4, 9000, Integer.MIN_VALUE}; long[] values = new long[docIds.length]; l.lookup(docIds, values, 3); assertEquals(Arrays.asList(1L, 9L, 25L, 0L, 0L), Longs.asList(values)); }