Skip to content

Commit

Permalink
change 'expiresIn' metadata field name to 'expires'
Browse files Browse the repository at this point in the history
  • Loading branch information
elmiomar committed Mar 6, 2024
1 parent 39fc9a3 commit de80e31
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*/
public class CacheExpiryCheck implements CacheObjectCheck {

// private static final long TWO_WEEKS_MILLIS = 14 * 24 * 60 * 60 * 1000; // 14 days in milliseconds
private StorageInventoryDB inventoryDB;

public CacheExpiryCheck(StorageInventoryDB inventoryDB) {
Expand All @@ -21,8 +20,8 @@ public CacheExpiryCheck(StorageInventoryDB inventoryDB) {

/**
* Checks if a cache object is expired and removes it from the cache if it is.
* The method uses the {@code expiresIn} metadata field to determine the expiration status.
* The expiration time is calculated based on the {@code LastModified} time plus the {@code expiresIn} duration.
* The method uses the {@code expires} metadata field to determine the expiration status.
* The expiration time is calculated based on the {@code LastModified} time plus the {@code expires} duration.
* If the current time is past the calculated expiry time, the object is removed from the inventory database.
*
* @param co The cache object to check for expiration.
Expand All @@ -36,18 +35,18 @@ public void check(CacheObject co) throws IntegrityException, StorageVolumeExcept
throw new IllegalArgumentException("CacheObject or StorageInventoryDB is null");
}

if (co.hasMetadatum("expiresIn")) {
long expiresInDuration = co.getMetadatumLong("expiresIn", -1L);
if (expiresInDuration == -1L) {
throw new IntegrityException("Invalid 'expiresIn' metadata value");
if (co.hasMetadatum("expires")) {
long expiresDuration = co.getMetadatumLong("expires", -1L);
if (expiresDuration == -1L) {
throw new IntegrityException("Invalid 'expires' metadata value");
}

long lastModified = co.getLastModified();
if (lastModified == -1L) {
throw new IntegrityException("CacheObject 'lastModified' time not available");
}

long expiryTime = lastModified + expiresInDuration;
long expiryTime = lastModified + expiresDuration;
long currentTime = Instant.now().toEpochMilli();

// Check if the object is expired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,8 +295,8 @@ protected void cacheFromBag(String bagfile, Collection<String> need, Collection<
protected void updateMetadata(JSONObject md, int prefs) {
if ((prefs & ROLE_RESTRICTED_DATA) != 0) {
// Calculate the expiration time as current time + expiryTime
long expiresIn = System.currentTimeMillis() + expiryTime;
md.put("expiresIn", expiresIn);
long expires = System.currentTimeMillis() + expiryTime;
md.put("expires", expires);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void setUp() {

/**
* Test to verify that {@link CacheExpiryCheck} correctly identifies and processes an expired cache object.
* An object is considered expired based on the `expiresIn` metadata, which defines the duration after which
* An object is considered expired based on the `expires` metadata, which defines the duration after which
* an object should be considered expired from the time of its last modification. This test ensures that an object
* past its expiration is appropriately removed from the inventory database.
*
Expand All @@ -40,8 +40,8 @@ public void setUp() {
public void testExpiredObjectRemoval() throws Exception {
// Setup an expired cache object
cacheObject.volume = mockVolume;
when(cacheObject.hasMetadatum("expiresIn")).thenReturn(true);
when(cacheObject.getMetadatumLong("expiresIn", -1L)).thenReturn(1000L); // Expires in 1 second
when(cacheObject.hasMetadatum("expires")).thenReturn(true);
when(cacheObject.getMetadatumLong("expires", -1L)).thenReturn(1000L); // Expires in 1 second
when(cacheObject.getLastModified()).thenReturn(Instant.now().minusSeconds(10).toEpochMilli());
when(cacheObject.volume.remove(cacheObject.name)).thenReturn(true);

Expand All @@ -54,7 +54,7 @@ public void testExpiredObjectRemoval() throws Exception {

/**
* Test to ensure that {@link CacheExpiryCheck} does not flag a cache object as expired if the current time has not
* exceeded its `expiresIn` duration since its last modification. This test verifies that no removal action is taken
* exceeded its `expires` duration since its last modification. This test verifies that no removal action is taken
* for such non-expired objects.
*
* @throws Exception to handle any exceptions thrown during the test execution
Expand All @@ -64,8 +64,8 @@ public void testNonExpiredObject() throws Exception {
// Setup mock
cacheObject.name = "nonExpiredObject";
cacheObject.volname = "testVolume";
when(cacheObject.hasMetadatum("expiresIn")).thenReturn(true);
when(cacheObject.getMetadatumLong("expiresIn", -1L)).thenReturn(14 * 24 * 60 * 60 * 1000L); // 14 days in milliseconds
when(cacheObject.hasMetadatum("expires")).thenReturn(true);
when(cacheObject.getMetadatumLong("expires", -1L)).thenReturn(14 * 24 * 60 * 60 * 1000L); // 14 days in milliseconds
long lastModified = System.currentTimeMillis() - (7 * 24 * 60 * 60 * 1000L); // 7 days ago, within expiry period
when(cacheObject.getLastModified()).thenReturn(lastModified);

Expand All @@ -77,16 +77,16 @@ public void testNonExpiredObject() throws Exception {
}

/**
* Tests that no action is taken and no exception is thrown for a cache object without the {@code expiresIn} metadata.
* This verifies that the absence of {@code expiresIn} metadata does not trigger any removal process or result in an error.
* Tests that no action is taken and no exception is thrown for a cache object without the {@code expires} metadata.
* This verifies that the absence of {@code expires} metadata does not trigger any removal process or result in an error.
*
* @throws Exception to handle any exceptions thrown during the test execution
*/
@Test
public void testObjectWithoutExpiresInMetadata_NoActionTaken() throws Exception {
cacheObject.name = "objectWithoutExpiresIn";
public void testObjectWithoutExpiresMetadata_NoActionTaken() throws Exception {
cacheObject.name = "objectWithoutExpires";
cacheObject.volname = "testVolume";
when(cacheObject.hasMetadatum("expiresIn")).thenReturn(false);
when(cacheObject.hasMetadatum("expires")).thenReturn(false);

expiryCheck.check(cacheObject);

Expand All @@ -96,15 +96,15 @@ public void testObjectWithoutExpiresInMetadata_NoActionTaken() throws Exception
/**
* Test to ensure that a cache object with an expiration date in the future is not removed from the cache.
* This test verifies the {@code check} method's correct behavior in handling non-expired objects based
* on the {@code expiresIn} metadata.
* on the {@code expires} metadata.
*
* @throws Exception to handle any exceptions thrown during the test execution.
*/
@Test
public void testNonExpiredObject_NoRemoval() throws Exception {
// Setup a non-expired cache object
when(cacheObject.hasMetadatum("expiresIn")).thenReturn(true);
when(cacheObject.getMetadatumLong("expiresIn", -1L)).thenReturn(System.currentTimeMillis() + 10000L); // Expires in the future
when(cacheObject.hasMetadatum("expires")).thenReturn(true);
when(cacheObject.getMetadatumLong("expires", -1L)).thenReturn(System.currentTimeMillis() + 10000L); // Expires in the future
when(cacheObject.getLastModified()).thenReturn(System.currentTimeMillis());

expiryCheck.check(cacheObject);
Expand All @@ -115,32 +115,32 @@ public void testNonExpiredObject_NoRemoval() throws Exception {


/**
* Tests that an {@link IntegrityException} is thrown when a cache object has the {@code expiresIn} metadata
* Tests that an {@link IntegrityException} is thrown when a cache object has the {@code expires} metadata
* but lacks a valid {@code lastModified} time.
*
* @throws Exception to handle any exceptions thrown during the test execution
*/
@Test(expected = IntegrityException.class)
public void testObjectWithExpiresInButNoLastModified_ThrowsException() throws Exception {
public void testObjectWithExpiresButNoLastModified_ThrowsException() throws Exception {
cacheObject.name = "objectWithNoLastModified";
cacheObject.volname = "testVolume";
when(cacheObject.hasMetadatum("expiresIn")).thenReturn(true);
when(cacheObject.getMetadatumLong("expiresIn", -1L)).thenReturn(1000L); // Expires in 1 second
when(cacheObject.hasMetadatum("expires")).thenReturn(true);
when(cacheObject.getMetadatumLong("expires", -1L)).thenReturn(1000L); // Expires in 1 second
when(cacheObject.getLastModified()).thenReturn(-1L); // Last modified not available

expiryCheck.check(cacheObject);
}

/**
* Test to verify that no action is taken for a cache object missing the {@code expiresIn} metadata.
* This test ensures that the absence of {@code expiresIn} metadata does not trigger any removal or error.
* Test to verify that no action is taken for a cache object missing the {@code expires} metadata.
* This test ensures that the absence of {@code expires} metadata does not trigger any removal or error.
*
* @throws Exception to handle any exceptions thrown during the test execution.
*/
@Test
public void testObjectWithoutExpiresIn_NoAction() throws Exception {
// Setup an object without expiresIn metadata
when(cacheObject.hasMetadatum("expiresIn")).thenReturn(false);
public void testObjectWithoutExpires_NoAction() throws Exception {
// Setup an object without expires metadata
when(cacheObject.hasMetadatum("expires")).thenReturn(false);

expiryCheck.check(cacheObject);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ public void testCacheFromBag()
}

@Test
public void testExpiresIn() throws StorageVolumeException, ResourceNotFoundException, CacheManagementException {
public void testExpires() throws StorageVolumeException, ResourceNotFoundException, CacheManagementException {

assertTrue(! cache.isCached("mds1491/trial1.json"));
assertTrue(! cache.isCached("mds1491/trial2.json"));
Expand All @@ -429,33 +429,33 @@ public void testExpiresIn() throws StorageVolumeException, ResourceNotFoundExcep

List<CacheObject> found = cache.getInventoryDB().findObject("mds1491/trial1.json", VolumeStatus.VOL_FOR_INFO);
assertEquals(1, found.size());
assertTrue("CacheObject should contain expiresIn metadata", found.get(0).hasMetadatum("expiresIn"));
assertTrue("CacheObject should contain expires metadata", found.get(0).hasMetadatum("expires"));

// Verify the "expiresIn" value is approximately 2 weeks from the current time
// Verify the "expires" value is approximately 2 weeks from the current time
long TWO_WEEKS_MILLIS = 14L * 24 * 60 * 60 * 1000;
long expectedExpiresIn = System.currentTimeMillis() + TWO_WEEKS_MILLIS;
long actualExpiresIn = found.get(0).getMetadatumLong("expiresIn", 0);
// Check that the absolute difference between the expected and actual expiresIn values is less than 1000ms
assertTrue("expiresIn should be set to 2 weeks from the current time",
Math.abs(expectedExpiresIn - actualExpiresIn) < 1000);
long expectedExpires = System.currentTimeMillis() + TWO_WEEKS_MILLIS;
long actualExpires = found.get(0).getMetadatumLong("expires", 0);
// Check that the absolute difference between the expected and actual expires values is less than 1000ms
assertTrue("expires field should be set to 2 weeks from the current time",
Math.abs(expectedExpires - actualExpires) < 1000);

found = cache.getInventoryDB().findObject("mds1491/trial2.json", VolumeStatus.VOL_FOR_INFO);
assertEquals(1, found.size());
assertTrue("CacheObject should contain expiresIn metadata", found.get(0).hasMetadatum("expiresIn"));
assertTrue("CacheObject should contain expires metadata", found.get(0).hasMetadatum("expires"));

expectedExpiresIn = System.currentTimeMillis() + TWO_WEEKS_MILLIS;
actualExpiresIn = found.get(0).getMetadatumLong("expiresIn", 0);
assertTrue("expiresIn should be set to 2 weeks from the current time",
Math.abs(expectedExpiresIn - actualExpiresIn) < 1000);
expectedExpires= System.currentTimeMillis() + TWO_WEEKS_MILLIS;
actualExpires = found.get(0).getMetadatumLong("expires", 0);
assertTrue("expires field should be set to 2 weeks from the current time",
Math.abs(expectedExpires - actualExpires) < 1000);

found = cache.getInventoryDB().findObject("mds1491/README.txt", VolumeStatus.VOL_FOR_INFO);
assertEquals(1, found.size());
assertTrue("CacheObject should contain expiresIn metadata", found.get(0).hasMetadatum("expiresIn"));
assertTrue("CacheObject should contain expires metadata", found.get(0).hasMetadatum("expires"));

expectedExpiresIn = System.currentTimeMillis() + TWO_WEEKS_MILLIS;
actualExpiresIn = found.get(0).getMetadatumLong("expiresIn", 0);
assertTrue("expiresIn should be set to 2 weeks from the current time",
Math.abs(expectedExpiresIn - actualExpiresIn) < 1000);
expectedExpires = System.currentTimeMillis() + TWO_WEEKS_MILLIS;
actualExpires = found.get(0).getMetadatumLong("expires", 0);
assertTrue("expires field should be set to 2 weeks from the current time",
Math.abs(expectedExpires - actualExpires) < 1000);

}

Expand Down

0 comments on commit de80e31

Please sign in to comment.