private void tryDestroy() { try { boolean destroyed = reference.destroy(); if (destroyed) { LOGGER.debug("Destroyed lock entity " + reference.getName()); } } catch (EntityNotProvidedException e) { throw new AssertionError(e); } catch (EntityNotFoundException e) { // Nothing to do } catch (PermanentEntityException e) { LOGGER.error("Failed to destroy lock entity - server says it is permanent", e); throw new AssertionError(e); } }
private ClusterTierClientEntity fetchClusterTierClientEntity(String storeIdentifier, EntityRef<InternalClusterTierClientEntity, ClusterTierEntityConfiguration, ClusterTierUserData> entityRef) throws EntityNotFoundException { try { return entityRef.fetchEntity(new ClusterTierUserData(entityTimeouts, storeIdentifier)); } catch (EntityNotFoundException e) { throw e; } catch (EntityException e) { throw new AssertionError(e); } }
@Override public void create() throws EntityAlreadyExistsException { EntityRef<E, C, U> ref = getEntityRef(); try { while (true) { ref.create(configuration); try { ref.fetchEntity(null).close(); return; } catch (EntityNotFoundException e) { //continue; } } } catch (EntityNotProvidedException | EntityVersionMismatchException e) { LOGGER.error("Unable to create entity {} for id {}", entityType.getName(), entityIdentifier, e); throw new AssertionError(e); } catch (EntityConfigurationException e) { LOGGER.error("Unable to create entity - configuration exception", e); throw new AssertionError(e); } }
@Test public void testDestroyedEntityAllowsRecreation() throws Throwable { try (Connection client = CLUSTER.newConnection()) { EntityRef<ClusterTierManagerClientEntity, ClusterTierManagerConfiguration, Void> ref = getEntityRef(client); ref.create(blankConfiguration); ref.destroy(); ref.create(blankConfiguration); assertThat(ref.fetchEntity(null), not(nullValue())); } }
private VoltronReadWriteLockClient createClientEntity() { try { while (true) { try { reference.create(null); LOGGER.debug("Created lock entity " + reference.getName()); } catch (EntityAlreadyExistsException f) { //ignore } catch (EntityConfigurationException e) { LOGGER.error("Error creating lock entity - configuration exception", e); throw new AssertionError(e); } try { return reference.fetchEntity(null); } catch (EntityNotFoundException e) { //ignore } } } catch (EntityVersionMismatchException | EntityNotProvidedException e) { throw new IllegalStateException(e); } }
@Test public void testDestroy() throws Exception { ClusterTierManagerClientEntity mockEntity = mock(ClusterTierManagerClientEntity.class); when(entityRef.fetchEntity(null)).thenReturn(mockEntity); doReturn(Boolean.TRUE).when(entityRef).destroy(); when(getEntityRef(ClusterTierManagerClientEntity.class)).thenReturn(entityRef); addMockUnlockedLock(connection, "VoltronReadWriteLock-ClusterTierManagerClientEntityFactory-AccessLock-test"); ClusterTierManagerClientEntityFactory factory = new ClusterTierManagerClientEntityFactory(connection); factory.destroy("test"); verify(entityRef).destroy(); }
@Test public void testAbsentEntityCreationSucceeds() throws Throwable { try (Connection client = CLUSTER.newConnection()) { EntityRef<ClusterTierManagerClientEntity, ClusterTierManagerConfiguration, Void> ref = getEntityRef(client); ref.create(blankConfiguration); assertThat(ref.fetchEntity(null), not(Matchers.nullValue())); } }
private static <T extends Entity> void fetchEntity(Connection connection, Class<T> aClass, String myCacheManager) throws EntityNotFoundException, ConnectionException { try { connection.getEntityRef(aClass, EhcacheEntityVersion.ENTITY_VERSION, myCacheManager).fetchEntity(null).close(); } catch (EntityNotProvidedException | EntityVersionMismatchException e) { throw new AssertionError(e); } }
public void destroyClusteredStoreEntity(String clusterTierManagerIdentifier, String storeIdentifier) throws EntityNotFoundException, CachePersistenceException { EntityRef<InternalClusterTierClientEntity, ClusterTierEntityConfiguration, Void> entityRef; try { entityRef = connection.getEntityRef(InternalClusterTierClientEntity.class, ENTITY_VERSION, entityName(clusterTierManagerIdentifier, storeIdentifier)); if (!entityRef.destroy()) { throw new CachePersistenceException("Cannot destroy cluster tier '" + storeIdentifier + "': in use by other client(s)"); } } catch (EntityNotProvidedException | PermanentEntityException e) { throw new AssertionError(e); } }
@Test @Ignore @SuppressWarnings("try") public void testPresentEntityDestroyBlockedByHeldReferenceSucceeds() throws Throwable { try (Connection client = CLUSTER.newConnection()) { EntityRef<ClusterTierManagerClientEntity, ClusterTierManagerConfiguration, Void> ref = getEntityRef(client); ref.create(blankConfiguration); try (ClusterTierManagerClientEntity entity = ref.fetchEntity(null)) { ref.destroy(); } } }
@Override public E retrieve() throws EntityNotFoundException { try { return getEntityRef().fetchEntity(null); } catch (EntityVersionMismatchException e) { LOGGER.error("Unable to retrieve entity {} for id {}", entityType.getName(), entityIdentifier, e); throw new AssertionError(e); } }
@Test public void testWriteUnlockDestroysEntity() throws Exception { when(entityRef.fetchEntity(null)).thenReturn(client); when(connection.<VoltronReadWriteLockClient, Void, Void>getEntityRef(VoltronReadWriteLockClient.class, 1, "VoltronReadWriteLock-TestLock")).thenReturn(entityRef); VoltronReadWriteLock lock = new VoltronReadWriteLock(connection, "TestLock"); lock.writeLock().unlock(); verify(entityRef).destroy(); }
@Test public void testAbsentEntityDestroyFails() throws Throwable { try (Connection client = CLUSTER.newConnection()) { EntityRef<ClusterTierManagerClientEntity, ClusterTierManagerConfiguration, Void> ref = getEntityRef(client); try { ref.destroy(); fail("Expected EntityNotFoundException"); } catch (EntityNotFoundException e) { //expected } } }
@Test public void testPresentEntityDestroyNotBlockedByReleasedReferenceSucceeds() throws Throwable { try (Connection client = CLUSTER.newConnection()) { EntityRef<ClusterTierManagerClientEntity, ClusterTierManagerConfiguration, Void> ref = getEntityRef(client); ref.create(blankConfiguration); ref.fetchEntity(null).close(); ref.destroy(); } }
private static void addMockLock(Connection connection, String lockname, boolean result, Boolean ... results) throws Exception { VoltronReadWriteLockClient lock = mock(VoltronReadWriteLockClient.class); when(lock.tryLock(any(HoldType.class))).thenReturn(result, results); @SuppressWarnings("unchecked") EntityRef<VoltronReadWriteLockClient, Object, Object> interlockRef = mock(EntityRef.class); when(connection.getEntityRef(eq(VoltronReadWriteLockClient.class), anyLong(), eq(lockname))).thenReturn(interlockRef); when(interlockRef.fetchEntity(null)).thenReturn(lock); }
@Test public void testPresentEntityDestroySucceeds() throws Throwable { try (Connection client = CLUSTER.newConnection()) { EntityRef<ClusterTierManagerClientEntity, ClusterTierManagerConfiguration, Void> ref = getEntityRef(client); ref.create(blankConfiguration); ref.destroy(); try { ref.fetchEntity(null); fail("Expected EntityNotFoundException"); } catch (EntityNotFoundException e) { //expected } } }
private void mockLockForWriteLockSuccess() throws org.terracotta.exception.EntityNotProvidedException, org.terracotta.exception.EntityNotFoundException, org.terracotta.exception.EntityVersionMismatchException { when(connection.<VoltronReadWriteLockClient, Object, Void>getEntityRef(same(VoltronReadWriteLockClient.class), eq(1L), any())).thenReturn(lockEntityRef); VoltronReadWriteLockClient lockClient = mock(VoltronReadWriteLockClient.class); when(lockEntityRef.fetchEntity(null)).thenReturn(lockClient); when(lockClient.tryLock(LockMessaging.HoldType.WRITE)).thenReturn(true); }
private void mockLockForReadLockSuccess() throws org.terracotta.exception.EntityNotProvidedException, org.terracotta.exception.EntityNotFoundException, org.terracotta.exception.EntityVersionMismatchException { when(connection.<VoltronReadWriteLockClient, Object, Void>getEntityRef(same(VoltronReadWriteLockClient.class), eq(1L), any())).thenReturn(lockEntityRef); VoltronReadWriteLockClient lockClient = mock(VoltronReadWriteLockClient.class); when(lockEntityRef.fetchEntity(null)).thenReturn(lockClient); when(lockClient.tryLock(LockMessaging.HoldType.READ)).thenReturn(true); }
@Test public void testReadLockLocksRead() throws Exception { when(entityRef.fetchEntity(null)).thenReturn(client); when(connection.<VoltronReadWriteLockClient, Void, Void>getEntityRef(VoltronReadWriteLockClient.class, 1, "VoltronReadWriteLock-TestLock")).thenReturn(entityRef); VoltronReadWriteLock lock = new VoltronReadWriteLock(connection, "TestLock"); lock.readLock(); verify(client).lock(READ); }
@Test public void testAbsentEntityRetrievalFails() throws Throwable { try (Connection client = CLUSTER.newConnection()) { EntityRef<ClusterTierManagerClientEntity, ClusterTierManagerConfiguration, Void> ref = getEntityRef(client); try { ref.fetchEntity(null); fail("Expected EntityNotFoundException"); } catch (EntityNotFoundException e) { //expected } } }