private Chain dropLastElement(Chain chain) { if (!chain.isEmpty()) { List<Element> elements = StreamSupport.stream(chain.spliterator(), false) .collect(Collectors.toList()); elements.remove(elements.size() - 1); // remove last return Util.getChain(elements); } else { return chain; } }
@Test public void testGetKeyNotPresent() throws Exception { ClusterTierClientEntity clientEntity = createClientEntity("testGetKeyNotPresent"); CommonServerStoreProxy serverStoreProxy = new CommonServerStoreProxy("testGetKeyNotPresent", clientEntity, mock(ServerCallback.class)); Chain chain = serverStoreProxy.get(1); assertThat(chain.isEmpty(), is(true)); }
@Test public void testGetNoMappingExists() throws Exception { ServerStore store = newStore(); Chain chain = store.get(1); assertThat(chain.isEmpty(), is(true)); assertThat(chain.iterator().hasNext(), is(false)); }
@Test public void testGetAndAppendKeyNotPresent() throws Exception { ClusterTierClientEntity clientEntity = createClientEntity("testGetAndAppendKeyNotPresent"); CommonServerStoreProxy serverStoreProxy = new CommonServerStoreProxy("testGetAndAppendKeyNotPresent", clientEntity, mock(ServerCallback.class)); Chain chain = serverStoreProxy.getAndAppend(4L, createPayload(4L)); assertThat(chain.isEmpty(), is(true)); chain = serverStoreProxy.get(4L); assertThat(chain.isEmpty(), is(false)); assertChainHas(chain, 4L); }
@Test public void testEmptyChain() { Chain decoded = ChainCodec.decode(ChainCodec.encode(getChain(false))); assertThat(decoded.isEmpty(), is(true)); }
@Test @SuppressWarnings("unchecked") public void testNullGetExpiryForCreation() { ExpiryPolicy<Long, String> expiry = mock(ExpiryPolicy.class); ExpiryChainResolver<Long, String> chainResolver = new ExpiryChainResolver<>(codec, expiry); when(expiry.getExpiryForCreation(anyLong(), anyString())).thenReturn(null); List<Operation<Long, String>> list = new ArrayList<>(); list.add(new PutOperation<>(1L, "Replaced", 10L)); Chain chain = getChainFromOperations(list); ResolvedChain<?, ?> resolvedChain = chainResolver.resolve(chain, 1L, timeSource.getTimeMillis()); assertTrue(resolvedChain.getCompactedChain().isEmpty()); assertThat(resolvedChain.isCompacted(), is(true)); }
@Test public void testChainWithMultipleElements() { Chain chain = getChain(false, createPayload(1L), createPayload(2L), createPayload(3L)); assertThat(chain.isEmpty(), is(false)); Util.assertChainHas(chain, 1L, 2L, 3L); Chain decoded = ChainCodec.decode(ChainCodec.encode(chain)); assertThat(decoded.isEmpty(), is(false)); Util.assertChainHas(decoded, 1L, 2L, 3L); }
@Test public void testAppendKeyNotPresent() throws Exception { ClusterTierClientEntity clientEntity = createClientEntity("testAppendKeyNotPresent"); CommonServerStoreProxy serverStoreProxy = new CommonServerStoreProxy("testAppendKeyNotPresent", clientEntity, mock(ServerCallback.class)); serverStoreProxy.append(2, createPayload(2)); Chain chain = serverStoreProxy.get(2); assertThat(chain.isEmpty(), is(false)); assertThat(readPayLoad(chain.iterator().next().getPayload()), is(2L)); }
@Test public void testChainWithMultipleSequencedElements() { Chain chain = getChain(true, createPayload(1L), createPayload(2L), createPayload(3L)); assertThat(chain.isEmpty(), is(false)); Util.assertChainHas(chain, 1L, 2L, 3L); Chain decoded = ChainCodec.decode(ChainCodec.encode(chain)); assertThat(decoded.isEmpty(), is(false)); Util.assertChainHas(decoded, 1L, 2L, 3L); assertSameSequenceChain(chain, decoded); }
@Test public void testGetMappingExists() throws Exception { ServerStore store = newStore(); populateStore(store); Chain chain = store.get(1); assertThat(chain.isEmpty(), is(false)); assertChainAndReverseChainOnlyHave(chain, 1); }
@Test public void testClear() throws Exception { ClusterTierClientEntity clientEntity = createClientEntity("testClear"); CommonServerStoreProxy serverStoreProxy = new CommonServerStoreProxy("testClear", clientEntity, mock(ServerCallback.class)); serverStoreProxy.append(1L, createPayload(100L)); serverStoreProxy.clear(); Chain chain = serverStoreProxy.get(1); assertThat(chain.isEmpty(), is(true)); }
@Test public void testAppendNoMappingExists() throws Exception { ServerStore store = newStore(); store.append(1, createPayload(1)); Chain chain = store.get(1); assertThat(chain.isEmpty(), is(false)); assertChainAndReverseChainOnlyHave(chain, 1); }
@Test public void testGetAndAppendNoMappingExists() throws Exception { ServerStore store = newStore(); Chain chain = store.getAndAppend(1, createPayload(1)); assertThat(chain.isEmpty(), is(true)); chain = store.get(1); assertChainAndReverseChainOnlyHave(chain, 1); }
@Test public void testAppendMappingExists() throws Exception { ServerStore store = newStore(); populateStore(store); store.append(2, createPayload(22)); Chain chain = store.get(2); assertThat(chain.isEmpty(), is(false)); assertChainAndReverseChainOnlyHave(chain, 2, 22); }
@Test public void testGetAfterMultipleAppendsOnSameKey() throws Exception { ClusterTierClientEntity clientEntity = createClientEntity("testGetAfterMultipleAppendsOnSameKey"); CommonServerStoreProxy serverStoreProxy = new CommonServerStoreProxy("testGetAfterMultipleAppendsOnSameKey", clientEntity, mock(ServerCallback.class)); serverStoreProxy.append(3L, createPayload(3L)); serverStoreProxy.append(3L, createPayload(33L)); serverStoreProxy.append(3L, createPayload(333L)); Chain chain = serverStoreProxy.get(3L); assertThat(chain.isEmpty(), is(false)); assertChainHas(chain, 3L, 33L, 333l); }
@Test public void testRemoveMissingKey() { OffHeapChainMap<String> map = new OffHeapChainMap<>(new UnlimitedPageSource(new OffHeapBufferSource()), StringPortability.INSTANCE, minPageSize, maxPageSize, steal); map.remove("foo"); assertThat(map.get("foo").isEmpty(), is(true)); }
@Test public void testGetAndAppendMultipleTimesOnSameKey() throws Exception { ClusterTierClientEntity clientEntity = createClientEntity("testGetAndAppendMultipleTimesOnSameKey"); CommonServerStoreProxy serverStoreProxy = new CommonServerStoreProxy("testGetAndAppendMultipleTimesOnSameKey", clientEntity, mock(ServerCallback.class)); serverStoreProxy.getAndAppend(5L, createPayload(5L)); serverStoreProxy.getAndAppend(5L, createPayload(55L)); serverStoreProxy.getAndAppend(5L, createPayload(555L)); Chain chain = serverStoreProxy.getAndAppend(5l, createPayload(5555L)); assertThat(chain.isEmpty(), is(false)); assertChainHas(chain, 5L, 55L, 555L); }
@Test public void testOversizeChainReplicationMessage() throws Exception { ClusterTierPassiveEntity passiveEntity = new ClusterTierPassiveEntity(defaultRegistry, defaultConfiguration, DEFAULT_MAPPER); passiveEntity.createNew(); TestInvokeContext context = new TestInvokeContext(); long key = 2L; Chain oversizeChain = Util.getChain(true, createPayload(key, 1024 * 1024)); PassiveReplicationMessage oversizeMsg = new PassiveReplicationMessage.ChainReplicationMessage(key, oversizeChain, 2L, 1L, (long) 3); passiveEntity.invokePassive(context, oversizeMsg); // Should be cleared, the value is oversize. assertThat(passiveEntity.getStateService().getStore(passiveEntity.getStoreIdentifier()).get(key).isEmpty(), is(true)); }
@Test public void testRemoveDoubleChain() { OffHeapChainMap<String> map = new OffHeapChainMap<>(new UnlimitedPageSource(new OffHeapBufferSource()), StringPortability.INSTANCE, minPageSize, maxPageSize, steal); map.append("foo", buffer(1)); map.append("foo", buffer(2)); assertThat(map.get("foo"), contains(element(1), element(2))); map.remove("foo"); assertThat(map.get("foo").isEmpty(), is(true)); }
@Test public void testRemoveSingleChain() { OffHeapChainMap<String> map = new OffHeapChainMap<>(new UnlimitedPageSource(new OffHeapBufferSource()), StringPortability.INSTANCE, minPageSize, maxPageSize, steal); map.append("foo", buffer(1)); map.append("bar", buffer(2)); assertThat(map.get("foo"), contains(element(1))); assertThat(map.get("bar"), contains(element(2))); map.remove("foo"); assertThat(map.get("foo").isEmpty(), is(true)); assertThat(map.get("bar"), contains(element(2))); }