@Override protected Iterator<Element> iterator() { return chain.iterator(); } }
public static boolean chainsEqual(Chain chain1, Chain chain2) { Iterator<Element> it1 = chain1.iterator(); Iterator<Element> it2 = chain2.iterator(); while (it1.hasNext() && it2.hasNext()) { Element next1 = it1.next(); Element next2 = it2.next(); if (!next1.getPayload().equals(next2.getPayload())) { return false; } } return !it1.hasNext() && !it2.hasNext(); }
private static void assertChainHas(Chain chain, long... payLoads) { Iterator<Element> elements = chain.iterator(); for (long payLoad : payLoads) { assertThat(readPayLoad(elements.next().getPayload()), is(Long.valueOf(payLoad))); } assertThat(elements.hasNext(), is(false)); }
public static void assertChainHas(Chain chain, long... payLoads) { Iterator<Element> elements = chain.iterator(); for (long payLoad : payLoads) { assertThat(readPayLoad(elements.next().getPayload()), is(Long.valueOf(payLoad))); } assertThat(elements.hasNext(), is(false)); } }
private static void assertChainHas(Chain chain, long... payLoads) { Iterator<Element> elements = chain.iterator(); for (long payLoad : payLoads) { assertThat(Util.readPayLoad(elements.next().getPayload()), is(Long.valueOf(payLoad))); } assertThat(elements.hasNext(), is(false)); } }
private static void assertChainAndReverseChainOnlyHave(Chain chain, long... payLoads) { Iterator<Element> elements = chain.iterator(); for (long payLoad : payLoads) { assertThat(readPayLoad(elements.next().getPayload()), is(Long.valueOf(payLoad))); } assertThat(elements.hasNext(), is(false)); Iterator<Element> reverseElements = chain.reverseIterator(); for (int i = payLoads.length -1; i >= 0; i--) { assertThat(readPayLoad(reverseElements.next().getPayload()), is(Long.valueOf(payLoads[i]))); } assertThat(reverseElements.hasNext(), is(false)); }
private static void assertSameSequenceChain(Chain original, Chain decoded) { Iterator<Element> decodedIterator = decoded.iterator(); for (Element element : original) { assertEquals(((SequencedElement) element).getSequenceNumber(), ((SequencedElement) decodedIterator.next()).getSequenceNumber()); } } }
@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 testChainWithSingleElement() { Chain chain = getChain(false, createPayload(1L)); assertThat(chain.isEmpty(), is(false)); Iterator<Element> chainIterator = chain.iterator(); assertThat(readPayLoad(chainIterator.next().getPayload()), is(1L)); assertThat(chainIterator.hasNext(), is(false)); Chain decoded = ChainCodec.decode(ChainCodec.encode(chain)); assertThat(decoded.isEmpty(), is(false)); chainIterator = decoded.iterator(); assertThat(readPayLoad(chainIterator.next().getPayload()), is(1L)); assertThat(chainIterator.hasNext(), is(false)); }
@Test public void testChainWithSingleSequencedElement() { Chain chain = getChain(true, createPayload(1L)); assertThat(chain.isEmpty(), is(false)); Iterator<Element> chainIterator = chain.iterator(); assertThat(readPayLoad(chainIterator.next().getPayload()), is(1L)); assertThat(chainIterator.hasNext(), is(false)); Chain decoded = ChainCodec.decode(ChainCodec.encode(chain)); assertThat(decoded.isEmpty(), is(false)); chainIterator = decoded.iterator(); assertThat(readPayLoad(chainIterator.next().getPayload()), is(1L)); assertThat(chainIterator.hasNext(), is(false)); assertSameSequenceChain(chain, decoded); }
@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 testResolveForMultipleOperationHasCorrectIsFirstAndTimeStamp() { Chain chain = getChainFromOperations( new PutOperation<>(1L, "Albin1", 0), new PutOperation<>(1L, "Albin2", 1), new RemoveOperation<>(1L, 2), new PutOperation<>(1L, "AlbinAfterRemove", 3)); ExpiryChainResolver<Long, String> resolver = new ExpiryChainResolver<>(codec, ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofHours(1))); ResolvedChain<Long, String> resolvedChain = resolver.resolve(chain, 1L, 3); Operation<Long, String> operation = codec.decode(resolvedChain.getCompactedChain().iterator().next().getPayload()); assertThat(operation.isExpiryAvailable(), is(true)); assertThat(operation.expirationTime(), is(TimeUnit.HOURS.toMillis(1) + 3)); try { operation.timeStamp(); fail(); } catch (Exception ex) { assertThat(ex.getMessage(), is("Timestamp not available")); } assertThat(resolvedChain.isCompacted(), is(true)); }
@Test public void testResolveForMultipleOperationHasCorrectIsFirstAndTimeStampWithExpiry() { Chain chain = getChainFromOperations( new PutOperation<>(1L, "Albin1", 0L), new PutOperation<>(1L, "Albin2", 1L), new PutOperation<>(1L, "Albin3", 2L), new PutOperation<>(1L, "Albin4", 3L) ); ExpiryChainResolver<Long, String> resolver = new ExpiryChainResolver<>(codec, ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofMillis(1L))); ResolvedChain<Long, String> resolvedChain = resolver.resolve(chain, 1L, 3L); Operation<Long, String> operation = codec.decode(resolvedChain.getCompactedChain().iterator().next().getPayload()); assertThat(operation.isExpiryAvailable(), is(true)); assertThat(operation.expirationTime(), is(4L)); try { operation.timeStamp(); fail(); } catch (Exception ex) { assertThat(ex.getMessage(), is("Timestamp not available")); } assertThat(resolvedChain.isCompacted(), is(true)); }
public static boolean chainsEqual(Chain chain1, Chain chain2) { Iterator<Element> it1 = chain1.iterator(); Iterator<Element> it2 = chain2.iterator(); while (it1.hasNext() && it2.hasNext()) { Element next1 = it1.next(); Element next2 = it2.next(); if (!next1.getPayload().equals(next2.getPayload())) { return false; } } return !it1.hasNext() && !it2.hasNext(); }