Skip to content

Commit

Permalink
Merge branch 'refs/heads/10.0.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
1t5j0y committed Oct 1, 2024
2 parents 63fc90b + c301caa commit 733ad47
Showing 1 changed file with 51 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ public class AddressLevelCacheIntegrationTest extends AbstractControllerIntegrat
private static final long CATCHMENT_2_ID = 2L;
private static final long CATCHMENT_3_ID = 3L;
private static final long CATCHMENT_4_ID = 4L;
private static final long CATCHMENT_5_ID = 5L;
private static final int CATCHMENT_1_SIZE = 2;
private static final int CATCHMENT_2_SIZE = 10;
private static final int CATCHMENT_3_SIZE = 8;
private static final int CATCHMENT_4_SIZE = 20;
public static final int TIMEOUT = 100;
private static final int CATCHMENT_5_SIZE = 301;
public static final int TIMEOUT_IN_MS = 100;

private long addressIdStartIdx;

Expand All @@ -58,7 +60,7 @@ public class AddressLevelCacheIntegrationTest extends AbstractControllerIntegrat
@Autowired
private TestDataSetupService testDataSetupService;

private Catchment catchment1, catchment2, catchment3, catchment4;
private Catchment catchment1, catchment2, catchment3, catchment4, catchment5Big;

private Cache addrPerCatchmentCache;

Expand All @@ -83,6 +85,7 @@ public void setUpAddressLevelCache() {
catchment2 = initCatchmentAndMock(CATCHMENT_2_ID, getAddressIdStartIdx( CATCHMENT_1_SIZE), CATCHMENT_2_SIZE);
catchment3 = initCatchmentAndMock(CATCHMENT_3_ID, getAddressIdStartIdx( CATCHMENT_2_SIZE), CATCHMENT_3_SIZE);
catchment4 = initCatchmentAndMock(CATCHMENT_4_ID, getAddressIdStartIdx( CATCHMENT_3_SIZE), CATCHMENT_4_SIZE);
catchment5Big = initCatchmentAndMock(CATCHMENT_5_ID, getAddressIdStartIdx( CATCHMENT_4_SIZE), CATCHMENT_5_SIZE);

keysReferenceQueue = new ReferenceQueue<>();
valuesReferenceQueue = new ReferenceQueue<>();
Expand Down Expand Up @@ -119,17 +122,17 @@ private ArrayList<CatchmentAddressProjection> getCatchmentAddressProjectionArray
}

@Test
public void givenAddressLevelCacheIsConfigured_whenCallgetCatchmentAddressesForCatchmentId_thenDataShouldBeInAddressPerCatchmentCache() {
public void givenAddressLevelCacheIsConfigured_whenCallGetCatchmentAddressesForCatchmentId_thenDataShouldBeInAddressPerCatchmentCache() {
//Fetch and cache
List<CatchmentAddressProjection> cachedVirtualCatchmentProjList = addressLevelCache.getAddressLevelsForCatchment(catchment1);
List<CatchmentAddressProjection> cachedCatchmentAddressesList = addressLevelCache.getAddressLevelsForCatchment(catchment1);

//Validate cache content
Assert.notNull(cachedVirtualCatchmentProjList, "addrPerCatchmentCache should have had the data");
Assert.isTrue(CATCHMENT_1_SIZE == cachedVirtualCatchmentProjList.size(), "addrPerCatchmentCache size should have been 2");
Assert.notNull(cachedCatchmentAddressesList, "addrPerCatchmentCache should have had the data");
Assert.isTrue(CATCHMENT_1_SIZE == cachedCatchmentAddressesList.size(), "addrPerCatchmentCache size should have been 2");
}

@Test
public void givenAddressLevelCacheIsConfigured_whenMultipleCallgetCatchmentAddressesForCatchmentId_thenValidateCacheMissAndHits() {
public void givenAddressLevelCacheIsConfigured_whenMultipleCallGetCatchmentAddressesForCatchmentId_thenValidateCacheMissAndHits() {
//Fetch and cache
addressLevelCache.getAddressLevelsForCatchment(catchment1);

Expand All @@ -150,7 +153,7 @@ public void givenAddressLevelCacheIsConfigured_whenMultipleCallgetCatchmentAddre
}

@Test
public void givenAddressLevelCacheIsConfigured_whenCallGetVirtualCatchmentsForDiffCatchmentId_thenValidateCacheMiss() {
public void givenAddressLevelCacheIsConfigured_whenCallGetCatchmentsAddressesForDiffCatchmentId_thenValidateCacheMiss() {
Cache addrPerCatchmentCache = cacheManager.getCache(ADDRESSES_PER_CATCHMENT);

//Validate missing cache entry for second catchment
Expand All @@ -172,7 +175,7 @@ public void givenAddressLevelCacheIsConfigured_whenCallGetVirtualCatchmentsForDi
}

@Test
public void givenAddressLevelCachesAreFullyPopulated_whenCallGetVirtualCatchmentsForNewCatchmentId_thenValidateCacheClearedAndNewOneIsPopulated() {
public void givenAddressLevelCachesAreFullyPopulated_whenCallGetCatchmentAddressesForNewCatchmentId_thenValidateCacheClearedAndNewOneIsPopulated() {
Cache addrPerCatchmentCache = initFirst3CatchmentsAndValidateTheirCacheEntriesAndContent();

//Invoke for a fourth catchment
Expand All @@ -191,6 +194,31 @@ public void givenAddressLevelCachesAreFullyPopulated_whenCallGetVirtualCatchment
"addrPerCatchmentCache4 size should have been 20");
}

@Test
public void givenAddressLevelCachesAreFullyPopulated_whenCallGetCatchmentAddressesForVeryLargeCatchment_thenValidateCacheIsUntouchedAndCallHitsRepoEverytime() {
Cache addrPerCatchmentCache = initFirst3CatchmentsAndValidateTheirCacheEntriesAndContent();

//Invoke for a big catchment
addressLevelCache.getAddressLevelsForCatchment(catchment5Big);

//Validate cache miss the first time for big catchment
verify(mockLocationRepository).getCatchmentAddressesForCatchmentId(catchment5Big.getId());
verifyNoMoreInteractions(mockLocationRepository);

//Ensure cache has remained intact without any new entries
Assert.isTrue(getCountOfNullObjects(addrPerCatchmentCache.get(catchment1),
addrPerCatchmentCache.get(catchment2),
addrPerCatchmentCache.get(catchment3)) == 0L,
"only one of addrPerCatchmentCache 1,2 or 3 should not have had the data");
Assert.isNull(addrPerCatchmentCache.get(catchment5Big), "addrPerCatchmentCache5 is not present in cache");

//Invoke for the same big catchment
addressLevelCache.getAddressLevelsForCatchment(catchment5Big);

//Validate cache miss again for the big catchment
verify(mockLocationRepository, times(2)).getCatchmentAddressesForCatchmentId(catchment5Big.getId());
}

private Cache initFirst3CatchmentsAndValidateTheirCacheEntriesAndContent() {
Cache addrPerCatchmentCache = cacheManager.getCache(ADDRESSES_PER_CATCHMENT);

Expand Down Expand Up @@ -224,7 +252,7 @@ private Cache initFirst3CatchmentsAndValidateTheirCacheEntriesAndContent() {
return addrPerCatchmentCache;
}

private void givenAddressLevelCachesAreFullyPopulated_whenCallGetVirtualCatchmentsForNewCatchmentId_thenValidateCacheClearedAndGCRemovesAllTheEntriesKeysFromMemory() throws InterruptedException {
private void givenAddressLevelCachesAreFullyPopulated_whenCallGetCatchmentAddressesForNewCatchmentId_thenValidateCacheClearedAndGCRemovesAllTheEntriesKeysFromMemory() throws InterruptedException {
Cache addrPerCatchmentCache = initFirst3CatchmentsAndValidateTheirCacheEntriesAndContent();

WeakReference weakReferenceForCatchment1 = new WeakReference<>(addrPerCatchmentCache.get(catchment1), keysReferenceQueue);
Expand All @@ -245,10 +273,10 @@ private void givenAddressLevelCachesAreFullyPopulated_whenCallGetVirtualCatchmen
weakReferenceForCatchment2.get(),
weakReferenceForCatchment3.get()) == 3L,
"only one of referentForCatchments should not have had the data");
Assert.notNull(keysReferenceQueue.remove(TIMEOUT), "should return one cached element");
Assert.notNull(keysReferenceQueue.remove(TIMEOUT), "should return one cached element");
Assert.notNull(keysReferenceQueue.remove(TIMEOUT), "should return one cached element");
Assert.isNull(keysReferenceQueue.remove(TIMEOUT), "should return null");
Assert.notNull(keysReferenceQueue.remove(TIMEOUT_IN_MS), "should return one cached element");
Assert.notNull(keysReferenceQueue.remove(TIMEOUT_IN_MS), "should return one cached element");
Assert.notNull(keysReferenceQueue.remove(TIMEOUT_IN_MS), "should return one cached element");
Assert.isNull(keysReferenceQueue.remove(TIMEOUT_IN_MS), "should return null");

//Ensure cache has overflown and one entry got replaced with fourth catchment entry
Assert.isTrue(getCountOfNullObjects(addrPerCatchmentCache.get(catchment1),
Expand All @@ -259,7 +287,7 @@ private void givenAddressLevelCachesAreFullyPopulated_whenCallGetVirtualCatchmen
"addrPerCatchmentCache4 size should have been 20");
}

private void givenAddressLevelCachesAreFullyPopulated_whenCallGetVirtualCatchmentsForNewCatchmentId_thenValidateCacheClearedAndGCRemovesTheOldEntryValueFromMemory() throws InterruptedException {
private void givenAddressLevelCachesAreFullyPopulated_whenCallGetCatchmentAddressesForNewCatchmentId_thenValidateCacheClearedAndGCRemovesTheOldEntryValueFromMemory() throws InterruptedException {
Cache addrPerCatchmentCache = initFirst3CatchmentsAndValidateTheirCacheEntriesAndContent();

WeakReference weakReferenceForCatchment1Value = new WeakReference<>(addrPerCatchmentCache.get(catchment1).get(), valuesReferenceQueue);
Expand All @@ -280,8 +308,8 @@ private void givenAddressLevelCachesAreFullyPopulated_whenCallGetVirtualCatchmen
weakReferenceForCatchment2Value.get(),
weakReferenceForCatchment3Value.get()) == 1L,
"one of referentForCatchmentValues should not have had the data");
Assert.notNull(valuesReferenceQueue.remove(TIMEOUT), "should return one cached element's value");
Assert.isNull(valuesReferenceQueue.remove(TIMEOUT), "should return null");
Assert.notNull(valuesReferenceQueue.remove(TIMEOUT_IN_MS), "should return one cached element's value");
Assert.isNull(valuesReferenceQueue.remove(TIMEOUT_IN_MS), "should return null");

//Ensure cache has overflown and one entry got replaced with fourth catchment entry
Assert.isTrue(getCountOfNullObjects(addrPerCatchmentCache.get(catchment1),
Expand All @@ -298,31 +326,31 @@ private long getCountOfNullObjects(Object ... objects) {

@Test
public void validateCacheClearedAndGCRemovesAllTheEntriesKeysFromMemory_attempt_1() throws InterruptedException {
givenAddressLevelCachesAreFullyPopulated_whenCallGetVirtualCatchmentsForNewCatchmentId_thenValidateCacheClearedAndGCRemovesAllTheEntriesKeysFromMemory();
givenAddressLevelCachesAreFullyPopulated_whenCallGetCatchmentAddressesForNewCatchmentId_thenValidateCacheClearedAndGCRemovesAllTheEntriesKeysFromMemory();
}

@Test
public void validateCacheClearedAndGCRemovesAllTheEntriesKeysFromMemory_attempt_2() throws InterruptedException {
givenAddressLevelCachesAreFullyPopulated_whenCallGetVirtualCatchmentsForNewCatchmentId_thenValidateCacheClearedAndGCRemovesAllTheEntriesKeysFromMemory();
givenAddressLevelCachesAreFullyPopulated_whenCallGetCatchmentAddressesForNewCatchmentId_thenValidateCacheClearedAndGCRemovesAllTheEntriesKeysFromMemory();
}

@Test
public void validateCacheClearedAndGCRemovesAllTheEntriesKeysFromMemory_attempt_3() throws InterruptedException {
givenAddressLevelCachesAreFullyPopulated_whenCallGetVirtualCatchmentsForNewCatchmentId_thenValidateCacheClearedAndGCRemovesAllTheEntriesKeysFromMemory();
givenAddressLevelCachesAreFullyPopulated_whenCallGetCatchmentAddressesForNewCatchmentId_thenValidateCacheClearedAndGCRemovesAllTheEntriesKeysFromMemory();
}

@Test
public void validateCacheClearedAndGCRemovesTheOldEntryValueFromMemory_attempt_1() throws InterruptedException {
givenAddressLevelCachesAreFullyPopulated_whenCallGetVirtualCatchmentsForNewCatchmentId_thenValidateCacheClearedAndGCRemovesTheOldEntryValueFromMemory();
givenAddressLevelCachesAreFullyPopulated_whenCallGetCatchmentAddressesForNewCatchmentId_thenValidateCacheClearedAndGCRemovesTheOldEntryValueFromMemory();
}

@Test
public void validateCacheClearedAndGCRemovesTheOldEntryValueFromMemory_attempt_2() throws InterruptedException {
givenAddressLevelCachesAreFullyPopulated_whenCallGetVirtualCatchmentsForNewCatchmentId_thenValidateCacheClearedAndGCRemovesTheOldEntryValueFromMemory();
givenAddressLevelCachesAreFullyPopulated_whenCallGetCatchmentAddressesForNewCatchmentId_thenValidateCacheClearedAndGCRemovesTheOldEntryValueFromMemory();
}

@Test
public void validateCacheClearedAndGCRemovesTheOldEntryValueFromMemory_attempt_3() throws InterruptedException {
givenAddressLevelCachesAreFullyPopulated_whenCallGetVirtualCatchmentsForNewCatchmentId_thenValidateCacheClearedAndGCRemovesTheOldEntryValueFromMemory();
givenAddressLevelCachesAreFullyPopulated_whenCallGetCatchmentAddressesForNewCatchmentId_thenValidateCacheClearedAndGCRemovesTheOldEntryValueFromMemory();
}
}

0 comments on commit 733ad47

Please sign in to comment.