@Test public void testPutAdvicedAgainstEvictionComputesMetadata() throws Exception { EhcacheOffHeapBackingMap<String, String> segment = createTestSegment((key, value) -> "please-do-not-evict-me".equals(key)); try { segment.put("please-do-not-evict-me", "value"); assertThat(getMetadata("please-do-not-evict-me", EhcacheSegmentFactory.EhcacheSegment.ADVISED_AGAINST_EVICTION, segment), is(EhcacheSegmentFactory.EhcacheSegment.ADVISED_AGAINST_EVICTION)); } finally { destroySegment(segment); } }
@Test public void testComputeIfPresentAndPinDoesPin() throws Exception { EhcacheOffHeapBackingMap<String, String> segment = createTestSegment(); final String value = "value"; segment.put("key", value); segment.computeIfPresentAndPin("key", (s, s2) -> { assertThat(s2, is(value)); return value; }); assertThat(isPinned("key", segment), is(true)); }
@Test public void testComputeFunctionReturnsSamePins() throws Exception { EhcacheOffHeapBackingMap<String, String> segment = createTestSegment(); try { segment.put("key", "value"); String value = segment.compute("key", (s, s2) -> s2, true); assertThat(value, is("value")); assertThat(isPinned("key", segment), is(true)); } finally { destroySegment(segment); } }
@Test public void testComputeIfPresentAndPinReplacesAndPins() throws Exception { EhcacheOffHeapBackingMap<String, String> segment = createTestSegment(); final String value = "value"; final String newValue = "newValue"; segment.put("key", value); segment.computeIfPresentAndPin("key", (s, s2) -> { assertThat(s2, is(value)); return newValue; }); assertThat(isPinned("key", segment), is(true)); assertThat(segment.get("key"), is(newValue)); }
@Test public void testComputeFunctionReturnsSameNoPin() throws Exception { EhcacheOffHeapBackingMap<String, String> segment = createTestSegment(); try { segment.put("key", "value"); String value = segment.compute("key", (s, s2) -> s2, false); assertThat(value, is("value")); assertThat(isPinned("key", segment), is(false)); } finally { destroySegment(segment); } }
@Test public void testComputeFunctionReturnsDifferentNoPin() throws Exception { EhcacheOffHeapBackingMap<String, String> segment = createTestSegment(); try { segment.put("key", "value"); String value = segment.compute("key", (s, s2) -> "otherValue", false); assertThat(value, is("otherValue")); assertThat(isPinned("key", segment), is(false)); } finally { destroySegment(segment); } }
@Test public void testComputeFunctionReturnsDifferentPins() throws Exception { EhcacheOffHeapBackingMap<String, String> segment = createTestSegment(); try { segment.put("key", "value"); String value = segment.compute("key", (s, s2) -> "otherValue", true); assertThat(value, is("otherValue")); assertThat(isPinned("key", segment), is(true)); } finally { destroySegment(segment); } }
@Test public void testComputeIfPresentReturnsNullRemovesMapping() throws Exception { EhcacheOffHeapBackingMap<String, String> segment = createTestSegment(); try { segment.put("key", "value"); String value = segment.computeIfPresent("key", (s, s2) -> null); assertThat(segment.containsKey("key"), is(false)); } finally { destroySegment(segment); } }
@Test public void testComputeIfPresentReturnsDifferentValue() throws Exception { EhcacheOffHeapBackingMap<String, String> segment = createTestSegment(); try { segment.put("key", "value"); String value = segment.computeIfPresent("key", (s, s2) -> "newValue"); assertThat(segment.get("key"), is(value)); } finally { destroySegment(segment); } }
@Test public void testComputeIfPresentReturnsSameValue() throws Exception { EhcacheOffHeapBackingMap<String, String> segment = createTestSegment(); try { segment.put("key", "value"); String value = segment.computeIfPresent("key", (s, s2) -> s2); assertThat(segment.get("key"), is(value)); } finally { destroySegment(segment); } }
@Test public void testComputeIfPinnedNoOpUnpinned() throws Exception { EhcacheOffHeapBackingMap<String, String> segment = createTestSegment(); try { segment.put("key", "value"); boolean result = segment.computeIfPinned("key", (s, s2) -> { fail("Method should not be invoked"); return null; }, s -> { fail("Method should not be invoked"); return false; }); assertThat(isPinned("key", segment), is(false)); assertThat(result, is(false)); } finally { destroySegment(segment); } }