@Test public void testConcurrencyKeysEqualForDifferentCachesSameKey() throws Exception { ConcurrentEntityMessage m1 = new ServerStoreOpMessage.AppendMessage(1L, createPayload(1L)); ConcurrentEntityMessage m2 = new ServerStoreOpMessage.GetAndAppendMessage(1L, createPayload(1L)); assertThat(m1.concurrencyKey(), is(m2.concurrencyKey())); }
private LockSuccess getLockSuccessResponse() { ByteBuffer[] buffers = new ByteBuffer[3]; for (int i = 1; i <= 3; i++) { buffers[i-1] = Util.createPayload(i); } Chain chain = Util.getChain(false, buffers); return EhcacheEntityResponse.lockSuccess(chain); }
@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 testGetResponseCodec() { EhcacheEntityResponse getResponse = getResponse(getChain(false, createPayload(1L), createPayload(11L), createPayload(111L))); EhcacheEntityResponse decoded = RESPONSE_CODEC.decode(RESPONSE_CODEC.encode(getResponse)); Chain decodedChain = ((EhcacheEntityResponse.GetResponse) decoded).getChain(); Util.assertChainHas(decodedChain, 1L, 11L, 111L); }
@Test public void testConcurrencyKeysEqualForSameCacheAndKey() throws Exception { ConcurrentEntityMessage m1 = new ServerStoreOpMessage.AppendMessage(1L, createPayload(1L)); ConcurrentEntityMessage m2 = new ServerStoreOpMessage.GetAndAppendMessage(1L, createPayload(1L)); ConcurrentEntityMessage m3 = new ServerStoreOpMessage.ReplaceAtHeadMessage(1L, getChain(Collections.<Element>emptyList()), getChain(Collections.<Element>emptyList())); assertThat(m1.concurrencyKey(), is(m2.concurrencyKey())); assertThat(m2.concurrencyKey(), is(m3.concurrencyKey())); }
@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 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 testConcurrencyKeysNotEqualForDifferentCachesAndKeys() throws Exception { ConcurrentEntityMessage m1 = new ServerStoreOpMessage.AppendMessage(1L, createPayload(1L)); ConcurrentEntityMessage m2 = new ServerStoreOpMessage.GetAndAppendMessage(2L, createPayload(1L)); ConcurrentEntityMessage m3 = new ServerStoreOpMessage.AppendMessage(3L, createPayload(1L)); assertThat(m1.concurrencyKey(), not(m2.concurrencyKey())); assertThat(m1.concurrencyKey(), not(m3.concurrencyKey())); }
@Test public void testResolveRequest() throws Exception { long hash = 42L; EhcacheEntityResponse.ResolveRequest response = new EhcacheEntityResponse.ResolveRequest(hash, getChain(false, createPayload(1L), createPayload(11L), createPayload(111L))); byte[] encoded = RESPONSE_CODEC.encode(response); EhcacheEntityResponse.ResolveRequest decodedResponse = (EhcacheEntityResponse.ResolveRequest) RESPONSE_CODEC.decode(encoded); assertThat(decodedResponse.getResponseType(), is(EhcacheResponseType.RESOLVE_REQUEST)); assertThat(decodedResponse.getKey(), is(42L)); Util.assertChainHas(decodedResponse.getChain(), 1L, 11L, 111L); }
@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 untrackedMessageAreNotStored() throws Exception { // Nothing tracked assertThat(activeMessageHandler.getTrackedClients().count()).isZero(); List<Element> elements = new ArrayList<>(1); elements.add(getElement(createPayload(44L))); // Send a replace message, those are not tracked storeProxy.replaceAtHead(44L, getChain(elements), getChain(new ArrayList<>(0))); // Not tracked as well storeProxy.get(42L); assertThat(activeMessageHandler.getTrackedClients().count()).isZero(); }
@Test public void messageTrackedAndRemovedByPassiveWhenClientLeaves() throws Exception { assertThat(passiveMessageHandler.getTrackedClients().count()).isZero(); // nothing tracked right now storeProxy.getAndAppend(42L, createPayload(42L)); Map<Long, EhcacheEntityResponse> responses = passiveMessageHandler.getTrackedResponsesForSegment(KEY_ENDS_UP_IN_SEGMENT_11, passiveMessageHandler.getTrackedClients().findFirst().get()); assertThat(responses).hasSize(1); // should now track one message service.stop(); // stop the service. It will remove the client activeEntity.notifyDestroyed(passiveMessageHandler.getTrackedClients().iterator().next()); passiveEntity.notifyDestroyed(passiveMessageHandler.getTrackedClients().iterator().next()); // Notify that the client was removed. A real clustered server will do that. But the Passthrough doesn't. So we simulate it waitForPredicate(e -> activeEntity.getConnectedClients().size() > 0, 2000); // wait for the client to be removed, might be async, so we wait assertThat(passiveMessageHandler.getTrackedClients().count()).isZero(); // all tracked messages for this client should have been removed }
@Test public void messageTrackedAndRemovedWhenClientLeaves() throws Exception { assertThat(activeMessageHandler.getTrackedClients().count()).isZero(); // no client tracked storeProxy.getAndAppend(42L, createPayload(42L)); Map<Long, EhcacheEntityResponse> responses = activeMessageHandler.getTrackedResponsesForSegment(KEY_ENDS_UP_IN_SEGMENT_11, activeMessageHandler.getTrackedClients().findFirst().get()); assertThat(responses).hasSize(1); // should now track one message assertThat(activeEntity.getConnectedClients()).hasSize(1); // make sure we currently have one client attached service.stop(); // stop the service. It will remove the client activeEntity.notifyDestroyed(activeMessageHandler.getTrackedClients().iterator().next()); // Notify that the client was removed. A real clustered server will do that. But the Passthrough doesn't. So we simulate it waitForPredicate(e -> activeEntity.getConnectedClients().size() > 0, 2000); // wait for the client to be removed, might be async, so we wait assertThat(activeMessageHandler.getTrackedClients().count()).isZero(); // all tracked messages for this client should have been removed }
@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 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 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 testAppendMessageCodec() { ServerStoreOpMessage.AppendMessage appendMessage = new ServerStoreOpMessage.AppendMessage(1L, createPayload(1L)); byte[] encoded = STORE_OP_CODEC.encode(appendMessage); EhcacheEntityMessage decodedMsg = STORE_OP_CODEC.decode(appendMessage.getMessageType(), wrap(encoded)); ServerStoreOpMessage.AppendMessage decodedAppendMessage = (ServerStoreOpMessage.AppendMessage) decodedMsg; assertThat(decodedAppendMessage.getKey(), is(1L)); assertThat(readPayLoad(decodedAppendMessage.getPayload()), is(1L)); assertThat(decodedAppendMessage.getMessageType(), is(EhcacheMessageType.APPEND)); }
@Test public void testGetAndAppendMessageCodec() { ServerStoreOpMessage getAndAppendMessage = new ServerStoreOpMessage.GetAndAppendMessage(10L, createPayload(10L)); byte[] encoded = STORE_OP_CODEC.encode(getAndAppendMessage); EhcacheEntityMessage decodedMsg = STORE_OP_CODEC.decode(getAndAppendMessage.getMessageType(), wrap(encoded)); ServerStoreOpMessage.GetAndAppendMessage decodedGetAndAppendMessage = (ServerStoreOpMessage.GetAndAppendMessage) decodedMsg; assertThat(decodedGetAndAppendMessage.getKey(), is(10L)); assertThat(readPayLoad(decodedGetAndAppendMessage.getPayload()), is(10L)); assertThat(decodedGetAndAppendMessage.getMessageType(), is(EhcacheMessageType.GET_AND_APPEND)); }
@Test public void testLockResponse() { EhcacheEntityResponse.LockSuccess lockSuccess = new EhcacheEntityResponse.LockSuccess(getChain(false, createPayload(1L), createPayload(10L))); byte[] sucessEncoded = RESPONSE_CODEC.encode(lockSuccess); EhcacheEntityResponse.LockSuccess successDecoded = (EhcacheEntityResponse.LockSuccess) RESPONSE_CODEC.decode(sucessEncoded); assertThat(successDecoded.getResponseType(), is(EhcacheResponseType.LOCK_SUCCESS)); Util.assertChainHas(successDecoded.getChain(), 1L, 10L); EhcacheEntityResponse.LockFailure lockFailure = EhcacheEntityResponse.lockFailure(); byte[] failureEncoded = RESPONSE_CODEC.encode(lockFailure); EhcacheEntityResponse.LockFailure failureDecoded = (EhcacheEntityResponse.LockFailure) RESPONSE_CODEC.decode(failureEncoded); assertThat(failureDecoded.getResponseType(), is(EhcacheResponseType.LOCK_FAILURE)); } }
@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)); }