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 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); } }
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); } }
@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 testWriteLockLocksWrite() 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(); verify(client).lock(WRITE); }
@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 testTryWriteUnlockUnlocksWrite() throws Exception { when(client.tryLock(WRITE)).thenReturn(true); 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.tryWriteLock().unlock(); verify(client).unlock(WRITE); }
@Test public void testTryReadUnlockUnlocksRead() throws Exception { when(client.tryLock(READ)).thenReturn(true); 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.tryReadLock().unlock(); verify(client).unlock(READ); }
@Test public void testTryWriteUnlockClosesEntity() throws Exception { when(client.tryLock(WRITE)).thenReturn(true); 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.tryWriteLock().unlock(); verify(client).close(); }
@Test public void testTryReadUnlockDestroysEntity() throws Exception { when(client.tryLock(READ)).thenReturn(true); 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.tryReadLock().unlock(); verify(entityRef).destroy(); }
@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 testWriteUnlockUnlocksWrite() 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(client).unlock(WRITE); }
@Test public void testReadUnlockUnlocksRead() 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().unlock(); verify(client).unlock(READ); }
@Test public void testReadUnlockClosesEntity() 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().unlock(); verify(client).close(); }
@Test public void testTryWriteLockFailingDestroysEntity() throws Exception { when(client.tryLock(WRITE)).thenReturn(false); 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"); assertThat(lock.tryWriteLock(), nullValue()); verify(entityRef).destroy(); }
@Test public void testRetrieve() throws Exception { when(entityRef.fetchEntity(null)).thenReturn(entity); when(getEntityRef(ClusterTierManagerClientEntity.class)).thenReturn(entityRef); addMockUnlockedLock(connection, "VoltronReadWriteLock-ClusterTierManagerClientEntityFactory-AccessLock-test"); ClusterTierManagerClientEntityFactory factory = new ClusterTierManagerClientEntityFactory(connection); assertThat(factory.retrieve("test", null), is(entity)); verify(entity).validate(isNull()); verify(entity, never()).close(); }
@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())); } }
@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 } } }
@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(); } } }
@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(); } }